Mmdetection reports an error when running its own coco dataset. Does not match the length of \ ` classes \ ` 80) in cocodataset

Mmdetection trains its own data set to report errors ⚠️ :

# AssertionError: The `num_ classes` (3) in Shared2FCBBoxHead of MMDataParallel does not matches the length of `CLASSES` 80) in CocoDataset

This means that the category (3) you specified does not match the category (80) of cocodataset.

You may have modified the following files, but you still report an error:

mmdetection-master\mmdet\core\evaluation\class_ names.py

 def coco_classes():
     return [
         # 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
         # 'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign',
         # 'parking_meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
         # 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
         # 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
         # 'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard',
         # 'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'fork',
         # 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
         # 'broccoli', 'carrot', 'hot_dog', 'pizza', 'donut', 'cake', 'chair',
         # 'couch', 'potted_plant', 'bed', 'dining_table', 'toilet', 'tv',
         # 'laptop', 'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave',
         # 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
         # 'scissors', 'teddy_bear', 'hair_drier', 'toothbrush'
         'lm','ls'
     ]

mmdetection-master\mmdet\datasets\coco.py

 class CocoDataset(CustomDataset):
     # CLASSES = (
     #     'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
     #            'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
     #            'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
     #            'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
     #            'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
     #            'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
     #            'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
     #            'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
     #            'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
     #            'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
     #            'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
     #            'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
     #            'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
     #            'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
     #     )
     CLASSES = ('lm', 'ls')

No more nonsense, go straight to the method. There are several methods:

one ️⃣ If you have two classes, you can replace the first two classes in the above two places with your classes. The method is relatively simple, but there may be hidden dangers.

two ️⃣ The second method is to modify the class_ After names.py and voc.py, the code must be recompiled (run Python setup.py install) and then trained.

I tried, but I still made the same mistake. Maybe my method is wrong.

reference resources:

New mmdetection v2.3.0 training test notes – it610.com

Mmdetectionv2. X version trains its own VOC dataset_ Peach jam Momo blog – CSDN blog

three ️⃣ The third method, which I use, is actually the same as recompilation. The reason for recompilation is that you report an error because the source file in the environment has not been modified. There are only some Python files in the mmdetection master directory. When the program is actually running, it is still the source file in the environment, because we directly modify the source file in the environment.

Suppose my CONDA environment is called CONDA_ env_ Name, so go to the following directory and modify two files respectively:

\anaconda3\envs\conda_ env_ name\lib\python3.7\site-packages\mmdet\core\evaluation\class_ names.py

\anaconda3\envs\conda_ env_ name\lib\python3.7\site-packages\mmdet\datasets\coco.py

Modify the categories in these two files in the CONDA environment.

⭐ In the end, I did my best to solve this bug and write this blog to help you avoid detours.

Read More: