将图层格式更改为不同的图像分辨率

Change the layer format to a different image resolution

有一个 class,其中所有内容都设置为 32x32 图像格式取自此处

class Net(nn.Module):
        def __init__(self):
            super().__init__()
            self.conv1 = nn.Conv2d(1, 6, 5) # here I changed the image channel from 3 to 1
            self.pool = nn.MaxPool2d(2, 2)
            self.conv2 = nn.Conv2d(6, 64, 5)
            self.fc1 = nn.Linear(64 * 5 * 5, 120)
            self.fc2 = nn.Linear(120, 84)
            self.fc3 = nn.Linear(84, 22) # here I changed the number of output neurons from 10 to 22

        def forward(self, x):
            x = self.pool(F.relu(self.conv1(x)))
            x = self.pool(F.relu(self.conv2(x)))
            x = torch.flatten(x, 1) # flatten all dimensions except batch
            x = F.relu(self.fc1(x))
            x = F.relu(self.fc2(x))
            x = self.fc3(x)
            return x

如何将分辨率为 96 的所有这些更改为 96?通道 1(灰度)?

分辨率为 32x32 conv2 的输出形状为 (1, 64, 5, 5)。另一方面,如果输入的分辨率为 96x96,它将是 (1, 64, 21, 21)。这意味着 fc1 需要 28_224 个输入神经元。

>>> self.fc1 = nn.Linear(64 * 21 * 21, 120)

或者,您可以使用 nn.LazyLinear,它会根据第一个推断为您推断出这个数字。

>>> self.fc1 = nn.Linear(120)