open-mmlab/mmdetection

Training a customised Deformable DETR

saadyousuf45 opened this issue · 2 comments

In deformable DETR code I am implementing:
'''
base = [
'../base/datasets/coco_detection.py', '../base/default_runtime.py'
]

model = dict(
type='DeformableDETR',
backbone=dict(
type='ResNet',
depth=152,
num_stages=10,
out_indices=(1, 2, 3, 4, 5, 6, 7, 8, 9),
frozen_stages=1,
norm_cfg=dict(type='BN', requires_grad=False),
norm_eval=True,
style='pytorch',
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet152')),
neck=dict(
type='ChannelMapper',
in_channels=[128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768],
kernel_size=1,
out_channels=256,
act_cfg=None,
norm_cfg=dict(type='GN', num_groups=32),
num_outs=12
),
bbox_head=dict(
type='DeformableDETRHead',
num_query=300,
num_classes=162,
in_channels=2048,
sync_cls_avg_factor=True,
as_two_stage=False,
transformer=dict(
type='DeformableDetrTransformer',
encoder=dict(
type='DetrTransformerEncoder',
num_layers=6,
transformerlayers=dict(
type='BaseTransformerLayer',
attn_cfgs=dict(
type='MultiScaleDeformableAttention', embed_dims=256),
feedforward_channels=1024,
ffn_dropout=0.1,
operation_order=('self_attn', 'norm', 'ffn', 'norm')
)
)
),
positional_encoding=dict(
type='SinePositionalEncoding',
num_feats=128,
normalize=True,
offset=-0.5
),
loss_cls_config={},
train_cfg=dict(
assigner=dict(
type='HungarianAssigner',
cls_cost=dict(type='FocalLossCost', weight=2.0),
reg_cost=dict(type='BBoxL1Cost', weight=5.0, box_format='xywh'),
iou_cost=dict(type='IoUCost', iou_mode='giou', weight=2.0)
)
),
test_cfg=dict(max_per_img=100)
)
)

loss_cls_config dictionary population

for num_classes in range(0, 161):
if num_classes <= 59:
model['bbox_head']['loss_cls_config'][num_classes] = {
'type': 'FocalLoss',
'use_sigmoid': True,
'gamma': 2.0,
'alpha': 0.25,
'loss_weight': 2.0
}
elif 60 <= num_classes <= 61:
model['bbox_head']['loss_cls_config'][num_classes] = {
'type': 'BinaryCrossEntropyLoss',
'loss_weight': 1.0
}
else:
model['bbox_head']['loss_cls_config'][num_classes] = {
'type': 'L1Loss',
'loss_weight': 1.0
}

Construct the loss configuration

model['bbox_head']['loss_cfg'] = dict(
loss_cls=model['bbox_head']['loss_cls_config'],
loss_bbox=dict(type='L1Loss', loss_weight=5.0),
loss_iou=dict(type='GIoULoss', loss_weight=2.0)
)

img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
train_pipeline = [
# ...
]
test_pipeline = [
# ...
]
data = dict(
samples_per_gpu=2,
workers_per_gpu=2,
train=dict(filter_empty_gt=False, pipeline=train_pipeline),
val=dict(pipeline=test_pipeline),
test=dict(pipeline=test_pipeline)
)
optimizer = dict(
# ...
)
optimizer_config = dict(grad_clip=dict(max_norm=0.1, norm_type=2))
lr_config = dict(policy='step', step=[40])
runner = dict(type='EpochBasedRunner', max_epochs=50)
auto_scale_lr = dict(base_batch_size=32)
'''

Error I am getting:

TypeError Traceback (most recent call last)
File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:69, in build_from_cfg(cfg, registry, default_args)
68 try:
---> 69 return obj_cls(**args)
70 except Exception as e:
71 # Normal TypeError does not print class name.

TypeError: init() got an unexpected keyword argument 'bg_cls_weight'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:69, in build_from_cfg(cfg, registry, default_args)
68 try:
---> 69 return obj_cls(**args)
70 except Exception as e:
71 # Normal TypeError does not print class name.

File ~/Desktop/mmdetection/mmdet/models/dense_heads/deformable_detr_head.py:47, in DeformableDETRHead.init(self, with_box_refine, as_two_stage, transformer, *args, **kwargs)
45 transformer['as_two_stage'] = self.as_two_stage
---> 47 super(DeformableDETRHead, self).init(
48 *args, transformer=transformer, **kwargs)

File ~/Desktop/mmdetection/mmdet/models/dense_heads/detr_head.py:130, in DETRHead.init(self, num_classes, in_channels, num_query, num_reg_fcs, transformer, sync_cls_avg_factor, positional_encoding, loss_cls, loss_bbox, loss_iou, train_cfg, test_cfg, init_cfg, **kwargs)
129 self.fp16_enabled = False
--> 130 self.loss_cls = build_loss(loss_cls)
131 self.loss_bbox = build_loss(loss_bbox)

File ~/Desktop/mmdetection/mmdet/models/builder.py:45, in build_loss(cfg)
44 """Build loss."""
---> 45 return LOSSES.build(cfg)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:237, in Registry.build(self, *args, **kwargs)
236 def build(self, *args, **kwargs):
--> 237 return self.build_func(*args, **kwargs, registry=self)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/cnn/builder.py:27, in build_model_from_cfg(cfg, registry, default_args)
26 else:
---> 27 return build_from_cfg(cfg, registry, default_args)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:72, in build_from_cfg(cfg, registry, default_args)
70 except Exception as e:
71 # Normal TypeError does not print class name.
---> 72 raise type(e)(f'{obj_cls.name}: {e}')

TypeError: CrossEntropyLoss: init() got an unexpected keyword argument 'bg_cls_weight'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:69, in build_from_cfg(cfg, registry, default_args)
68 try:
---> 69 return obj_cls(**args)
70 except Exception as e:
71 # Normal TypeError does not print class name.

File ~/Desktop/mmdetection/mmdet/models/detectors/deformable_detr.py:10, in DeformableDETR.init(self, *args, **kwargs)
9 def init(self, *args, **kwargs):
---> 10 super(DETR, self).init(*args, **kwargs)

File ~/Desktop/mmdetection/mmdet/models/detectors/single_stage.py:37, in SingleStageDetector.init(self, backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg)
36 bbox_head.update(test_cfg=test_cfg)
---> 37 self.bbox_head = build_head(bbox_head)
38 self.train_cfg = train_cfg

File ~/Desktop/mmdetection/mmdet/models/builder.py:40, in build_head(cfg)
39 """Build head."""
---> 40 return HEADS.build(cfg)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:237, in Registry.build(self, *args, **kwargs)
236 def build(self, *args, **kwargs):
--> 237 return self.build_func(*args, **kwargs, registry=self)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/cnn/builder.py:27, in build_model_from_cfg(cfg, registry, default_args)
26 else:
---> 27 return build_from_cfg(cfg, registry, default_args)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:72, in build_from_cfg(cfg, registry, default_args)
70 except Exception as e:
71 # Normal TypeError does not print class name.
---> 72 raise type(e)(f'{obj_cls.name}: {e}')

TypeError: DeformableDETRHead: CrossEntropyLoss: init() got an unexpected keyword argument 'bg_cls_weight'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
Input In [8], in <cell line: 9>()
4 from mmdet.utils import get_device
7 datasets = [build_dataset(cfg.data.train)]
----> 9 model = build_detector(
10 cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))
12 model.CLASSES = datasets[0].CLASSES
14 #model.CLASSES = datasets[0].CLASSES[-100:]

File ~/Desktop/mmdetection/mmdet/models/builder.py:58, in build_detector(cfg, train_cfg, test_cfg)
54 assert cfg.get('train_cfg') is None or train_cfg is None,
55 'train_cfg specified in both outer field and model field '
56 assert cfg.get('test_cfg') is None or test_cfg is None,
57 'test_cfg specified in both outer field and model field '
---> 58 return DETECTORS.build(
59 cfg, default_args=dict(train_cfg=train_cfg, test_cfg=test_cfg))

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:237, in Registry.build(self, *args, **kwargs)
236 def build(self, *args, **kwargs):
--> 237 return self.build_func(*args, **kwargs, registry=self)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/cnn/builder.py:27, in build_model_from_cfg(cfg, registry, default_args)
25 return Sequential(*modules)
26 else:
---> 27 return build_from_cfg(cfg, registry, default_args)

File ~/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/registry.py:72, in build_from_cfg(cfg, registry, default_args)
69 return obj_cls(**args)
70 except Exception as e:
71 # Normal TypeError does not print class name.
---> 72 raise type(e)(f'{obj_cls.name}: {e}')

TypeError: DeformableDETR: DeformableDETRHead: CrossEntropyLoss: init() got an unexpected keyword argument 'bg_cls_weight'

Please advise what can I do to get rid of it.

Any progress?

I am reading DETR paper. I noticed that most papers using the mmdetection framework have PyTorch version 1.9 and cuda 10.2. However, when I tried to reproduce it using an NVIDIA 3060 graphics card, I found that the versions didn't match. It seems that PyTorch 1.9.0 is a bit too low. How can I solve this issue? Looking forward to your reply.