1、普通卷积和动态卷积的区别
普通卷积
在普通卷积中,卷积核(或滤波器)是固定的,且在整个输入图像或特征图上共享。这意味着相同的卷积核会应用到输入的所有区域,无论数据的具体内容如何。这种方式具有较少的参数(对于固定卷积核来说),计算效率较高。
动态卷积
动态卷积的特点是卷积核在网络运行过程中是动态生成的,通常由输入数据的不同部分决定。这意味着不同的输入可能会使用不同的卷积核来进行处理,卷积核不再是固定的。动态卷积在某些任务(如多尺度处理、变换不变性处理等)中能提供更好的性能,但其计算开销较大,需要更多的参数和更高的计算资源。
2、举例
普通卷积
import torch
import torch.nn as nn
# 定义普通卷积模型
class StaticConvNet(nn.Module):
def __init__(self):
super(StaticConvNet, self).__init__()
# 定义一个普通的卷积层
self.conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
def forward(self, x):
return self.conv(x)
# 测试普通卷积
input_tensor = torch.randn(1, 3, 32, 32) # 1个样本,3个通道,32x32的输入图像
model = StaticConvNet()
output = model(input_tensor)
print(output.shape) # 输出形状
动态卷积
import torch
import torch.nn as nn
import torch.nn.functional as F
class Dynamic_conv2d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, K=4, ):
super(Dynamic_conv2d, self).__init__()
assert in_planes % groups == 0
self.in_planes = in_planes
self.out_planes = out_planes
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.bias = bias
self.K = K
self.attention = attention2d(in_planes, K, )
self.weight = nn.Parameter(torch.Tensor(K, out_planes, in_planes // groups, kernel_size, kernel_size),
requires_grad=True)
if bias:
self.bias = nn.Parameter(torch.Tensor(K, out_planes))
else:
self.bias = None
def forward(self, x): # 将batch视作维度变量,进行组卷积,因为组卷积的权重是不同的,动态卷积的权重也是不同的
softmax_attention = self.attention(x)
print(softmax_attention.size())
batch_size, in_planes, height, width = x.size()
x = x.view(1, -1, height, width) # 变化成一个维度进行组卷积
weight = self.weight.view(self.K, -1)
print('weight',weight.size())
# 动态卷积的权重的生成, 生成的是batch_size个卷积参数(每个参数不同)
aggregate_weight = torch.mm(softmax_attention, weight).view(self.out_planes, -1, self.kernel_size,
self.kernel_size)
print(aggregate_weight.size())
if self.bias is not None:
aggregate_bias = torch.mm(softmax_attention, self.bias).view(-1)
output = F.conv2d(x, weight=aggregate_weight, bias=aggregate_bias, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups * batch_size)
else:
output = F.conv2d(x, weight=aggregate_weight, bias=None, stride=self.stride, padding=self.padding,
dilation=self.dilation, groups=self.groups * batch_size)
output = output.view(batch_size, self.out_planes, output.size(-2), output.size(-1))
return output
class attention2d(nn.Module):
def __init__(self, in_planes, K,):
super(attention2d, self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.fc1 = nn.Conv2d(in_planes, K, 1,)
self.fc2 = nn.Conv2d(K, K, 1,)
def forward(self, x):
x = self.avgpool(x)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x).view(x.size(0), -1)
return F.softmax(x, 1)
# 测试动态卷积层
input_tensor = torch.randn(1, 3, 32, 32) # 假设输入图像为1个样本,3个通道,32x32的尺寸
model = Dynamic_conv2d(in_planes=3, out_planes=16, kernel_size=3)
output = model(input_tensor)
print(output.shape) # 输出的形状

被折叠的 条评论
为什么被折叠?



