open-mmlab / mmdetection

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

TypeError: SwinTransformer: __init__() got an unexpected keyword argument 'pretrained' #5392

Closed Bin-ze closed 3 years ago

Bin-ze commented 3 years ago

I try to register swin's backbone to my code, but when I run the code, the following problems occur: Traceback (most recent call last): File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg return obj_cls(**args) TypeError: init() got an unexpected keyword argument 'pretrained'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg return obj_cls(*args) File "/home/liuguangcan/mmdetection/mmdet/models/detectors/centernet.py", line 25, in init test_cfg, pretrained, init_cfg) File "/home/liuguangcan/mmdetection/mmdet/models/detectors/single_stage.py", line 26, in init self.backbone = build_backbone(backbone) File "/home/liuguangcan/mmdetection/mmdet/models/builder.py", line 19, in build_backbone return BACKBONES.build(cfg) File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/utils/registry.py", line 210, in build return self.build_func(args, **kwargs, registry=self) File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/cnn/builder.py", line 26, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.name}: {e}') TypeError: SwinTransformer: init() got an unexpected keyword argument 'pretrained'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/liuguangcan/mmdetection/tools/debug_train.py", line 190, in main() File "/home/liuguangcan/mmdetection/tools/debug_train.py", line 164, in main test_cfg=cfg.get('test_cfg')) File "/home/liuguangcan/mmdetection/mmdet/models/builder.py", line 58, in build_detector cfg, default_args=dict(train_cfg=train_cfg, test_cfg=test_cfg)) File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/utils/registry.py", line 210, in build return self.build_func(*args, **kwargs, registry=self) File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/cnn/builder.py", line 26, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "/home/liuguangcan/anaconda3/envs/open-mmlab/lib/python3.6/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.name}: {e}') TypeError: CenterNet: SwinTransformer: init() got an unexpected keyword argument 'pretrained'

my config file as following: 2021-06-19 04:10:00,734 - mmdet - INFO - Environment info:

sys.platform: linux Python: 3.6.13 |Anaconda, Inc.| (default, Feb 23 2021, 21:15:04) [GCC 7.3.0] CUDA available: True GPU 0,1,2,3: GeForce RTX 2080 Ti CUDA_HOME: /usr/local/cuda NVCC: Cuda compilation tools, release 10.1, V10.1.105 GCC: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609 PyTorch: 1.7.1 PyTorch compiling details: PyTorch built with:

TorchVision: 0.8.2 OpenCV: 4.5.2 MMCV: 1.3.7 MMCV Compiler: GCC 5.4 MMCV CUDA Compiler: 10.1 MMDetection: 2.13.0+dc12b09

2021-06-19 04:10:01,187 - mmdet - INFO - Distributed training: False 2021-06-19 04:10:01,512 - mmdet - INFO - Config: dataset_type = 'CocoDataset' data_root = '../data/coco/' 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='LoadImageFromFile', to_float32=True, color_type='color'), dict(type='LoadAnnotations', with_bbox=True), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict( type='RandomCenterCropPad', crop_size=(512, 512), ratios=(0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3), mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_pad_mode=None), dict(type='Resize', img_scale=(512, 512), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ] test_pipeline = [ dict(type='LoadImageFromFile', to_float32=True), dict( type='MultiScaleFlipAug', scale_factor=1.0, flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict( type='RandomCenterCropPad', ratios=None, border=None, mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_mode=True, test_pad_mode=['logical_or', 31], test_pad_add_pix=1), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='DefaultFormatBundle'), dict( type='Collect', meta_keys=('filename', 'ori_shape', 'img_shape', 'pad_shape', 'scale_factor', 'flip', 'flip_direction', 'img_norm_cfg', 'border'), keys=['img']) ]) ] data = dict( samples_per_gpu=16, workers_per_gpu=4, train=dict( type='CocoDataset', ann_file='../data/coco/annotations/instances_train2017.json', img_prefix='../data/coco/train2017/', pipeline=[ dict( type='LoadImageFromFile', to_float32=True, color_type='color'), dict(type='LoadAnnotations', with_bbox=True), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict( type='RandomCenterCropPad', crop_size=(512, 512), ratios=(0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3), mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_pad_mode=None), dict(type='Resize', img_scale=(512, 512), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ]), val=dict( type='CocoDataset', ann_file='../data/coco/annotations/instances_val2017.json', img_prefix='../data/coco/val2017/', pipeline=[ dict(type='LoadImageFromFile', to_float32=True), dict( type='MultiScaleFlipAug', scale_factor=1.0, flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict( type='RandomCenterCropPad', ratios=None, border=None, mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_mode=True, test_pad_mode=['logical_or', 31], test_pad_add_pix=1), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='DefaultFormatBundle'), dict( type='Collect', meta_keys=('filename', 'ori_shape', 'img_shape', 'pad_shape', 'scale_factor', 'flip', 'flip_direction', 'img_norm_cfg', 'border'), keys=['img']) ]) ]), test=dict( type='CocoDataset', ann_file='../data/coco/annotations/instances_val2017.json', img_prefix='../data/coco/val2017/', pipeline=[ dict(type='LoadImageFromFile', to_float32=True), dict( type='MultiScaleFlipAug', scale_factor=1.0, flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict( type='RandomCenterCropPad', ratios=None, border=None, mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_mode=True, test_pad_mode=['logical_or', 31], test_pad_add_pix=1), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='DefaultFormatBundle'), dict( type='Collect', meta_keys=('filename', 'ori_shape', 'img_shape', 'pad_shape', 'scale_factor', 'flip', 'flip_direction', 'img_norm_cfg', 'border'), keys=['img']) ]) ])) evaluation = dict(interval=5, metric='bbox') optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001) optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2)) lr_config = dict( policy='step', warmup='linear', warmup_iters=1000, warmup_ratio=0.001, step=[90, 120]) runner = dict(type='EpochBasedRunner', max_epochs=140) checkpoint_config = dict(interval=5) log_config = dict(interval=50, hooks=[dict(type='TextLoggerHook')]) custom_hooks = [dict(type='NumClassCheckHook')] dist_params = dict(backend='nccl') log_level = 'INFO' load_from = None resume_from = None workflow = [('train', 1)] model = dict( type='CenterNet', pretrained='None', backbone=dict( type='SwinTransformer', embed_dim=128, depths=[2, 2, 6, 2], num_heads=[3, 6, 12, 24], window_size=7, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop_rate=0.0, attn_drop_rate=0.0, drop_path_rate=0.2, ape=False, patch_norm=True, out_indices=(0, 1, 2, 3), use_checkpoint=False), neck=dict( type='CTResNetNeck', in_channel=512, num_deconv_filters=(256, 128, 64), num_deconv_kernels=(4, 4, 4), use_dcn=True), bbox_head=dict( type='CenterNetHead', num_classes=80, in_channel=64, feat_channel=64, loss_center_heatmap=dict(type='GaussianFocalLoss', loss_weight=1.0), loss_wh=dict(type='L1Loss', loss_weight=0.1), loss_offset=dict(type='L1Loss', loss_weight=1.0)), train_cfg=None, test_cfg=dict(topk=100, local_maximum_kernel=3, max_per_img=100)) work_dir = './work_dirs/centernet_swin_140e_coco' gpu_ids = [0] I don’t know if this is a bug or what’s wrong with me, I hope someone can help me

shinya7y commented 3 years ago

backbones/swin_transformer.py should be updated for MMDetection >= 2.12.0. Example: https://github.com/shinya7y/UniverseNet/commit/56b9a877254a7b7da1a85b6af7b3d1b7f4d7fb4a#diff-dd4367c7411bef92546776eb142ceb3af12a587bc62eb998f563a2048d070961

Bin-ze commented 3 years ago

I just use it as a backbone. If I put it under the latest mmdetection version, can I achieve the goal by simply modifying the swin code instead of reconfiguring it to adapt to its environment?

Bin-ze commented 3 years ago

think you very much

myGithubSiki commented 3 years ago

hi, like you,the following problems occur:

Traceback (most recent call last): File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg return obj_cls(**args) TypeError: init() got an unexpected keyword argument 'pretrained'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg return obj_cls(args) File "/home/rpz/rotmmdet-master/mmdetrot/models/detectors/single_stageR.py", line 10, in init super(SingleStageDetectorR, self).init(*args, *kwargs) File "/home/rpz/mmdet-yolov4-master/mmdet/models/detectors/single_stage.py", line 26, in init self.backbone = build_backbone(backbone) File "/home/rpz/mmdet-yolov4-master/mmdet/models/builder.py", line 19, in build_backbone return BACKBONES.build(cfg) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 210, in build return self.build_func(args, kwargs, registry=self) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/cnn/builder.py", line 26, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.name}: {e}') TypeError: SwinTransformer: init() got an unexpected keyword argument 'pretrained'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/rpz/rotmmdet-master/tools/train.py", line 191, in main() File "/home/rpz/rotmmdet-master/tools/train.py", line 165, in main test_cfg=cfg.get('test_cfg')) File "/home/rpz/mmdet-yolov4-master/mmdet/models/builder.py", line 58, in build_detector cfg, default_args=dict(train_cfg=train_cfg, test_cfg=test_cfg)) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 210, in build return self.build_func(*args, **kwargs, registry=self) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/cnn/builder.py", line 26, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.name}: {e}') TypeError: SingleStageDetectorR: SwinTransformer: init() got an unexpected keyword argument 'pretrained'

can you help me solve this problem?

Bin-ze commented 3 years ago

hi, like you,the following problems occur:

Traceback (most recent call last): File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg return obj_cls(args) TypeError: init**() got an unexpected keyword argument 'pretrained'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg return obj_cls(args) File "/home/rpz/rotmmdet-master/mmdetrot/models/detectors/single_stageR.py", line 10, in init super(SingleStageDetectorR, self).init(*args, kwargs) File "/home/rpz/mmdet-yolov4-master/mmdet/models/detectors/single_stage.py", line 26, in init* self.backbone = build_backbone(backbone) File "/home/rpz/mmdet-yolov4-master/mmdet/models/builder.py", line 19, in build_backbone return BACKBONES.build(cfg) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 210, in build return self.build_func(args, kwargs, registry=self) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/cnn/builder.py", line 26, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.name}: {e}') TypeError: SwinTransformer: init() got an unexpected keyword argument 'pretrained'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/rpz/rotmmdet-master/tools/train.py", line 191, in main() File "/home/rpz/rotmmdet-master/tools/train.py", line 165, in main test_cfg=cfg.get('test_cfg')) File "/home/rpz/mmdet-yolov4-master/mmdet/models/builder.py", line 58, in build_detector cfg, default_args=dict(train_cfg=train_cfg, test_cfg=test_cfg)) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 210, in build return self.build_func(*args, kwargs, registry=self) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/cnn/builder.py", line 26, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "/home/rpz/anaconda3/envs/swim_transformer_ryolov5_gwd/lib/python3.7/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg raise type(e)(f'{obj_cls.name}: {e}') TypeError: SingleStageDetectorR: SwinTransformer: init**() got an unexpected keyword argument 'pretrained'

can you help me solve this problem?

If you want to use s-win as the backbone of YOLOV5, you should register the backbone of s-win when you change the configuration file of mm-detection. Your s-win network should also be obtained from the official detection s-win network, or from the following: backbones /swin_transformer.py should be updated for MM-Detection >= 2.12.0. Example: shinya7y/UniverseNet@56b9a87#diff-dd4367c7411bef92546776eb142ceb3af12a587bc62eb998f563a2048d070961 to obtain,

Your problem is probably because the s-win version you are using is not compatible with mm-detection. You can get the backbone from the above URL and copy it to mmdet/model/backbone, and it should be resolved.