Open maziyi234 opened 2 years ago
你是不是没安装对呀?是pip install torch_pruning,不是torch-pruning
你是没安装对呀?是pip install torch_pruning,不是torch-pruning
其实我两个都试了一遍 还是不行,它好像安装到别的地方了 ,但是我把库文件复制过来了,按理说也不该出问题的啊
那你就是没正确安装上呗,你试试在终端或者用conda激活你的环境然后安装,可以加上版本进行安装pip install torch_pruning==0.2.7
剪枝后 有转onnx的方法嘛,转onnx出现了size mismatch for backbone.backbone.stem.conv.conv.weight: copying a param with shape torch.Size([10, 12, 3, 3]) from checkpoint, the shape in current model is torch.Size([16, 12, 3, 3]).问题
剪枝后我没有转onnx,你这个是出现了维度不匹配的问题。首先你在转onnx或者推理的时候不需要再用model=YOLOX()进行实例化,可以直接用model = torch.load('你剪枝后的权重')进行模型实例化,看看这里对不对,还有就是你传入的shape以及类别数量是否正确
尹以鹏 | |
---|---|
@. | On 7/21/2022 @.> wrote:
剪枝后 有转onnx的方法嘛,转onnx出现了size mismatch for backbone.backbone.stem.conv.conv.weight: copying a param with shape torch.Size([10, 12, 3, 3]) from checkpoint, the shape in current model is torch.Size([16, 12, 3, 3]).问题
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
/
剪枝后我没有转onnx,你这个是出现了维度不匹配的问题。首先你在转onnx或者推理的时候不需要再用model=YOLOX()进行实例化,可以直接用model = torch.load('你剪枝后的权重')进行模型实例化,看看这里对不对,还有就是你传入的shape以及类别数量是否正确 | | 尹以鹏 | | @. | On 7/21/2022 @.> wrote: 剪枝后 有转onnx的方法嘛,转onnx出现了size mismatch for backbone.backbone.stem.conv.conv.weight: copying a param with shape torch.Size([10, 12, 3, 3]) from checkpoint, the shape in current model is torch.Size([16, 12, 3, 3]).问题 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
但是如何能够不加载YoloBody模型,直接用torch.load实例化呢
首先你需要区分开仅保存权值和权值与网络结构一起保存。 平常我们保存模型是采用torch.save(model.state_dict(),"mymodel.pth")这种方式是仅保存权值,因此我们在网络实例化的时候,首先需要用model = YOLOBody()先实例化模型【此刻的moel也有权重,但它是默认的权重,我们只是为了获取网络结构而已】,然后我们一般是再用model.load_state_dict(torch.load("mymodel.pth")),将我们训练好的权值加载到网络中,这样是没问题的,只要我们的pth权重shape和model shape一样就行。
但是!剪枝后的模型网络结构发生了变化!所以最后模型的保存是用torch.save(model,"pruned.pth"),这里的保存会将我们训练好的权重与我们的网络结构一起保存,即进行了捆绑。如果你还用model = YOLOBody()实例化模型后再用load会报shape不匹配问题,因为剪枝不会帮助你修改代码的。因此你仅仅用model = torch.load("pruned.pth")即可,这会将网络模型和权重都加载,后面的model.eval()推理等和之间一样就行。不知道这样你明白没有
尹以鹏 | |
---|---|
@. | On 7/21/2022 @.> wrote:
/
剪枝后我没有转onnx,你这个是出现了维度不匹配的问题。首先你在转onnx或者推理的时候不需要再用model=YOLOX()进行实例化,可以直接用model = torch.load('你剪枝后的权重')进行模型实例化,看看这里对不对,还有就是你传入的shape以及类别数量是否正确 | | 尹以鹏 | | @.*** | On 7/21/2022 @.> wrote: 剪枝后 有转onnx的方法嘛,转onnx出现了size mismatch for backbone.backbone.stem.conv.conv.weight: copying a param with shape torch.Size([10, 12, 3, 3]) from checkpoint, the shape in current model is torch.Size([16, 12, 3, 3]).问题 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>
但是如何能够不加载YoloBody模型,直接用torch.load实例化呢
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
要是转onnx的话 基本上就不能微调了是嘛,另外如果想进行通道剪枝 该怎么做呢
大佬,有没有BN层剪枝的代码啊
将代码中的
ifisinstance(m, nn.Conv2d) andminincluded_layers:
改成ifisinstance(m, nn.nn.BatchNorm2d()) andminincluded_layers:
尹以鹏 | |
---|---|
@. | ---- Replied Message ---- | From | @.> | | Date | 9/3/2022 15:09 | | To | @.> | | Cc | @.> , @.***> | | Subject | Re: [YINYIPENG-EN/Pruning_for_YOLOX] AttributeError: module 'torch_pruning' has no attribute 'prune_conv' (Issue #1) |
大佬,有没有BN层剪枝的代码啊
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
感觉还要稀疏化训练,我再看看,自己写了一部分
onnx我没转,不过应该是可以的。这个代码就是通道剪枝啊 | 尹以鹏 | |
---|---|---|
@. | On 8/5/2022 @.> wrote:
要是转onnx的话 基本上就不能微调了是嘛,另外如果想进行通道剪枝 该怎么做呢
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
您好,我明明安装了pip install torch-pruning,但是还是再pruning_plan = DG.get_pruning_plan((model.backbone.backbone.dark2)[0].dconv.conv, tp.prune_conv, idxs=pruning_idxs)这个函数运行时候出现 AttributeError: module 'torch_pruning' has no attribute 'prune_conv'的错误,请问是什么原因呢