转置卷积 上采样

转置卷积 上采样

转置卷积也就是逆卷积

一句话解释:逆卷积相对于卷积在神经网络结构的正向和反向传播中做相反的运算

逆卷积(Deconvolution)比较容易引起误会,转置卷积(Transposed Convolution)是一个更为合适的叫法

输入矩阵可展开为16维向量,记作[公式]
输出矩阵可展开为4维向量,记作[公式]
卷积运算可表示为[公式]

反向传播时为

所谓逆卷积其实就是正向时左乘[公式],而反向时左乘[公式],即[公式]的运算。

转置卷积 上采样 公式推导

Input:

Output:

转置卷积公式:

转置卷积 上采样 在pytorch中的用法

from torch import nn
import torch

upsample = nn.ConvTranspose2d(in_channels=3,
out_channels=3,
kernal_size=3,
stride=1,
padding=0,
output_padding=0,
groups=1,
bias=True)

# in_channels、out_channels为当前输入通道数和输出通道数
# 注意,这上面的stride、padding是争对于与原始卷积上的stride和padding
# out_padding为最后输出时的填充,即做完所有操作之后的输出最后再做out_padding

input = torch.randn(1, 3, 2, 2)
output = upsample(input)
print(output.size())
from torch import nn
import torch

maxpool = nn.MaxPool2d((2, 2), stride=(2, 2),return_indices=True)

maxunpool = nn.MaxUnpool2d(kernel_size=(2,2),stride=(2,2),padding=0)

input =torch.Tensor(np.arange(64).reshape((8,8)).reshape(1,1,8,8))

x, indices = maxpool(input)

output1 = maxunpool(x, indices)
output2 = nn.functional.interpolate(x, scale_factor=2, mode="bilinear")

# torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)
# Down/up samples the input to either the given size or the given scale_factor
# The input dimensions are interpreted in the form: mini-batch x channels x [optional depth] x [optional height] x width.
# The modes available for resizing are: nearest, linear (3D-only), bilinear, bicubic (4D-only), trilinear (5D-only), area
# input (Tensor) – the input tensor
# size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) – output spatial size.
# scale_factor (float or Tuple[float]) – multiplier for spatial size. Has to match input size if it is a tuple.
# mode (str) – algorithm used for upsampling: 'nearest' | 'linear' | 'bilinear' | 'bicubic' | 'trilinear' | 'area'. Default: 'nearest'

最邻近、线性、双线性、双三次、三线性、区域插值

就是图像的插值方法

用得最多的是最邻近插值、双线性插值、双三次插值

Author: pangzibo243
Link: https://litianbo243.github.io/2019/09/10/转置卷积-上采样/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
支付宝打赏
微信打赏