三种上采样(转置卷积,插值,反池化)的实现(Pytorch)与可视化(Tensorboard)

三种上采样:
1.转置卷积(不是卷积的逆运算,是卷积运算)
2.插值
3.反池化

Three kinds of upsampling:
1. Transposed convolution
2. Interpolation
3. Unpooling 

代码(code)

import torch.nn as nn
import cv2
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
import torch.nn.functional as F

img = cv2.imread("1.jpg")
width = img.shape[1]
height = img.shape[2]
img = cv2.resize(img, (250, 250))

tran_tensor = transforms.ToTensor()
img = tran_tensor(img)
print("img.shape: ", img.shape)
print("type(img): ", type(img))

img = img.view(1, 3, 250, 250)

writer = SummaryWriter("logs")

Conv = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3, stride=2)

ConvTrans = nn.ConvTranspose2d(in_channels=3, out_channels=3, kernel_size=51)

Maxpool = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)
MaxUnpool = nn.MaxUnpool2d(kernel_size=2, stride=2)

img = img.reshape(3, 250, 250)
writer.add_image("input", img, 0)

# Transposed convolution
img = img.reshape(1, 3, 250, 250)
output_ConvTrans = ConvTrans(img)
print("output_ConvTrans.shape: ", output_ConvTrans.shape)
output_ConvTrans = output_ConvTrans.reshape(3, 300, 300)
writer.add_image("output_ConvTrans", output_ConvTrans, 1)

#  F.interpolate
out_interpolate = F.interpolate(img, scale_factor=2, mode='bilinear')
print("out_interpolate.shape: ", out_interpolate.shape)
out_interpolate = out_interpolate.reshape(3, 500, 500)
writer.add_image("out_interpolate: ", out_interpolate, 2)

# Unpooling
out_maxpool, indices = Maxpool(img)
print("out_maxpool.shape: ", out_maxpool.shape)
out_maxpool_1 = out_maxpool.reshape(3, 125, 125)
writer.add_image("out_maxpool: ", out_maxpool_1, 3)
out_maxunpool = MaxUnpool(out_maxpool, indices)
print("out_maxunpool.shape: ", out_maxunpool.shape)
out_maxunpool = out_maxunpool.reshape(3, 250, 250)
writer.add_image("out_maxunpool: ", out_maxunpool, 4)
writer.close()

运行结果(result)
在这里插入图片描述在这里插入图片描述
可视化(visualization)

  • 反池化Unpooling
    在这里插入图片描述
  • 插值 Interpolation
    在这里插入图片描述
  • 转置卷积(即以前的反卷积) Transposed convolution (i.e. previous deconvolution)
    在这里插入图片描述
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值