resnet18、resnet50
网络结构来源:https://www.jianshu.com/p/085f4c8256f1
resnet18剪枝策略:
1.只剪basicblock的bn1相关层通道
2.同时剪basicblock的bn1和bn2相关层通道,其中bn2剪枝需要注意上下层通道一致性
resnet50剪枝策略:
1.只剪bottleneck的bn1和bn2相关层的通道
2.同时剪bottleneck的bn1、bn2和bn3相关层通道,其中bn2剪枝需要注意上下层通道一致性
关于通道一致性:以resnet18为例,每个bn1、bn2剪10个通道,剪bn2会出现重复剪的情况,需要手动调整。
resnet18调整示例代码:
def adjustment_pruningnumber(num_list):
for index,num in enumerate(num_list):
if index >0 and index%4 == 3:
tmp = int(min(num_list[index],num_list[index-2])/2)
num_list[index] = tmp
num_list[index-2] = tmp
adjustment_pruningnumber(num_list)
resnet50调整示例代码
def adjustment_pruningnumber(num_list,config):
config_index = 0
for index,num in enumerate(num_list):
if index+1 == sum([config[i] for i in range(config_index+1)]) * 3:
tmp = int(min([num_list[index-i*3] for i in range(config[config_index])])/config[config_index])
for i in range(config[config_index]):
num_list[index-i*3] = tmp
config_index += 1
adjustment_pruningnumber(num_list,[3,4,6,3])
resnet18v2
网络解读:https://www.cnblogs.com/puheng/p/9690160.html
resnetv1:Conv-BN-ReLU
resnetv2:BN-ReLU-Conv
resnet18v2剪枝策略:
1.只剪basicblock的bn2相关层通道
2.同时剪basicblock的bn1和bn2相关层通道,其中bn1剪枝需要注意上下层通道一致性
保持通道一致性resnet18v2调整示意代码:
def adjustment_pruningnumber(num_list):
for index,num in enumerate(num_list):
if index == 4:
tmp = int(min(num_list[index],num_list[index-2],num_list[index-4])/3)
num_list[index] = tmp
num_list[index-2] = tmp
num_list[index-4] = tmp
if index >=5 and index%4 == 0:
tmp = int(min(num_list[index],num_list[index-2])/2)
num_list[index] = tmp
num_list[index-2] = tmp
adjustment_pruningnumber(num_list)
senet(resnet18)
网络解读:https://zhuanlan.zhihu.com/p/65459972
简单实现:
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
class SEBottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None, reduction=16):
super(SEBottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.se = SELayer(planes * 4, reduction)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
out = self.se(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
剪枝策略同上。
resnext50
网络解读:https://zhuanlan.zhihu.com/p/32913695
结构上和resnet50基本上一样,通道多一点而已,最大的区别就是3x3的卷积使用分组卷积。
剪枝总体思路和resnet50差不多,但要注意分组卷积,因为目前使用的剪枝工具不支持:
1.使用以上等效结构,按照resnet50剪枝来剪
2.不使用等效结构,怎么解决分组问题?因为剪了某组的通道,组数变了,输入输出通道也不能倍组整除了:
添加判断条件,根据网络结构修改剪枝数量,并且重新计算组数,部分代码如下,32是根据具体网络通道参数来的
def computer_eachlayer_pruned_number(bn_modules,thresh):
num_list = []
#print(bn_modules)
for index,module in enumerate(bn_modules):
num = 0
#print(module.weight.data.abs(),thresh)
for data in module.weight.data.abs():
if thresh > data.float():
num +=1
if (module.num_features - num)%32 !=0:
num = module.num_features - ((module.num_features - num) // 32) * 32 - 32
num_list.append(num)
return num_list
mobilenetv2
v1、v2、v3网络解读:https://zhuanlan.zhihu.com/p/70703846
与resnet比较
剪枝策略,类似于resnet结构:
1.shortcut相关层不减,注意前几个Conv-BN-ReLU不要剪。
2.shortcut和Conv-BN-ReLU都剪(前几层除外),注意保持输入和输出的通道数量一致。
shufflenetv2
网络解读:https://zhuanlan.zhihu.com/p/67009992
v1的结构如图(a)、(b)
v2的结构如图©、(d)
shufflenetv2 torchvision里面的结构是重复多个[4,8,4],每组第一个是(d),剩下的是©
剪枝策略:
剪(d)右边的第一个1x1conv和3x3DWConv
xception
网络解读:https://zhuanlan.zhihu.com/p/127042277
剪枝思路:
1.涉及分支相关的层都不减
2.类似于resnet结构,有shortcut的相关层(middle flow的第三个SeparableConv)都不剪。
3.middle flow的第三个SeparableConv剪,但是middle flow的上一层不剪。
densenet121
网络解读:https://zhuanlan.zhihu.com/p/37189203
剪枝策略:
1.注意BN-ReLU-Conv的顺序,剪每一个denselayer的bn2。
2.连同bn1、bn2一起剪,剪枝率需要设置的非常小,因为每个后面层的bn1剪枝都会影响前面通道的减少。
darknet53
训练太慢了,可以剪枝,同上。
Faster RCNN剪枝
参考代码:https://github.com/supernotman/Faster-RCNN-with-torchvision
算法解读:https://zhuanlan.zhihu.com/p/103745786