GhostNet
論文地址
文章目錄
- 前言
- 一、論文解讀
- 1.Ghost卷積設計
- 2.Gbneck殘差模塊設計
- 3.GhostNet網絡架構
- 二、代碼實現
- 1.卷積模塊
- 2.殘差模塊實現
- 3.網絡實現
- 三、驗證模型
- 總結
前言
Ghostnet提出自己的架構設計思路:特征圖的冗余是深度神經網絡成功的一個重要特征。Ghostnet傾向于以一種代價低廉的線性操作來獲取它們,而不是避免冗余的特性映射!
一、論文解讀
1.Ghost卷積設計
在本文中,我們引入了一個新的Ghost模塊,用更少的參數生成更多的特征。具體來說,將深度神經網絡中的普通卷積層分為兩部分。第一部分涉及普通的卷積,但它們的總數將被嚴格控制。給定第一部分的固有特征圖,然后應用一系列簡單的線性操作來生成更多的特征圖。在不改變輸出特征圖大小的情況下,與普通卷積神經網絡相比,Ghost模塊所需的參數總數和計算復雜度都有所降低。實現過程如下:
首先通過主卷積生成基本特征圖集合:X為輸入特征圖(尺寸參數為h:寬度,w:長度,c:通道數),f’為卷積層(尺寸參數為c:輸入通道數,k:卷積核尺寸大小,m為輸出通道數),X在與卷積層卷積后輸出特征圖集合Y’(尺寸參數為h’,w’,m)。此處省略了偏置參數。
然后,對輸出的基本特征圖集合Y’做線性變化,以生成Ghost特征圖。其中 fi是基本特征圖集合Y’中第i個特征圖, 是通過 生成的第j個Ghost特征圖所做的線性變化,此處操作即指對應基本特征圖集合中每個特征圖通過線性操作生成s個Ghost特征圖,最后一個線性操作為恒等映射以保留固有特征。
最后輸出ms張特征圖來作為最終輸出特征圖集合,相比于直接通過用ckkn(n=m*s)的卷積層做卷積,其計算量大幅減小(衰減比值大約為s)。整個特征圖變化過程如下圖:
2.Gbneck殘差模塊設計
此處作者參照Resnet的殘差模塊設計出Ghost瓶頸模塊,ghost瓶頸主要由兩個堆疊的ghost模塊組成。第一個Ghost模塊作為擴展層,增加通道的數量。我們把輸出通道的數量與輸入通道的數量之比稱為擴展比。第二個Ghost模塊減少通道數量以匹配捷徑路徑。然后在這兩個Ghost模塊的輸入和輸出之間連接快捷方式。每一層之后都使用批處理歸一化(BN)和ReLU非線性。
3.GhostNet網絡架構
以本文之愚見,網絡架構與殘差模塊設計基本是延續前人的思想,Ghostnet最重要的創新點在于卷積的設計(確實NB)!
二、代碼實現
源代碼比較惡心,嫖幾行代碼根本無法實現,貌似它把所有的操作都重寫了一遍,對于我們這種想快速嫖代碼的人來說就很難受了,沒得辦法,只有參考源代碼自己寫一個簡單的出來了。前面說了最重要的創新是卷積模塊的設計,那如果能嫖卷積模塊的設計,剩下來的不都是之前論文實現的工作了嗎?當然自己寫出的代碼還是不敢直接就用,還是對比下模型參數及計算量的好。經過驗證,果然嫖代碼能力天賦異稟的我完美實現了該網絡。
各位同志如果要嫖作者大大的代碼的話,先交個一鍵三連的學費,謝謝各位!
1.卷積模塊
代碼如下:
class Ghostconv(nn.Module):"""Ghostnet的基礎卷積模塊"""def __init__(self,in_channels,out_channels):super(Ghostconv, self).__init__()main_channels=math.ceil(0.5*out_channels)#主卷積輸出通道數cheap_channels=out_channels-main_channels#通過線性變化廉價操作得到的通道數self.piontconv1=nn.Sequential(nn.Conv2d(in_channels,main_channels,kernel_size=1,stride=1,bias=False),nn.BatchNorm2d(main_channels,eps=1e-5),nn.ReLU(inplace=True))self.linear_option=nn.Sequential(nn.Conv2d(main_channels,cheap_channels,kernel_size=3,padding=1,stride=1,groups=cheap_channels),nn.BatchNorm2d(cheap_channels,eps=1e-5),nn.ReLU(inplace=True))#線性操作通過深度可分離卷積實現,其中一半的特征圖通過1*1卷積實現,一半的特征圖由線性操作提供def forward(self,x):x=self.piontconv1(x)y=self.linear_option(x)return torch.cat((x,y),dim=1)2.殘差模塊實現
代碼如下(示例):
class GhostUnit(nn.Module):"""實現基本殘差塊和降維殘差塊"""def __init__(self,in_channels,out_channels,stride,use_se,ex_factor,k_size,padding):super(GhostUnit, self).__init__()# stride為2,3*3或5*5需要做下采樣操作self.stride=strideself.use_se=use_seself.res=nn.Sequential()mid_channels=math.ceil(ex_factor*in_channels)# 主模塊self.expconv=Ghostconv(in_channels,mid_channels)if stride==2:self.dwconv=nn.Sequential(nn.Conv2d(mid_channels,mid_channels,stride=stride,kernel_size=k_size,groups=mid_channels,bias=False,padding=padding),nn.BatchNorm2d(mid_channels,eps=1e-5),nn.ReLU(inplace=True))# 此處卷積核大小有兩種可能:3或5if stride==2 or in_channels!=out_channels:self.res=nn.Sequential(nn.Conv2d(in_channels,in_channels,kernel_size=3,groups=in_channels,padding=1,stride=stride,bias=False),nn.BatchNorm2d(in_channels,eps=1e-5),nn.ReLU(inplace=True),nn.Conv2d(in_channels,out_channels,kernel_size=1,stride=1,bias=False),nn.BatchNorm2d(out_channels))#此處不對1*1卷積的結果做激活操作self.pwconv=Ghostconv(mid_channels,out_channels)#SE模塊if use_se==True:self.se=SElayer(4,mid_channels)def forward(self,x):residual=self.res(x)x=self.expconv(x)if self.stride==2:x=self.dwconv(x)if self.use_se:x=self.se(x)x=self.pwconv(x)return x+residual3.網絡實現
代碼如下:
class Ghostnet(nn.Module):def __init__(self,num_calss,width_ratio=1):super(Ghostnet, self).__init__()self.inital=nn.Sequential(nn.Conv2d(in_channels=3,out_channels=int(16*width_ratio),kernel_size=3,padding=1,stride=2,bias=False),nn.BatchNorm2d(int(16*width_ratio)),nn.ReLU(inplace=True))self.block1=nn.Sequential(GhostUnit(in_channels=int(16*width_ratio),out_channels=int(16*width_ratio),stride=1,use_se=False,ex_factor=1,k_size=3,padding=1),GhostUnit(in_channels=int(16*width_ratio),out_channels=int(24*width_ratio),stride=2,use_se=False,ex_factor=3,k_size=3,padding=1))self.block2 = nn.Sequential(GhostUnit(in_channels=int(24*width_ratio), out_channels=int(24*width_ratio), stride=1, use_se=False, ex_factor=3, k_size=3, padding=1),GhostUnit(in_channels=int(24*width_ratio), out_channels=int(40*width_ratio), stride=2, use_se=True, ex_factor=3, k_size=5, padding=2))self.block3 = nn.Sequential(GhostUnit(in_channels=int(40*width_ratio), out_channels=int(40*width_ratio), stride=1, use_se=True, ex_factor=3, k_size=5, padding=2),GhostUnit(in_channels=int(40*width_ratio), out_channels=int(80*width_ratio), stride=2, use_se=False, ex_factor=6, k_size=3, padding=1))self.block4 = nn.Sequential(GhostUnit(in_channels=int(80*width_ratio), out_channels=int(80*width_ratio), stride=1, use_se=False, ex_factor=2.5, k_size=3, padding=1),GhostUnit(in_channels=int(80*width_ratio), out_channels=int(80*width_ratio), stride=1, use_se=False, ex_factor=2.3, k_size=3, padding=1),GhostUnit(in_channels=int(80*width_ratio), out_channels=int(80*width_ratio), stride=1, use_se=False, ex_factor=2.3, k_size=3, padding=1),GhostUnit(in_channels=int(80*width_ratio), out_channels=int(112*width_ratio), stride=1, use_se=True, ex_factor=6, k_size=3, padding=1),GhostUnit(in_channels=int(112*width_ratio), out_channels=int(112*width_ratio), stride=1, use_se=True, ex_factor=6, k_size=3, padding=1),GhostUnit(in_channels=int(112*width_ratio), out_channels=int(160*width_ratio), stride=2, use_se=True, ex_factor=6, k_size=5, padding=2),)self.block5 = nn.Sequential(GhostUnit(in_channels=int(160*width_ratio), out_channels=int(160*width_ratio), stride=1, use_se=False, ex_factor=6, k_size=5, padding=2),GhostUnit(in_channels=int(160*width_ratio), out_channels=int(160*width_ratio), stride=1, use_se=True, ex_factor=6, k_size=5, padding=2),GhostUnit(in_channels=int(160*width_ratio), out_channels=int(160*width_ratio), stride=1, use_se=False, ex_factor=6, k_size=5, padding=2),GhostUnit(in_channels=int(160*width_ratio), out_channels=int(160*width_ratio), stride=1, use_se=True, ex_factor=6, k_size=5, padding=2),)self.conv_last=nn.Sequential(nn.Conv2d(in_channels=int(160*width_ratio),out_channels=960,kernel_size=1,stride=1,bias=False),nn.BatchNorm2d(960),nn.ReLU(inplace=True))self.pool=nn.AdaptiveAvgPool2d(1)self.finally_conv=nn.Conv2d(in_channels=960,out_channels=1280,kernel_size=1,stride=1,bias=True)self.fc=nn.Linear(in_features=1280,out_features=num_calss)def forward(self,x):x=self.inital(x)x=self.block1(x)x=self.block2(x)x = self.block3(x)x = self.block4(x)x = self.block5(x)x=self.conv_last(x)x=self.pool(x)x=self.finally_conv(x)x=x.view(x.size(0),-1)x=self.fc(x)return F.softmax(x,dim=1)三、驗證模型
代碼如下:
with torch.cuda.device(0):net = Ghostnet(1000,1)macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True,print_per_layer_stat=True, verbose=True)print('{:<30} {:<8}'.format('Computational complexity: ', macs))print('{:<30} {:<8}'.format('Number of parameters: ', params))原文中給出了模型的參數量和計算量,這不就可以驗證一下模型有沒有問題嗎!
我模型跑出來的參數量(Weight)和計算量(FLOPs)如下:
1)當寬度因子取到0.5時:
這里和2.6M的Weight及42M的FLOPs相差無幾!
2)當寬度因子取到1時:
這里和5.2M的Weight及142M的FLOPs差的一般!
總的來說:還行(此處省略一萬只草泥馬)!!!
總結
本文介紹了GhostNet的核心思想及其代碼實現,以供大家交流討論!
往期回顧:
(1)CBAM論文解讀+CBAM-ResNeXt的Pytorch實現
(2)SENet論文解讀及代碼實例
(3)ShuffleNet-V1論文理解及代碼復現
(4) ShuffleNet-V2論文理解及代碼復現
下期預告:
EfficientNet論文閱讀及代碼實現
總結
- 上一篇: dcs world f15c教学_苏教版
- 下一篇: 我的世界java作弊怎么开_我的世界怎么