由fire模块组成的轻量级网络SqueezeNet,精度与ALexNet相近,但模型大小缩小了50倍。
下面是SqueezeNet的网络结构图:
整个网络包含10层。
第1层为卷积层,提取96通道特征。
第2到9层为fire模块,每两个模块之后,通道数会增加。
在1,4,8层之后加入降采样的max pooling,缩小一半尺寸。
第10层又是卷积层,为小图的每个像素预测1000类分类得分。
最后average pooling得到这张图的1000类得分,使用softmax函数归一化为概率。
注意,网络中所有激活函数默认使用Relu。
下面是SqueezeNet的Fire模块结构图:
默认先经卷积压缩通道至1/4,再经量两通道放大后再组合。其中卷积后产生的通道变换数目可以为参数的形式:
pytorch代码为:
import os.path
from typing import Iterator
import numpy as np
import torch
import cv2
from PIL import Image
from torch.utils.data import Dataset, DataLoader, Subset, random_split
import re
from functools import reduce
from torch.utils.tensorboard import SummaryWriter as Writer
from torchvision import transforms, datasets
import torchvision as tv
from torch import nn
import torch.nn.functional as F
import time
class myCustomerFire(nn.Module):
#indim,outdim为输入及输出的通道数:
def __init__(self,channalIn,squeezeFactor=0.25,expendFactor=0.5):
super().__init__()
#压缩后的通道数目,int直接截断小数,round取最近丁达整数值
self.channelAfterSqueeze=round(channalIn*squeezeFactor)
#扩张后的通道数目
self.channelAfterExpansion=round(channalIn*expendFactor)
self.squeezeLay=self._getCnnWithRelu(channalIn,self.channelAfterSqueeze,1,0)
self.cnnLeft=self._getCnnWithRelu(self.channelAfterSqueeze,self.channelAfterExpansion,1,0)
self.cnnRight =self._getCnnWithRelu(self.channelAfterSqueeze,self.channelAfterExpansion,3,1)
def _getCnnWithRelu(self,indim,outdim,ksize,padding,hasRelu=True):
if hasRelu:
return nn.Sequential(nn.Conv2d(indim, outdim, kernel_size=ksize, padding=padding), nn.ReLU(True))
else:
return nn.Sequential(nn.Conv2d(indim, outdim, kernel_size=ksize, padding=padding))
def forward(self,x):
x1 =self.squeezeLay(x)
L=self.cnnLeft(x1)
R=self.cnnRight(x1)
return torch.cat([L,R],1)
myNet=myCustomerFire(32)
k = torch.rand(1, 32, 56, 56)
print(myNet(k).shape)
输出结果为:
torch.Size([1, 32, 56, 56])