想知道是否可以将shift conv 是否也能像普通卷积一样进行结构重参数化
感谢对SCNet的关注! 这个是一个很有趣的问题,关于重参数化的设计,我们在之前也进行过一些简单的验证。
基于RepVGG [1] 的重参数方法,将多分支的conv结构合并成最大的单一conv层。
在SCNet的消融部分,我们验证了Shift-Conv在不同‘kernel size’下的表现,例如3x3、5x5、非对称结构或者空洞卷积。 SCNet中Shift-Conv的一种实现形式是基于Depth-wise Conv,从功能实现上看是可以结合重参数化技巧的,例如在5x5的Shift-Conv中融合3x3Shift-Conv。这部分我们在实验中进行过一些简单的验证,验证的结果是并没有明显提升。这部分并没有引入额外的先验帮助模型初始化或者更好的学习。
Thank you for your interest in SCNet! This is an intriguing question. We have previously conducted some preliminary evaluation regarding the design of reparametrization.
Based on the reparameterization method in RepVGG [1], we can merge multi-branch conv structures into a single conv layer with the maximum kernel size.
In the ablation study of SCNet, we examined the performance of Shift-Conv under different 'kernel sizes' such as 3x3, 5x5, asymmetrical structures, and dilated convolutions. Notably, one implementation of Shift-Conv in SCNet is based on Depth-wise Conv. From this point, it can be combined with reparameterization techniques, such as integrating a 3x3 Shift-Conv within a 5x5 Shift-Conv. We have done some preliminary tests on this, but the results did not show significant improvement. We think this approach did not introduce any additional priors to assist in model initialization or enhanced learning.
[1] Xiaohan Ding, Xiangyu Zhang, Ningning Ma, Jungong Han, Guiguang Ding, Jian Sun: RepVGG: Making VGG-Style ConvNets Great Again. CVPR 2021: 13733-13742
感谢对SCNet的关注! 这个是一个很有趣的问题,关于重参数化的设计,我们在之前也进行过一些简单的验证。
基于RepVGG [1] 的重参数方法,将多分支的conv结构合并成最大的单一conv层。
在SCNet的消融部分,我们验证了Shift-Conv在不同‘kernel size’下的表现,例如3x3、5x5、非对称结构或者空洞卷积。 SCNet中Shift-Conv的一种实现形式是基于Depth-wise Conv,从功能实现上看是可以结合重参数化技巧的,例如在5x5的Shift-Conv中融合3x3Shift-Conv。这部分我们在实验中进行过一些简单的验证,验证的结果是并没有明显提升。这部分并没有引入额外的先验帮助模型初始化或者更好的学习。
Thank you for your interest in SCNet! This is an intriguing question. We have previously conducted some preliminary evaluation regarding the design of reparametrization.
Based on the reparameterization method in RepVGG [1], we can merge multi-branch conv structures into a single conv layer with the maximum kernel size.
In the ablation study of SCNet, we examined the performance of Shift-Conv under different 'kernel sizes' such as 3x3, 5x5, asymmetrical structures, and dilated convolutions. Notably, one implementation of Shift-Conv in SCNet is based on Depth-wise Conv. From this point, it can be combined with reparameterization techniques, such as integrating a 3x3 Shift-Conv within a 5x5 Shift-Conv. We have done some preliminary tests on this, but the results did not show significant improvement. We think this approach did not introduce any additional priors to assist in model initialization or enhanced learning.
[1] Xiaohan Ding, Xiangyu Zhang, Ningning Ma, Jungong Han, Guiguang Ding, Jian Sun: RepVGG: Making VGG-Style ConvNets Great Again. CVPR 2021: 13733-13742
想知道有对应的代码实现吗,我想学习一下shift conv与常规卷积的结构重参数化有什么区别呢
感谢对SCNet的关注! 这个是一个很有趣的问题,关于重参数化的设计,我们在之前也进行过一些简单的验证。
基于RepVGG [1] 的重参数方法,将多分支的conv结构合并成最大的单一conv层。
在SCNet的消融部分,我们验证了Shift-Conv在不同‘kernel size’下的表现,例如3x3、5x5、非对称结构或者空洞卷积。 SCNet中Shift-Conv的一种实现形式是基于Depth-wise Conv,从功能实现上看是可以结合重参数化技巧的,例如在5x5的Shift-Conv中融合3x3Shift-Conv。这部分我们在实验中进行过一些简单的验证,验证的结果是并没有明显提升。这部分并没有引入额外的先验帮助模型初始化或者更好的学习。
Thank you for your interest in SCNet! This is an intriguing question. We have previously conducted some preliminary evaluation regarding the design of reparametrization.
Based on the reparameterization method in RepVGG [1], we can merge multi-branch conv structures into a single conv layer with the maximum kernel size.
In the ablation study of SCNet, we examined the performance of Shift-Conv under different 'kernel sizes' such as 3x3, 5x5, asymmetrical structures, and dilated convolutions. Notably, one implementation of Shift-Conv in SCNet is based on Depth-wise Conv. From this point, it can be combined with reparameterization techniques, such as integrating a 3x3 Shift-Conv within a 5x5 Shift-Conv. We have done some preliminary tests on this, but the results did not show significant improvement. We think this approach did not introduce any additional priors to assist in model initialization or enhanced learning.
[1] Xiaohan Ding, Xiangyu Zhang, Ningning Ma, Jungong Han, Guiguang Ding, Jian Sun: RepVGG: Making VGG-Style ConvNets Great Again. CVPR 2021: 13733-13742
如论文所说,我因为刚了解重参数化,如果这样进行,只能将不同分支的1X1卷积权重,bias进行叠加,想知道各自分支对应的移位部分应该怎么处理呢,如何进行进一步的结构重参数化
使用Claude4.5对Shift8算子进行重参数化,得到代码如下。经过简单验证,计算结果是等价的,加速比在不同环境下差异较大,但总体来说是提升稳定。
import torch
from torch import nn as nn
from torch.nn import functional as F
class Shift8(nn.Module):
def __init__(self, groups=4, stride=1, mode="constant") -> None:
super().__init__()
self.g = groups
self.mode = mode
self.stride = stride
# 添加一个标志来控制是否使用重参数化版本
self.reparam_conv = None
def forward(self, x):
# 如果已经重参数化,使用卷积
if self.reparam_conv is not None:
return self.reparam_conv(x)
# 否则使用原始的shift操作
b, c, h, w = x.shape
out = torch.zeros_like(x)
pad_x = F.pad(x, pad=[self.stride for _ in range(4)], mode=self.mode)
assert c == self.g * 8
cx, cy = self.stride, self.stride
stride = self.stride
out[:, 0 * self.g : 1 * self.g, :, :] = pad_x[
:, 0 * self.g : 1 * self.g, cx - stride : cx - stride + h, cy : cy + w
]
out[:, 1 * self.g : 2 * self.g, :, :] = pad_x[
:, 1 * self.g : 2 * self.g, cx + stride : cx + stride + h, cy : cy + w
]
out[:, 2 * self.g : 3 * self.g, :, :] = pad_x[
:, 2 * self.g : 3 * self.g, cx : cx + h, cy - stride : cy - stride + w
]
out[:, 3 * self.g : 4 * self.g, :, :] = pad_x[
:, 3 * self.g : 4 * self.g, cx : cx + h, cy + stride : cy + stride + w
]
out[:, 4 * self.g : 5 * self.g, :, :] = pad_x[
:,
4 * self.g : 5 * self.g,
cx + stride : cx + stride + h,
cy + stride : cy + stride + w,
]
out[:, 5 * self.g : 6 * self.g, :, :] = pad_x[
:,
5 * self.g : 6 * self.g,
cx + stride : cx + stride + h,
cy - stride : cy - stride + w,
]
out[:, 6 * self.g : 7 * self.g, :, :] = pad_x[
:,
6 * self.g : 7 * self.g,
cx - stride : cx - stride + h,
cy + stride : cy + stride + w,
]
out[:, 7 * self.g : 8 * self.g, :, :] = pad_x[
:,
7 * self.g : 8 * self.g,
cx - stride : cx - stride + h,
cy - stride : cy - stride + w,
]
return out
def reparameterize(self):
"""将Shift操作转换为等效的深度卷积"""
kernel_size = 2 * self.stride + 1
channels = self.g * 8
# 创建深度卷积 (每个通道独立卷积)
conv = nn.Conv2d(
channels,
channels,
kernel_size=kernel_size,
stride=1,
padding=self.stride,
groups=channels, # 深度卷积
bias=False
)
# 初始化卷积核权重
with torch.no_grad():
weight = torch.zeros(channels, 1, kernel_size, kernel_size)
center = self.stride
# 为每个通道组设置对应的shift模式
for i in range(8):
start_ch = i * self.g
end_ch = (i + 1) * self.g
if i == 0: # 上移
weight[start_ch:end_ch, 0, center - self.stride, center] = 1.0
elif i == 1: # 下移
weight[start_ch:end_ch, 0, center + self.stride, center] = 1.0
elif i == 2: # 左移
weight[start_ch:end_ch, 0, center, center - self.stride] = 1.0
elif i == 3: # 右移
weight[start_ch:end_ch, 0, center, center + self.stride] = 1.0
elif i == 4: # 右下移
weight[start_ch:end_ch, 0, center + self.stride, center + self.stride] = 1.0
elif i == 5: # 左下移
weight[start_ch:end_ch, 0, center + self.stride, center - self.stride] = 1.0
elif i == 6: # 右上移
weight[start_ch:end_ch, 0, center - self.stride, center + self.stride] = 1.0
elif i == 7: # 左上移
weight[start_ch:end_ch, 0, center - self.stride, center - self.stride] = 1.0
conv.weight.data = weight
conv.weight.requires_grad = False
conv.eval()
self.reparam_conv = conv
return conv
# 为整个模型添加重参数化方法
def switch_to_deploy(model):
"""将模型中所有Shift8层转换为卷积"""
for module in model.modules():
if isinstance(module, Shift8):
module.reparameterize()
return model
# 测试代码
if __name__ == "__main__":
# 创建Shift8层
shift = Shift8(groups=8, stride=1)
x = torch.randn(1, 64, 32, 32)
# 原始输出
with torch.no_grad():
out1 = shift(x)
# 重参数化后的输出
shift.reparameterize()
with torch.no_grad():
out2 = shift(x)
# 验证结果一致性
print(f"输出差异: {torch.abs(out1 - out2).max().item()}")
print(f"相对误差: {(torch.abs(out1 - out2) / (torch.abs(out1) + 1e-8)).mean().item()}")
# 性能测试
import time
# 原始Shift8
shift_original = Shift8(groups=8, stride=1)
x = torch.randn(4, 64, 256, 256).cuda()
shift_original = shift_original.cuda()
torch.cuda.synchronize()
start = time.time()
for _ in range(100):
_ = shift_original(x)
torch.cuda.synchronize()
original_time = time.time() - start
# 重参数化版本
shift_reparam = Shift8(groups=8, stride=1)
shift_reparam.reparameterize()
shift_reparam = shift_reparam.cuda()
torch.cuda.synchronize()
start = time.time()
for _ in range(100):
_ = shift_reparam(x)
torch.cuda.synchronize()
reparam_time = time.time() - start
print(f"\n原始Shift8耗时: {original_time:.4f}s")
print(f"重参数化卷积耗时: {reparam_time:.4f}s")
print(f"加速比: {original_time / reparam_time:.2f}x")