pytorch的输出尺寸和模型摘要中报告的输出尺寸不匹配

时间:2020-10-14 19:00:44

标签: python pytorch classification

我正在努力确定导致我下面具有3类分类代码的以下行为的原因。分类器采用torch.Size([1,1,512,512])的2D矩阵。输出为torch.Size([256,3])。

class Classifier(nn.Module):
    def __init__(self):
        super(Classifier, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.drop_out = nn.Dropout()
        self.fc1 = nn.Linear(4096, 1024)
        self.fc2 = nn.Linear(1024, 3)
        
        
    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(-1, 4096)
        out = self.drop_out(out)
        out = self.fc1(out)
        out = self.fc2(out)
        return out

问题是打印模型摘要时,同一模型提供以下尺寸。我的问题是我需要将其与存储类标签的torch.Size([1])进行比较。一切都表明这是专为3类分类问题而设计的,但无法确定输出张量中256的值来自哪里。

summary(activity_recognizer,(1,512,512))

==========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
├─Sequential: 1-1                        [-1, 32, 256, 256]        --
|    └─Conv2d: 2-1                       [-1, 32, 512, 512]        832
|    └─ReLU: 2-2                         [-1, 32, 512, 512]        --
|    └─MaxPool2d: 2-3                    [-1, 32, 256, 256]        --
├─Sequential: 1-2                        [-1, 64, 128, 128]        --
|    └─Conv2d: 2-4                       [-1, 64, 256, 256]        51,264
|    └─ReLU: 2-5                         [-1, 64, 256, 256]        --
|    └─MaxPool2d: 2-6                    [-1, 64, 128, 128]        --
├─Dropout: 1-3                           [-1, 4096]                --
├─Linear: 1-4                            [-1, 1024]                4,195,328
├─Linear: 1-5                            [-1, 3]                   3,075
==========================================================================================
Total params: 4,250,499
Trainable params: 4,250,499
Non-trainable params: 0
Total mult-adds (G): 3.57
==========================================================================================
Input size (MB): 1.00
Forward/backward pass size (MB): 96.01
Params size (MB): 16.21
Estimated Total Size (MB): 113.22
==========================================================================================

花几个小时,但好像我快要死了。感谢我在这里可以提供的任何帮助。

0 个答案:

没有答案