源码地址:https://github.com/aitorzip/PyTorch-CycleGAN

如图所示,cycleGAN的网络结构包括两个生成器G(X->Y)和F(Y->X),两个判别器Dx和Dy

生成器部分:网络整体上经过一个降采样然后上采样的过程,中间是一系列残差块,数目由实际情况确定,根据论文中所说,当输入分辨率为128x128,采用6个残差块,当输入分辨率为256x256甚至更高时,采用9个残差块,其源代码如下,

class Generator(nn.Module):
def __init__(self, input_nc, output_nc, n_residual_blocks=9):
super(Generator, self).__init__() # Initial convolution block
model = [ nn.ReflectionPad2d(3),
nn.Conv2d(input_nc, 64, 7),
nn.InstanceNorm2d(64),
nn.ReLU(inplace=True) ] # Downsampling
in_features = 64
out_features = in_features*2
for _ in range(2):
model += [ nn.Conv2d(in_features, out_features, 3, stride=2, padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True) ]
in_features = out_features
out_features = in_features*2 # Residual blocks
for _ in range(n_residual_blocks):
model += [ResidualBlock(in_features)] # Upsampling
out_features = in_features//2
for _ in range(2):
model += [ nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1),
nn.InstanceNorm2d(out_features),
nn.ReLU(inplace=True) ]
in_features = out_features
out_features = in_features//2 # Output layer
model += [ nn.ReflectionPad2d(3),
nn.Conv2d(64, output_nc, 7),
nn.Tanh() ] self.model = nn.Sequential(*model) def forward(self, x):
return self.model(x)

其中,值得注意的网络层是nn.ReflectionPad2d和nn.InstanceNorm2d,前者搭配7x7卷积,先在特征图周围以反射的方式补长度,使得卷积后特征图尺寸不变,示例如下,输出结果就是以特征图边界为反射边,向外补充

nn.InstanceNorm2d是相比于batchNorm更加适合图像生成,风格迁移的归一化方法,相比于batchNorm跨样本,单通道统计,InstanceNorm采用单样本,单通道统计,括号中的参数代表通道数。

判别器部分:结构比生成器更加简单,经过5层卷积,通道数缩减为1,最后池化平均,尺寸也缩减为1x1,最最后reshape一下,变为(batchsize,1)

class Discriminator(nn.Module):
def __init__(self, input_nc):
super(Discriminator, self).__init__() # A bunch of convolutions one after another
model = [ nn.Conv2d(input_nc, 64, 4, stride=2, padding=1),
nn.LeakyReLU(0.2, inplace=True) ] model += [ nn.Conv2d(64, 128, 4, stride=2, padding=1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.2, inplace=True) ] model += [ nn.Conv2d(128, 256, 4, stride=2, padding=1),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.2, inplace=True) ] model += [ nn.Conv2d(256, 512, 4, padding=1),
nn.InstanceNorm2d(512),
nn.LeakyReLU(0.2, inplace=True) ] # FCN classification layer
model += [nn.Conv2d(512, 1, 4, padding=1)] self.model = nn.Sequential(*model) def forward(self, x):
x = self.model(x)
# Average pooling and flatten
return F.avg_pool2d(x, x.size()[2:]).view(x.size()[0])

最新文章

  1. UIViewController 生命周期
  2. 使用jQuery封装实用函数
  3. css3中transition和animation的回调处理
  4. CubieTruck上安装mjpg_streamer
  5. C#开发COM组件
  6. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)
  7. Palindrome Pairs 解答
  8. 初学.NET小技巧(不断更新)
  9. CodeForces 567B Berland National Library hdu-5477 A Sweet Journey
  10. ●BZOJ 1444 [Jsoi2009]有趣的游戏
  11. c# 上传图片到一个外链相册服务器
  12. MDS
  13. mac 启动php-fpm报错 failed to open configuration file '/private/etc/php-fpm.conf': No such file or direc
  14. Magento 2 Error: A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later.
  15. c# BackgroundWorker初试
  16. 过滤数组中的空数组array_filter()
  17. 上下文无关的GMM-HMM声学模型
  18. 设置redis 密码
  19. 2018.10.14 NOIP训练 圣诞树(简单dp)
  20. mysqli返回受影响行数

热门文章

  1. node.js入门学习(三)--npm
  2. UVa 1596 Bug Hunt (string::find && map && 模拟)
  3. MongoClient类
  4. sqli-labs(28a)
  5. [CSP-S模拟测试]:bird(线段树优化DP)
  6. python双划线类型
  7. 译-使用Scroll Snapping实现CSS控制页面滚动
  8. linux下libusb的安装与测试
  9. jQuery file upload cropper的 click .preview事件没有绑定成功
  10. 用Vue来实现音乐播放器(二十一):歌手详情数据抓取