roytseng-tw / Detectron.pytorch

A pytorch implementation of Detectron. Both training from scratch and inferring directly from pretrained Detectron weights are available.
MIT License
2.82k stars 565 forks source link

Cannot run inference in Jupyter #57

Open QiaoranC opened 6 years ago

QiaoranC commented 6 years ago

@roytseng-tw Hi, I run the inference based on your infer_simple.py successfully . At same environment (one gpu, same machine, same folder path), i use it in Jupyter for inference, but give me error in the data_parallel. Any ideal?

This is my Jupyter move: I use following load the per-trained model with success return model structure.

cfg.MODEL.NUM_CLASSES = 3
cfg_file = 'configs/e2e_mask_rcnn_R-50-C4_1x.yaml'
load_name= '/home/ubuntu/Detectron_master/Outputs/e2e_mask_rcnn_R-50-C4_1x/May04-11-28-11_ubuntu16_step/ckpt/model_step19999.pth'

cfg_from_file(cfg_file)
assert_and_infer_cfg()

maskRCNN = Generalized_RCNN()
maskRCNN.cuda()
checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage)
net_utils.load_ckpt(maskRCNN, checkpoint['model'])
maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'],
                                 minibatch=True)
maskRCNN.eval()

However, when i next call it in a im_detect_all

im=cv2.imread('test.jpg')
cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers)

It give me a mini_kwargs = dict([(k, v[i]) for k, v in kwargs.items()]) error.

IndexError                                Traceback (most recent call last)
<ipython-input-5-f3b1e8bf1385> in <module>()
      9 timers = defaultdict(Timer)
     10 print('entry[image]',entry['image'])
---> 11 cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers)

~/Detectron_master/lib/core/test.py in im_detect_all(model, im, box_proposals, timers)
     68     else:
     69         scores, boxes, im_scale, blob_conv = im_detect_bbox(
---> 70             model, im, cfg.TEST.SCALE, cfg.TEST.MAX_SIZE, box_proposals)
     71     timers['im_detect_bbox'].toc()
     72 

~/Detectron_master/lib/core/test.py in im_detect_bbox(model, im, target_scale, target_max_size, boxes)
    133     inputs['im_info'] = [Variable(torch.from_numpy(inputs['im_info']), volatile=True)]
    134 
--> 135     return_dict = model(**inputs)
    136 
    137     if cfg.MODEL.FASTER_RCNN:

~/.local/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

~/Detectron_master/lib/nn/parallel/data_parallel.py in forward(self, *inputs, **kwargs)
     83                 mini_inputs = [x[i] for x in inputs]
     84 
---> 85                 mini_kwargs = dict([(k, v[i]) for k, v in kwargs.items()])
     86                 # print('mini_kwargs',mini_kwargs)
     87                 a, b = self._minibatch_scatter(device_id, *mini_inputs, **mini_kwargs)

~/Detectron_master/lib/nn/parallel/data_parallel.py in <listcomp>(.0)
     83                 mini_inputs = [x[i] for x in inputs]
     84 
---> 85                 mini_kwargs = dict([(k, v[i]) for k, v in kwargs.items()])
     86                 # print('mini_kwargs',mini_kwargs)
     87                 a, b = self._minibatch_scatter(device_id, *mini_inputs, **mini_kwargs)

IndexError: list index out of range
Rizhiy commented 6 years ago

Jupyter has its internal state, try to run the notebook from scratch, running each cell only once. Also perhaps share the notebook?

wangfei-cs commented 6 years ago

@QiaoranC Hi, I have some problem in running infer_simple.py, Is there any mistakes I had?

python tools/infer_simple.py --dataset coco --cfg configs/baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml --load_ckpt data/pretrained_model/resnet50_caffe.pth --images ~/image/farm/img1_keypoints-pydetectron-R50-FPN.jpg --output_dir ~/image/farm_out Called with args: Namespace(cfg_file='configs/baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml', cuda=True, dataset='coco', image_dir=None, images=['/home/feiw/image/farm/img1_keypoints-pydetectron-R50-FPN.jpg'], load_ckpt='data/pretrained_model/resnet50_caffe.pth', load_detectron=None, merge_pdfs=True, output_dir='/home/feiw/image/farm_out', set_cfgs=[]) load cfg from file: configs/baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml loading checkpoint data/pretrained_model/resnet50_caffe.pth Traceback (most recent call last): File "tools/infer_simple.py", line 176, in main() File "tools/infer_simple.py", line 124, in main net_utils.load_ckpt(maskRCNN, checkpoint['model']) KeyError: 'model'



python tools/infer_simple.py --dataset coco --cfg configs/baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml --load_detectron data/pretrained_model/R-50.pkl --images ~/image/farm/img1_keypoints-pydetectron-R50-FPN.jpg --output_dir ~/image/farm_out
Called with args:
Namespace(cfg_file='configs/baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml', cuda=True, dataset='coco', image_dir=None, images=['/home/feiw/image/farm/img1_keypoints-pydetectron-R50-FPN.jpg'], load_ckpt=None, load_detectron='data/pretrained_model/R-50.pkl', merge_pdfs=True, output_dir='/home/feiw/image/farm_out', set_cfgs=[])
load cfg from file: configs/baselines/e2e_keypoint_rcnn_R-50-FPN_s1x.yaml
loading detectron weights data/pretrained_model/R-50.pkl
Traceback (most recent call last):
  File "tools/infer_simple.py", line 176, in <module>
    main()
  File "tools/infer_simple.py", line 128, in main
    load_detectron_weight(maskRCNN, args.load_detectron)
  File "/home/feiw/Detectron/Detectron.pytorch/lib/utils/detectron_weight_helper.py", line 21, in load_detectron_weight
    p_tensor.copy_(torch.Tensor(src_blobs[d_name]))
KeyError: 'fpn_inner_res5_2_sum_w'