小白介紹一下SqueezeNet的Model部分
小白介紹一下SqueezeNet的Model部分

源碼全部
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.functional as F
import numpy as np
import torch.optim as optim
import math
class fire(nn.Module):
? ? def __init__(self, inplanes,squeeze_planes, expand_planes):
? ? ? ? super(fire,self).__init__()
? ? ? ? self.conv1= nn.Conv2d(inplanes,squeeze_planes, kernel_size=1,stride=1)
? ? ? ? self.bn1=nn.BatchNorm2d(squeeze_planes)
? ? ? ? self.relu1=nn.ReLU(inplanes = True)
?
? ? ? ? self.conv2 = nn.Conv2d(squeeze_planes,expand_planes,kernel_size=1,stride=1)
? ? ? ? self.bn2=nn.BatchNorm2d(expand_planes)
?
? ? ? ? self.conv3 = nn.Conv2d(squeeze_planes, expand_planes, kernel_size=1, stride=1, padding=1)
?
? ? ? ? self.bn3=nn.BatchNorm2d(expand_planes)
? ? ? ? self.relu2 = nn.ReLU(inplanes=True)
?
? ? ? ? #using MSR
? ? ? ? for m in self.modules():
? ? ? ? ? ? if isinstance((m, nn.Conv2d)):
? ? ? ? ? ? ? ? n= m.kernel_size[0] * m.kernel_size[1]* m.in_channels
? ? ? ? ? ? ? ? m.weight.data.normal_(0,math.sqrt(2./n))
?
? ? def forward(self,x):
? ? ? ? x = self.conv1(x)
? ? ? ? x = self.bn1(x)
? ? ? ? x = self.relu1(x)
? ? ? ? out1 = self.conv2(x)
? ? ? ? out1 = self.bn2(out1)
?
? ? ? ? out2 = self.conv3(x)
? ? ? ? out2 = self.bn3(out2)
?
? ? ? ? out = torch.cat([out1,out2 ],1)
? ? ? ? out = self.relu2(out)
? ? ? ? return out
?
class SqueezeNet(nn.Module):
? ? def __init__(self):
? ? ? ? super(SqueezeNet, self).__init__()
? ? ? ? self.conv1 = nn.Conv2d(3,96,kernel_size=3,stride=1, padding=1) #32
? ? ? ? self.bn1 =nn.BatchNorm2d(96)
? ? ? ? self.relu= nn.ReLU(inplace=True)
?
? ? ? ? self.maxpool1= nn.MaxPool2d(kernel_size=2,stride=2) #16
? ? ? ? self.fire2= fire(96,16,64)
? ? ? ? self.fire3 = fire(128, 16, 64)
? ? ? ? self.fire4 = fire(128, 32, 128)
?
? ? ? ? self.maxpool2 = nn.MaxPool2d(kernel_size=2 ,stride=2) #8
?
? ? ? ? self.fire5 = fire(256, 32, 128)
? ? ? ? self.fire6 = fire(256, 48, 192)
? ? ? ? self.fire7 = fire(384, 48, 192)
? ? ? ? self.fire8 = fire(384, 64, 256)
?
? ? ? ? self.maxpool3 = nn.MaxPool2d(kernel_size=2,stride=2)? #4
? ? ? ? self.fire9= fire(512,64,256)
? ? ? ? self.conv2 = nn.Conv2d(512, 10, kernel_size=1, stride=1, padding=1)
?
? ? ? ? self.avg_pool =nn.AvgPool2d(kernel_size=4,stride=4)
? ? ? ? self.softmax = nn.LogSoftmax(dim=1)
?
? ? ? ? for m in self.modules():
? ? ? ? ? ? if isinstance(m,nn.Conv2d):
? ? ? ? ? ? ? ? n = m.kernel_size[0] * m.kernel_size[1] * n.in_channels
? ? ? ? ? ? ? ? m.weight.data.normal_(0,math.sqrt(2. /n))
? ? ? ? ? ? elif isinstance(m, nn.BatchNorm2d):
? ? ? ? ? ? ? ? m.weight.data.fill_(1)
? ? ? ? ? ? ? ? m.bias.data.zero_()
?
? ? def forward(self, x):
? ? ? ? x = self.conv1(x)
? ? ? ? x = self.bn1(x)
? ? ? ? x = self.relu(x)
? ? ? ? x = self.maxpool1(x)
?
? ? ? ? x = self.fire2(x)
? ? ? ? x = self.fire3(x)
? ? ? ? x = self.fire4(x)
? ? ? ? x = self.maxpool2(x)
? ? ? ? x = self.fire5(x)
? ? ? ? x = self.fire6(x)
?
?
? ? ? ? x = self.fire7(x)
? ? ? ? x = self.fire8(x)
? ? ? ? x = self.maxpool3(x)
? ? ? ? x = self.fire9(x)
? ? ? ? x = self.conv2(x)
? ? ? ? x = self.avg_pool(x)
? ? ? ? x = self.softmax(x)
?
? ? ? ? return x
? ? def fire_layer(inp, s, e):
? ? ? ? f= fire( inp, s ,e )
? ? ? ? return f
? ? def squeezenet(pretrained = False):
? ? ? ? net = SqueezeNet()
? ? ? ? return net
首先導(dǎo)入依賴包
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.functional as F
import numpy as np
import torch.optim as optim
import math
導(dǎo)入pytorch,以及pytorch的神經(jīng)網(wǎng)絡(luò)模塊nn,自動(dòng)梯度計(jì)算autograd,numpy和參數(shù)訓(xùn)練優(yōu)化optim,math計(jì)算函數(shù)
定義conv1 1X1的卷積模塊,以及該模塊的BatchNorm和ReLu
? ? ? ? self.conv1= nn.Conv2d(inplanes,squeeze_planes, kernel_size=1,stride=1)
? ? ? ? self.bn1=nn.BatchNorm2d(squeeze_planes)
? ? ? ? self.relu1=nn.ReLU(inplanes = True)
定義conv2 1X1的卷積模塊,以及該模塊的BatchNorm
? ? ? ? self.conv2 = nn.Conv2d(squeeze_planes,expand_planes,kernel_size=1,stride=1)
? ? ? ? self.bn2=nn.BatchNorm2d(expand_planes)
定義conv3 1X1的卷積模塊,以及該模塊的BatchNorm和ReLu,修補(bǔ)
? ? ? ? self.conv3 = nn.Conv2d(squeeze_planes, expand_planes, kernel_size=1, stride=1, padding=1)
? ? ? ? self.bn3=nn.BatchNorm2d(expand_planes)
? ? ? ? self.relu2 = nn.ReLU(inplanes=True)
定義用MSR進(jìn)行 3組卷積模塊對(duì)所有輸入?yún)?shù)進(jìn)行歸一化
? ? ? ? for m in self.modules():
? ? ? ? ? ? if isinstance((m, nn.Conv2d)):
? ? ? ? ? ? ? ? n= m.kernel_size[0] * m.kernel_size[1]* m.in_channels
? ? ? ? ? ? ? ? m.weight.data.normal_(0,math.sqrt(2./n))
類fire,定義了forward前向計(jì)算
def forward(self,x):
? ? ? ?x = self.conv1(x)
? ? ? ? x = self.bn1(x)
? ? ? ? x = self.relu1(x)
? ? ? ? out1 = self.conv2(x)
? ? ? ? out1 = self.bn2(out1)
?
? ? ? ? out2 = self.conv3(x)
? ? ? ? out2 = self.bn3(out2)
?
? ? ? ? out = torch.cat([out1,out2 ],1)
? ? ? ? out = self.relu2(out)
? ? ? ? return out
定義了conv1 的輸入,通過卷積的BatchNorm和Relu,分別是到conv2和conv3的輸入,如卷積conv2經(jīng)過BatchNorm 輸出out1, conv3經(jīng)過BatchNorm輸出out2,通過2個(gè)部分的輸出通過cat連接到一起,最好在通過第2個(gè)relu的輸出,完成整個(gè)的Module的前向過程。
定義了SqueezeNet網(wǎng)絡(luò)的類
? def __init__(self):
? ? ? ? super(SqueezeNet, self).__init__()
? ? ? ? self.conv1 = nn.Conv2d(3,96,kernel_size=3,stride=1, padding=1) #32
? ? ? ? self.bn1 =nn.BatchNorm2d(96)
? ? ? ? self.relu= nn.ReLU(inplace=True)
?
? ? ? ? self.maxpool1= nn.MaxPool2d(kernel_size=2,stride=2) #16
? ? ? ? self.fire2= fire(96,16,64)
? ? ? ? self.fire3 = fire(128, 16, 64)
? ? ? ? self.fire4 = fire(128, 32, 128)
? ? ? ? self.maxpool2 = nn.MaxPool2d(kernel_size=2 ,stride=2) #8?
? ? ? ? self.fire5 = fire(256, 32, 128)
? ? ? ? self.fire6 = fire(256, 48, 192)
? ? ? ? self.fire7 = fire(384, 48, 192)
? ? ? ? self.fire8 = fire(384, 64, 256)
? ? ? ? self.maxpool3 = nn.MaxPool2d(kernel_size=2,stride=2)? #4
? ? ? ? self.fire9= fire(512,64,256)
? ? ? ? self.conv2 = nn.Conv2d(512, 10, kernel_size=1, stride=1, padding=1)
?
? ? ? ? self.avg_pool =nn.AvgPool2d(kernel_size=4,stride=4)
? ? ? ? self.softmax = nn.LogSoftmax(dim=1)
定義了初始化的__init__()函數(shù)
分別定義了3X3的卷積層,一個(gè)BatchNorm bn層,一個(gè)ReLu激活層
1個(gè)2X2的MaxPooling 卷積池
后面有定義了3個(gè)Fire Module,接著定義了2X2的卷積池,接下來定義了4個(gè)FireModule。接著定義了1個(gè)2X2的卷積池。后面定義一個(gè)1X1的FireModule,最后定義了AvgPooling層,網(wǎng)絡(luò)輸出一個(gè)Sofemax層。完成多個(gè)分類
將所有卷積層的參數(shù)歸一化,將BatchNorm層的所權(quán)重參數(shù)這種為1,偏置參數(shù)設(shè)置為0
? ? ? ? for m in self.modules():
? ? ? ? ? ? if isinstance(m,nn.Conv2d):
? ? ? ? ? ? ? ? n = m.kernel_size[0] * m.kernel_size[1] * n.in_channels
? ? ? ? ? ? ? ? m.weight.data.normal_(0,math.sqrt(2. /n))
? ? ? ? ? ? elif isinstance(m, nn.BatchNorm2d):
? ? ? ? ? ? ? ? m.weight.data.fill_(1)
? ? ? ? ? ? ? ? m.bias.data.zero_()
定義SqueezeNet的前向處理foreword
? def forward(self, x):
? ? ? ? x = self.conv1(x)
? ? ? ? x = self.bn1(x)
? ? ? ? x = self.relu(x)
? ? ? ? x = self.maxpool1(x)
? ? ? ? x = self.fire2(x)
? ? ? ? x = self.fire3(x)
? ? ? ? x = self.fire4(x)
? ? ? ? x = self.maxpool2(x)
? ? ? ? x = self.fire5(x)
? ? ? ? x = self.fire6(x)
? ? ? ? x = self.fire7(x)
? ? ? ? x = self.fire8(x)
? ? ? ? x = self.maxpool3(x)
? ? ? ? x = self.fire9(x)
? ? ? ? x = self.conv2(x)
? ? ? ? x = self.avg_pool(x)
? ? ? ? x = self.softmax(x)
按照順序依次連接就結(jié)束,返回x