使用TensorFlow从头开始实现这个架构
# 绘制模型
tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True, show_dtype=False,show_layer_names=True, rankdir='TB', expand_nested=False, dpi=96)
模型图的一个片段:
使用TensorFlow的MobileNet模型实现:
import tensorflow as tf
# 导入所有必要的层
from tensorflow.keras.layers import Input, DepthwiseConv2D
from tensorflow.keras.layers import Conv2D, BatchNormalization
from tensorflow.keras.layers import ReLU, AvgPool2D, Flatten, Dense
from tensorflow.keras import Model
# MobileNet block
def mobilnet_block (x, filters, strides):
x = DepthwiseConv2D(kernel_size = 3, strides = strides, padding = 'same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(filters = filters, kernel_size = 1, strides = 1)(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
# 模型主干
input = Input(shape = (224,224,3))
x = Conv2D(filters = 32, kernel_size = 3, strides = 2, padding = 'same')(input)
x = BatchNormalization()(x)
x = ReLU()(x)
# 模型的主要部分
x = mobilnet_block(x, filters = 64, strides = 1)
x = mobilnet_block(x, filters = 128, strides = 2)
x = mobilnet_block(x, filters = 128, strides = 1)
x = mobilnet_block(x, filters = 256, strides = 2)
x = mobilnet_block(x, filters = 256, strides = 1)
x = mobilnet_block(x, filters = 512, strides = 2)
for _ in range (5):
x = mobilnet_block(x, filters = 512, strides = 1)
x = mobilnet_block(x, filters = 1024, strides = 2)
x = mobilnet_block(x, filters = 1024, strides = 1)
x = AvgPool2D (pool_size = 7, strides = 1, data_format='channels_first')(x)
output = Dense (units = 1000, activation = 'softmax')(x)
model = Model(inputs=input, outputs=output)
model.summary()
# 绘制模型
tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True, show_dtype=False,show_layer_names=True, rankdir='TB', expand_nested=False, dpi=96)
结论
MobileNet是最小的深度神经网络之一,它速度快、效率高,可以在没有高端GPU的设备上运行。
当使用Keras(在TensorFlow上)这样的框架时,这些网络的实现非常简单。

最新活动更多
-
3月27日立即报名>> 【工程师系列】汽车电子技术在线大会
-
4月1日立即下载>> 【村田汽车】汽车E/E架构革新中,新智能座舱挑战的解决方案
-
即日-4.22立即报名>> 【在线会议】汽车腐蚀及防护的多物理场仿真
-
4月23日立即报名>> 【在线会议】研华嵌入式核心优势,以Edge AI驱动机器视觉升级
-
4月25日立即报名>> 【线下论坛】新唐科技2025新品发布会
-
5月15日立即下载>> 【白皮书】精确和高效地表征3000V/20A功率器件应用指南
发表评论
请输入评论内容...
请输入评论/评论长度6~500个字
暂无评论
暂无评论