[英]Implementation Issue: Deep ConvNet for Pattern Recognition
I'm trying to implement a pattern recognition model using a fully convolutional network (fig 1 in https://www.sciencedirect.com/science/article/pii/S0031320318304370 , I was able to get the full text without signing in or anything but if it's a problem I can attach a picture too.) but I'm getting a size error when moving from the final Conv2D layer to the first fc_layer.我正在尝试使用完全卷积网络实现模式识别 model ( https://www.sciencedirect.com/science/article/pii/S0031320318304370中的图 1 或任何内容,我无需签名即可获得全文但如果这是一个问题,我也可以附上图片。)但是从最终的 Conv2D 层移动到第一个 fc_layer 时出现大小错误。
Here is my error message:这是我的错误信息:
RuntimeError: size mismatch, m1: [4 x 1024], m2: [4 x 1024] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:283
Originally, as in the figure, my first linear layer was:原来,如图,我的第一个线性层是:
nn.Linear(4*4*512, 1024)
but after getting the size mismatch, I changed it to:但在尺寸不匹配后,我将其更改为:
nn.Linear(4,1024)
Now, I have a strange error message as written above.现在,我有一个奇怪的错误消息,如上所述。
For reference (if it helps), here is my code:供参考(如果有帮助),这是我的代码:
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
class convnet(nn.Module):
def __init__(self, num_classes=1000):
super(convnet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=3, stride=2, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.MaxPool2d(kernel_size=1),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),# stride=2),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2), #stride=2),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True), #nn.Dropout(p=0.5)
)
self.classifier = nn.Sequential(
nn.Linear(4, 1024),
nn.Dropout(p=0.5),
nn.ReLU(inplace=True),
#nn.Dropout(p=0.5),
nn.Linear(1024, 1024),
nn.ReLU(inplace=True),
nn.Linear(1024, num_classes),
)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x,1)
x = self.classifier(x)
return x
I suspect it's an issue with the padding and stride.我怀疑这是填充和步幅的问题。 Thanks!
谢谢!
The error is from a matrix multiplication, where m1
should be an m xn matrix and m2
an n xp matrix and the result would be an m xp matrix.错误来自矩阵乘法,其中
m1
应该是m xn矩阵, m2
应该是n xp矩阵,结果将是m xp矩阵。 In your case it's 4 x 1024 and 4 x 1024 , but that doesn't work since 1024 != 4
.在您的情况下,它是4 x 1024和4 x 1024 ,但是从
1024 != 4
开始就不起作用了。
That means your input to the first linear layer has size [4, 1024] (4 being the batch size), therefore the input features of the first linear layer should be 1024.这意味着您对第一个线性层的输入大小为[4, 1024] (4 是批量大小),因此第一个线性层的输入特征应该是 1024。
self.classifier = nn.Sequential(
nn.Linear(1024, 1024),
nn.Dropout(p=0.5),
nn.ReLU(inplace=True),
#nn.Dropout(p=0.5),
nn.Linear(1024, 1024),
nn.ReLU(inplace=True),
nn.Linear(1024, num_classes),
)
If you are uncertain how many features your input has, you can print out its size just before the layer:如果你不确定你的输入有多少特征,你可以在层之前打印出它的大小:
x = self.features(x)
x = torch.flatten(x,1)
print(x.size()) # => torch.Size([4, 1024])
x = self.classifier(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.