三种上采样(转置卷积,插值,反池化)的实现(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)
三种上采样(转置卷积,插值,反池化)的实现(Pytorch)与可视化(Tensorboard)_第1张图片在这里插入图片描述
可视化(visualization)

  • 反池化Unpooling
    三种上采样(转置卷积,插值,反池化)的实现(Pytorch)与可视化(Tensorboard)_第2张图片
  • 插值 Interpolation
    三种上采样(转置卷积,插值,反池化)的实现(Pytorch)与可视化(Tensorboard)_第3张图片
  • 转置卷积(即以前的反卷积) Transposed convolution (i.e. previous deconvolution)
    三种上采样(转置卷积,插值,反池化)的实现(Pytorch)与可视化(Tensorboard)_第4张图片

你可能感兴趣的:(语义分割,Semantic,Segmentation,pytorch,深度学习,python,神经网络,计算机视觉)