About the mismatch of batch size in loss calculation

(sun shuo feng ) #1

I am learning fcn, so I tried to write the downsample part of fcn and tried to run, the code is as follows:
class myVGG(nn.Module):
def init(self, num_classes):
super(myVGG, self).init()

    self.conv1_1 = nn.Conv2d(3, 64, 3, padding=100)
    self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1)

    self.relu = nn.ReLU(inplace=True)
    self.pool = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)


    self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1)
    self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1)

    self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1)
    self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1)
    self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1)

    self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1)
    self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1)
    self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1)

    self.conv5_1 = nn.Conv2d(512, 512, 3, padding=1)
    self.conv5_2 = nn.Conv2d(512, 512, 3, padding=1)
    self.conv5_3 = nn.Conv2d(512, 512, 3, padding=1)

    self.fc1 = nn.Conv2d(512 , 4096,7)

    self.fc2 = nn.Conv2d(4096,4096,1)

    self.fc3 = nn.Conv2d(4096,num_classes,1)

    self.drop_out = nn.Dropout()

def forward(self, x):
    print(x.shape)
    x = self.conv1_1(x)
    x = self.relu(x)
    x = self.conv1_2(x)
    x = self.relu(x)
    x = self.pool(x)
    print(x.shape)

    x = self.conv2_1(x)
    x = self.relu(x)
    x = self.conv2_2(x)
    x = self.relu(x)
    x = self.pool(x)
    print(x.shape)

    x = self.conv3_1(x)
    x = self.relu(x)
    x = self.conv3_2(x)
    x = self.relu(x)
    x = self.conv3_3(x)
    x = self.relu(x)
    x = self.pool(x)
    print(x.shape)

    x = self.conv4_1(x)
    x = self.relu(x)
    x = self.conv4_2(x)
    x = self.relu(x)
    x = self.conv4_3(x)
    x = self.relu(x)
    x = self.pool(x)
    print(x.shape)

    x = self.conv5_1(x)
    x = self.relu(x)
    x = self.conv5_2(x)
    x = self.relu(x)
    x = self.conv5_3(x)
    x = self.relu(x)
    x = self.pool(x)

    print(x.shape)
    x = self.fc1(x)
    x = self.relu(x)
    x = self.drop_out(x)

    print(x.shape)

    x = self.fc2(x)
    x = self.relu(x)
    x = self.drop_out(x)

    print(x.shape)
    x = self.fc3(x)
    print(x.shape)
    return x

The following is the output of each x dimension and error information:
%E6%8D%95%E8%8E%B7

It seems that the batch size does not match when calculating the loss. How can I solve it?

0 Likes