facebookresearch / Detectron

FAIR's research platform for object detection research, implementing popular algorithms like Mask R-CNN and RetinaNet.
Apache License 2.0
26.21k stars 5.45k forks source link

Shape error running inference on exported pretrained COCO Mask-RCNN #996

Closed xavierdcruz closed 4 years ago

xavierdcruz commented 4 years ago

Expected results

The Mask-RCNN model outputs for a single inference image.

My eventual goal is to export a Mask-RCNN that I fine-tuned on my own dataset for deployment, but to start with I'm just trying to export a standard pre-trained COCO Mask-RCNN, as is shown in the example.

Actual results

/home/sal9000/virtualenvs/detectron_env_2/bin/python /home/sal9000/Sources/detectron2/tools/deploy/caffe2_model/run_model.py
Imported stuff
Loaded model files
Created a predictor
Loaded & preprocessed the test image
INput shape:  (1, 3, 224, 224)
Forward passing the net...
Traceback (most recent call last):
  File "/home/sal9000/Sources/detectron2/tools/deploy/caffe2_model/run_model.py", line 34, in <module>
    results = p.run({'data': img})
RuntimeError: [enforce fail at generate_proposals_op.cc:289] im_info_tensor.sizes() == (vector<int64_t>{num_images, 3}). [0] vs 1 3
Error from operator: 
input: "511" input: "512" input: "im_info" input: "_wrapped_model.proposal_generator.anchor_generator.cell_anchors.0" output: "529" output: "530" name: "" type: "GenerateProposals" arg { name: "spatial_scale" f: 0.25 } arg { name: "pre_nms_topN" i: 1000 } arg { name: "post_nms_topN" i: 1000 } arg { name: "nms_thresh" f: 0.7 } arg { name: "min_size" f: 0 } arg { name: "angle_bound_on" i: 1 } arg { name: "angle_bound_lo" i: -180 } arg { name: "angle_bound_hi" i: 180 } arg { name: "clip_angle_thresh" f: 1 } arg { name: "legacy_plus_one" i: 0 } device_option { device_type: 0 device_id: 0 }frame #0: c10::ThrowEnforceNotMet(char const*, int, char const*, std::string const&, void const*) + 0x47 (0x7f3d102b34f7 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libc10.so)
frame #1: <unknown function> + 0x23d8030 (0x7f3d4ef6f030 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libtorch_cpu.so)
frame #2: <unknown function> + 0x1fbb57d (0x7f3d4eb5257d in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libtorch_cpu.so)
frame #3: caffe2::SimpleNet::Run() + 0x1a9 (0x7f3d4e90d169 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libtorch_cpu.so)
frame #4: caffe2::Workspace::RunNet(std::string const&) + 0x802 (0x7f3d4e955242 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libtorch_cpu.so)
frame #5: caffe2::Predictor::run_map_workspace(std::unordered_map<std::string, caffe2::Tensor, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, caffe2::Tensor> > > const&) + 0x761 (0x7f3d4ebd0a21 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libtorch_cpu.so)
frame #6: caffe2::Predictor::operator()(std::unordered_map<std::string, caffe2::Tensor, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, caffe2::Tensor> > > const&, std::vector<caffe2::Tensor, std::allocator<caffe2::Tensor> >*) + 0x19 (0x7f3d4ebd19b9 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/../../torch/lib/libtorch_cpu.so)
frame #7: <unknown function> + 0x530a2 (0x7f3d5bb9a0a2 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/caffe2_pybind11_state_gpu.cpython-36m-x86_64-linux-gnu.so)
frame #8: <unknown function> + 0x91e17 (0x7f3d5bbd8e17 in /home/sal9000/virtualenvs/detectron_env_2/lib/python3.6/site-packages/caffe2/python/caffe2_pybind11_state_gpu.cpython-36m-x86_64-linux-gnu.so)
frame #9: _PyCFunction_FastCallDict + 0x35c (0x5669ac in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #10: /home/sal9000/virtualenvs/detectron_env_2/bin/python() [0x50a5c3]
frame #11: _PyEval_EvalFrameDefault + 0x444 (0x50bfb4 in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #12: /home/sal9000/virtualenvs/detectron_env_2/bin/python() [0x507d64]
frame #13: PyEval_EvalCode + 0x23 (0x50ae13 in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #14: /home/sal9000/virtualenvs/detectron_env_2/bin/python() [0x634c82]
frame #15: PyRun_FileExFlags + 0x97 (0x634d37 in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #16: PyRun_SimpleFileExFlags + 0x17f (0x6384ef in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #17: Py_Main + 0x591 (0x639091 in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #18: main + 0xe0 (0x4b0d00 in /home/sal9000/virtualenvs/detectron_env_2/bin/python)
frame #19: __libc_start_main + 0xe7 (0x7f3d75d20b97 in /lib/x86_64-linux-gnu/libc.so.6)
frame #20: _start + 0x2a (0x5b250a in /home/sal9000/virtualenvs/detectron_env_2/bin/python)

Process finished with exit code 1

Detailed steps to reproduce

First I followed the instructions here https://github.com/facebookresearch/detectron2/blob/master/docs/tutorials/deployment.md#usage

and the script executed successfully, ran inference through several test images, and created a directory caffe2_model, containing several files: coco_instances_results.json instances_predictions.pth model_init.pb model.pb model.pbtxt

I wrote the following python script to try to make use of the above exported model files:

run_model.py:

from caffe2.python import core, workspace, model_helper, utils, brew
import numpy as np
import cv2
print('Imported stuff')

IMAGE_PATH = '1acb46f2-b23f-4696-8049-180ce25e0b51.18aecbee-2d62-4e4d-a527-8e99cc9d74e8.jpg'

with open("model_init.pb", "rb") as f:
    init_net = f.read()
with open("model.pb", "rb") as f:
    predict_net = f.read()
print('Loaded model files')

p = workspace.Predictor(init_net, predict_net)
print('Created a predictor')

SIZE = 224
img = cv2.imread(IMAGE_PATH)
img = cv2.resize(img, (SIZE, SIZE), cv2.INTER_CUBIC)
img = np.reshape(img, (3, SIZE, SIZE))
img = np.float32(np.expand_dims(img, axis=0))
print('Loaded & preprocessed the test image')
print('INput shape: ', img.shape)

print('Forward passing the net...')
results = p.run({'data': img})
print('Forward passed the net')
print(results)
`$ python run_model.py:`

System information

xavierdcruz commented 4 years ago

I meant to submit this to Detectron2, not Detectron

bjz0926 commented 1 year ago

I meet the same problem. Have you solved the issue. If you do, please give me your method. Thank you.