mmpretrain icon indicating copy to clipboard operation
mmpretrain copied to clipboard

[Bug]AttributeError: 'ConfigDict' object has no attribute 'model'

Open Crower-1 opened this issue 3 years ago • 10 comments

Describe the bug

I was going through Visualization.

I try to ran

python tools/visualizations/vis_cam.py \
    demo/bird.JPEG \
    configs/resnet/resnet50_8xb32_in1k.py \
    https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_batch256_imagenet_20200708-cfb998bf.pth \
    --method GradCAM
    # GradCAM++, XGradCAM, EigenCAM, EigenGradCAM, LayerCAM

It raised the following error:

Traceback (most recent call last):
  File "tools/visualizations/vis_cam.py", line 356, in <module>
    main()
  File "tools/visualizations/vis_cam.py", line 310, in main
    model = init_model(cfg, args.checkpoint, device=args.device)
  File "/public/liushuo/mmclassification-master/mmcls/apis/inference.py", line 34, in init_model
    config.model.pretrained = None
  File "/public/apps/anaconda3/envs/liushuo_UNet/lib/python3.8/site-packages/mmcv/utils/config.py", line 519, in __getattr__
    return getattr(self._cfg_dict, name)
  File "/public/apps/anaconda3/envs/liushuo_UNet/lib/python3.8/site-packages/mmcv/utils/config.py", line 50, in __getattr__
    raise ex
AttributeError: 'ConfigDict' object has no attribute 'model'

Post related information

  1. The output of pip list | grep "mmcv\|mmcls\|^torch"
mmcls                          0.23.0                /public/liushuo/mmclassification-master
mmcv-full                      1.5.1
torch                          1.10.2
torchaudio                     0.10.2
torchsummary                   1.5.1
torchvision                    0.11.3

Please give me some advice to fix it. Thanks.

Crower-1 avatar May 27 '22 07:05 Crower-1

Can you provide your configs/resnet/resnet50_8xb32_in1k.py file?

mzr1996 avatar May 27 '22 07:05 mzr1996

This is the config:

Config:
model = dict(
    type='ImageClassifier',
    backbone=dict(
        type='ResNet',
        depth=50,
        num_stages=4,
        out_indices=(3, ),
        style='pytorch'),
    neck=dict(type='GlobalAveragePooling'),
    head=dict(
        type='LinearClsHead',
        num_classes=1000,
        in_channels=2048,
        loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
        topk=(1, 5)))
dataset_type = 'ImageNet'
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'),
    dict(type='RandomResizedCrop', size=224),
    dict(type='RandomFlip', flip_prob=0.5, direction='horizontal'),
    dict(
        type='Normalize',
        mean=[123.675, 116.28, 103.53],
        std=[58.395, 57.12, 57.375],
        to_rgb=True),
    dict(type='ImageToTensor', keys=['img']),
    dict(type='ToTensor', keys=['gt_label']),
    dict(type='Collect', keys=['img', 'gt_label'])
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='Resize', size=(256, -1)),
    dict(type='CenterCrop', crop_size=224),
    dict(
        type='Normalize',
        mean=[123.675, 116.28, 103.53],
        std=[58.395, 57.12, 57.375],
        to_rgb=True),
    dict(type='ImageToTensor', keys=['img']),
    dict(type='Collect', keys=['img'])
]
data = dict(
    samples_per_gpu=32,
    workers_per_gpu=2,
    train=dict(
        type='ImageNet',
        data_prefix='data/imagenet/train',
        pipeline=[
            dict(type='LoadImageFromFile'),
            dict(type='RandomResizedCrop', size=224),
            dict(type='RandomFlip', flip_prob=0.5, direction='horizontal'),
            dict(
                type='Normalize',
                mean=[123.675, 116.28, 103.53],
                std=[58.395, 57.12, 57.375],
                to_rgb=True),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='ToTensor', keys=['gt_label']),
            dict(type='Collect', keys=['img', 'gt_label'])
        ]),
    val=dict(
        type='ImageNet',
        data_prefix='data/imagenet/val',
        ann_file='data/imagenet/meta/val.txt',
        pipeline=[
            dict(type='LoadImageFromFile'),
            dict(type='Resize', size=(256, -1)),
            dict(type='CenterCrop', crop_size=224),
            dict(
                type='Normalize',
                mean=[123.675, 116.28, 103.53],
                std=[58.395, 57.12, 57.375],
                to_rgb=True),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img'])
        ]),
    test=dict(
        type='ImageNet',
        data_prefix='data/imagenet/val',
        ann_file='data/imagenet/meta/val.txt',
        pipeline=[
            dict(type='LoadImageFromFile'),
            dict(type='Resize', size=(256, -1)),
            dict(type='CenterCrop', crop_size=224),
            dict(
                type='Normalize',
                mean=[123.675, 116.28, 103.53],
                std=[58.395, 57.12, 57.375],
                to_rgb=True),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img'])
        ]))
evaluation = dict(interval=1, metric='accuracy')
optimizer = dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=None)
lr_config = dict(policy='step', step=[30, 60, 90])
runner = dict(type='EpochBasedRunner', max_epochs=100)
checkpoint_config = dict(interval=10)
log_config = dict(interval=100, hooks=[dict(type='TextLoggerHook')])
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = None
resume_from = None
workflow = [('train', 1)]

Crower-1 avatar May 27 '22 08:05 Crower-1

A little strange, can you execuate the below command in Python interpreter, and provide the output

>>> from mmcv import Config
>>> from mmcls.apis import init_model
>>> cfg = Config.fromfile('configs/resnet/resnet50_8xb32_in1k.py')
>>> init_model(cfg)

mzr1996 avatar May 27 '22 08:05 mzr1996

It raised the same error😵:

Python 3.8.11 (default, Aug  3 2021, 15:09:35) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from mmcv import Config
>>> from mmcls.apis import init_model
>>> cfg = Config.fromfile('configs/resnet/resnet50_8xb32_in1k.py')
>>> init_model(cfg)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/public/liushuo/mmclassification-master/mmcls/apis/inference.py", line 34, in init_model
    config.model.pretrained = None
  File "/public/apps/anaconda3/envs/liushuo_UNet/lib/python3.8/site-packages/mmcv/utils/config.py", line 510, in __getattr__
    return getattr(self._cfg_dict, name)
  File "/public/apps/anaconda3/envs/liushuo_UNet/lib/python3.8/site-packages/mmcv/utils/config.py", line 48, in __getattr__
    raise ex
AttributeError: 'ConfigDict' object has no attribute 'model'

Crower-1 avatar May 27 '22 08:05 Crower-1

Ok, please print the cfg variable

mzr1996 avatar May 27 '22 08:05 mzr1996

Here is the output

>>> print(cfg)
Config (path: configs/resnet/resnet50_8xb32_in1k.py): {}
>>> 

Crower-1 avatar May 27 '22 08:05 Crower-1

Can you provide your raw configs/resnet/resnet50_8xb32_in1k.py? Looks mmcv.Config failed to read your config file. Maybe an mmcv issue.

mzr1996 avatar May 27 '22 08:05 mzr1996

Here is the content in file configs/resnet/resnet50_8xb32_in1k.py:

_base_ = [
    '../_base_/models/resnet50.py', '../_base_/datasets/imagenet_bs32.py',
    '../_base_/schedules/imagenet_bs256.py', '../_base_/default_runtime.py'
]

I had try mmcv-full version 1.4.2 and 1.4.3, but it got the same error😢.

Crower-1 avatar May 27 '22 08:05 Crower-1

What about configs/_base_/models/resnet50.py?What's the content of it?

mzr1996 avatar May 27 '22 08:05 mzr1996

Here is the content of configs/_base_/models/resnet50.py :

# model settings
model = dict(
    type='ImageClassifier',
    backbone=dict(
        type='ResNet',
        depth=50,
        num_stages=4,
        out_indices=(3, ),
        style='pytorch'),
    neck=dict(type='GlobalAveragePooling'),
    head=dict(
        type='LinearClsHead',
        num_classes=1000,
        in_channels=2048,
        loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
        topk=(1, 5),
    ))

Crower-1 avatar May 27 '22 08:05 Crower-1

This issue will be closed as it is inactive, feel free to re-open it if necessary.

tonysy avatar Dec 12 '22 15:12 tonysy

same problem but in mmengine. When I use featmap_vis_demo.py in mmyolo for visualize my traindata. At first it works, however the second time it doesn't work with the same sh.

KileyDong avatar Jun 08 '23 09:06 KileyDong