卷积Conv

卷积后H、W输出:

$H_{out}/W_{out} = \frac{H_{in}/W_{in} - k + 2\times p} {stride} + 1$ , 如有小数,向下取整。

参数:groups

未分组:

输入图片shape:$H\times W \times c_1$ ,目标输出shape:$H\times W \times c_2$,未分组卷积核shape:$h\times w \times c_1$ 。 参数量:$c_2 \times (h \times w \times c_1)$

分组: 

设置参数groups=g,即将输入特征图按通道分成g组 ,则每组shape:$H\times W \times \frac{c_1}{g}$, 所以对应卷积核shape $h\times w \times \frac{c_1}{g}$ ,每组输出特征图shape:$H\times W \times \frac{c_2}{g}$

最终将g组输出特征图concat,得到$H\times W \times c_2$ 的输出。

所需参数量:c_2 \times h \times w \times \frac{c_1}{g}

分组卷积的参数量是标准卷积的 \frac{1}{g}

Depthwise Convolution 

当groups = in_channel = out_channel=c_1时,每个feature map一一对应一个卷积核,即参数量$\frac{c_1}{c_1}(1) \times H \times W * c_1 = c_1 \times H \times W$ 进一步减少了参数。

Deformable Convolution 可变形卷积

CNN对于未知形状变换的建模存在缺陷,因为CNN模块有固定的形状结构,即感受野是固定的。在进行诸如分割等精确定位的任务上效果不佳。在卷积网络中加入可学习的偏移量offset,使卷积核在feature map上不断发生偏移,即可更好学习ROI特征。

(a)即常见3x3卷积核,(b)即deformable conv,加上offset后采样点发生变化 ; (c) (d)是deformable conv的特殊形式。

绿框是原始卷积window,deformable conv可视为2branch,1branch通过额外conv学习offset(HxWx2N,2N的意思是有x,y两个方向的偏移),获得的offsets与feature map共同作为input输入2branch中(即相当于在蓝框中对feature map做卷积操作)。

注意,offset而是对feature map中的每个位置学习而非对kernel内容学习!

torchvision.ops.DeformConv2d(input: Tensor, offset: Tensor, mask: Optional[Tensor] = None)

  • input (Tensor[batch_size, in_channels, in_height, in_width]): input tensor
  • offset (Tensor[batch_size, 2 * offset_groups * kernel_height * kernel_width, out_height, out_width]): offsets to be applied for each position in the convolution kernel.
  • mask (Tensor[batch_size, offset_groups * kernel_height * kernel_width, out_height, out_width]): masks to be applied for each position in the convolution kernel.
#首先在__init__函数定义:
deform_conv2d = DeformConv2d(dim, dim, kernel_size, padding = 2, groups = deform_groups)

input = torch.rand(4, 3, 10, 10)
kh, kw = 3, 3
weight = torch.rand(5, 3, kh, kw)


# offset and mask should have the same spatial size as the output of the convolution. 
# if input h,w = 10, k=3, s=1, p=0 -> output h,w = 8 
offset = torch.rand(4, 2 * kh * kw, 8, 8)
mask = torch.rand(4, kh * kw, 8, 8)
out = deform_conv2d(input, offset, weight, mask=mask)
print(out.shape)

>>> # returns
>>>  torch.Size([4, 5, 8, 8])

 函数方法实现Conv2d:torch.nn.functional.conv2d

torch.nn.functional.conv2d(inputweightbias=Nonestride=1padding=0dilation=1groups=1)

Parameters:

  • input – input tensor of shape  (minibatch, in_channels, inH, inW)

  • weight – filters of shape (out_channels, groups / in_channels​, kernel_H, kernel_W)

  • bias – optional bias tensor of shape (out_channels)(out_channels). Default: None

  • stride – the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1.

nn.Conv2d是nn.Module的子类,而此为一个函数。

因此nn.Conv2d可以放在nn.Sequential里,而nn.functional.conv2d不可,同时,nn.functional.conv2d需要定义weight,每次调用它需要手动输入weight

### MATLAB `conv` 函数详解 #### 一、基本概念 卷积是一种重要的信号处理技术,在MATLAB中通过`conv`函数来实现两个向量或多维数组之间的线性卷积运算。该函数广泛应用于滤波器设计、图像处理等领域。 #### 二、语法结构 对于一维数据,可以使用如下命令调用此功能: ```matlab w = conv(u,v) ``` 这里`u`和`v`代表输入序列,而输出变量`w`则包含了两者之间完成后的离散线性卷积结果[^1]。 当涉及到多维度情况时,则需采用专门针对特定维度设计的版本如`conv2`用于二维矩阵间的操作以及`convn`适用于更高阶张量的情形[^4]。 #### 三、参数说明 - 如果只提供两个参数,则默认执行全尺寸模式下的卷积; - 可选第三个字符串类型的形参指定返回值的形式: - `'full'`: 返回完整的卷积结果,默认选项; - `'same'`: 输出与第一个输入相同大小的结果,通常取自中间部分; - `'valid'`: 去除边界效应影响的部分,即仅保留那些完全由有效样本贡献构成的数据点[^5]。 #### 四、实例展示 下面给出一段简单的代码片段用来演示如何利用`conv`来进行基础的一维卷积计算,并绘制相应的图形表示效果: ```matlab % 定义原始信号x(t)=sin(πt)+0.5cos(3πt), t∈[-2,2] fs = 1e2; % 设置采样频率为100Hz t = linspace(-2*pi,2*pi,floor(fs*4)); xt = sin(pi*t) + .5*cos(3*pi*t); % 构造矩形窗h(n)=(1/N)*rect((n-N/2)/N), N=8 hn = ones(1,8)/8; % 执行卷积操作得到响应y(t) yt = conv(xt,hn,'same'); % 绘制原信号及其经过低通滤波后的形态对比图 figure; subplot(2,1,1); plot(t, xt); title('Original Signal'); xlabel('Time (s)'); ylabel('Amplitude'); subplot(2,1,2); plot(t,yt);title('Filtered Signal by Convolution with Rectangular Window'); xlabel('Time (s)'); ylabel('Amplitude'); ``` 上述程序首先定义了一个复合正弦波作为测试对象,接着创建了一个长度固定的单位脉冲响应模拟简单平均过程,最后借助于`conv()`实现了两者的合成并通过可视化手段直观呈现出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值