RuntimeError: The size of tensor a (30) must match the size of tensor b (81) at non-singleton

用mmdetection训练cascade_rcnn模型时,训练时没有问题,在validation的时候报错

Traceback (most recent call last):
  File "prepare_mmdet_detections_txts.py", line 36, in 
    bboxes, labels = image_detections(image_path)
  File "prepare_mmdet_detections_txts.py", line 16, in image_detections
    result = inference_detector(model, image_path)
  File "/home/ubuntu/repos/mmdetection/mmdet/apis/inference.py", line 86, in inference_detector
    result = model(return_loss=False, rescale=True, **data)
  File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/repos/mmdetection/mmdet/core/fp16/decorators.py", line 49, in new_func
    return old_func(*args, **kwargs)
  File "/home/ubuntu/repos/mmdetection/mmdet/models/detectors/base.py", line 140, in forward
    return self.forward_test(img, img_meta, **kwargs)
  File "/home/ubuntu/repos/mmdetection/mmdet/models/detectors/base.py", line 123, in forward_test
    return self.simple_test(imgs[0], img_metas[0], **kwargs)
  File "/home/ubuntu/repos/mmdetection/mmdet/models/detectors/cascade_rcnn.py", line 353, in simple_test
    cls_score = sum(ms_scores) / self.num_stages
RuntimeError: The size of tensor a (30) must match the size of tensor b (81) at non-singleton dimension 1

原因是,训练检测的类别数量num_classes不一致。我要识别的类别数量是29个,加上背景总共是30个,而mmdetection中默认的num_classes为81(加上背景)。因此,在cascade_rcnn_r50_fpn.py文件中,三个分支的num_classes的类别改为自己的类别数量即可解决。

你可能感兴趣的:(bug,深度学习)