Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'dict' object has no attribute 'bboxes' #622

Closed
3 tasks done
nuonepeaceyy opened this issue Mar 3, 2023 · 6 comments
Closed
3 tasks done

AttributeError: 'dict' object has no attribute 'bboxes' #622

nuonepeaceyy opened this issue Mar 3, 2023 · 6 comments
Labels
question Further information is requested

Comments

@nuonepeaceyy
Copy link

nuonepeaceyy commented Mar 3, 2023

Prerequisite

💬 Describe the reimplementation questions

When I try to use DCFF in mmrazor to prune the yolov8 model, I first try to get the target_pruning_ratio by python tools/pruning/get_channel_units.py configs/pruning/mmdet/new_dcff/yolov8_pruning.py, but get AttributeError: class DCFF in mmrazor/models/algorithms/pruning/dcff.py: 'dict' object has no attribute 'bboxes'; then I try to run python tools/train.py configs/pruning/mmdet/new_dcff/yolov8_pruning.py to train it directly, it still comes to the same error. The detail of this error as follows.

/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  /opt/conda/conda-bld/pytorch_1659484810403/work/aten/src/ATen/native/TensorShape.cpp:2894.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Traceback (most recent call last):
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 121, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/home/keli/Nuonepeaceyy/MMLAB/mmrazor/mmrazor/models/algorithms/pruning/dcff.py", line 62, in __init__
    super().__init__(architecture, mutator_cfg, data_preprocessor,
  File "/home/keli/Nuonepeaceyy/MMLAB/mmrazor/mmrazor/models/algorithms/pruning/ite_prune_algorithm.py", line 137, in __init__
    self.mutator.prepare_from_supernet(self.architecture)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmrazor/mmrazor/models/mutators/channel_mutator/channel_mutator.py", line 104, in prepare_from_supernet
    units = self._prepare_from_tracer(supernet, self.parse_cfg)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmrazor/mmrazor/models/mutators/channel_mutator/channel_mutator.py", line 300, in _prepare_from_tracer
    unit_configs = tracer.analyze(model)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmrazor/mmrazor/models/task_modules/tracer/channel_analyzer.py", line 127, in analyze
    return self._find_mutable_units(model, unit_configs)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmrazor/mmrazor/models/task_modules/tracer/channel_analyzer.py", line 154, in _find_mutable_units
    template_output = model(**inputs)
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/keli/.local/lib/python3.8/site-packages/mmdet/models/detectors/base.py", line 92, in forward
    return self.loss(inputs, data_samples)
  File "/home/keli/.local/lib/python3.8/site-packages/mmdet/models/detectors/single_stage.py", line 78, in loss
    losses = self.bbox_head.loss(x, batch_data_samples)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmyolo/mmyolo/models/dense_heads/yolov5_head.py", line 459, in loss
    losses = super().loss(x, batch_data_samples)
  File "/home/keli/.local/lib/python3.8/site-packages/mmdet/models/dense_heads/base_dense_head.py", line 123, in loss
    losses = self.loss_by_feat(*loss_inputs)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmyolo/mmyolo/models/dense_heads/yolov8_head.py", line 314, in loss_by_feat
    gt_info = self.gt_instances_preprocess(batch_gt_instances, num_imgs)
  File "/home/keli/Nuonepeaceyy/MMLAB/mmyolo/mmyolo/models/dense_heads/yolov8_head.py", line 424, in gt_instances_preprocess
    bboxes = gt_instance.bboxes
AttributeError: 'dict' object has no attribute 'bboxes'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tools/train.py", line 121, in <module>
    main()
  File "tools/train.py", line 114, in main
    runner = Runner.from_cfg(cfg)
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/runner/runner.py", line 431, in from_cfg
    runner = cls(
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/runner/runner.py", line 398, in __init__
    self.model = self.build_model(model)
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/runner/runner.py", line 800, in build_model
    model = MODELS.build(model)
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/registry/registry.py", line 521, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 240, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/keli/anaconda3/envs/mmrazor/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 135, in build_from_cfg
    raise type(e)(
AttributeError: class `DCFF` in mmrazor/models/algorithms/pruning/dcff.py: 'dict' object has no attribute 'bboxes'

and here is my prune config, and I don't change the yolov8 config file in mmyolo.

_base_ = 'mmyolo::yolov8/yolov8_l_syncbn_fast_8xb16-500e_coco.py'
# _base_ = 'mmyolo::razor/prune/dcff_yolov5_s.py'

data_root = 'data/coco/'

architecture = _base_.model

model = dict(
    _delete_=True,
    _scope_='mmrazor',
    type='DCFF',
    architecture=architecture,
    mutator_cfg=dict(
        type='DCFFChannelMutator',
        channel_unit_cfg=dict(
            type='DCFFChannelUnit', default_args=dict(choice_mode='ratio')),
        parse_cfg=dict(
            type='ChannelAnalyzer',
            demo_input=(1, 3, 224, 224),
            tracer_type='FxTracer')),
    # target_pruning_ratio=target_pruning_ratio,
    step_freq=1,
    linear_schedule=False)

# del _base_.train_dataloader.collate_fn
del _base_.custom_hooks

train_dataloader = dict(
    dataset=dict(
        _delete_=True,
        type='mmyolo.YOLOv5CocoDataset',
        data_root=data_root,
        # metainfo=_base_.metainfo,
        data_prefix=dict(img='train/'),
        ann_file='annotations/voc07_train.json',
        filter_cfg=dict(filter_empty_gt=False, min_size=32),
        pipeline=_base_.train_pipeline))
val_dataloader = dict(
    dataset=dict(
        data_root=data_root,
        # metainfo=_base_.metainfo,
        data_prefix=dict(img='val/'),
        ann_file='annotations/voc07_val.json'))
test_dataloader = dict(
    dataset=dict(
        data_root=data_root,
        # metainfo=_base_.metainfo,
        data_prefix=dict(img='test/'),
        ann_file='annotations/voc07_test.json'))
val_evaluator = dict(ann_file=data_root + 'annotations/voc07_val.json')
test_evaluator = dict(ann_file=data_root + 'annotations/voc07_test.json')

model_wrapper = dict(
    type='mmcv.MMDistributedDataParallel', find_unused_parameters=True)

default_hooks = dict(param_scheduler=dict(max_epochs=150))
train_cfg = dict(max_epochs=150, val_interval=5)
val_cfg = dict(_delete_=True, type='mmrazor.ItePruneValLoop')

Environment

mmcv                    2.0.0rc4
mmrazor                 1.0.0rc2           /home/keli/Nuonepeaceyy/MMLAB/mmrazor
mmyolo                  0.4.0              /home/keli/Nuonepeaceyy/MMLAB/mmyolo
torch                   1.12.1
torchaudio              0.12.1
torchvision             0.13.1

Expected results

No response

Additional information

This yolov8 model can be trained normally in mmyolo;
I print the gt_instance in mmyolo/mmyolo/models/dense_heads/yolov8_head.py line 423 as follows:

{'ori_shape': (224, 224), 'flip_direction': None, 'img_shape': (224, 224), 'img_id': 0, 'scale_factor': array([1.1, 1.2]), 'batch_input_shape': (224, 224), 'border': [1, 1, 1, 1], 'filename': '<demo>.png', 'pad_shape': (224, 224), 'flip': False}

there is no attribute like bboxes and labels which they should be in there, but I have no idea about this. This may be because it is not correctly read in the label, but I can train this model in mmyolo...

@hhaAndroid hhaAndroid added the question Further information is requested label Mar 6, 2023
@LKJacky
Copy link

LKJacky commented Mar 6, 2023

We are sorry for your inconvenience. We have improved the ChannelAnayler to adaptive yolov8. You can directly cherry-pick this pr or wait util it is merged.

@nuonepeaceyy
Copy link
Author

We are sorry for your inconvenience. We have improved the ChannelAnayler to adaptive yolov8. You can directly cherry-pick this pr or wait util it is merged.

Thank you very much for your previous response, and I have successfully run the train.py. And may I ask how to export the pruned compact model after finishing the pruning training?

@LKJacky
Copy link

LKJacky commented Mar 8, 2023

DCFF is developing, so there is no existing export method. We suggest you use group fisher algorithm that is developed well including export (and deploy is incomming soon).

@nuonepeaceyy
Copy link
Author

DCFF is developing, so there is no existing export method. We suggest you use group fisher algorithm that is developed well including export (and deploy is incomming soon).

Thanks for your reply, I'll try it out.

@nuonepeaceyy
Copy link
Author

DCFF is developing, so there is no existing export method. We suggest you use group fisher algorithm that is developed well including export (and deploy is incomming soon).

When I try to use group fisher, it cames:

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/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1673, in train
    self.call_hook('before_run')
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1748, in call_hook
    getattr(hook, fn_name)(self, **kwargs)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/implementations/pruning/group_fisher/hook.py", line 128, in before_run
    original_resource = self._evaluate(model)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/implementations/pruning/group_fisher/hook.py", line 170, in _evaluate
    res = self.estimator.estimate(model)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/models/task_modules/estimators/resource_estimator.py", line 136, in estimate
    flops, params = get_model_flops_params(model, **flops_params_cfg)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/models/task_modules/estimators/counters/flops_params_counter.py", line 98, in get_model_flops_params
    _ = flops_params_model(batch)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1148, in _call_impl
    result = forward_call(*input, **kwargs)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/models/algorithms/base.py", line 144, in forward
    return self._forward(inputs, data_samples)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/models/algorithms/base.py", line 165, in _forward
    return self.architecture(inputs, data_samples, mode='tensor')
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/mmdet/models/detectors/base.py", line 96, in forward
    return self._forward(inputs, data_samples)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/mmdet/models/detectors/single_stage.py", line 132, in _forward
    x = self.extract_feat(batch_inputs)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/mmdet/models/detectors/single_stage.py", line 146, in extract_feat
    x = self.backbone(batch_inputs)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmyolo/mmyolo/models/backbones/base_backbone.py", line 221, in forward
    x = layer(x)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/container.py", line 139, in forward
    input = module(input)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/mmcv/cnn/bricks/conv_module.py", line 207, in forward
    x = self.conv(x)
  File "/home/keli/anaconda3/envs/mmyolo/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1151, in _call_impl
    hook_result = hook(self, input, result)
  File "/home/keli/Nuonepeaceyy/MMLAB/plus/mmrazor/mmrazor/models/task_modules/estimators/counters/op_counters/conv_layer_counter.py", line 82, in add_count_hook
    if hasattr(mutable_channel[0], 'activated_tensor_channels'):
IndexError: list index out of range

and here is my config file:

#############################################################################
"""You have to fill these args.

_base_ (str): The path to your pretrained model checkpoint.
pretrained_path (str): The path to your pretrained model checkpoint.

interval (int): Interval between pruning two channels. You should ensure you
    can reach your target pruning ratio when the training ends.
normalization_type (str): GroupFisher uses two methods to normlized the channel
    importance, including ['flops','act']. The former uses flops, while the
    latter uses the memory occupation of activation feature maps.
lr_ratio (float): Ratio to decrease lr rate. As pruning progress is unstable,
    you need to decrease the original lr rate until the pruning training work
    steadly without getting nan.

target_flop_ratio (float): The target flop ratio to prune your model.
input_shape (Tuple): input shape to measure the flops.
"""

_base_ = 'mmyolo::yolov8/yolov8_s_syncbn_fast_8xb16-500e_coco.py'
pretrained_path = '../mmyolo/checkpoints/yolov8_s_mask-refine_syncbn_fast_8xb16-500e_coco_20230216_095938-ce3c1b3f.pth'
interval = 10
normalization_type = 'act'
lr_ratio = 0.1 / 4

target_flop_ratio = 0.5
input_shape = (1, 3, 640, 640)
##############################################################################

architecture = _base_.model

if hasattr(_base_, 'data_preprocessor'):
    architecture.update({'data_preprocessor': _base_.data_preprocessor})
    data_preprocessor = None

architecture.init_cfg = dict(type='Pretrained', checkpoint=pretrained_path)
architecture['_scope_'] = _base_.default_scope

model = dict(
    _delete_=True,
    _scope_='mmrazor',
    type='GroupFisherAlgorithm',
    architecture=architecture,
    interval=interval,
    mutator=dict(
        type='GroupFisherChannelMutator',
        parse_cfg=dict(type='ChannelAnalyzer', tracer_type='FxTracer'),
        channel_unit_cfg=dict(
            type='GroupFisherChannelUnit',
            default_args=dict(normalization_type=normalization_type, ),
        ),
    ),
)

model_wrapper_cfg = dict(
    type='mmrazor.GroupFisherDDP',
    broadcast_buffers=False,
)

optim_wrapper = dict(
    optimizer=dict(lr=_base_.optim_wrapper.optimizer.lr * lr_ratio))

custom_hooks = getattr(_base_, 'custom_hooks', []) + [
    dict(type='mmrazor.PruningStructureHook'),
    dict(
        type='mmrazor.ResourceInfoHook',
        interval=interval,
        demo_input=dict(
            type='mmrazor.DefaultDemoInput',
            input_shape=input_shape,
        ),
        save_ckpt_thr=[target_flop_ratio],
    ),
]

@LKJacky
Copy link

LKJacky commented Mar 15, 2023

@nuonepeaceyy We have fix the bug, please pull the latest code from dev-1.x branch of mmrazor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants