open-mmlab / mmdetection

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

Custom dataset question - classes specification #2755

Closed ecm200 closed 4 years ago

ecm200 commented 4 years ago

I am training a custom dataset, which has been converted to COCO dataset format with annotations saved in a JSON file. I am using the COCO model configs as the default configuration and changing the configuration to suit my specific problem.

I have modified the dataset dictionaries to include an updated classes list (in my case it is only a binary problem initially, although plans are to expand to multiple classes). The modified configuration file is shown below for reference.

I have made sure to specify the number of classes in both the bounding box head and the mask head classifiers, but all other parameters remain the same as the COCO example.

Are there any other specifications that need to be changed when changing the number of classes of objects?

_base_ = [
    '../configs/_base_/models/mask_rcnn_r50_fpn.py',
    '../configs/_base_/datasets/coco_instance.py',
    '../configs/_base_/schedules/schedule_1x.py', 
    '../configs/_base_/default_runtime.py'
]
dataset_type = 'CocoDataset'
classes=['particle']
data_root = 'datasets/spherical_test_data_v1p1_5000_1500/' # 'datasets/spherical_test_data_v1_5000_1500/'
# Update model due to classes
model = dict(
    pretrained=None, # Don't load the pretrained weights. TODO Find out if this is needed changing number of classes.
    roi_head=dict(
        bbox_head=dict(
            type='Shared2FCBBoxHead',
            in_channels=256,
            fc_out_channels=1024,
            roi_feat_size=7,
            num_classes=len(classes), # Modified to number of classes in this problem. COCO default is 80.
            bbox_coder=dict(
                type='DeltaXYWHBBoxCoder',
                target_means=[0., 0., 0., 0.],
                target_stds=[0.1, 0.1, 0.2, 0.2]),
            reg_class_agnostic=False,
            loss_cls=dict(
                type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
            loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0)), # This is L1Loss in standard COCO, specified this way in CityScapes
        mask_head=dict(
            type='FCNMaskHead',
            num_convs=4,
            in_channels=256,
            conv_out_channels=256,
            num_classes=len(classes), # Modified to number of classes in this problem. COCO default is 80.
            loss_mask=dict(
                type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)))
)
img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
train_pipeline = [
    dict(type='LoadMorphologiSynImage'),
    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
    dict(type='Resize', img_scale=(1296, 972), keep_ratio=True),
    dict(type='Normalize', **img_norm_cfg),
    #dict(type='Pad', size_divisor=32),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']),
]
test_pipeline = [
    dict(type='LoadMorphologiSynImage'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(1296, 972),
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True),
            dict(type='Normalize', **img_norm_cfg),
            #dict(type='Pad', size_divisor=32),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img']),
        ])
]
data = dict(
    imgs_per_gpu=2,
    workers_per_gpu=5,
    train=dict(
        type=dataset_type,
        classes=classes,
        ann_file=data_root + 'annotations/train_coco.json',
        img_prefix=data_root + 'train/',
        pipeline=train_pipeline),
    val=dict(
        type=dataset_type,
        classes=classes,
        ann_file=data_root + 'annotations/valid_coco.json',
        img_prefix=data_root + 'valid/',
        pipeline=test_pipeline),
    test=dict(
        type=dataset_type,
        classes=classes,
        ann_file=data_root + 'annotations/valid_coco.json',
        img_prefix=data_root + 'valid/',
        pipeline=test_pipeline))
optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001)
checkpoint_config = dict(interval=1)
log_config = dict(
    interval=50,
    hooks=[
        dict(type='TextLoggerHook'),
        dict(type='TensorboardLoggerHook')
    ])
total_epochs = 30
gpus = 1
aimhabo commented 4 years ago

make a Derived class of CocoDataset like ../mmdet/datasets/cityscapes.py

ZwwWayne commented 4 years ago

Hi @ecm200 , Yes there is sth to do. You do not need to modify the code, but you need to set the classes of dataset through the config, like that in the Modify classes of existing dataset of tutorial. Then it should be fine.

we1pingyu commented 4 years ago

Same question as topic. Tutorial doesnot work.

ZwwWayne commented 4 years ago

Hi @we1pingyu , Could you open a new issue following the Error Template? Please be more specific about your doesnot work thus we could more precisely locate the problem and fix it.

we1pingyu commented 4 years ago

https://github.com/open-mmlab/mmdetection/issues/2965#issue-635488115 thank you for your reply

we1pingyu commented 4 years ago

@ZwwWayne