RuntimeError:MAT1和MAT2形状无法乘以(256x16和4096x1024)
我是深度学习的新手,并创建了一个模型来对我的图像进行分类。
目前,该项目在Google Colab或Kaggle(CPU和GPU)上引起了错误,但没有在我的个人计算机上(使用CPU)。
模型:
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.network1 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size = 3, padding = 1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.MaxPool2d(2,2),
# nn.AdaptiveAvgPool2d((128,128)),
nn.Conv2d(64, 128, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.Conv2d(128 ,128, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.MaxPool2d(2,2),
nn.Conv2d(128, 256, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.AdaptiveMaxPool2d((4,4))
)
self.network2 = nn.Sequential(
nn.Flatten(),
nn.Linear(256*4*4, 1024),
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, n_classes)
)
self.sigm = nn.Sigmoid()
def forward(self,x):
x = self.network1(x)
x = self.network2(x)
return self.sigm(x)
训练:
epoch = 0
model.train()
criterion = nn.BCELoss()
while True:
batch_losses = []
for imgs, labels in data:
imgs, labels = imgs.float().to(device), labels.to(device)
optimizer.zero_grad()
model_result = model(imgs)
loss = criterion(model_result, labels.type(torch.float))
batch_loss_value = loss.item()
loss.backward()
optimizer.step()
batch_losses.append(batch_loss_value)
loss_value = np.mean(batch_losses)
print("epoch:{:2d} iter:{:3d} train: loss:{:.3f}".format(epoch, iteration, loss_value))
if epoch % SAVE_FREQ == 0:
checkpoint_save(model, epoch)
epoch += 1
if EPOCHS < epoch:
break
错误:
RuntimeError Traceback (most recent call last)
/tmp/ipykernel_33/872363799.py in <module>
14 optimizer.zero_grad()
15
---> 16 model_result = model(imgs)
17 loss = criterion(model_result, labels.type(torch.float))
18
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
/tmp/ipykernel_33/1050848783.py in forward(self, x)
32 self.sigm = nn.Sigmoid()
33 def forward(self,x):
---> 34 x = self.network(x)
35 return self.sigm(x)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
139 def forward(self, input):
140 for module in self:
--> 141 input = module(input)
142 return input
143
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
101
102 def forward(self, input: Tensor) -> Tensor:
--> 103 return F.linear(input, self.weight, self.bias)
104
105 def extra_repr(self) -> str:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x16 and 4096x1024)
输入形状为(3,406,565) 此模型的摘要是:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 32, 406, 565] 896
ReLU-2 [-1, 32, 406, 565] 0
Conv2d-3 [-1, 64, 406, 565] 18,496
ReLU-4 [-1, 64, 406, 565] 0
MaxPool2d-5 [-1, 64, 203, 282] 0
Conv2d-6 [-1, 128, 203, 282] 73,856
ReLU-7 [-1, 128, 203, 282] 0
Conv2d-8 [-1, 128, 203, 282] 147,584
ReLU-9 [-1, 128, 203, 282] 0
MaxPool2d-10 [-1, 128, 101, 141] 0
Conv2d-11 [-1, 256, 101, 141] 295,168
ReLU-12 [-1, 256, 101, 141] 0
Conv2d-13 [-1, 256, 101, 141] 590,080
ReLU-14 [-1, 256, 101, 141] 0
AdaptiveAvgPool2d-15 [-1, 256, 4, 4] 0
Flatten-16 [-1, 4096] 0
Linear-17 [-1, 1024] 4,195,328
ReLU-18 [-1, 1024] 0
Linear-19 [-1, 512] 524,800
ReLU-20 [-1, 512] 0
Linear-21 [-1, 18] 9,234
Sigmoid-22 [-1, 18] 0
================================================================
Total params: 5,855,442
Trainable params: 5,855,442
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 2.63
Forward/backward pass size (MB): 712.84
Params size (MB): 22.34
Estimated Total Size (MB): 737.80
----------------------------------------------------------------
解决
方案,问题是我没有考虑图像形状中的批处理大小,并且我的数据集的图像很少,灰度尺寸很少,几乎没有Alpha通道。
I am new to Deep Learning and have created a model to classify my images.
Currently, this project raises an error on Google Colab or Kaggle (CPU and GPU) but not on my personal computer (with CPU).
Model:
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.network1 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size = 3, padding = 1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.MaxPool2d(2,2),
# nn.AdaptiveAvgPool2d((128,128)),
nn.Conv2d(64, 128, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.Conv2d(128 ,128, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.MaxPool2d(2,2),
nn.Conv2d(128, 256, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size = 3, stride = 1, padding = 1),
nn.ReLU(),
nn.AdaptiveMaxPool2d((4,4))
)
self.network2 = nn.Sequential(
nn.Flatten(),
nn.Linear(256*4*4, 1024),
nn.ReLU(),
nn.Linear(1024, 512),
nn.ReLU(),
nn.Linear(512, n_classes)
)
self.sigm = nn.Sigmoid()
def forward(self,x):
x = self.network1(x)
x = self.network2(x)
return self.sigm(x)
Training:
epoch = 0
model.train()
criterion = nn.BCELoss()
while True:
batch_losses = []
for imgs, labels in data:
imgs, labels = imgs.float().to(device), labels.to(device)
optimizer.zero_grad()
model_result = model(imgs)
loss = criterion(model_result, labels.type(torch.float))
batch_loss_value = loss.item()
loss.backward()
optimizer.step()
batch_losses.append(batch_loss_value)
loss_value = np.mean(batch_losses)
print("epoch:{:2d} iter:{:3d} train: loss:{:.3f}".format(epoch, iteration, loss_value))
if epoch % SAVE_FREQ == 0:
checkpoint_save(model, epoch)
epoch += 1
if EPOCHS < epoch:
break
ERROR:
RuntimeError Traceback (most recent call last)
/tmp/ipykernel_33/872363799.py in <module>
14 optimizer.zero_grad()
15
---> 16 model_result = model(imgs)
17 loss = criterion(model_result, labels.type(torch.float))
18
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
/tmp/ipykernel_33/1050848783.py in forward(self, x)
32 self.sigm = nn.Sigmoid()
33 def forward(self,x):
---> 34 x = self.network(x)
35 return self.sigm(x)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
139 def forward(self, input):
140 for module in self:
--> 141 input = module(input)
142 return input
143
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
101
102 def forward(self, input: Tensor) -> Tensor:
--> 103 return F.linear(input, self.weight, self.bias)
104
105 def extra_repr(self) -> str:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x16 and 4096x1024)
Input shape is (3, 406, 565)
The summary for this model with this shape is:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 32, 406, 565] 896
ReLU-2 [-1, 32, 406, 565] 0
Conv2d-3 [-1, 64, 406, 565] 18,496
ReLU-4 [-1, 64, 406, 565] 0
MaxPool2d-5 [-1, 64, 203, 282] 0
Conv2d-6 [-1, 128, 203, 282] 73,856
ReLU-7 [-1, 128, 203, 282] 0
Conv2d-8 [-1, 128, 203, 282] 147,584
ReLU-9 [-1, 128, 203, 282] 0
MaxPool2d-10 [-1, 128, 101, 141] 0
Conv2d-11 [-1, 256, 101, 141] 295,168
ReLU-12 [-1, 256, 101, 141] 0
Conv2d-13 [-1, 256, 101, 141] 590,080
ReLU-14 [-1, 256, 101, 141] 0
AdaptiveAvgPool2d-15 [-1, 256, 4, 4] 0
Flatten-16 [-1, 4096] 0
Linear-17 [-1, 1024] 4,195,328
ReLU-18 [-1, 1024] 0
Linear-19 [-1, 512] 524,800
ReLU-20 [-1, 512] 0
Linear-21 [-1, 18] 9,234
Sigmoid-22 [-1, 18] 0
================================================================
Total params: 5,855,442
Trainable params: 5,855,442
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 2.63
Forward/backward pass size (MB): 712.84
Params size (MB): 22.34
Estimated Total Size (MB): 737.80
----------------------------------------------------------------
Solution
In my case, the problem was that I didn't consider the batch size in my image shape and that my dataset had few images with grayscale and few with an alpha channel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后,您提供的错误与您的代码不符。在错误中存在:
但是您提供的代码是:
Then the Error provided by you is not align with your code. In the Error there is:
But the code you provide is:
就我而言,问题是,首先,我没有考虑图像形状中的批处理大小,其次,我的数据集的图像很少,灰度尺寸很少,几乎没有带有alpha频道的图像。
In my case the problem was that first of all I didn't consider the batch size in my image shape and secondly that my dataset had few images with grayscale and few with an alpha channel.