【发布时间】:2021-03-08 08:08:53
【问题描述】:
class Discriminator(nn.Module):
def __init__(self, channels=3):
super(Discriminator, self).__init__()
self.channels = channels
def convlayer(n_input, n_output, k_size=4, stride=2, padding=0, bn=False):
block = [nn.Conv2d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, bias=False)]
if bn:
block.append(nn.BatchNorm2d(n_output))
block.append(nn.LeakyReLU(0.2, inplace=True))
return block
self.model = nn.Sequential(
*convlayer(self.channels, 32, 4, 2, 1),
*convlayer(32, 64, 4, 2, 1),
*convlayer(64, 128, 4, 2, 1, bn=True),
*convlayer(128, 256, 4, 2, 1, bn=True),
nn.Conv2d(256, 1, 4, 1, 0, bias=False), # FC with Conv.
)
def forward(self, imgs):
logits = self.model(imgs)
out = torch.sigmoid(logits)
return out.view(-1,1)
上面的架构是GAN模型的判别器,我和第一层一样有点困惑
*convlayer(self.channels, 32, 4, 2, 1)
self.channels ,即 3(彩色图像)已通过,我有一个 64 * 64 * 3 的输入图像。我的第一个问题是在上述架构中输入图像的尺寸在哪里得到注意?
我之所以感到困惑,是因为当我看到生成器架构时,
class Generator(nn.Module):
def __init__(self, nz=128, channels=3):
super(Generator, self).__init__()
self.nz = nz
self.channels = channels
def convlayer(n_input, n_output, k_size=4, stride=2, padding=0):
block = [
nn.ConvTranspose2d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, bias=False),
nn.BatchNorm2d(n_output),
nn.ReLU(inplace=True),
]
return block
self.model = nn.Sequential(
*convlayer(self.nz, 1024, 4, 1, 0), # Fully connected layer via convolution.
*convlayer(1024, 512, 4, 2, 1),
*convlayer(512, 256, 4, 2, 1),
*convlayer(256, 128, 4, 2, 1),
*convlayer(128, 64, 4, 2, 1),
nn.ConvTranspose2d(64, self.channels, 3, 1, 1),
nn.Tanh()
)
def forward(self, z):
z = z.view(-1, self.nz, 1, 1)
img = self.model(z)
return img
在第一层
*convlayer(self.nz, 1024, 4, 1, 0)
他们正在传递 self.nz ,这是生成 64 * 64 * 3 图像所需的 128 个随机潜在点,这与上面传递 通道 的鉴别器模型相反。
我的第二个问题是,如果我有一个 300 * 300 * 3 的图像,我应该改变我的鉴别器架构来处理图像吗?
附:我是 Pytorch 的新手。
【问题讨论】:
标签: python-3.x deep-learning pytorch generative-adversarial-network