FCOS for object detecion: TypeError: __init__() got an unexpected keyword argument 'data_root'
ly-lim opened this issue · 0 comments
Describe the bug
I encountered this problem when I followed the document on this website 'https://mmdetection.readthedocs.io/en/latest/user_guides/new_model.html' to train my own dataset. The difference from the document is that I trained the FCOS model, while the document used Mask R-CNN.
Reproduction
- I followed the tutorial in the document and created a config file named 'fcos-r18_fpn_cattle_coco.py', which is located in the 'config/fcos' directory. The config file is as follows.
_base_ = './fcos_r50_fpn_gn-head-center-normbbox-centeronreg-giou_8xb8-amp-lsj-200e_coco.py' # noqa
model = dict(
backbone=dict(
depth=18,
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet18')),
neck=dict(in_channels=[64, 128, 256, 512]),
bbox_head=dict(num_classes=1),
)
# Modify dataset related settings
data_root = '/home/ly/code/objectDetection/mmdetection/data/cattleCOCO'
metainfo = {
'classes': ('cattle', ),
# 'palette': [
# (220, 20, 60),
# ]
}
train_dataloader = dict(
batch_size=1,
dataset=dict(
data_root=data_root,
metainfo=metainfo,
ann_file='annotations/instances_train.json',
data_prefix=dict(img='train/'))
)
val_dataloader = dict(
dataset=dict(
data_root=data_root,
metainfo=metainfo,
ann_file='annotations/instances_val.json',
data_prefix=dict(img='val/'))
)
# test_dataloader = val_dataloader
test_dataloader = dict(
dataset=dict(
data_root=data_root,
metainfo=metainfo,
ann_file='annotations/instances_test.json',
data_prefix=dict(img='test/'))
)
# Modify metric related settings
val_evaluator = dict(ann_file=data_root + 'annotations/instances_val.json')
test_evaluator = dict(ann_file=data_root + 'annotations/instances_test.json')
- Then I ran the following command.
python tools/train.py configs/fcos/fcos-r18_fpn_cattle_coco.pypython
Press the Enter key, and the command line output is as follows:
Traceback (most recent call last):
File "tools/train.py", line 121, in <module>
main()
File "tools/train.py", line 117, in main
runner.train()
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1728, in train
self._train_loop = self.build_train_loop(
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1520, in build_train_loop
loop = LOOPS.build(
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/registry/registry.py", line 570, in build
return self.build_func(cfg, *args, **kwargs, registry=self)
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 121, in build_from_cfg
obj = obj_cls(**args) # type: ignore
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/runner/loops.py", line 46, in __init__
super().__init__(runner, dataloader)
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/runner/base_loop.py", line 26, in __init__
self.dataloader = runner.build_dataloader(
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1370, in build_dataloader
dataset = DATASETS.build(dataset_cfg)
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/registry/registry.py", line 570, in build
return self.build_func(cfg, *args, **kwargs, registry=self)
File "/home/ly/anaconda3/envs/mm/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 121, in build_from_cfg
obj = obj_cls(**args) # type: ignore
TypeError: __init__() got an unexpected keyword argument 'data_root'
Bug fix
I finally discovered that the issue was due to the base configuration file I referenced, which included a file named 'lsj-100e_coco-detection.py' located in the 'config/common' directory. Within that file, the definition of train_dataloader contains a dictionary within the dataset dictionary with the key 'dataset'. I resolved the issue by modifying my configuration file to add a dictionary with the key 'dataset'. The modified configuration file is as follows.
_base_ = './fcos_r50_fpn_gn-head-center-normbbox-centeronreg-giou_8xb8-amp-lsj-200e_coco.py' # noqa
model = dict(
backbone=dict(
depth=18,
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet18')),
neck=dict(in_channels=[64, 128, 256, 512]),
bbox_head=dict(num_classes=1),
)
# Modify dataset related settings
data_root = '/home/ly/code/objectDetection/mmdetection/data/cattleCOCO'
metainfo = {
'classes': ('cattle', ),
# 'palette': [
# (220, 20, 60),
# ]
}
train_dataloader = dict(
batch_size=1,
dataset=dict(
dataset=dict(
data_root=data_root,
metainfo=metainfo,
ann_file='annotations/instances_train.json',
data_prefix=dict(img='train/')))
)
val_dataloader = dict(
dataset=dict(
dataset=dict(
data_root=data_root,
metainfo=metainfo,
ann_file='annotations/instances_val.json',
data_prefix=dict(img='val/')))
)
# test_dataloader = val_dataloader
test_dataloader = dict(
dataset=dict(
dataset=dict(
data_root=data_root,
metainfo=metainfo,
ann_file='annotations/instances_test.json',
data_prefix=dict(img='test/')))
)
# Modify metric related settings
val_evaluator = dict(ann_file=data_root + 'annotations/instances_val.json')
test_evaluator = dict(ann_file=data_root + 'annotations/instances_test.json')