【问题标题】:How to create a neural network that takes in an image and ouputs another image?如何创建一个接收图像并输出另一个图像的神经网络?
【发布时间】:2020-12-12 00:42:34
【问题描述】:

我正在尝试创建一个具有 L(来自 Lab)格式的图像并输出 ab 尺寸的神经网络。我能够毫无问题地通过 L 尺寸,但我无法弄清楚如何输出 ab 尺寸。输出应该是形状 1x2xHxW,其中 H 和 W 是输入图像的高度和宽度。到目前为止,这是我的网络:

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        # Get the resnet18 model from torchvision.model library
        self.model = models.resnet18(pretrained=True)
        
        self.model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)

        # Replace fully connected layer of our model to a 2048 feature vector output
        self.model.classifier = nn.Sequential()

        # Add custom classifier layers
        self.fc1 = nn.Linear(1000, 1024)
        self.Dropout1 = nn.Dropout()
        self.PRelU1 = nn.PReLU()

        self.fc2 = nn.Linear(1024, 512)
        self.Dropout2 = nn.Dropout()
        self.PRelU2 = nn.PReLU()

        self.fc3 = nn.Linear(512, 256)
        self.Dropout3 = nn.Dropout()
        self.PRelU3 = nn.PReLU()

        self.fc4 = nn.Linear(256, 313)
        # self.PRelU3 = nn.PReLU()
        
        self.softmax = nn.Softmax(dim=1)
        self.model_out = nn.Conv2d(313, 2, kernel_size=1, padding=0, dilation=1, stride=1, bias=False)
        self.upsample4 = nn.Upsample(scale_factor=4, mode='bilinear')


    def forward(self, x):
        # x is our input data
        x = self.model(x)
        x = self.Dropout1(self.PRelU1(self.fc1(x)))
        x = self.Dropout2(self.PRelU2(self.fc2(x)))
        x = self.Dropout3(self.PRelU3(self.fc3(x)))
        x = self.softmax(self.fc4(x))
        
        return x

【问题讨论】:

  • 拍摄图像,执行您的功能并生成与您在其上执行的功能相同的图像,这对于 autoencoder 来说是完美的任务。参考这个:jeremyjordan.me/autoencoders
  • U-net style architectures 可能是最流行的架构类型。基本上类似于自动编码器或编码器/解码器网络,但在编码器和解码器之间有跳跃连接。

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


【解决方案1】:

我真的不知道“ab 尺寸”是什么意思,我不确定“L 格式”是什么,但我可以告诉你如何使用 cnns 生成图像。

通常您会使用自动编码器,但这取决于任务。自动编码器将图像作为输入,并且与正常分类类似,可以减少尺寸。但与分类不同,您不会展平特征图并添加分类层,而是对它们进行解采样和反卷积。所以首先你“编码”“图像”,然后你“解码”它。开始上采样之前的中间层称为瓶颈。没有密集层,也不需要 softmax 激活。

这是一个示例,它看起来像一个 pytorch 模型(cifar10 数据集的自动编码器):

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

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

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

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

        self.maxpool2x2 = nn.MaxPool2d(2)   # not in usage

        """ decoder """
        self.upsample2x2 = nn.Upsample(scale_factor=2)   # not in usage

        self.deconv1 = nn.ConvTranspose2d(128, 64, kernel_size=(3, 3), stride=3)
        self.batchnorm1 = nn.BatchNorm2d(64)

        self.deconv2 = nn.ConvTranspose2d(64, 32, kernel_size=(4, 4), stride=3)
        self.batchnorm2 = nn.BatchNorm2d(32)

        self.deconv3 = nn.ConvTranspose2d(32, 3, kernel_size=(5, 5))
        self.batchnorm3 = nn.BatchNorm2d(3)
    

    def forward(self, x, train_: bool=True, print_: bool=False, return_bottlenecks: bool=False):

        """ encoder """
        x = self.conv1(x)
        x = self.batchnorm1(x)
        x = F.relu(x)

        x = self.conv2(x)
        x = self.batchnorm2(x)
        x = F.relu(x)

        x = self.conv3(x)
        x = self.batchnorm3(x)
        bottlenecks = F.relu(x)

        """ decoder """
        x = self.deconv1(bottlenecks)
        x = self.batchnorm1(x)
        x = F.relu(x)

        x = self.deconv2(x)
        x = self.batchnorm2(x)
        x = F.relu(x)

        x = self.deconv3(x)
        x = torch.sigmoid(x)

        return x

在此示例中,我不使用“maxpool”和“upsample”,但这取决于您的模型。 Upsample 基本上是 maxpool 的反面,你可以看到 ConvTranspose2d 也像卷积的反面(尽管这不是他正确的解释)。

因此,您基本上希望“解码器”部分与“编码器”部分相反(或镜像版本)。确定每一层的尺寸 kernel-sizes 等可能非常棘手,但您基本上必须设置它们,以便架构几乎是对称的,并且模型的输出尺寸是您想要生成的图像的尺寸。

这就是“图像生成”架构的样子:

来源:https://www.semanticscholar.org/paper/Feature-discovery-and-visualization-of-robot-data-Flaspohler-Roy/514a2f7461edd3e4c2d56d57f9002e1dc445eb58/figure/1

【讨论】:

  • 感谢您的精彩回答!澄清一下,图像的 Lab 颜色空间将图像分成 3 个维度,L 是光强度(灰度),a 表示绿-品红色平衡,b 表示蓝-黄色平衡。所以在我的情况下,我正在输入灰度并想要输出 ab 尺寸。按照您的示例,我将只使用 2 维而不是 3 维的最后一层(如附图所示)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2011-01-06
  • 2017-09-16
  • 2020-09-20
  • 2014-06-24
相关资源
最近更新 更多