卷积自动编码器图像尺寸误差

时间:2018-10-10 19:07:49

标签: python machine-learning conv-neural-network pytorch autoencoder

我有以下卷积自动编码器设置:

class autoencoder(nn.Module):
def  __init__(self):
    super(autoencoder, self).__init__()
    self.encoder = nn.Sequential(
        nn.Conv2d(1, 16, 3, stride=3, padding=1),  # b, 16, 10, 10
        nn.ReLU(True),
        nn.MaxPool2d(2, stride=2),  # b, 16, 5, 5
        nn.Conv2d(16, 8, 3, stride=2, padding=1),  # b, 8, 3, 3
        nn.ReLU(True),
        nn.MaxPool2d(2, stride=1)  # b, 8, 2, 2                     
                                )

    self.decoder = nn.Sequential(
        nn.ConvTranspose2d(8, 16, 3, stride=2),  # b, 16, 5, 5
        nn.ReLU(True),
        nn.ConvTranspose2d(16, 8, 5, stride=3, padding=1),  # b, 8, 15, 15
        nn.ReLU(True),
        nn.ConvTranspose2d(8, 1, 2, stride=2, padding=1),  # b, 1, 28, 28
        nn.Tanh()          
                                )

这是主循环:

for epoch in range(epochs):
running_loss = 0
for data in (train_loader):
    image,_=data
    inputs = image.view(image.size(0),-1)
    optimizer.zero_grad()
    #image = np.expand_dims(img, axis=0)

    outputs = net(inputs)
    loss = criterion(outputs,inputs)
    loss.backward()
    optimizer.step()
    running_loss += loss.data[0]
print('At Iteration : %d   ;  Mean-Squared Error : %f'%(epoch + 1,running_loss/(train_set.train_data.size(0)/batch_size)))

这是错误:

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [16, 1, 3, 3], but got input of size [1000, 784] instead

这与图像的展平有关,但我不确定如何展平它。

1 个答案:

答案 0 :(得分:1)

为什么要“展平”输入图像(主循环的第二行):

inputs = image.view(image.size(0),-1)

此行将您的4维image(批处理-通道-高度-宽度)转换为二维“平坦”矢量(批处理-c * h * w)。
autoencoder期望其输入为4D而非“平坦”。只需删除此行,您就可以了。