MobileNetV1
2017_MobileNetV1_谷歌:
图:
网络描述:
depthwise convolution(卷积后通道数不改变): 对于128x128x512的特征图, 用512个3x3的卷积核分别对每个通道进行卷积, 得到了通道为512的特征图
pointwise convolution:用n个1x1卷积,将上述通道数为512的特征图变为通道数为n的特征图。
MobileNet的基本单元是深度级可分离卷积(depthwise separable convolution),其实这种结构之前已经被使用在Inception模型中。==深度级可分离卷积其实是一种可分解卷积操作(factorized convolutions),其可以分解为两个更小的操作:depthwise convolution和pointwise convolution。Depthwise convolution和标准卷积不同,对于标准卷积其卷积核是用在所有的输入通道(inputchannels),而depthwise convolution针对每个输入通道采用不同的卷积核,就是说一个卷积核对应一个输入通道,所以说depthwise convolution是depth级别的操作。==而pointwise convolution其实就是普通的卷积,只不过其采用1x1的卷积核。
对于 depthwise separable convolution,其首先是采用depthwise convolution对不同输入通道分别进行卷积,然后采用pointwise convolution将上面的输出再进行结合,这样其实整体效果和一个标准卷积是差不多的,但是会大大减少计算量和模型参数量。
特点,优点:
(1)主要应用了深度可分离卷积来代替传统的卷积操作,并且放弃pooling层,直接采用stride = 2进行卷积运算。
(2)把标准卷积分解成深度卷积(depthwise convolution)和逐点卷积(pointwise convolution)。这么做的好处是可以大幅度降低参数量和计算量。
(3)用两个超参数来控制网络计算速度与准确度之间的平衡,宽度调节参数和分辨率参数,主要用于压缩模型。
代码:
kerea实现:
#数据预处理并设置 learning schedule
def color_preprocessing(x_train,x_test):x_train = x_train.astype('float32')x_test = x_test.astype('float32')mean = [125.307, 122.95, 113.865]std = [62.9932, 62.0887, 66.7048]for i in range(3):x_train[:,:,:,i] = (x_train[:,:,:,i] - mean[i]) / std[i]x_test[:,:,:,i] = (x_test[:,:,:,i] - mean[i]) / std[i]return x_train, x_testdef scheduler(epoch):if epoch < 100:return 0.01if epoch < 200:return 0.001return 0.0001# load data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
x_train, x_test = color_preprocessing(x_train, x_test)#定义网络结构
def depthwise_separable(x,params):# f1/f2 filter size, s1 stride of conv(s1,f2) = paramsx = DepthwiseConv2D((3,3),strides=(s1[0],s1[0]), padding='same')(x)x = BatchNormalization()(x)x = Activation('relu')(x)x = Conv2D(int(f2[0]), (1,1), strides=(1,1), padding='same')(x)x = BatchNormalization()(x)x = Activation('relu')(x)return x#搭建网络
def MobileNet(img_input,shallow=False, classes=10):"""Instantiates the MobileNet.Network has two hyper-parameterswhich are the width of network (controlled by alpha)and input size.# Argumentsalpha: optional parameter of the network to change the width of model.shallow: optional parameter for making network smaller.classes: optional number of classes to classify imagesinto."""x = Conv2D(int(32), (3,3), strides=(2,2), padding='same')(img_input)x = BatchNormalization()(x)x = Activation('relu')(x)x = depthwise_separable(x,params=[(1,),(64,)])x = depthwise_separable(x,params=[(2,),(128,)])x = depthwise_separable(x,params=[(1,),(128,)])x = depthwise_separable(x,params=[(2,),(256,)])x = depthwise_separable(x,params=[(1,),(256,)])x = depthwise_separable(x,params=[(2,),(512,)])if not shallow:for _ in range(5):x = depthwise_separable(x,params=[(1,),(512,)])x = depthwise_separable(x,params=[(2,),(1024,)])x = depthwise_separable(x,params=[(1,),(1024,)])x = GlobalAveragePooling2D()(x)out = Dense(classes, activation='softmax')(x)return out#生成模型
img_input=Input(shape=(32,32,3))
output = MobileNet(img_input)
model=Model(img_input,output)
model.summary()#开始训练
# set optimizer
sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])# set callback
tb_cb = TensorBoard(log_dir=log_filepath, histogram_freq=0)
change_lr = LearningRateScheduler(scheduler)
cbks = [change_lr,tb_cb]# set data augmentation
datagen = ImageDataGenerator(horizontal_flip=True,width_shift_range=0.125,height_shift_range=0.125,fill_mode='constant',cval=0.)
datagen.fit(x_train)# start training
model.fit_generator(datagen.flow(x_train, y_train,batch_size=batch_size),steps_per_epoch=iterations,epochs=epochs,callbacks=cbks,validation_data=(x_test, y_test))
model.save('mobilenet.h5')
pytorch实现:
class Block(nn.Module):'''Depthwise conv + Pointwise conv'''def __init__(self, in_planes, out_planes, stride=1):super(Block, self).__init__()self.conv1 = nn.Conv2d\(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes, bias=False)self.bn1 = nn.BatchNorm2d(in_planes)self.conv2 = nn.Conv2d\(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)self.bn2 = nn.BatchNorm2d(out_planes)def forward(self, x):out = F.relu(self.bn1(self.conv1(x)))out = F.relu(self.bn2(self.conv2(out)))return outclass MobileNet(nn.Module):# (128,2) means conv planes=128, conv stride=2, # by default conv stride=1cfg = [64, (128,2), 128, (256,2), 256, (512,2), 512, 512, 512, 512, 512, (1024,2), 1024]def __init__(self, num_classes=10):super(MobileNet, self).__init__()self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)self.bn1 = nn.BatchNorm2d(32)self.layers = self._make_layers(in_planes=32)self.linear = nn.Linear(1024, num_classes)def _make_layers(self, in_planes):layers = []for x in self.cfg:out_planes = x if isinstance(x, int) else x[0]stride = 1 if isinstance(x, int) else x[1]layers.append(Block(in_planes, out_planes, stride))in_planes = out_planesreturn nn.Sequential(*layers)def forward(self, x):out = F.relu(self.bn1(self.conv1(x)))out = self.layers(out)out = F.avg_pool2d(out, 2)out = out.view(out.size(0), -1)out = self.linear(out)return outdef test():net = MobileNet()x = torch.randn(1,3,32,32)y = net(x)print(y.size())test()
MobileNetV1相关推荐
- 【神经网络】(11) 轻量化网络MobileNetV1代码复现、解析,附Tensorflow完整代码
各位同学好,今天和大家分享一下如何使用 Tensorflow 复现轻量化神经网络模型 MobileNetV1.为了能将神经网络模型用于移动端(手机)和终端(安防监控.无人驾驶)的实时计算,通常这些设备 ...
- MobileNetV1/V2/V3简述 | 轻量级网络
MobileNet系列很重要的轻量级网络家族,出自谷歌,MobileNetV1使用深度可分离卷积来构建轻量级网络,MobileNetV2提出创新的inverted residual with line ...
- 交叉编译docker_端侧推理引擎Tengine初识:安卓平台交叉编译并跑通MobileNetV1
前阵子看到Tengine为OpenCV4.3版本贡献了ARM CPU底层汇编代码,加速深度学习计算.最近也看到Tengine的不少同学在做相关PR.可能有小伙伴不了解Tengine.根据ARM官网也有 ...
- 【轻量级网络】MobileNet-v1详解
论文:<MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications> 论文链接:h ...
- 第七课:MobileNetv1、MobileNetv2、MobileNetv3学习
前言 随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容.来源于哔哩哔哩博主"霹雳吧啦Wz",博主学习作为笔记记录,欢 ...
- 学习笔记(一)Mobilenetv1的解读
学习笔记(一)Mobilenetv1的解读 1.Mobilenetv1 paper地址:https://arxiv.org/abs/1704.04861?context=cs (1) 简介 Mobil ...
- OUC暑期培训(深度学习)——第四周学习记录:MobileNetV1,V2,V3
第四周学习:MobileNetV1,V2,V3 Part 1 视频学习及论文阅读 1.MobileNetV1 MobileNetV1论文网址:[1704.04861] MobileNets: Effi ...
- MobileNetV1实战:使用MobileNetV1实现植物幼苗分类
文章目录 摘要 数据增强Cutout和Mixup 项目结构 导入项目使用的库 设置全局参数 图像预处理与增强 读取数据 设置模型 定义训练和验证函数 测试 摘要 本例提取了植物幼苗数据集中的部分数据做 ...
- 一文掌握MobileNetV1和MobileNetV2(基于pytorch实现的人像背景虚化)
目录 一.概述 二.MobileNetV1原理和实现 2.1 原理 2.1.1 深度卷积 2.1.2 逐像素卷积 2.2 Pytorch实现 三.MobileNetV2原理和实现 3.1 原理 3.1 ...
- MobileNetv1、v2网络详解、使用pytorch搭建模型MobileNetv2并基于迁移学习训练
1.MobileNetv1网络详解 传统卷积神经网络专注于移动端或者嵌入式设备中的轻量级CNN网络,相比于传统卷积神经网络,在准确率小幅降低的前提下大大减少模型参数与运算量.(相比VGG16准确率减少 ...
最新文章
- 使用uni-app实现使用浏览器下载功能时,安卓端部分机型不兼容问题(小米/华为)
- 继国美处罚“摸鱼”员工后,网易出品之摸鱼计算器...
- 经典C语言程序100例之五六
- 2015 跨年博文总结
- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
- camera (13)---智能手机双摄像头工作原理详解:RBG +RGB, RGB + Mono
- linux boot efi分区 容量,解决Ubuntu上由于/boot容量不足引起的无法更新系统
- python 浮点数比较_Python中的浮点数
- AtmosphericCorrection大气校正_landsat8
- C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编
- mysql 中逆向表模型_PowerDesigner 逆向生成数据库物理模型,以 MySQL 为例
- python邮件处理(1)-IMAP收取邮件
- Matlab 两条曲线间填充颜色,改变透明度
- 终于知道怎么看辐射3的地图了
- 为什么说大数据就业前景一片光明?
- jquery获取已选择和未选择的checkBox项以及清空所选项
- 【读论文】基于三支决策的不平衡数据过采样方法
- 双重否定的翻译 百度翻译 VS. 谷歌翻译
- 支持向量机(二)——松弛变量处理异常点
- 乌班图搭建sftp服务器