前言
作为一种增强数据分辨率的方法,亚像素卷积被用于计算机视觉中的超分任务当中。与反卷积不同的是,反卷积是先补零,在进行卷积操作,而亚像素卷积是直接对数据进行channel,维度的扩大,再通过reshape的方式来提高分辨率。 亚像素卷积走的是: [B,H,W,C] -》[B,H,W,Crr] -》[B,Hr,Wr,C] 的过程,集体流程如下图所示
代码实现
import torch
import torch.nn as nn
import torch.nn.functional as F
class oneDConv(nn.Module):
# 卷积+ReLU函数
def __init__(self, in_channels, out_channels, kernel_sizes, paddings, dilations):
super().__init__()
self.conv = nn.Sequential(
nn.Conv1d(in_channels, out_channels, kernel_size=kernel_sizes , padding = paddings, dilation = dilations),
nn.BatchNorm1d(out_channels),
#nn.LayerNorm([out_channels,3199]),
)
def forward(self, x):
x = self.conv(x)
return x
class SubConv(nn.Module):
# 卷积+ReLU函数
def __init__(self, in_channels, r, kernel_sizes, paddings, dilations):
super().__init__()
self.conv = nn.Sequential(
nn.Conv1d(in_channels, in_channels*r, kernel_size=kernel_sizes , padding = paddings, dilation = dilations),
nn.BatchNorm1d(in_channels*r),
#nn.LayerNorm([out_channels,3199]),
)
def forward(self, x):
B,C,T = x.size()
print(B,C,T)
x = self.conv(x)
x = torch.reshape(x,(B,C,-1)) ###变形
return x
x=torch.randn(5, 1, 32000)
net = SubConv(1,2,1,0,1)
outputs = net(x)
print(outputs.size())
结果
|