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

When I ran the script pruning/get_channel_units.py, I got this error: TypeError: expected Tensor as element 0 in argument 0, but got tuple. #585

Open HePengguang opened 1 year ago

HePengguang commented 1 year ago

Describe the bug

A clear and concise description of what the bug is.

When I ran the script pruning/get_channel_units.py, I got this error: TypeError: expected Tensor as element 0 in argument 0, but got tuple.

To Reproduce

The command you executed.

I just used the recommended conmand: python tools/pruning/get_channel_units.py \ configs/pruning/mmseg/dcff/dcff_pointrend_resnet50_8xb2_cityscapes.py \ -c -i --output-path=configs/pruning/mmseg/dcff/resnet_seg.json

Post related information

  1. The output of pip list | grep "mmcv\|mmrazor\|^torch" mmcv 2.0.0 mmrazor 1.0.0 d:\allcode\mmlab\mmrazor-main torch 1.13.1+cu117

  2. Your config file if you modified it or created a new one. I changed the script dcff_pointrend_resnet50_8xb2_cityscapes.py base = [

    TODO: use autoaug pipeline.

    'mmseg::base/datasets/feces.py', # from cityscapes to feces (my own mmseg dataset) 'mmseg::base/schedules/schedule_160k.py', 'mmseg::base/default_runtime.py', './pointrend_resnet50.py' ]

feces.py

dataset settings

dataset_type = 'Feces' data_root = 'D:\Allcode\mmlab\mmsegmentation-dev-1.x\data\data'

img_scale = (1024, 1024) crop_size = (512, 512) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations'), dict( type='RandomResize', scale=img_scale, ratio_range=(0.5, 2.0), keep_ratio=True),

dict(type='RandomMosaic', img_scale=img_scale, prob=0.5, pad_val=0), # batch_size是2,不知道能不能成功使用,因为它需要4张图片

dict(type='RandomFlip', prob=0.5),
dict(type='PhotoMetricDistortion'),
dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
dict(type='PackSegInputs')

] test_pipeline = [ dict(type='LoadImageFromFile'), dict(type='Resize', scale=img_scale, keep_ratio=True),

add loading annotation after Resize because ground truth

# does not need to do resize data transform
dict(type='LoadAnnotations'),
dict(type='PackSegInputs')

] img_ratios = [0.5, 0.75, 1.0, 1.25, 1.5, 1.75] tta_pipeline = [ dict(type='LoadImageFromFile', backend_args=None), dict( type='TestTimeAug', transforms=[ [ dict(type='Resize', scale_factor=r, keep_ratio=True) for r in img_ratios ], [ dict(type='RandomFlip', prob=0., direction='horizontal'), dict(type='RandomFlip', prob=1., direction='horizontal') ], [dict(type='LoadAnnotations')], [dict(type='PackSegInputs')] ]) ] train_dataloader = dict( batch_size=2, num_workers=4, persistent_workers=True, sampler=dict(type='InfiniteSampler', shuffle=True), dataset=dict( type='RepeatDataset', times=40000, dataset=dict( type=dataset_type, data_root=data_root, data_prefix=dict( img_path='images/training', seg_map_path='annotations/training'), pipeline=train_pipeline))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, data_prefix=dict( img_path='images/validation', seg_map_path='annotations/validation'), pipeline=test_pipeline)) test_dataloader = val_dataloader

val_evaluator = dict(type='IoUMetric', iou_metrics=['mDice', 'mIoU', 'mFscore']) test_evaluator = val_evaluator image

  1. Your train log file if you meet the problem during training. d:\allcode\mmlab\mmsegmentation-dev-1.x\mmseg\models\builder.py:36: UserWarning: build_loss would be deprecated soon, please use mmseg.registry.MODELS.build() warnings.warn('build_loss would be deprecated soon, please use ' d:\allcode\mmlab\mmsegmentation-dev-1.x\mmseg\models\losses\cross_entropy_loss.py:235: UserWarning: Default avg_non_ignore is False, if you would like to ignore the certain label and average loss over non-ignore labels, which is the same with PyTorch officia l cross_entropy, set avg_non_ignore=True. warnings.warn( Traceback (most recent call last): File "tools/pruning/get_channel_units.py", line 84, in main() File "tools/pruning/get_channel_units.py", line 48, in main model = MODELS.build(config['model']) File "G:\softwares\path\Anaconda3\envs\mmseg\lib\site-packages\mmengine\registry\registry.py", line 570, in build return self.build_func(cfg, args, kwargs, registry=self) File "G:\softwares\path\Anaconda3\envs\mmseg\lib\site-packages\mmengine\registry\build_functions.py", line 232, in build_model_from_cfg return build_from_cfg(cfg, registry, default_args) File "G:\softwares\path\Anaconda3\envs\mmseg\lib\site-packages\mmengine\registry\build_functions.py", line 121, in build_from_cfg obj = obj_cls(args) # type: ignore File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\algorithms\pruning\dcff.py", line 62, in init super().init(architecture, mutator_cfg, data_preprocessor, File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\algorithms\pruning\ite_prune_algorithm.py", line 137, in init self.mutator.prepare_from_supernet(self.architecture) File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\mutators\channel_mutator\channel_mutator.py", line 113, in prepare_from_supernet units = self._prepare_from_tracer(supernet, self.parse_cfg) File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\mutators\channel_mutator\channel_mutator.py", line 311, in _prepare_from_tracer unit_configs = tracer.analyze(model) File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\task_modules\tracer\channel_analyzer.py", line 103, in analyze path_list = self.tracer.trace(model) File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\task_modules\tracer\backward_tracer.py", line 187, in trace pseudo_loss = self.loss_calculator(model) File "d:\allcode\mmlab\mmrazor-main\mmrazor\models\task_modules\tracer\loss_calculator\sum_loss_calculator.py", line 23, in call pseudo_output = model(pseudo_img) File "G:\softwares\path\Anaconda3\envs\mmseg\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl return forward_call(input, **kwargs) File "d:\allcode\mmlab\mmsegmentation-dev-1.x\mmseg\models\segmentors\base.py", line 98, in forward return self._forward(inputs, data_samples) File "d:\allcode\mmlab\mmsegmentation-dev-1.x\mmseg\models\segmentors\cascade_encoder_decoder.py", line 136, in _forward out = self.decode_head[i].forward(x, out) File "d:\allcode\mmlab\mmsegmentation-dev-1.x\mmseg\models\decode_heads\point_head.py", line 127, in forward x = torch.cat([fine_grained_point_feats, coarse_point_feats], dim=1) TypeError: expected Tensor as element 0 in argument 0, but got tuple

  2. Other code you modified in the mmrazor folder. nothing

Additional context

Add any other context about the problem here.

[here]

LKJacky commented 1 year ago

Please use "Fxtracer" to replace "BackwardTracer". We cannot maintain BackwardTracer anymore.

HePengguang commented 1 year ago

Please use "Fxtracer" to replace "BackwardTracer". We cannot maintain BackwardTracer anymore.

Thanks for your reply, do you mean "MMFxTracer" or "FxTracer"? I tried to modify the "BackwardTracer" to "MMFxTracer" or "FxTracer" in some files. But there is still that error: File "d:\allcode\mmlab\mmsegmentation-dev-1.x\mmseg\models\decode_heads\point_head.py", line 127, in forward x = torch.cat([fine_grained_point_feats, coarse_point_feats], dim=1) TypeError: expected Tensor as element 0 in argument 0, but got tuple,

I think it's not the problem about "BackwardTracer", so I still do not know what should be modified. Could you please supply a more detailed answer? Thank you so much! I chaned these lines: image image image

HePengguang commented 1 year ago

Please use "Fxtracer" to replace "BackwardTracer". We cannot maintain BackwardTracer anymore.

https://github.com/open-mmlab/mmrazor/tree/main/configs/pruning/mmseg/dcff I refer to this recommendation command. image

HePengguang commented 1 year ago

Please use "Fxtracer" to replace "BackwardTracer". We cannot maintain BackwardTracer anymore.

Is it possible that your documentation does not match the repository code? As someone with some coding experience, I have no idea how to use mmrazor to prune my mmseg model. Could you consider improving the documentation? Thank you so much for your hard work.