【发布时间】:2021-09-23 11:24:35
【问题描述】:
我有一个从头开始定义的预训练模型 LeNet5。我正在对下面显示的模型中存在的卷积层中的过滤器进行修剪。
class LeNet5(nn.Module):
def __init__(self, n_classes):
super(LeNet5, self).__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=20, kernel_size=5, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=20, out_channels=50, kernel_size=5, stride=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
self.classifier = nn.Sequential(
nn.Linear(in_features=800, out_features=500),
nn.ReLU(),
nn.Linear(in_features=500, out_features=10), # 10 - possible classes
)
def forward(self, x):
#x = x.view(x.size(0), -1)
x = self.feature_extractor(x)
x = torch.flatten(x, 1)
logits = self.classifier(x)
probs = F.softmax(logits, dim=1)
return logits, probs
我已经成功地从第 1 层的 20 个过滤器中删除了 2 个过滤器(现在 conv2d 第 1 层中有 18 个过滤器)和第 2 层中的 50 个过滤器中的 5 个过滤器(现在 conv2d 第3 层中的 45 个过滤器)。所以,现在我需要用如下所做的更改来更新模型 -
- 第 1 - 20 到 18 层的 out_channel
- in_channel 层 3 - 20 到 18
- 第 3 层的 out_channel - 50 到 45
但是,我无法运行模型,因为它给出了尺寸错误。
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x720 and 800x500)
如何更新号码。使用 Pytorch 执行修剪的模型中存在的过滤器层数?有没有我可以使用的库?
【问题讨论】:
-
您能以文本形式提供您的代码以及完整的错误回溯吗?
标签: python pytorch conv-neural-network pruning