Detectron2 Panoptic FPN Model Partial Execution - TypeError: 'NoneType' object is not iterable

Detectron2 Panoptic FPN Model Partial Execution - TypeError: 'NoneType' object is not iterable

我正在尝试为 Detectron2 ResNet50 - based FPN model 的全景输出提取预输出特征图。

因此,为了获得部分模型输出,我遵循官方 Detectron2 建模文档 Partially Execute Models

请查找以下代码:

# Setting the model Configurations
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file(
    "COCO-PanopticSegmentation/panoptic_fpn_R_50_1x.yaml")
)
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
    "COCO-PanopticSegmentation/panoptic_fpn_R_50_1x.yaml"
)

# Build the model from config
model = build_model(cfg)

# Loading an image just for testing
im = cv2.imread("./detectron/input.jpg")
im = torch.from_numpy(im).cuda().permute(2, 0, 1).unsqueeze(0).float()

# Extracting Features - This part works fine
features = model.backbone(im)

# Extracting the proposals - Throws Error
proposals = model.proposal_generator(im, features)

请找出上面步骤抛出的错误如下所示:

TypeError                                 Traceback (most recent call last)
<ipython-input-17-dd53471cf2d2> in <module>
----> 1 proposals = model.proposal_generator(im, features)

~/miniconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/media/Data/Documents/Python-Codes/Freelance/detectron2/detectron2/modeling/proposal_generator/rpn.py in forward(self, images, features, gt_instances)
    409 
    410         if self.training:
--> 411             gt_labels, gt_boxes = self.label_and_sample_anchors(anchors, gt_instances)
    412             losses = self.losses(
    413                 anchors, pred_objectness_logits, gt_labels, pred_anchor_deltas, gt_boxes

~/miniconda3/envs/pytorch/lib/python3.7/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
     13         def decorate_context(*args, **kwargs):
     14             with self:
---> 15                 return func(*args, **kwargs)
     16         return decorate_context
     17 

/media/Data/Documents/Python-Codes/Freelance/detectron2/detectron2/modeling/proposal_generator/rpn.py in label_and_sample_anchors(self, anchors, gt_instances)
    272         anchors = Boxes.cat(anchors)
    273 
--> 274         gt_boxes = [x.gt_boxes for x in gt_instances]
    275         image_sizes = [x.image_size for x in gt_instances]
    276         del gt_instances

TypeError: 'NoneType' object is not iterable

请让我知道我做错了什么,以及如何解决它。关于为什么会出现此错误的解释也会非常有帮助。

如果需要任何其他信息,请告诉我。

通过更多的挖掘,我解决了这个问题。上面的代码有几个问题:

  1. 我没有先将模型设置为评估模式 - model.eval()。模型需要设置为eval fist.
  2. mode.proposal_generator() 需要 ImageList 对象形式的输入,有关详细信息可以在 here.
  3. 中找到

执行以上两个步骤解决了问题。