0


论文阅读笔记:ShuffleNet

1. 背景

由于深度学习模型结构越来越复杂,参数量也越来越大,需要大量的算力去做模型的训练和推理。然而随着移动设备的普及,将深度学习模型部署于计算资源有限基于ARM的移动设备成为了研究的热点。

ShuffleNet[1]是一种专门为计算资源有限的设备设计的神经网络结构,主要采用了pointwise group convolutionchannel shuffle两种技术,在保留了模型精度的同时极大减少了计算开销。

[1] Zhang X, Zhou X, Lin M, et al. Shufflenet: An extremely efficient convolutional neural network for mobile devices[C].Proceedings of the IEEE conference on computer vision and pattern recognition. 2018: 6848-6856.

2. 相关工作

在论文中,提到了目前sota的两个工作,一个是谷歌的Xception,另一个是facebook推出的ResNeXt。

2.1 Xception

Xception[2]主要涉及了一个技术:深度可分离卷积,即把原本常规的卷积操作分为两步去做。
常规卷积是利用若干个多通道卷积核对输入的多通道图像进行处理,输出的是既提取了通道特征又提取了空间特征的feature map。
在这里插入图片描述
而深度可分离卷积将提取通道特征(PointWise Convolution)和空间特征(DepthWise Convolution)分为了两步去做:
首先卷积核从三维变为了二维的,每个卷积核只负责输入图像的一个通道,用于提取空间特征,这一步操作中不涉及通道和通道之间的信息交互。接着通过一维卷积来完成通道之间特征提取的工作,即一个常规的卷积操作,只不过卷积核是1*1的。
在这里插入图片描述
在这里插入图片描述
这样做的好处是降低了常规卷积时的参数量,假设输入通道为

  1. M
  2. M
  3. M, 输出通道为
  4. N
  5. N
  6. N,卷积核大小为
  7. k
  8. ×
  9. k
  10. ,
  11. k \times k, 那么
  12. k×k,那么常规卷积的参数是:
  13. N
  14. ×
  15. M
  16. ×
  17. k
  18. ×
  19. k
  20. N \times M \times k \times k
  21. N×M×k×k。而通过深度可分离卷积之后,参数量为
  22. M
  23. ×
  24. k
  25. ×
  26. k
  27. +
  28. N
  29. ×
  30. M
  31. ×
  32. 1
  33. ×
  34. 1
  35. M \times k \times k + N \times M \times 1 \times 1
  36. M×k×k+N×M×1×1

代码如下:

  1. classSeparableConv2d(nn.Module):def__init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, dilation=1):super(SeparableConv2d, self).__init__()
  2. self.conv1 = nn.Conv2d(
  3. in_channels, in_channels, kernel_size, stride, padding, dilation, groups=in_channels, bias=False)
  4. self.pointwise = nn.Conv2d(in_channels, out_channels,1,1,0,1,1, bias=False)defforward(self, x):
  5. x = self.conv1(x)
  6. x = self.pointwise(x)return x

[2] Chollet F. Xception: Deep learning with depthwise separable convolutions[C]. Proceedings of the IEEE conference on computer vision and pattern recognition. 2017: 1251-1258.

2.2 ResNeXt

作者灵感来源于VGG的模块化堆叠的结构,提出了一种基于分组卷积和残差连接的模块化卷积块从而降低了参数的数量。简单来说,理解了分组卷积的思想就能理解ResNeXt。
在这里插入图片描述

[3] Xie S, Girshick R, Dollár P, et al. Aggregated residual transformations for deep neural networks[C]. Proceedings of the IEEE conference on computer vision and pattern recognition. 2017: 1492-1500.

3. ShuffleNet

由于使用

  1. 1
  2. ×
  3. 1
  4. 1 \times 1
  5. 1×1卷积核进行操作时的复杂度较高,因为需要和每个像素点做互相关运算,作者关注到ResNeXt的设计中,
  6. 1
  7. ×
  8. 1
  9. 1 \times 1
  10. 1×1卷积操作的那一层需要消耗大量的计算资源,因此提出将这一层也设计为分组卷积的形式。然而,分组卷积只会在组内进行卷积,因此组和组之间不存在信息的交互,为了使得信息在组之间流动,作者提出将每次分组卷积后的结果进行组内分组,再互相交换各自的组内的子组。

在这里插入图片描述
在这里插入图片描述
上图c就是一个shufflenet块,图a是一个简单的残差连接块,区别在于,shufflenet将残差连接改为了一个平均池化的操作与卷积操作之后做cancat,并且将

  1. 1
  2. ×
  3. 1
  4. 1 \times 1
  5. 1×1卷积改为了分组卷积,并且在分组之后进行了channel shuffle操作。

3.1 代码讲解

代码如下:
首先定义1x1,3x3,depthwise_3x3卷积操作:

  1. import torch
  2. import torch.nn as nn
  3. import torchvision
  4. from torch.utils import data
  5. import matplotlib.pyplot as plt
  6. import copy
  7. defconv1x1(in_channels, out_channels, stride=1, groups=1, bias=False):# 1x1卷积操作return nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
  8. kernel_size=1, stride=stride, groups=groups, bias=bias)defconv3x3(in_channels, out_channels, stride=1, padding=1, dilation=1, groups=1, bias=False):# 3x3卷积操作# 默认不是下采样return nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
  9. kernel_size=3, stride=stride, padding=padding, dilation=dilation,
  10. groups=groups,bias=bias)defdepthwise_con3x3(channels, stride):# 空间特征抽取# 输入通道和输出通道相等,且分组数等于通道数return nn.Conv2d(in_channels=channels, out_channels=channels,
  11. kernel_size=3, stride=stride, padding=1, groups=channels,bias=False)

接着是核心的channel shuffle操作:
通过矩阵变化即可实现,此操作并不会改变通道数和图像的尺寸
在这里插入图片描述

  1. defchannel_shuffle(x, groups):# x[batch_size, channels, H, W]
  2. batch, channels, height, width = x.size()
  3. channels_per_group = channels // groups # 每组通道数
  4. x = x.view(batch, groups, channels_per_group, height, width)
  5. x = torch.transpose(x,1,2).contiguous()
  6. x = x.view(batch, channels, height, width)return x
  7. classChannelShuffle(nn.Module):def__init__(self, channels, groups):super(ChannelShuffle, self).__init__()if channels % groups !=0:raise ValueError("通道数必须可以整除组数")
  8. self.groups = groups
  9. defforward(self, x):return channel_shuffle(x, self.groups)

然后定义shufflenet块,分为下采样和不下采样:

  1. classShuffleUnit(nn.Module):def__init__(self, in_channels, out_channels, groups, downsample, ignore_group):# 如果做下采样,那么通道数翻倍,高宽减半# 如果不做下采样,那么输入输出通道数相等,高宽不变super(ShuffleUnit, self).__init__()
  2. self.downsample = downsample
  3. mid_channels = out_channels //4if downsample:
  4. out_channels -= in_channels
  5. else:assert in_channels == out_channels,"不做下采样时应该输入输出通道相等"
  6. self.compress_conv1 = conv1x1(
  7. in_channels=in_channels,
  8. out_channels=mid_channels,
  9. groups=(1if ignore_group else groups))
  10. self.compress_bn1 = nn.BatchNorm2d(num_features=mid_channels)
  11. self.c_shuffle = ChannelShuffle(channels=mid_channels, groups=groups)
  12. self.dw_conv2 = depthwise_con3x3(channels=mid_channels, stride=(2if downsample else1))
  13. self.dw_bn2 = nn.BatchNorm2d(num_features=mid_channels)
  14. self.expand_conv3 = conv1x1(
  15. in_channels=mid_channels,
  16. out_channels=out_channels,
  17. groups=groups
  18. )
  19. self.expand_bn3 = nn.BatchNorm2d(num_features=out_channels)if downsample:
  20. self.avgpool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
  21. self.activ = nn.ReLU(inplace=True)defforward(self, x):
  22. identity = x
  23. x = self.compress_conv1(x)# x[batch_size, mid_channels, H, W]
  24. x = self.compress_bn1(x)
  25. x = self.activ(x)
  26. x = self.c_shuffle(x)
  27. x = self.dw_conv2(x)# x[batch_size, mid_channels, H, w]
  28. x = self.dw_bn2(x)
  29. x = self.expand_conv3(x)# x[batch_size, out_channels, H, W]
  30. x = self.expand_bn3(x)if self.downsample:
  31. identity = self.avgpool(identity)
  32. x = torch.cat((x, identity), dim=1)# 通道维上拼接else:
  33. x = x + identity
  34. x = self.activ(x)return x

在进入shufflenet之前常规地做一个下采样:

  1. classShuffleInitBlock(nn.Module):def__init__(self, in_channels, out_channels):super(ShuffleInitBlock, self).__init__()
  2. self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1)# 下采样
  3. self.bn = nn.BatchNorm2d(out_channels)
  4. self.activ = nn.ReLU(inplace=True)
  5. self.pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)# 下采样defforward(self, x):
  6. x = self.conv(x)
  7. x = self.bn(x)
  8. x = self.activ(x)
  9. x = self.pool(x)return x

建立shufflenet完整的流程:

  1. classShuffleNet(nn.Module):def__init__(self, channels, init_block_channels, groups, in_channels=1, in_size=(224,224), num_classes=10):super(ShuffleNet, self).__init__()
  2. self.in_size = in_size
  3. self.num_classes = num_classes
  4. self.features = nn.Sequential()
  5. self.features.add_module("init_block", ShuffleInitBlock(in_channels, init_block_channels))
  6. in_channels = init_block_channels
  7. for i, channels_per_stage inenumerate(channels):
  8. stage = nn.Sequential()for j, out_channels inenumerate(channels_per_stage):
  9. downsample =(j ==0)
  10. ignore_group =(i==0)and(j==0)
  11. stage.add_module("unit{}".format(j +1), ShuffleUnit(
  12. in_channels=in_channels,
  13. out_channels=out_channels,
  14. groups=groups,
  15. downsample=downsample,
  16. ignore_group=ignore_group))
  17. in_channels = out_channels
  18. self.features.add_module("stage{}".format(i +1), stage)
  19. self.features.add_module("final_pool", nn.AvgPool2d(
  20. kernel_size=7,
  21. stride=1))
  22. self.output = nn.Linear(
  23. in_features=in_channels,
  24. out_features=num_classes)defforward(self, x):
  25. x = self.features(x)
  26. x = x.view(x.size(0),-1)
  27. x = self.output(x)return x
  28. defget_shufflenet(groups, width_scale):
  29. init_block_channels =24
  30. layers =[2,4,2]if groups ==1:
  31. channels_per_layers =[144,288,576]elif groups ==2:
  32. channels_per_layers =[200,400,800]elif groups ==3:
  33. channels_per_layers =[240,480,960]elif groups ==4:
  34. channels_per_layers =[272,544,1088]elif groups ==8:
  35. channels_per_layers =[384,768,1536]else:raise ValueError("The {} of groups is not supported".format(groups))
  36. channels =[[ci]* li for(ci, li)inzip(channels_per_layers, layers)]if width_scale !=1.0:
  37. channels =[[int(cij * width_scale)for cij in ci]for ci in channels]
  38. init_block_channels =int(init_block_channels * width_scale)
  39. net = ShuffleNet(
  40. channels=channels,
  41. init_block_channels=init_block_channels,
  42. groups=groups)return net

训练过程:

  1. net = get_shufflenet(groups=2, width_scale=1.0)
  2. NUM_EPOCHS =10
  3. BATCH_SIZE =64
  4. NUM_CLASSES =10
  5. LR =0.001defload_data_fashion_mnist(batch_size, resize=None):
  6. trans =[torchvision.transforms.ToTensor()]if resize:
  7. trans.insert(0, torchvision.transforms.Resize(resize))
  8. trans = torchvision.transforms.Compose(trans)
  9. mnist_train = torchvision.datasets.FashionMNIST(
  10. root="./FashionMinist", train=True, transform=trans, download=True)
  11. mnist_test = torchvision.datasets.FashionMNIST(
  12. root="./FashionMinist", train=False, transform=trans, download=True)return(data.DataLoader(mnist_train, batch_size, shuffle=True,),
  13. data.DataLoader(mnist_test, batch_size, shuffle=False,))
  14. train_loader, test_loader = load_data_fashion_mnist(BATCH_SIZE,224)
  15. device = torch.device('cuda'if torch.cuda.is_available()else'cpu')defvalidate(net, data):
  16. total =0
  17. correct =0
  18. net.eval()with torch.no_grad():for i,(images, labels)inenumerate(data):
  19. images = images.to(device)
  20. x = net(images)
  21. value, pred = torch.max(x,1)
  22. pred = pred.data.cpu()
  23. total += x.size(0)
  24. correct += torch.sum(pred == labels)return correct*100./total
  25. deftrain(net):
  26. lossfunc = nn.CrossEntropyLoss()
  27. optimizer = torch.optim.Adam(net.parameters(), lr=LR)
  28. max_accuracy =0
  29. accuracies =[]definit_weights(m):iftype(m)== nn.Linear ortype(m)== nn.Conv2d:
  30. nn.init.xavier_uniform_(m.weight)
  31. net.apply(init_weights)
  32. net = net.to(device)
  33. net.train()for epoch inrange(NUM_EPOCHS):for i,(images, labels)inenumerate(train_loader):
  34. images = images.to(device)
  35. labels = labels.to(device)
  36. optimizer.zero_grad()
  37. out = net(images)
  38. loss = lossfunc(out, labels)
  39. loss_item = loss.item()
  40. loss.backward()
  41. optimizer.step()
  42. accuracy =float(validate(net, test_loader))
  43. accuracies.append(accuracy)print("Epoch %d accuracy: %f loss: %f"%(epoch, accuracy, loss_item))if accuracy > max_accuracy:
  44. best_model = copy.deepcopy(net)
  45. max_accuracy = accuracy
  46. print("Saving Best Model with Accuracy: ", accuracy)
  47. plt.plot(accuracies)return best_model
  48. shufflenet = train(net)

4. ShuffleNetV2

Ma N, Zhang X, Zheng H T, et al. Shufflenet v2: Practical guidelines for efficient cnn architecture design[C]. Proceedings of the European conference on computer vision (ECCV). 2018: 116-131.

ShuffleNetV2 这篇文章对shufflenet进行了进一步的改进,并且提出了四个设计轻量化网络的原则:

  • 输入输出通道相同时,内存访问量MAC最小
  • 分组数过大会导致MAC增加
  • 碎片化操作对并行加速不友好
  • 逐元素操作会增加内存的消耗在这里插入图片描述 可以看到,shufflenetv2替换了1x1的分组卷积,并且尽量避免了add操作。 变动很小,就直接给出代码了:
  1. classShuffleUnitV2(nn.Module):def__init__(self, in_channels, out_channels, downsample, use_residual):super(ShuffleUnitV2, self).__init__()
  2. self.downsample = downsample
  3. self.use_residual = use_residual
  4. mid_channels = out_channels //2
  5. self.compress_conv1 = conv1x1(
  6. in_channels=(in_channels if downsample else mid_channels),
  7. out_channels=mid_channels
  8. )
  9. self.compress_bn1 = nn.BatchNorm2d(num_features=mid_channels)
  10. self.dw_conv2 = depthwise_con3x3(
  11. channels=mid_channels,
  12. stride=(2if downsample else1))
  13. self.dw_bn2 = nn.BatchNorm2d(mid_channels)
  14. self.expand_conv3 = conv1x1(
  15. in_channels=mid_channels,
  16. out_channels=mid_channels
  17. )
  18. self.expand_bn3 = nn.BatchNorm2d(num_features=mid_channels)if downsample:
  19. self.dw_conv4 = depthwise_con3x3(
  20. channels=in_channels,
  21. stride=2)
  22. self.dw_bn4 = nn.BatchNorm2d(num_features=in_channels)
  23. self.expand_conv5 = conv1x1(
  24. in_channels=in_channels,
  25. out_channels=mid_channels
  26. )
  27. self.expand_bn5 = nn.BatchNorm2d(num_features=mid_channels)
  28. self.activ = nn.ReLU(inplace=True)
  29. self.c_shuffle = ChannelShuffle(
  30. channels=out_channels,
  31. groups=2)defforward(self, x):if self.downsample:
  32. y1 = self.dw_conv4(x)
  33. y1 = self.dw_bn4(y1)
  34. y1 = self.expand_conv5(y1)
  35. y1 = self.expand_bn5(y1)
  36. y1 = self.activ(y1)
  37. x2 = x
  38. else:
  39. y1, x2 = torch.chunk(x, chunks=2, dim=1)
  40. y2 = self.compress_conv1(x2)
  41. y2 = self.compress_bn1(y2)
  42. y2 = self.activ(y2)
  43. y2 = self.dw_conv2(y2)
  44. y2 = self.dw_bn2(y2)
  45. y2 = self.expand_conv3(y2)
  46. y2 = self.expand_bn3(y2)
  47. y2 = self.activ(y2)if self.use_residual andnot self.downsample:
  48. y2 = y2 + x2
  49. x = torch.cat((y1, y2), dim=1)
  50. x = self.c_shuffle(x)return x
  1. classShuffleNetV2(nn.Module):def__init__(self, channels, init_block_channels, final_block_channels,
  2. use_residual=False, in_channels=1, in_size=(224,224), num_classes=10):super(ShuffleNetV2, self).__init__()
  3. self.in_size = in_size
  4. self.num_classes = num_classes
  5. self.features = nn.Sequential()
  6. self.features.add_module("init_block", ShuffleInitBlock(
  7. in_channels=in_channels,
  8. out_channels=init_block_channels))
  9. in_channels = init_block_channels
  10. for i, channels_per_stage inenumerate(channels):
  11. stage = nn.Sequential()for j, out_channels inenumerate(channels_per_stage):
  12. downsample =(j==0)
  13. stage.add_module("unit{}".format(j+1), ShuffleUnitV2(
  14. in_channels=in_channels,
  15. out_channels=out_channels,
  16. downsample=downsample,
  17. use_residual=use_residual
  18. ))
  19. in_channels=out_channels
  20. self.features.add_module("stage{}".format(i +1), stage)
  21. self.features.add_module("final_block", conv1x1(
  22. in_channels=in_channels,
  23. out_channels=final_block_channels
  24. ))
  25. in_channels = final_block_channels
  26. self.features.add_module("final_bn", nn.BatchNorm2d(num_features=in_channels))
  27. self.features.add_module("final_pool", nn.AdaptiveAvgPool2d(output_size=(1,1)))
  28. self.features.add_module("flatten", nn.Flatten())
  29. self.output = nn.Linear(
  30. in_features=in_channels,
  31. out_features=num_classes
  32. )defforward(self, x):
  33. x = self.features(x)
  34. x = x.view(x.size(0),-1)
  35. x = self.output(x)return x
  1. defget_shufflenetv2(width_scale):
  2. init_block_channels =24
  3. final_block_channels =1024
  4. layers =[4,8,4]
  5. channels_per_layers =[116,232,464]
  6. channels =[[ci]* li for(ci, li)inzip(channels_per_layers, layers)]if width_scale !=1.0:
  7. channels =[[int(cij * width_scale)for cij in ci]for ci in channels]if width_scale >1.5:
  8. final_block_channels =int(final_block_channels * width_scale)
  9. net = ShuffleNetV2(
  10. channels=channels,
  11. init_block_channels=init_block_channels,
  12. final_block_channels=final_block_channels)return net

本文转载自: https://blog.csdn.net/loki2018/article/details/124077822
版权归原作者 loki2018 所有, 如有侵权,请联系我们删除。

“论文阅读笔记:ShuffleNet”的评论:

还没有评论