open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.14k stars 9.39k forks source link

Cannot load my own coco data #7602

Closed yeungkong closed 2 years ago

yeungkong commented 2 years ago

I use mmdet v2.23.0 and I set my own data set and try to train my own model.

    cfg.dataset_type = 'CocoDataset'
    cfg.data_root = 'data/'

    cfg.data.test.type = 'CocoDataset'
    cfg.data.test.data_root = 'data/'
    cfg.data.test.ann_file = 'coco/annotations/instances_val2017.json'
    cfg.data.test.img_prefix = 'coco/train2017/'

    cfg.data.train.type = 'CocoDataset'
    cfg.data.train.data_root = 'data/'
    cfg.data.train.ann_file = 'coco/annotations/instances_train2017.json'
    cfg.data.train.img_prefix = 'coco/train2017/'

    cfg.data.val.type = 'CocoDataset'
    cfg.data.val.data_root = 'data/'
    cfg.data.val.ann_file = 'coco/annotations/instances_val2017.json'
    cfg.data.val.img_prefix = 'coco/train2017/'

    datasets = [build_dataset(cfg.data.train)]
    print(datasets)

But I get something like this.

CocoDataset Train dataset with number of images 0, and instance counts: 
+---------------------+-------+-----------------+-------+--------------------+-------+--------------------+-------+-------------------+-------+
| category            | count | category        | count | category           | count | category           | count | category          | count |
+---------------------+-------+-----------------+-------+--------------------+-------+--------------------+-------+-------------------+-------+
| 0 [person]          | 0     | 1 [bicycle]     | 0     | 2 [car]            | 0     | 3 [motorcycle]     | 0     | 4 [airplane]      | 0     |
| 5 [bus]             | 0     | 6 [train]       | 0     | 7 [truck]          | 0     | 8 [boat]           | 0     | 9 [traffic light] | 0     |
| 10 [fire hydrant]   | 0     | 11 [stop sign]  | 0     | 12 [parking meter] | 0     | 13 [bench]         | 0     | 14 [bird]         | 0     |
| 15 [cat]            | 0     | 16 [dog]        | 0     | 17 [horse]         | 0     | 18 [sheep]         | 0     | 19 [cow]          | 0     |
| 20 [elephant]       | 0     | 21 [bear]       | 0     | 22 [zebra]         | 0     | 23 [giraffe]       | 0     | 24 [backpack]     | 0     |
| 25 [umbrella]       | 0     | 26 [handbag]    | 0     | 27 [tie]           | 0     | 28 [suitcase]      | 0     | 29 [frisbee]      | 0     |
| 30 [skis]           | 0     | 31 [snowboard]  | 0     | 32 [sports ball]   | 0     | 33 [kite]          | 0     | 34 [baseball bat] | 0     |
| 35 [baseball glove] | 0     | 36 [skateboard] | 0     | 37 [surfboard]     | 0     | 38 [tennis racket] | 0     | 39 [bottle]       | 0     |
| 40 [wine glass]     | 0     | 41 [cup]        | 0     | 42 [fork]          | 0     | 43 [knife]         | 0     | 44 [spoon]        | 0     |
| 45 [bowl]           | 0     | 46 [banana]     | 0     | 47 [apple]         | 0     | 48 [sandwich]      | 0     | 49 [orange]       | 0     |
| 50 [broccoli]       | 0     | 51 [carrot]     | 0     | 52 [hot dog]       | 0     | 53 [pizza]         | 0     | 54 [donut]        | 0     |
| 55 [cake]           | 0     | 56 [chair]      | 0     | 57 [couch]         | 0     | 58 [potted plant]  | 0     | 59 [bed]          | 0     |
| 60 [dining table]   | 0     | 61 [toilet]     | 0     | 62 [tv]            | 0     | 63 [laptop]        | 0     | 64 [mouse]        | 0     |
| 65 [remote]         | 0     | 66 [keyboard]   | 0     | 67 [cell phone]    | 0     | 68 [microwave]     | 0     | 69 [oven]         | 0     |
| 70 [toaster]        | 0     | 71 [sink]       | 0     | 72 [refrigerator]  | 0     | 73 [book]          | 0     | 74 [clock]        | 0     |
| 75 [vase]           | 0     | 76 [scissors]   | 0     | 77 [teddy bear]    | 0     | 78 [hair drier]    | 0     | 79 [toothbrush]   | 0     |
+---------------------+-------+-----------------+-------+--------------------+-------+--------------------+-------+-------------------+-------+]

I check the build_dataset function in /mmdet/datasets/builder.py and find the following lines are run.

DATASETS = Registry('dataset')
dataset = build_from_cfg(cfg, DATASETS, default_args)

It is not loading my own dataset. Could someone please tell me what goes wrong? Thank you very much.

BIGWangYuDong commented 2 years ago

You should change cocodataset.CLASS and the num_classes in config file. For more details please refer to the tutorials: https://github.com/open-mmlab/mmdetection/blob/master/docs/en/2_new_data_model.md and https://github.com/open-mmlab/mmdetection/blob/master/docs/en/tutorials/customize_dataset.md

yeungkong commented 2 years ago

It works! Really appreciate your help.

Robotatron commented 2 years ago

You should change cocodataset.CLASS and the num_classes in config file. For more details please refer to the tutorials: https://github.com/open-mmlab/mmdetection/blob/master/docs/en/2_new_data_model.md and https://github.com/open-mmlab/mmdetection/blob/master/docs/en/tutorials/customize_dataset.md

Following the tutorial I am getting

2022-07-31 01:52:40,722 - mmdet - INFO - initialize FPN with init_cfg {'type': 'Xavier', 'layer': 'Conv2d', 'distribution': 'uniform'}
2022-07-31 01:52:40,759 - mmdet - INFO - initialize RPNHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01}
2022-07-31 01:52:40,766 - mmdet - INFO - initialize Shared2FCBBoxHead with init_cfg [{'type': 'Normal', 'std': 0.01, 'override': {'name': 'fc_cls'}}, {'type': 'Normal', 'std': 0.001, 'override': {'name': 'fc_reg'}}, {'type': 'Xavier', 'distribution': 'uniform', 'override': [{'name': 'shared_fcs'}, {'name': 'cls_fcs'}, {'name': 'reg_fcs'}]}]
Traceback (most recent call last):
  File "/home/jupyter/notebooks/Unity/mmdetection/tools/train.py", line 242, in <module>
    main()
  File "/home/jupyter/notebooks/Unity/mmdetection/tools/train.py", line 218, in main
    datasets = [build_dataset(cfg.data.train)]
  File "/home/jupyter/notebooks/Unity/mmdetection/mmdet/datasets/builder.py", line 82, in build_dataset
    dataset = build_from_cfg(cfg, DATASETS, default_args)
  File "/opt/conda/envs/icevision/lib/python3.9/site-packages/mmcv/utils/registry.py", line 61, in build_from_cfg
    raise KeyError(
KeyError: 'COCODataset is not in the dataset registry'

My config:

_base_ = ["../mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco.py",]

load_from = 'https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'

model = dict(
    roi_head=dict(
        bbox_head=dict(num_classes=7),
        mask_head=dict(num_classes=7)))

dataset_type = 'COCODataset'
classes = ('hood', 'Door-Front', 'roof', 'tyre', 'Door-Rear', 'Bumper-Front', 'Bumper-Rear')
data_root = '/home/jupyter/data/Unity/03-segm-test-100-images/converted/'
img_prefix = data_root + "images"
data = dict(
    train=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/train.json',
        classes=classes,
        img_prefix=img_prefix,),
    val=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/test.json',
        classes=classes,
        img_prefix=img_prefix,),
    test=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/test.json',
        classes=classes,
        img_prefix=img_prefix)
)