I want convert mask rcnn model to caffe2 in docker, and I use tools/caffe2_converter.py follow the instructions to call the model in C++. I get an error RuntimeError: [enforce fail at batch_permutation_op.cu:66] X.dim32(0) > 0. 0 vs 0.
Traceback (most recent call last):
File "caffe2_converter.py", line 77, in <module>
caffe2_model = tracer.export_caffe2()
File "/home/appuser/detectron2_repo/detectron2/export/api.py", line 107, in export_caffe2
predict_net, init_net = export_caffe2_detection_model(model, inputs)
File "/home/appuser/detectron2_repo/detectron2/export/caffe2_export.py", line 143, in export_caffe2_detection_model
onnx_model = export_onnx_model(model, (tensor_inputs,))
File "/home/appuser/detectron2_repo/detectron2/export/caffe2_export.py", line 60, in export_onnx_model
operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK,
File "/home/appuser/.local/lib/python3.6/site-packages/torch/onnx/__init__.py", line 168, in export
custom_opsets, enable_onnx_checker, use_external_data_format)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 69, in export
use_external_data_format=use_external_data_format)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 488, in _export
fixed_batch_size=fixed_batch_size)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 334, in _model_to_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args, training)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 291, in _trace_and_get_graph_from_model
torch.jit._get_trace_graph(model, args, _force_outplace=False, _return_inputs_states=True)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 278, in _get_trace_graph
outs = ONNXTracedModule(f, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(*input, **kwargs)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 361, in forward
self._force_outplace,
File "/home/appuser/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 348, in wrapper
outs.append(self.inner(*trace_inputs))
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 548, in __call__
result = self._slow_forward(*input, **kwargs)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 534, in _slow_forward
result = self.forward(*input, **kwargs)
File "/usr/lib/python3.6/contextlib.py", line 52, in inner
return func(*args, **kwds)
File "/home/appuser/detectron2_repo/detectron2/export/caffe2_modeling.py", line 272, in forward
detector_results, _ = self._wrapped_model.roi_heads(images, features, proposals)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 548, in __call__
result = self._slow_forward(*input, **kwargs)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 534, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/appuser/detectron2_repo/detectron2/modeling/roi_heads/roi_heads.py", line 676, in forward
pred_instances = self.forward_with_given_boxes(features, pred_instances)
File "/home/appuser/detectron2_repo/detectron2/modeling/roi_heads/roi_heads.py", line 702, in forward_with_given_boxes
instances = self._forward_mask(features, instances)
File "/home/appuser/detectron2_repo/detectron2/modeling/roi_heads/roi_heads.py", line 776, in _forward_mask
mask_features = self.mask_pooler(features, pred_boxes)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 548, in __call__
result = self._slow_forward(*input, **kwargs)
File "/home/appuser/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 534, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/appuser/detectron2_repo/detectron2/export/c10.py", line 341, in forward
roi_feat = torch.ops._caffe2.BatchPermutation(roi_feat_shuffled, rois_idx_restore_int32)
RuntimeError: [enforce fail at batch_permutation_op.cu:66] X.dim32(0) > 0. 0 vs 0
caffe2_converter.py
#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
"""
python caffe2_converter.py --config-file /home/appuser/detectron2_repo/detectron2/model_zoo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
--output ./caffe2_model \
--run-eval \
MODEL.WEIGHTS /home/appuser/project/output/model_final.pkl \
MODEL.DEVICE cuda
"""
import argparse
import os
import onnx
import torch
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2.data import build_detection_test_loader
from detectron2.evaluation import COCOEvaluator, inference_on_dataset, print_csv_format
from detectron2.export import Caffe2Tracer, add_export_config
from detectron2.modeling import build_model
from detectron2.utils.logger import setup_logger
def setup_cfg(args):
cfg = get_cfg()
# cuda context is initialized before creating dataloader, so we don't fork anymore
cfg.DATALOADER.NUM_WORKERS = 0
cfg = add_export_config(cfg)
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 6
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
if cfg.MODEL.DEVICE != "cpu":
TORCH_VERSION = tuple(int(x) for x in torch.__version__.split(".")[:2])
assert TORCH_VERSION >= (1, 5), "PyTorch>=1.5 required for GPU conversion!"
return cfg
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert a model using caffe2 tracing.")
parser.add_argument(
"--format",
choices=["caffe2", "onnx", "torchscript"],
help="output format",
default="caffe2",
)
parser.add_argument("--config-file", default="", metavar="FILE", help="path to config file")
parser.add_argument("--run-eval", action="store_true")
parser.add_argument("--output", help="output directory for the converted model")
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
logger = setup_logger()
logger.info("Command line arguments: " + str(args))
os.makedirs(args.output, exist_ok=True)
cfg = setup_cfg(args)
# create a torch model
torch_model = build_model(cfg)
DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS)
# get a sample data
data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])
first_batch = next(iter(data_loader))
# convert and save caffe2 model
tracer = Caffe2Tracer(cfg, torch_model, first_batch)
if args.format == "caffe2":
caffe2_model = tracer.export_caffe2()
caffe2_model.save_protobuf(args.output)
# draw the caffe2 graph
caffe2_model.save_graph(os.path.join(args.output, "model.svg"), inputs=first_batch)
elif args.format == "onnx":
onnx_model = tracer.export_onnx()
onnx.save(onnx_model, os.path.join(args.output, "model.onnx"))
elif args.format == "torchscript":
script_model = tracer.export_torchscript()
script_model.save(os.path.join(args.output, "model.ts"))
# Recursively print IR of all modules
with open(os.path.join(args.output, "model_ts_IR.txt"), "w") as f:
try:
f.write(script_model._actual_script_module._c.dump_to_str(True, False, False))
except AttributeError:
pass
# Print IR of the entire graph (all submodules inlined)
with open(os.path.join(args.output, "model_ts_IR_inlined.txt"), "w") as f:
f.write(str(script_model.inlined_graph))
# Print the model structure in pytorch style
with open(os.path.join(args.output, "model.txt"), "w") as f:
f.write(str(script_model))
# run evaluation with the converted model
if args.run_eval:
assert args.format == "caffe2", "Python inference in other format is not yet supported."
dataset = cfg.DATASETS.TEST[0]
data_loader = build_detection_test_loader(cfg, dataset)
# NOTE: hard-coded evaluator. change to the evaluator for your dataset
evaluator = COCOEvaluator(dataset, cfg, True, args.output)
metrics = inference_on_dataset(caffe2_model, data_loader, evaluator)
print_csv_format(metrics)
I want convert mask rcnn model to caffe2 in
docker
, and I usetools/caffe2_converter.py
follow the instructions to call the model in C++. I get an error RuntimeError: [enforce fail at batch_permutation_op.cu:66] X.dim32(0) > 0. 0 vs 0.caffe2_converter.py