生成 300 * 300 * 3 图像的 GAN 的生成器和判别器模型的架构应该是什么?
What should be the architecture of the generator and discriminator model of the GAN for generating 300 * 300 * 3 images?
我经常看到人们生成 28 * 28 、 64 * 64 等大小的图像。为了创建这种大小的图像,他们通常从过滤器数量 512、256、128 等开始,以递减方式生成并以相反的方式用于鉴别器。通常他们在鉴别器和生成器中保持相同数量的层。
我的第一个问题是鉴别器和生成器模型的架构应该是什么来创建 300 * 300 张图像。
我的第二个问题是..判别器和生成器中的层数是否必须相同。如果我的鉴别器中的层数比生成器中的层数多怎么办?
我的第三个问题仅取决于第二个问题,我可以使用任何著名模型(如 resnet、vgg 等)的特征提取器部分来制作鉴别器吗?
P.S。如果您正在编写架构代码,请在 pytorch 或 keras 中编写。
- 生成器的架构完全取决于您想要的图像分辨率。如果需要输出更高分辨率的图像,则需要相应地修改
ConvTranspose2d
层的kernel_size
、stride
和padding
。请参阅以下示例:
# 64 * 64 * 3
# Assuming a latent dimension of 128, you will perform the following sequence to generate a 64*64*3 image.
latent = torch.randn(1, 128, 1, 1)
out = nn.ConvTranspose2d(128, 512, 4, 1)(latent)
out = nn.ConvTranspose2d(512, 256, 4, 2, 1)(out)
out = nn.ConvTranspose2d(256, 128, 4, 2, 1)(out)
out = nn.ConvTranspose2d(128, 64, 4, 2, 1)(out)
out = nn.ConvTranspose2d(64, 3, 4, 2, 1)(out)
print(out.shape) # torch.Size([1, 3, 64, 64])
# Note the values of the kernel_size, stride, and padding.
# 284 * 284 * 3
# Assuming the same latent dimension of 128, you will perform the following sequence to generate a 284*284*3 image.
latent = torch.randn(1, 128, 1, 1)
out = nn.ConvTranspose2d(128, 512, 4, 1)(latent)
out = nn.ConvTranspose2d(512, 256, 4, 3, 1)(out)
out = nn.ConvTranspose2d(256, 128, 4, 3, 1)(out)
out = nn.ConvTranspose2d(128, 64, 4, 3, 1)(out)
out = nn.ConvTranspose2d(64, 3, 4, 3, 1)(out)
print(out.shape) # torch.Size([1, 3, 284, 284])
# I have only increased the stride from 2 to 3 and you could see the difference in the output size. You can play with the values to get 300*300*3.
如果您想生成更大尺寸的输出,请查看渐进式 GAN。
在生成器和鉴别器中使用对称层背后的一般想法是您希望两个网络同样强大。他们与自己竞争并随着时间的推移学习。具有不对称层可能会导致训练时不平衡。
是的。您可以使用任何特征提取器来代替基本的 Conv
和 ConvTranspose
层。您可以使用 ResidualBlock
作为编码器的一部分,使用 ResidualBlockUp
作为解码器的一部分。
我经常看到人们生成 28 * 28 、 64 * 64 等大小的图像。为了创建这种大小的图像,他们通常从过滤器数量 512、256、128 等开始,以递减方式生成并以相反的方式用于鉴别器。通常他们在鉴别器和生成器中保持相同数量的层。
我的第一个问题是鉴别器和生成器模型的架构应该是什么来创建 300 * 300 张图像。
我的第二个问题是..判别器和生成器中的层数是否必须相同。如果我的鉴别器中的层数比生成器中的层数多怎么办?
我的第三个问题仅取决于第二个问题,我可以使用任何著名模型(如 resnet、vgg 等)的特征提取器部分来制作鉴别器吗?
P.S。如果您正在编写架构代码,请在 pytorch 或 keras 中编写。
- 生成器的架构完全取决于您想要的图像分辨率。如果需要输出更高分辨率的图像,则需要相应地修改
ConvTranspose2d
层的kernel_size
、stride
和padding
。请参阅以下示例:
# 64 * 64 * 3
# Assuming a latent dimension of 128, you will perform the following sequence to generate a 64*64*3 image.
latent = torch.randn(1, 128, 1, 1)
out = nn.ConvTranspose2d(128, 512, 4, 1)(latent)
out = nn.ConvTranspose2d(512, 256, 4, 2, 1)(out)
out = nn.ConvTranspose2d(256, 128, 4, 2, 1)(out)
out = nn.ConvTranspose2d(128, 64, 4, 2, 1)(out)
out = nn.ConvTranspose2d(64, 3, 4, 2, 1)(out)
print(out.shape) # torch.Size([1, 3, 64, 64])
# Note the values of the kernel_size, stride, and padding.
# 284 * 284 * 3
# Assuming the same latent dimension of 128, you will perform the following sequence to generate a 284*284*3 image.
latent = torch.randn(1, 128, 1, 1)
out = nn.ConvTranspose2d(128, 512, 4, 1)(latent)
out = nn.ConvTranspose2d(512, 256, 4, 3, 1)(out)
out = nn.ConvTranspose2d(256, 128, 4, 3, 1)(out)
out = nn.ConvTranspose2d(128, 64, 4, 3, 1)(out)
out = nn.ConvTranspose2d(64, 3, 4, 3, 1)(out)
print(out.shape) # torch.Size([1, 3, 284, 284])
# I have only increased the stride from 2 to 3 and you could see the difference in the output size. You can play with the values to get 300*300*3.
如果您想生成更大尺寸的输出,请查看渐进式 GAN。
在生成器和鉴别器中使用对称层背后的一般想法是您希望两个网络同样强大。他们与自己竞争并随着时间的推移学习。具有不对称层可能会导致训练时不平衡。
是的。您可以使用任何特征提取器来代替基本的
Conv
和ConvTranspose
层。您可以使用ResidualBlock
作为编码器的一部分,使用ResidualBlockUp
作为解码器的一部分。