Commit 95ab768e authored by Yanghan Wang's avatar Yanghan Wang Committed by Facebook GitHub Bot
Browse files

rename @legacy to @c2_ops

Reviewed By: sstsai-adl

Differential Revision: D32216605

fbshipit-source-id: bebee1edae85e940c7dcc6a64dbe341a2fde36a2
parent 70e99867
......@@ -64,7 +64,7 @@ class GeneralizedRCNNPatch:
@RCNN_PREPARE_FOR_EXPORT_REGISTRY.register()
def default_rcnn_prepare_for_export(self, cfg, inputs, predictor_type):
if (
"@legacy" in predictor_type
"@c2_ops" in predictor_type
or "caffe2" in predictor_type
or "onnx" in predictor_type
):
......@@ -90,7 +90,7 @@ def default_rcnn_prepare_for_export(self, cfg, inputs, predictor_type):
# Caffe2MetaArch takes a single tuple as input (which is the return of
# preprocess_func), data_generator requires all positional args as a tuple.
data_generator=lambda x: (preprocess_func(x),),
model_export_method=predictor_type.replace("@legacy", "", 1),
model_export_method=predictor_type.replace("@c2_ops", "", 1),
model_export_kwargs=model_export_kwargs,
preprocess_info=preprocess_info,
postprocess_info=postprocess_info,
......
......@@ -29,9 +29,9 @@ class TestFBNetV3MaskRCNNFP32(RCNNBaseTestCases.TemplateTestCase):
@RCNNBaseTestCases.expand_parameterized_test_export(
[
["torchscript@legacy", True],
["torchscript@c2_ops", True],
["torchscript", True],
["torchscript_int8@legacy", False],
["torchscript_int8@c2_ops", False],
["torchscript_int8", False],
]
)
......@@ -60,7 +60,7 @@ class TestFBNetV3MaskRCNNQATEager(RCNNBaseTestCases.TemplateTestCase):
@RCNNBaseTestCases.expand_parameterized_test_export(
[
["torchscript_int8@legacy", False], # TODO: fix mismatch
["torchscript_int8@c2_ops", False], # TODO: fix mismatch
["torchscript_int8", False], # TODO: fix mismatch
]
)
......@@ -87,7 +87,7 @@ class TestFBNetV3KeypointRCNNFP32(RCNNBaseTestCases.TemplateTestCase):
@RCNNBaseTestCases.expand_parameterized_test_export(
[
["torchscript_int8@legacy", False], # TODO: fix mismatch
["torchscript_int8@c2_ops", False], # TODO: fix mismatch
["torchscript_int8", False], # TODO: fix mismatch
]
)
......
......@@ -2,7 +2,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import tempfile
import unittest
import torch
......@@ -13,7 +12,7 @@ from d2go.utils.testing.rcnn_helper import get_quick_test_config_opts
from mobile_cv.common.misc.file_utils import make_temp_directory
def maskrcnn_export_legacy_vs_new_format_example():
def maskrcnn_export_caffe2_vs_torchvision_opset_format_example():
with make_temp_directory("export_demo") as tmp_dir:
# use a fake dataset for ci
dataset_name = create_local_dataset(tmp_dir, 5, 224, 224)
......@@ -31,45 +30,49 @@ def maskrcnn_export_legacy_vs_new_format_example():
cfg.merge_from_list(config_list)
# equivalent to running:
# exporter.par --runner GeneralizedRCNNRunner --config-file config.yaml --predictor-types torchscript tourchscript@legacy --output-dir tmp_dir
# exporter.par --runner GeneralizedRCNNRunner --config-file config.yaml --predictor-types torchscript tourchscript@c2_ops --output-dir tmp_dir
_ = main(
cfg,
tmp_dir,
runner,
predictor_types=["torchscript@legacy", "torchscript"],
predictor_types=["torchscript@c2_ops", "torchscript"],
)
# the path can be fetched from the return of main, here just use hard-coded values
new_path = os.path.join(tmp_dir, "torchscript", "model.jit")
lagacy_path = os.path.join(tmp_dir, "torchscript@legacy", "model.jit")
new_model = torch.jit.load(new_path)
legacy_model = torch.jit.load(lagacy_path)
torchvision_ops_model = torch.jit.load(
os.path.join(tmp_dir, "torchscript", "model.jit")
)
caffe2_ops_model = torch.jit.load(
os.path.join(tmp_dir, "torchscript@c2_ops", "model.jit")
)
# Running inference using new format
# Running inference using torchvision-style format
image = torch.zeros(1, 64, 96) # chw 3D tensor
new_outputs = new_model(image) # suppose N instances are detected
torchvision_style_outputs = torchvision_ops_model(
image
) # suppose N instances are detected
# NOTE: the output are flattened tensors of the real output (which is a dict), they're
# ordered by the key in dict, which is deterministic for the given model, but it might
# be difficult to figure out just from model.jit file. The predictor_info.json from
# the same directory contains the `outputs_schema`, which indicate how the final output
# is constructed from flattened tensors.
pred_boxes = new_outputs[0] # torch.Size([N, 4])
pred_classes = new_outputs[1] # torch.Size([N])
pred_masks = new_outputs[2] # torch.Size([N, 1, Hmask, Wmask])
scores = new_outputs[3] # torch.Size([N])
pred_boxes = torchvision_style_outputs[0] # torch.Size([N, 4])
pred_classes = torchvision_style_outputs[1] # torch.Size([N])
pred_masks = torchvision_style_outputs[2] # torch.Size([N, 1, Hmask, Wmask])
scores = torchvision_style_outputs[3] # torch.Size([N])
# Running inference using legacy caffe2 format
# Running inference using caffe2-style format
data = torch.zeros(1, 1, 64, 96)
im_info = torch.tensor([[64, 96, 1.0]])
legacy_outputs = legacy_model([data, im_info])
caffe2_style_outputs = caffe2_ops_model([data, im_info])
# NOTE: the output order is determined in the order of creating the tensor during
# forward function, it's also follow the order of original Caffe2 model.
roi_bbox_nms = legacy_outputs[0] # torch.Size([N, 4])
roi_score_nms = legacy_outputs[1] # torch.Size([N])
roi_class_nms = legacy_outputs[2] # torch.Size([N])
mask_fcn_probs = legacy_outputs[3] # torch.Size([N, Cmask, Hmask, Wmask])
roi_bbox_nms = caffe2_style_outputs[0] # torch.Size([N, 4])
roi_score_nms = caffe2_style_outputs[1] # torch.Size([N])
roi_class_nms = caffe2_style_outputs[2] # torch.Size([N])
mask_fcn_probs = caffe2_style_outputs[3] # torch.Size([N, Cmask, Hmask, Wmask])
# relations between legacy outputs and new outputs
# relations between torchvision-style outputs and caffe2-style outputs
torch.testing.assert_allclose(pred_boxes, roi_bbox_nms)
torch.testing.assert_allclose(pred_classes, roi_class_nms)
torch.testing.assert_allclose(
......@@ -81,4 +84,4 @@ def maskrcnn_export_legacy_vs_new_format_example():
class TestOptimizer(unittest.TestCase):
def test_maskrcnn_export_legacy_vs_new_format_example(self):
maskrcnn_export_legacy_vs_new_format_example()
maskrcnn_export_caffe2_vs_torchvision_opset_format_example()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment