美食网站设计论文,红色餐饮网站源码,钦州公司做网站,flashfxp怎么做网站Mobilenet概念#xff1a;
MobileNet模型是Google针对手机等嵌入式设备提出的一种轻量级的深层神经网络#xff0c;其使用的核心思想便是depthwise separable convolution。
Mobilenet思想#xff1a;
通俗地理解就是3x3的卷积核厚度只有一层#xff0c;然后在输入张量上…Mobilenet概念
MobileNet模型是Google针对手机等嵌入式设备提出的一种轻量级的深层神经网络其使用的核心思想便是depthwise separable convolution。
Mobilenet思想
通俗地理解就是3x3的卷积核厚度只有一层然后在输入张量上一层一层地滑动每一次卷积完生成 一个输出通道当卷积完成后在利用1x1的卷积调整厚度。 对于一个卷积点而言 假设有一个3×3大小的卷积层其输入通道为16、输出通道为32。具体为32个3×3大小的卷积核会 遍历16个通道中的每个数据最后可得到所需的32个输出通道所需参数为16×32×3×34608个。 应用深度可分离卷积用16个3×3大小的卷积核分别遍历16通道的数据得到了16个特征图谱。在 融合操作之前接着用32个1×1大小的卷积核遍历这16个特征图谱所需参数为 16×3×316×32×1×1656个。 可以看出来depthwise separable convolution可以减少模型的参数。
Mobilenet网络代码实现
网络主体部分
#-------------------------------------------------------------#
# MobileNet的网络部分
#-------------------------------------------------------------#
import warnings
import numpy as npfrom keras.preprocessing import imagefrom keras.models import Model
from keras.layers import DepthwiseConv2D,Input,Activation,Dropout,Reshape,BatchNormalization,GlobalAveragePooling2D,GlobalMaxPooling2D,Conv2D
from keras.applications.imagenet_utils import decode_predictions
from keras import backend as Kdef MobileNet(input_shape[224,224,3],depth_multiplier1,dropout1e-3,classes1000):img_input Input(shapeinput_shape)# 224,224,3 - 112,112,32x _conv_block(img_input, 32, strides(2, 2))# 112,112,32 - 112,112,64x _depthwise_conv_block(x, 64, depth_multiplier, block_id1)# 112,112,64 - 56,56,128x _depthwise_conv_block(x, 128, depth_multiplier,strides(2, 2), block_id2)# 56,56,128 - 56,56,128x _depthwise_conv_block(x, 128, depth_multiplier, block_id3)# 56,56,128 - 28,28,256x _depthwise_conv_block(x, 256, depth_multiplier,strides(2, 2), block_id4)# 28,28,256 - 28,28,256x _depthwise_conv_block(x, 256, depth_multiplier, block_id5)# 28,28,256 - 14,14,512x _depthwise_conv_block(x, 512, depth_multiplier,strides(2, 2), block_id6)# 14,14,512 - 14,14,512x _depthwise_conv_block(x, 512, depth_multiplier, block_id7)x _depthwise_conv_block(x, 512, depth_multiplier, block_id8)x _depthwise_conv_block(x, 512, depth_multiplier, block_id9)x _depthwise_conv_block(x, 512, depth_multiplier, block_id10)x _depthwise_conv_block(x, 512, depth_multiplier, block_id11)# 14,14,512 - 7,7,1024x _depthwise_conv_block(x, 1024, depth_multiplier,strides(2, 2), block_id12)x _depthwise_conv_block(x, 1024, depth_multiplier, block_id13)# 7,7,1024 - 1,1,1024x GlobalAveragePooling2D()(x)x Reshape((1, 1, 1024), namereshape_1)(x)x Dropout(dropout, namedropout)(x)x Conv2D(classes, (1, 1),paddingsame, nameconv_preds)(x)x Activation(softmax, nameact_softmax)(x)x Reshape((classes,), namereshape_2)(x)inputs img_inputmodel Model(inputs, x, namemobilenet_1_0_224_tf)model_name mobilenet_1_0_224_tf.h5model.load_weights(model_name)return modeldef _conv_block(inputs, filters, kernel(3, 3), strides(1, 1)):x Conv2D(filters, kernel,paddingsame,use_biasFalse,stridesstrides,nameconv1)(inputs)x BatchNormalization(nameconv1_bn)(x)return Activation(relu6, nameconv1_relu)(x)def _depthwise_conv_block(inputs, pointwise_conv_filters,depth_multiplier1, strides(1, 1), block_id1):x DepthwiseConv2D((3, 3),paddingsame,depth_multiplierdepth_multiplier,stridesstrides,use_biasFalse,nameconv_dw_%d % block_id)(inputs)x BatchNormalization(nameconv_dw_%d_bn % block_id)(x)x Activation(relu6, nameconv_dw_%d_relu % block_id)(x)x Conv2D(pointwise_conv_filters, (1, 1),paddingsame,use_biasFalse,strides(1, 1),nameconv_pw_%d % block_id)(x)x BatchNormalization(nameconv_pw_%d_bn % block_id)(x)return Activation(relu6, nameconv_pw_%d_relu % block_id)(x)def relu6(x):return K.relu(x, max_value6)def preprocess_input(x):x / 255.x - 0.5x * 2.return xif __name__ __main__:model MobileNet(input_shape(224, 224, 3))img_path elephant.jpgimg image.load_img(img_path, target_size(224, 224))x image.img_to_array(img)x np.expand_dims(x, axis0)x preprocess_input(x)print(Input image shape:, x.shape)preds model.predict(x)print(np.argmax(preds))print(Predicted:, decode_predictions(preds,1)) # 只显示top1