我正在尝试为回归目的创建CNN。输入是图像数据。
出于学习目的,我有10张形状为(10,3,448,448)
的图像,其中10张是图像,3张是通道,448张是高度和宽度。
输出标签为(10,245)
。
这是我的建筑
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=5)
self.conv2 = nn.Conv2d(32, 32, kernel_size=5)
self.conv3 = nn.Conv2d(32,64, kernel_size=5)
self.fc1 = nn.Linear(3*3*64, 256)
self.fc2 = nn.Linear(256, 245)
def forward(self, x):
x = F.relu(self.conv1(x))
#x = F.dropout(x, p=0.5, training=self.training)
x = F.relu(F.max_pool2d(self.conv2(x), 2))
x = F.dropout(x, p=0.5, training=self.training)
x = F.relu(F.max_pool2d(self.conv3(x),2))
x = F.dropout(x, p=0.5, training=self.training)
x = x.view(-1,3*3*64 )
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return x
cnn = CNN()
print(cnn)
it = iter(train_loader)
X_batch, y_batch = next(it)
print(cnn.forward(X_batch).shape)
使用批次大小2,我希望模型产生的数据形状为(2,245)
。但是它正在产生形状为(2592, 245)
答案 0 :(得分:1)
在self.conv3之后,您有形状为[2,64,108,108]的张量,该张量在重整后会产生[2592,576]。这就是2592的来源。 更改行: “ self.fc1 = nn.Linear(3 * 3 * 64,256)” 和 “ x = x.view(-1,3 * 3 * 64)” 以便在图层之后使用适当的图像尺寸。
下面是固定代码:
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=5)
self.conv2 = nn.Conv2d(32, 32, kernel_size=5)
self.conv3 = nn.Conv2d(32,64, kernel_size=5)
self.fc1 = nn.Linear(108*108*64, 256)
self.fc2 = nn.Linear(256, 245)
def forward(self, x):
print (x.shape)
x = F.relu(self.conv1(x))
print (x.shape)
#x = F.dropout(x, p=0.5, training=self.training)
x = F.relu(F.max_pool2d(self.conv2(x), 2))
print (x.shape)
x = F.dropout(x, p=0.5, training=self.training)
print (x.shape)
x = F.relu(F.max_pool2d(self.conv3(x),2))
print (x.shape)
x = F.dropout(x, p=0.5, training=self.training)
print (x.shape)
x = x.view(-1,108*108*64 )
print (x.shape)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return x
cnn = CNN()
print(cnn)
# X_batch, y_batch = next(it)
print(cnn.forward(X_batch).shape)