代码(code) :
import torch.nn as nn
import cv2
from torch.utils.data import DataLoader
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img = cv2.imread("1.jpg")
tran_tensor = transforms.ToTensor()
img = tran_tensor(img)
print("img.shape: ", img.shape)
print("type(img): ", type(img))
img = img.view(1, 3, 850, 850)
dataloader = DataLoader(dataset=img, batch_size=1)
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)
for data in dataloader:
img = data
img = img.reshape(3, 850, 850)
writer.add_image("input", img, 0)
img = img.reshape(1, 3, 850, 850)
output_ConvTrans = ConvTrans(img)
print("output_ConvTrans.shape: ", output_ConvTrans.shape)
output_ConvTrans = output_ConvTrans.reshape(3, 900, 900)
writer.add_image("output_ConvTrans", output_ConvTrans, 1)
out_Conv = Conv(img)
print("out_Conv.shape :", out_Conv.shape)
out_Conv = out_Conv.reshape(3, 424, 424)
writer.add_image("output_Conv", out_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
|