pytorch 尝试提取线状图层的要素时出错

ldioqlga  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(113)

我想得到第一个线性层的输出,并定义了一个这样的模型:

model = CatAndDogConvNet()
CatAndDogConvNet(
  (conv1): Conv2d(3, 16, kernel_size=(5, 5), stride=(2, 2), padding=(1, 1))
  (maxpool_1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (relu_1): ReLU()
  (conv2): Conv2d(16, 32, kernel_size=(5, 5), stride=(2, 2), padding=(1, 1))
  (maxpool_2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (relu_2): ReLU()
  (conv3): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (maxpool_3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (relu_3): ReLU()
  (fc1): Linear(in_features=2304, out_features=500, bias=True)
  (fc2): Linear(in_features=500, out_features=50, bias=True)
  (fc3): Linear(in_features=50, out_features=2, bias=True)
)

像这样的新模型
第一次
但是当我把1x3x224x224大小的图像通过它时,

res_4 = []
model_new = torch.nn.Sequential(*list(model.children())[:10])
print(model_new)
for i in range(len(random_samples)):
    # print(imgs[i][0].shape)
    temp = model_new.forward(random_samples[i][0])
    res_4.append([temp, random_samples[i][1]])

出现以下错误:

RuntimeError Traceback (most recent call last)
/tmp/ipykernel_95235/516937314.py in
2 model_new = torch.nn.Sequential(*list(model.children())[:7])
3 for i in range(len(imgs)):
----> 4 temp = model_new(imgs[i][0])
5 res_4.append([temp, imgs[i][1]])

~/anaconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1188 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1189 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1190 return forward_call(*input, **kwargs)
1191 # Do not call functions when jit is used
1192 full_backward_hooks, non_full_backward_hooks = [], []

~/anaconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/container.py in forward(self, input)
202 def forward(self, input):
203 for module in self:
→ 204 input = module(input)
205 return input
206

~/anaconda3/envs/torch/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1188 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1189 or _global_forward_hooks or _global_forward_pre_hooks):
…
→ 114 return F.linear(input, self.weight, self.bias)
115
116 def extra_repr(self) → str:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (384x6 and 2304x500)

这就是我传递图像时得到的模型

Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
Sequential                               --                        --
├─Conv2d: 1-1                            [1, 16, 111, 111]         1,216
├─MaxPool2d: 1-2                         [1, 16, 55, 55]           --
├─Conv2d: 1-3                            [1, 32, 27, 27]           12,832
├─MaxPool2d: 1-4                         [1, 32, 13, 13]           --
├─Conv2d: 1-5                            [1, 64, 13, 13]           18,496
├─MaxPool2d: 1-6                         [1, 64, 6, 6]             --
==========================================================================================
Total params: 32,544
Trainable params: 32,544
Non-trainable params: 0
Total mult-adds (M): 27.46
==========================================================================================
Input size (MB): 0.60
Forward/backward pass size (MB): 1.85
Params size (MB): 0.13
Estimated Total Size (MB): 2.58

该模型是在大小为3x224x224的图像上转换的。那么为什么我会得到这个错误呢?

fkvaft9z

fkvaft9z1#

发生这种情况是因为在Sequential中,我需要提到神经网络将如何进行。在Sequential()中,我提到了Flatten()默认值,它忽略了批处理大小,并将其他所有内容扁平化。由于在我的测试集中,我没有添加批处理大小,因此我得到了3x224x224,在Flatten()之后,它将64视为批处理大小。这就是为什么它会给出错误。所以我把它改成了Flatten(0,2),它成功了。

相关问题