VainF / Torch-Pruning

[CVPR 2023] DepGraph: Towards Any Structural Pruning
https://arxiv.org/abs/2301.12900
MIT License
2.71k stars 334 forks source link

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType' #271

Open WeiCL7777 opened 1 year ago

WeiCL7777 commented 1 year ago

您好,请问在运行基于Torch-Pruning实现MagnitudePruner的实验中,出现如上报错该怎么解决?谢谢。似乎是torch_pruning代码里面的错误,实在不知道如何解决 ---> [19] pruner = tp.pruner.MetaPruner( [20] model, [21] example_inputs, # 用于分析依赖的伪输入 [22] importance=imp, # 重要性评估指标 [23] iterative_steps=iterative_steps, # 迭代剪枝,设为1则一次性完成剪枝 [24] ch_sparsity=0.5, # 目标稀疏性,这里我们移除50%的通道 ResNet18 = {64, 128, 256, 512} => ResNet18_Half = {32, 64, 128, 256} [25] ignored_layers=ignored_layers, # 忽略掉最后的分类层 [26 )

File [~/anaconda3/envs/py/lib/python3.9/site-packages/torch_pruning/pruner/algorithms/scheduler.py:3], in (.0) 2 def linear_scheduler(pruning_ratio_dict, steps): ----> 3 return [((i) / float(steps)) * pruning_ratio_dict for i in range(steps+1)]

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

WeiCL7777 commented 1 year ago

我这里基本遇到pruner = tp.pruner.MetaPruner就会有如上报错,不知道是不是torch版本问题,希望请教作者,谢谢

jhnumfum commented 1 year ago

I have got the same problem even with the example in the wiki:

import torch
from torchvision.models import resnet18
import torch_pruning as tp

model = resnet18(pretrained=True)

# Importance criteria
example_inputs = torch.randn(1, 3, 224, 224)
imp = tp.importance.TaylorImportance()

ignored_layers = []
for m in model.modules():
    if isinstance(m, torch.nn.Linear) and m.out_features == 1000:
        ignored_layers.append(m) # DO NOT prune the final classifier!

iterative_steps = 5 # progressive pruning
pruner = tp.pruner.MagnitudePruner(
    model,
    example_inputs,
    importance=imp,
    iterative_steps=iterative_steps,
    ch_sparsity=0.5, # remove 50% channels, ResNet18 = {64, 128, 256, 512} => ResNet18_Half = {32, 64, 128, 256}
    ignored_layers=ignored_layers,
)

TypeError Traceback (most recent call last) in <cell line: 17>() 15 16 iterative_steps = 5 # progressive pruning ---> 17 pruner = tp.pruner.MagnitudePruner( 18 model, 19 example_inputs,

2 frames /usr/local/lib/python3.10/dist-packages/torch_pruning/pruner/algorithms/scheduler.py in (.0) 1 2 def linear_scheduler(pruning_ratio_dict, steps): ----> 3 return [((i) / float(steps)) * pruning_ratio_dict for i in range(steps+1)]

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

My torch version is : 2.0.1+cu118

jhnumfum commented 1 year ago

i looked through the code and the issue seems to be that the latest version does not support channel_sparsity "ch_sparsity is deprecated in v1.3.0. Please use pruning_ratio."

using pruning_ratio instead fixes the problem

VainF commented 1 year ago

Hi all, there is a bug with ch_sparsity. We will fix it today and release a new version.

Caused by this typo:

VainF commented 1 year ago

Fixed in the latest release. Thanks for the issue!