【问题标题】:Convolution Neural Network for regression using pytorch使用 pytorch 进行回归的卷积神经网络
【发布时间】:2019-11-30 05:22:12
【问题描述】:

我正在尝试为回归目的创建 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)

的数据

【问题讨论】:

    标签: pytorch


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-09
      • 2019-08-18
      • 2021-03-30
      • 2017-01-01
      • 1970-01-01
      • 2018-08-13
      • 2017-10-28
      相关资源
      最近更新 更多