转置卷积与卷积的实现(Pytorch)与可视化(Tensorboard)

注意:
转置卷积 (不是卷积的逆运算,是卷积运算)

代码(code)

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

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)

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)

# Convolution
output_Conv = Conv(img)
print("output_Conv.shape: ", output_Conv.shape)
output_Conv = output_Conv.reshape(3, 124, 124)
writer.add_image("output_Conv", output_Conv, 2)

writer.close()

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

转置卷积的特征图与原图对比

The feature map of the transposed convolution is compared with the original image 

在这里插入图片描述

卷积的特征图与原图对比

The feature map of the convolution is compared with the original image 

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值