上采样(upsampling)一般包括2种方式:

第二种方法如何用pytorch实现可见上面的链接

这里想要介绍的是如何使用pytorch实现第一种方法:

举例:

1)使用torch.nn模块实现一个生成器为:

import torch.nn as nn
import torch.nn.functional as F class ConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvLayer, self).__init__()
padding = kernel_size //
self.reflection_pad = nn.ReflectionPad2d(padding)
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride) def forward(self, x):
out = self.reflection_pad(x)
out = self.conv(out) return out class Generator(nn.Module):
def __init__(self, in_channels):
super(Generator, self).__init__()
self.in_channels = in_channels self.encoder = nn.Sequential(
ConvLayer(self.in_channels, , , ),
nn.BatchNorm2d(),
nn.ReLU(),
ConvLayer(, , , ),
nn.BatchNorm2d(),
nn.ReLU(),
ConvLayer(, , , ),
) upsample = nn.Upsample(scale_factor=, mode='bilinear', align_corners=True)
self.decoder = nn.Sequential(
upsample,
nn.Conv2d(, , ),
nn.BatchNorm2d(),
nn.ReLU(),
upsample,
nn.Conv2d(, , ),
nn.BatchNorm2d(),
nn.ReLU(),
upsample,
nn.Conv2d(, , ),
nn.Tanh()
) def forward(self, x):
x = self.encoder(x)
out = self.decoder(x) return out def test():
net = Generator()
for module in net.children():
print(module)
x = Variable(torch.randn(,,,))
output = net(x)
print('output :', output.size())
print(type(output)) if __name__ == '__main__':
test()

返回:

model.py .Sequential(
(): ConvLayer(
(reflection_pad): ReflectionPad2d((, , , ))
(conv): Conv2d(, , kernel_size=(, ), stride=(, ))
)
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
(): ConvLayer(
(reflection_pad): ReflectionPad2d((, , , ))
(conv): Conv2d(, , kernel_size=(, ), stride=(, ))
)
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
(): ConvLayer(
(reflection_pad): ReflectionPad2d((, , , ))
(conv): Conv2d(, , kernel_size=(, ), stride=(, ))
)
)
Sequential(
(): Upsample(scale_factor=, mode=bilinear)
(): Conv2d(, , kernel_size=(, ), stride=(, ))
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
(): Upsample(scale_factor=, mode=bilinear)
(): Conv2d(, , kernel_size=(, ), stride=(, ))
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
(): Upsample(scale_factor=, mode=bilinear)
(): Conv2d(, , kernel_size=(, ), stride=(, ))
(): Tanh()
)
output : torch.Size([, , , ])
<class 'torch.Tensor'>

但是这个会有警告:

 UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead.

可使用torch.nn.functional模块替换为:

import torch.nn as nn
import torch.nn.functional as F class ConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvLayer, self).__init__()
padding = kernel_size //
self.reflection_pad = nn.ReflectionPad2d(padding)
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride) def forward(self, x):
out = self.reflection_pad(x)
out = self.conv(out) return out class Generator(nn.Module):
def __init__(self, in_channels):
super(Generator, self).__init__()
self.in_channels = in_channels self.encoder = nn.Sequential(
ConvLayer(self.in_channels, , , ),
nn.BatchNorm2d(),
nn.ReLU(),
ConvLayer(, , , ),
nn.BatchNorm2d(),
nn.ReLU(),
ConvLayer(, , , ),
) self.decoder1 = nn.Sequential(
nn.Conv2d(, , ),
nn.BatchNorm2d(),
nn.ReLU()
)
self.decoder2 = nn.Sequential(
nn.Conv2d(, , ),
nn.BatchNorm2d(),
nn.ReLU()
)
self.decoder3 = nn.Sequential(
nn.Conv2d(, , ),
nn.Tanh()
) def forward(self, x):
x = self.encoder(x)
x = F.interpolate(x, scale_factor=, mode='bilinear', align_corners=True)
x = self.decoder1(x)
x = F.interpolate(x, scale_factor=, mode='bilinear', align_corners=True)
x = self.decoder2(x)
x = F.interpolate(x, scale_factor=, mode='bilinear', align_corners=True)
out = self.decoder3(x) return out def test():
net = Generator()
for module in net.children():
print(module)
x = Variable(torch.randn(,,,))
output = net(x)
print('output :', output.size())
print(type(output)) if __name__ == '__main__':
test()

返回:

model.py .Sequential(
(): ConvLayer(
(reflection_pad): ReflectionPad2d((, , , ))
(conv): Conv2d(, , kernel_size=(, ), stride=(, ))
)
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
(): ConvLayer(
(reflection_pad): ReflectionPad2d((, , , ))
(conv): Conv2d(, , kernel_size=(, ), stride=(, ))
)
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
(): ConvLayer(
(reflection_pad): ReflectionPad2d((, , , ))
(conv): Conv2d(, , kernel_size=(, ), stride=(, ))
)
)
Sequential(
(): Conv2d(, , kernel_size=(, ), stride=(, ))
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
)
Sequential(
(): Conv2d(, , kernel_size=(, ), stride=(, ))
(): BatchNorm2d(, eps=1e-, momentum=0.1, affine=True, track_running_stats=True)
(): ReLU()
)
Sequential(
(): Conv2d(, , kernel_size=(, ), stride=(, ))
(): Tanh()
)
output : torch.Size([, , , ])
<class 'torch.Tensor'>

最新文章

  1. 【python之路2】CMD中执行python程序中文显示乱码
  2. CST时间转换成 yyyy-MM-dd格式
  3. JQuery simpleModal插件的使用-遁地龙卷风
  4. 同步灵无线锂电鼠G11-580HX独特“五灵键”
  5. 14、到底改如何区分android的平板、电视、手机
  6. solr中竞价排名实现
  7. 阿里云服务器上安装mysql的心路历程(博友们进来看看哦)
  8. geotools导入shp文件到Oracle数据库时表名带下划线的问题解决
  9. poj1920 Towers of Hanoi
  10. 查看oracle数据库服务器的名字
  11. Python自然语言处理学习笔记之信息提取步骤&amp;分块(chunking)
  12. HTML5学习笔记&lt;五&gt;: HTML表单和PHP环境搭建
  13. android+eclipse+mysql+servlet(Android与mysql建立链接)
  14. 手动开发动态资源之servlet初步
  15. 移动App测试中的最佳做法
  16. 一般处理程序、Ajax多图片上传带进度条
  17. C++ map的方法
  18. fast-route的使用
  19. python项目离线环境配置指南
  20. linux远程windows执行命令

热门文章

  1. Django之路——7 django与ajax
  2. 虚拟机安装Linux从零到登陆成功教学
  3. Django上传文件和修改date格式
  4. 【DP】 路面修整 usaco 2008 feb_gold
  5. PHP流程控制之分支结构switch语句的使用
  6. Kubernetes 学习12 kubernetes 存储卷
  7. luogu 3466 对顶堆
  8. LOJ2434. 「ZJOI2018」历史 [LCT]
  9. P1016 旅行家的预算——贪心
  10. Pytest权威教程21-API参考-01-函数(Functions)