【数字图像处理】最近邻插值、插值理解 https://www.bilibili.com/video/BV1hi4y1c7g4 【数字图像处理】单线性插值 https://www.bilibili.com/video/BV1Ha4y1p7bH 【数字图像处理】双线性插值推导+python代码实现讲解 https://www.bilibili.com/video/BV1Xy4y1i7hy
#参考链接
#一文搞懂 deconvolution、transposed convolution、sub-pixel or fractional convolution
#https://www.cnblogs.com/shine-lee/p/11559825.html
#ConvTranspose2d原理,深度网络如何进行上采样?
#https://blog.csdn.net/qq_27261889/article/details/86304061
#Pytorch: conv2d、空洞卷积、maxpool2d、 ConvTranspose2d的输出特征图计算方式
#https://blog.csdn.net/paoxungan5156/article/details/115692758
from torch.nn import init
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import math
import torch
import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
torch.set_grad_enabled(False)
featureMap = torch.range(1, 16, 1)
featureMap = torch.reshape(featureMap, (4,4))
featureMap = featureMap.unsqueeze(0)
featureMap = featureMap.repeat(1, 8, 1, 1)
print(featureMap.shape)
#torch.Size([1, 8, 4, 4])
#N*C*H*W
indices = torch.tensor([0])
temp = torch.index_select(featureMap, 0, indices)
print(temp)
output = F.interpolate(featureMap, size=(8, 8), mode='bilinear', align_corners=False)
print(output.shape)
indices = torch.tensor([0])
temp = torch.index_select(output, 0, indices)
print(temp.numpy()[0,0,:,:])
#dconv = nn.ConvTranspose2d(in_channels=8, out_channels=8, kernel_size=3, stride=2, padding=1, output_padding=0, bias= False, dilation=2)
dconv = nn.ConvTranspose2d(in_channels=8, out_channels=8, kernel_size=4, stride=2, padding=1, output_padding=0, bias=False)
'''
第一步:对输入的特征图a进行一些变换,得到新的特征图a’
第二步:求新的卷积核设置,得到新的卷积核设置,后面都会用右上角加撇点的方式区分
第三步:用新的卷积核在新的特征图上做常规的卷积,得到的结果就是逆卷积的结果,就是我们要求的结果。
第一步:
Height’ = Height + (stride-1) * (Height-1)
在原先高度方向的每两个相邻中间插上(stride-1)列0,对于输入Height的特征图有Height-1个位置
所以在原先的基础上加上(stride-1) * (Height-1)
第二步:
stride’ = 1 这个数不管怎么输入,永远为1
kernelsize’ = kernelsize
padding’ = kernelsize-padding-1
第三步:
利用公式:output = (input + 2*padding’ - kernelsize’)/stride’ + 1
output’ = [(Height + (stride-1) * (Height-1)) + 2*(kernelsize-padding-1) - kernelsize]/stride + 1
output’ = (Height + stride*Height-Height-stride+1 + 2*kernelsize-2*padding-2 -kernelsize)/stride + 1
output’ = (Height*stride + kernelsize -2*padding -1-stride)/1 + 1
output’ = (Height*stride-stride + kernelsize -2*padding)
output’ = ((Height-1)*stride + kernelsize -2*padding)
'''
#nn.Conv2d
#output = (input + 2*padding - kernelsize)/stride + 1
#若存在dilation,则
#output = [input + 2*padding - (dilation*(kernelsize-1)+1) ]/stride + 1
#nn.ConvTranspose2d
#output = (input-1)*stride + kernelsize + output_padding - 2*padding
#若存在dilation,则
#output = (input-1)*stride + kernelsize + output_padding + dilation - 2*padding
init.constant(dconv.weight, 2)
output2 = dconv(featureMap)
print(output2.shape)
indices = torch.tensor([0])
temp = torch.index_select(output2, 0, indices)
print(temp.numpy()[0,0,:,:])
#参考链接
#一文搞懂 deconvolution、transposed convolution、sub-pixel or fractional convolution
#https://www.cnblogs.com/shine-lee/p/11559825.html
#ConvTranspose2d原理,深度网络如何进行上采样?
#https://blog.csdn.net/qq_27261889/article/details/86304061
#Pytorch: conv2d、空洞卷积、maxpool2d、 ConvTranspose2d的输出特征图计算方式
#https://blog.csdn.net/paoxungan5156/article/details/115692758
#原始
'''
[[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]],
'''
#双线性插值
'''
[[ 1. 1.25 1.75 2.25 2.75 3.25 3.75 4. ]
[ 2. 2.25 2.75 3.25 3.75 4.25 4.75 5. ]
[ 4. 4.25 4.75 5.25 5.75 6.25 6.75 7. ]
[ 6. 6.25 6.75 7.25 7.75 8.25 8.75 9. ]
[ 8. 8.25 8.75 9.25 9.75 10.25 10.75 11. ]
[10. 10.25 10.75 11.25 11.75 12.25 12.75 13. ]
[12. 12.25 12.75 13.25 13.75 14.25 14.75 15. ]
[13. 13.25 13.75 14.25 14.75 15.25 15.75 16. ]]
'''
#反卷积
'''
[[ 16. 48. 48. 80. 80. 112. 112. 64.]
[ 96. 224. 224. 288. 288. 352. 352. 192.]
[ 96. 224. 224. 288. 288. 352. 352. 192.]
[224. 480. 480. 544. 544. 608. 608. 320.]
[224. 480. 480. 544. 544. 608. 608. 320.]
[352. 736. 736. 800. 800. 864. 864. 448.]
[352. 736. 736. 800. 800. 864. 864. 448.]
[208. 432. 432. 464. 464. 496. 496. 256.]]
'''
|