Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError occurs in function show_result() of inference.py when class_names has only one class #1570

Closed
xuhuasheng opened this issue Oct 22, 2019 · 7 comments
Assignees

Comments

@xuhuasheng
Copy link

xuhuasheng commented Oct 22, 2019

Checklist

  1. I have searched related issues but cannot get the expected help.
  2. The bug has not been fixed in the latest version.

Describe the bug
the bug is in mmdet/apis/inference.py -line 116 assert isinstance(class_names, (tuple, list))
the bug will arise when there is only one class in class_names, which will raise AssertionError

Environment

  • OS: [Ubuntu 18.04.2]
  • GCC [5.5.0]
  • PyTorch version [1.1.0]
  • GPU model [1080Ti]
  • CUDA9.0and CUDNN7.4

Error traceback

raceback (most recent call last):
  File "inference_demo.py", line 81, in <module>
    main()
  File "inference_demo.py", line 75, in main
    show_result(img_fullFileName, result, model.CLASSES, wait_time=-1, show=False, out_file=save_img_fullFileName)
  File "/home/watson/anaconda3/envs/pytorch/mmdetection/mmdet/apis/inference.py", line 116, in show_result
    assert isinstance(class_names, (tuple, list)) 
AssertionError

Bug fix
the bug above will be fixed by modifing mmdet/apis/inference.py , line 116
modify
assert isinstance(class_names, (tuple, list))
to
assert isinstance(class_names, (tuple, list, str))

@hellock
Copy link
Member

hellock commented Oct 22, 2019

Thanks! The behavior is intended. If there is only one class, the class names should be ['object'] or ('object', ).

@xuhuasheng
Copy link
Author

Thanks! The behavior is intended. If there is only one class, the class names should be ['object'] or ('object', ).

Thank your reply!
In my project, there is only one class, so I have modified CLASS = ('myObject', ) in datasets/coco.py, but it will also raise the same AssertionError in assert isinstance(class_names, (tuple, list)) .
when I modified assert isinstance(class_names, (tuple, list, str)), it worked.

@yhcao6
Copy link
Collaborator

yhcao6 commented Oct 23, 2019

Hi, I just test show_result when class_names has only one class, there is no error occur. Here is my change and test pipeline:

  1. CLASSES = (person, ) in coco.py.
  2. num_classes=2 in cfg.
  3. Train one iteration and save the model.
  4. This is the CLASSES saved in checkpoint:
 ...
 'CLASSES': ('person',),
 'epoch': 1,
 'iter': 0,
 'mmcv_version': '0.2.13',
 'time': 'Wed Oct 23 14:56:18 2019'}
  1. Test show_result function, here is my script:
import os.path as osp
import sys

sys.path.insert(0, osp.abspath(osp.join(__file__, '../../')))

from mmdet.apis import init_detector, inference_detector, show_result

config_file = 'configs/test/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'work_dirs/faster_rcnn_r50_fpn_1x/epoch_1.pth'

# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# test a single image and show the results
img = 'demo/demo.jpg'
result = inference_detector(model, img)
# or save the visualization results to image files
show_result(img, result, model.CLASSES, show=False, out_file='img/result.jpg')

Could you provide a more concrete example where error will occur?

@xuhuasheng
Copy link
Author

Hi, I just test show_result when class_names has only one class, there is no error occur. Here is my change and test pipeline:

  1. CLASSES = (person, ) in coco.py.
  2. num_classes=2 in cfg.
  3. Train one iteration and save the model.
  4. This is the CLASSES saved in checkpoint:
 ...
 'CLASSES': ('person',),
 'epoch': 1,
 'iter': 0,
 'mmcv_version': '0.2.13',
 'time': 'Wed Oct 23 14:56:18 2019'}
  1. Test show_result function, here is my script:
import os.path as osp
import sys

sys.path.insert(0, osp.abspath(osp.join(__file__, '../../')))

from mmdet.apis import init_detector, inference_detector, show_result

config_file = 'configs/test/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'work_dirs/faster_rcnn_r50_fpn_1x/epoch_1.pth'

# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# test a single image and show the results
img = 'demo/demo.jpg'
result = inference_detector(model, img)
# or save the visualization results to image files
show_result(img, result, model.CLASSES, show=False, out_file='img/result.jpg')

Could you provide a more concrete example where error will occur?

How to checkout the CLASSES saved in checkpoint?

@yhcao6
Copy link
Collaborator

yhcao6 commented Oct 23, 2019

import torch
cpt = torch.load(cpt_path)
print(cpt['meta']['CLASSES'])

@xuhuasheng
Copy link
Author

Sorry, it's my fault. I checked the CLASSES saver in checkpoint file, I found it's a str instead of tuple.

My mistake:
I modified CLASSES = (object) in coco.py before training. but I corrected it to CLASSES = (object, ) when I realized it's wrong, before inferencing, whichi will raise AssertError.

My issue has been solved. Thank you very much!

@yhcao6 yhcao6 closed this as completed Oct 23, 2019
@goldwater668
Copy link

@xuhuasheng How to label a single category with labelme and instance segmentation? There are cat, cat, cat, cat0, cat1, cat2 or cat-0, cat-1, cat-2, etc. which annotation method is used?thank you!

I use cat-0, cat-1, cat-2 for annotation, and use labelme2coco to convert annotations.json, where the annotation is [], what's the matter?

FANGAreNotGnu pushed a commit to FANGAreNotGnu/mmdetection that referenced this issue Oct 23, 2023
* [security] Updated pillow and numpy

# safety check

+============================+===========+=====================================+
| REPORT                                                                       |
| checked 148 packages, using free DB (updated once a month)                   |
+============================+===========+==========================+==========+
| package                    | installed | affected                 | ID       |
+============================+===========+==========================+==========+
| pillow                     | 8.3.2     | <9.0.0                   | 44487    |
| pillow                     | 8.3.2     | <9.0.0                   | 44485    |
| pillow                     | 8.3.2     | <9.0.0                   | 44524    |
| pillow                     | 8.3.2     | <9.0.0                   | 44525    |
| pillow                     | 8.3.2     | <9.0.0                   | 44486    |
| numpy                      | 1.21.5    | <1.22.0                  | 44716    |
| numpy                      | 1.21.5    | <1.22.0                  | 44717    |
| numpy                      | 1.21.5    | >0                       | 44715    |
+==============================================================================+

* Updated tests to use smaller version of covertype for multiclass tests

* Updated tests to use smaller version of covertype for multiclass tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants