用于图像重建的自动编码器生成具有奇怪网格的灰度图像

2024-04-28 22:42:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用自动编码器创建一个深假。我使用一个编码器和两个解码器:一个用于目标图像,另一个用于源图像(目标面就是我要“粘贴”到源头上的面)。因此,首先我尝试训练编码器和两个解码器来重建输入面(300,300,3)。但对于这两种解码器,输出的不是彩色图像,而是灰度图像,因为每个像素的红、绿、蓝值几乎相同。此外,输出图像上还有一个奇怪的3x3网格: input image and produced image

我使用的批量大小为1,因为我不知道在这种情况下如何使用小批量(但这是另一个问题)。Im还使用剩余连接,从而提高了质量。最后一层有乙状结肠激活(这可能是错误的)。我的损失是二进制交叉熵,优化器是Adam。学习率是0.001(我也试过0.0001和0.00075)

这是我的模型:

import matplotlib.pyplot as plt
import torchvision
import torch
import torch.nn as nn
import torch.nn.functional as F




class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        """ encoder """
        self.conv1 = nn.Conv2d(3, 32, kernel_size=(4, 4))
        self.batchnorm1 = nn.BatchNorm2d(32)

        self.conv2 = nn.Conv2d(32, 64, kernel_size=(4, 4))
        self.batchnorm2 = nn.BatchNorm2d(64)

        self.conv3 = nn.Conv2d(64, 128, kernel_size=(3, 3))
        self.batchnorm3 = nn.BatchNorm2d(128)

        self.conv4 = nn.Conv2d(128, 256, kernel_size=(4, 4))
        self.batchnorm4 = nn.BatchNorm2d(256)

        self.maxpool3x3 = nn.MaxPool2d(3)
        self.maxpool2x2 = nn.MaxPool2d(2)

        """ target-decoder """
        self.targetDeconv1 = nn.ConvTranspose2d(256, 128, kernel_size=(4, 4))
        self.targetBatchnorm1 = nn.BatchNorm2d(128)

        self.targetDeconv2 = nn.ConvTranspose2d(128, 64, kernel_size=(3, 3))
        self.targetBatchnorm2 = nn.BatchNorm2d(64)

        self.targetDeconv3 = nn.ConvTranspose2d(64, 32, kernel_size=(4, 4))
        self.targetBatchnorm3 = nn.BatchNorm2d(32)

        self.targetDeconv4 = nn.ConvTranspose2d(32, 3, kernel_size=(4, 4))

        self.upsample3x3 = nn.Upsample(scale_factor=3)
        self.upsample2x2 = nn.Upsample(scale_factor=2)

        """ source-decoder """
        self.sourceDeconv1 = nn.ConvTranspose2d(256, 128, kernel_size=(4, 4))
        self.sourceBatchnorm1 = nn.BatchNorm2d(128)

        self.sourceDeconv2 = nn.ConvTranspose2d(128, 64, kernel_size=(3, 3))
        self.sourceBatchnorm2 = nn.BatchNorm2d(64)

        self.sourceDeconv3 = nn.ConvTranspose2d(64, 32, kernel_size=(4, 4))
        self.sourceBatchnorm3 = nn.BatchNorm2d(32)

        self.sourceDeconv4 = nn.ConvTranspose2d(32, 3, kernel_size=(4, 4))

        self.upsample3x3 = nn.Upsample(scale_factor=3)
        self.upsample2x2 = nn.Upsample(scale_factor=2)

    def _visualize_features(self, feature_maps, dim: tuple=(), title: str=""):
        try:
            x, y = dim
            fig, axs = plt.subplots(x, y)
            c = 0
            for i in range(x):
                for j in range(y):
                    axs[i][j].matshow(feature_maps.detach().cpu().numpy()[0][c])
                    c += 1

            fig.suptitle(title)
            plt.show()

        except:
            pass

    def forward(self, x, label: str="0", visualize: bool=False):
        """ encoder """
        x = self.conv1(x)
        x = self.batchnorm1(x)
        x = F.relu(x)
        x_1 = self.maxpool3x3(x)

        if visualize: print(x_1.shape); self._visualize_features(x_1, dim=(4, 4))

        x = self.conv2(x_1)
        x = self.batchnorm2(x)
        x = F.relu(x)
        x_2 = self.maxpool3x3(x)

        if visualize: print(x_2.shape); self._visualize_features(x_2, dim=(4, 4))

        x = self.conv3(x_2)
        x = self.batchnorm3(x)
        x = F.relu(x)
        x_3 = self.maxpool2x2(x)

        if visualize: print(x_3.shape); self._visualize_features(x_3, dim=(4, 4))

        x = self.conv4(x_3)
        x = self.batchnorm4(x)
        x = F.relu(x)
        x = self.maxpool2x2(x)

        if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))


        """ target-decoder """
        if label == "0":
            x = self.upsample2x2(x)
            x = self.targetDeconv1(x)
            x += x_3
            x = self.targetBatchnorm1(x)
            x = F.relu(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))

            x = self.upsample2x2(x)
            x = self.targetDeconv2(x)
            x += x_2
            x = self.targetBatchnorm2(x)
            x = F.relu(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))

            x = self.upsample3x3(x)
            x = self.targetDeconv3(x)
            x += x_1
            x = self.targetBatchnorm3(x)
            x = F.relu(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))

            x = self.upsample3x3(x)
            x = self.targetDeconv4(x)
            x = torch.sigmoid(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(3, 1))

            return x

        """ source-decoder """
        if label == "1":
            x = self.upsample2x2(x)
            x = self.sourceDeconv1(x)
            x += x_3
            x = self.sourceBatchnorm1(x)
            x = F.relu(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))

            x = self.upsample2x2(x)
            x = self.sourceDeconv2(x)
            x += x_2
            x = self.sourceBatchnorm2(x)
            x = F.relu(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))

            x = self.upsample3x3(x)
            x = self.sourceDeconv3(x)
            x += x_1
            x = self.sourceBatchnorm3(x)
            x = F.relu(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(4, 4))

            x = self.upsample3x3(x)
            x = self.sourceDeconv4(x)
            x = torch.sigmoid(x)

            if visualize: print(x.shape); self._visualize_features(x, dim=(3, 1))

            return x

所以我的问题是:为什么输出图像是灰色的?输出图像上的网格是什么?这两个问题都相关吗?我检查了它是否与rgb和bgr有任何关系,但似乎不是这样。 我希望任何人都能解决我的问题,提前谢谢:)


Tags: 图像selfsizeifnnkernelfeaturesrelu