open-mmlab / mmrazor

OpenMMLab Model Compression Toolbox and Benchmark.
https://mmrazor.readthedocs.io/en/latest/
Apache License 2.0
1.48k stars 229 forks source link

[Bug] AutoSlim For faster_rcnn_r50_fpn Error #161

Open tanghy2016 opened 2 years ago

tanghy2016 commented 2 years ago

Describe the bug

error log:

Traceback (most recent call last):
  File "/root/project/mmrazor/mmrazor/models/algorithms/autoslim.py", line 69, in _init_pruner
    pseudo_pruner.prepare_from_supernet(pseudo_architecture)
  File "/root/project/mmrazor/mmrazor/models/pruners/structure_pruning.py", line 152, in prepare_from_supernet
    pseudo_img = supernet.forward_dummy(pseudo_img)
  File "/root/project/mmrazor/mmrazor/models/architectures/base.py", line 21, in forward_dummy
    return self.model.forward_dummy(img)
  File "/root/project/mmdetection/mmdet/models/detectors/two_stage.py", line 86, in forward_dummy
    roi_outs = self.roi_head.forward_dummy(x, proposals)
  File "/root/project/mmdetection/mmdet/models/roi_heads/standard_roi_head.py", line 44, in forward_dummy
    bbox_results = self._bbox_forward(x, rois)
  File "/root/project/mmdetection/mmdet/models/roi_heads/standard_roi_head.py", line 121, in _bbox_forward
    bbox_feats = self.bbox_roi_extractor(
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/mmcv/runner/fp16_utils.py", line 198, in new_func
    return old_func(*args, **kwargs)
  File "/root/project/mmdetection/mmdet/models/roi_heads/roi_extractors/single_level_roi_extractor.py", line 103, in forward
    roi_feats_t = self.roi_layers[i](feats[i], rois_)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/mmcv/ops/roi_align.py", line 213, in forward
    return roi_align(input, rois, self.output_size, self.spatial_scale,
  File "/opt/conda/lib/python3.8/site-packages/mmcv/ops/roi_align.py", line 93, in forward
    ext_module.roi_align_forward(
RuntimeError: roi_width >= 0 && roi_height >= 0 INTERNAL ASSERT FAILED at "/tmp/mmcv/mmcv/ops/csrc/pytorch/cpu/roi_align.cpp":139, please report a bug to PyTorch. ROIs in ROIAlign cannot have non-negative size!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/site-packages/mmcv/utils/registry.py", line 52, in build_from_cfg
    return obj_cls(**args)
  File "/root/project/mmrazor/mmrazor/models/algorithms/autoslim.py", line 42, in __init__
    super(AutoSlim, self).__init__(**kwargs)
  File "/root/project/mmrazor/mmrazor/models/algorithms/base.py", line 56, in __init__
    self._init_pruner(pruner)
  File "/root/project/mmrazor/mmrazor/models/algorithms/autoslim.py", line 78, in _init_pruner
    raise NotImplementedError('Our current StructurePruner does not '
NotImplementedError: Our current StructurePruner does not support pruning this architecture. StructurePruner is not perfect enough to handle all the corner cases. We will appreciate it if you create a issue.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "MMCV/train.py", line 54, in <module>
    main()
  File "MMCV/train.py", line 48, in main
    train_pruning(args)
  File "MMCV/../../XbrainAiModels/MMCV/tools/train_pruning.py", line 43, in train_pruning
    train_mmdet(args, cfg)
  File "MMCV/../../XbrainAiModels/MMCV/tools/train_mmdet.py", line 26, in train_mmdet
    algorithm = build_algorithm(cfg.algorithm)
  File "/root/project/mmrazor/mmrazor/models/builder.py", line 20, in build_algorithm
    return ALGORITHMS.build(cfg)
  File "/opt/conda/lib/python3.8/site-packages/mmcv/utils/registry.py", line 215, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/opt/conda/lib/python3.8/site-packages/mmcv/cnn/builder.py", line 27, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/opt/conda/lib/python3.8/site-packages/mmcv/utils/registry.py", line 55, in build_from_cfg
    raise type(e)(f'{obj_cls.__name__}: {e}')
NotImplementedError: AutoSlim: Our current StructurePruner does not support pruning this architecture. StructurePruner is not perfect enough to handle all the corner cases. We will appreciate it if you create a issue.

config

_base_ = [
    '../../_base_/datasets/coco_detection.py',
    '../../_base_/schedules/schedule_det.py',
    '../../_base_/default_runtime_det.py'
]

model = dict(
    type='mmdet.FasterRCNN',
    backbone=dict(
        type='ResNet',
        depth=50,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        frozen_stages=1,
        norm_cfg=dict(type='BN', requires_grad=True),
        norm_eval=True,
        style='pytorch',
        init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),
    neck=dict(
        type='FPN',
        in_channels=[256, 512, 1024, 2048],
        out_channels=256,
        num_outs=5),
    rpn_head=dict(
        type='RPNHead',
        in_channels=256,
        feat_channels=256,
        anchor_generator=dict(
            type='AnchorGenerator',
            scales=[8],
            ratios=[0.5, 1.0, 2.0],
            strides=[4, 8, 16, 32, 64]),
        bbox_coder=dict(
            type='DeltaXYWHBBoxCoder',
            target_means=[.0, .0, .0, .0],
            target_stds=[1.0, 1.0, 1.0, 1.0]),
        loss_cls=dict(
            type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
        loss_bbox=dict(type='L1Loss', loss_weight=1.0)),
    roi_head=dict(
        type='StandardRoIHead',
        bbox_roi_extractor=dict(
            type='SingleRoIExtractor',
            roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),
            out_channels=256,
            featmap_strides=[4, 8, 16, 32]),
        bbox_head=dict(
            type='Shared2FCBBoxHead',
            in_channels=256,
            fc_out_channels=1024,
            roi_feat_size=7,
            num_classes=80,
            bbox_coder=dict(
                type='DeltaXYWHBBoxCoder',
                target_means=[0., 0., 0., 0.],
                target_stds=[0.1, 0.1, 0.2, 0.2]),
            reg_class_agnostic=False,
            loss_cls=dict(
                type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
            loss_bbox=dict(type='L1Loss', loss_weight=1.0))),
    # model training and testing settings
    train_cfg=dict(
        rpn=dict(
            assigner=dict(
                type='MaxIoUAssigner',
                pos_iou_thr=0.7,
                neg_iou_thr=0.3,
                min_pos_iou=0.3,
                match_low_quality=True,
                ignore_iof_thr=-1),
            sampler=dict(
                type='RandomSampler',
                num=256,
                pos_fraction=0.5,
                neg_pos_ub=-1,
                add_gt_as_proposals=False),
            allowed_border=-1,
            pos_weight=-1,
            debug=False),
        rpn_proposal=dict(
            nms_pre=2000,
            max_per_img=1000,
            nms=dict(type='nms', iou_threshold=0.7),
            min_bbox_size=0),
        rcnn=dict(
            assigner=dict(
                type='MaxIoUAssigner',
                pos_iou_thr=0.5,
                neg_iou_thr=0.5,
                min_pos_iou=0.5,
                match_low_quality=False,
                ignore_iof_thr=-1),
            sampler=dict(
                type='RandomSampler',
                num=512,
                pos_fraction=0.25,
                neg_pos_ub=-1,
                add_gt_as_proposals=True),
            pos_weight=-1,
            debug=False)),
    test_cfg=dict(
        rpn=dict(
            nms_pre=1000,
            max_per_img=1000,
            nms=dict(type='nms', iou_threshold=0.7),
            min_bbox_size=0),
        rcnn=dict(
            score_thr=0.05,
            nms=dict(type='nms', iou_threshold=0.5),
            max_per_img=100)
        # soft-nms is also supported for rcnn testing
        # e.g., nms=dict(type='soft_nms', iou_threshold=0.5, min_score=0.05)
    ))

algorithm = dict(
    type='AutoSlim',
    architecture=dict(type='MMDetArchitecture', model=model),
    pruner=dict(
        type='RatioPruner',
        ratios=(2 / 12, 3 / 12, 4 / 12, 5 / 12, 6 / 12, 7 / 12, 8 / 12, 9 / 12,
                10 / 12, 11 / 12, 1.0)),
    retraining=False,
    bn_training_mode=True,
    input_shape=None)

runner = dict(type='EpochBasedRunner', max_epochs=50)

use_ddp_wrapper = True
humu789 commented 2 years ago

@tanghy2016 AutoSlim: Our current StructurePruner does not support pruning this architecture. StructurePruner is not perfect enough to handle all the corner cases. We will appreciate it if you create a issue. Sorry, we will analyse your case as soon to confirm whether to add your requirement to our develop backlog. And you can also PR to MMRazor if you are instered in solving it.