1.构建迁移学习模型及其模型可视化
............(未完待续)
1.构建迁移学习模型及其模型可视化
要实现使用plot_model方法实现模型可视化,需要安装:
pip install graphviz
pip install pydot_ng
然后,https://graphviz.gitlab.io/_pages/Download/Download_windows.html 安装graphviz。
接着添加环境变量中的path,添加路径C:\Program Files (x86)\Graphviz2.38\bin
(或者在Python中运行os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/')(此方法只能暂时添加)
import tensorflow as tf
#迁移MobileNetV2模型,并且不加载顶层
base_model=tf.keras.applications.mobilenet_v2.MobileNetV2(include_top=False,weights='imagenet',input_shape=(224,224,3))
inputs=tf.keras.layers.Input(shape=(224,224,3))
x=base_model(inputs)#此处x为MobileNetV2模型去处顶层时输出的特征相应图。
#自己设计顶层并连接
x=tf.keras.layers.AveragePooling2D()(x)
x=tf.keras.layers.Dense(1024,activation='relu')(x)
outputs=tf.keras.layers.Dense(8,activation='softmax')(x)
model=tf.keras.models.Model(inputs=inputs,outputs=outputs)
#模型可视化1,使用model.summary()方法
model.summary()
#模型可视化2,使用plot_model()方法
tf.keras.utils.plot_model(model,
to_file='model.png', #模型结构图保存名字
show_layer_names=True, #是否现实层名
show_shapes=True) #是否展示层形状
model.summary()的输出结果如下:
Model: "model_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_12 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
mobilenetv2_1.00_224 (Model) (None, 7, 7, 1280) 2257984
_________________________________________________________________
average_pooling2d_6 (Average (None, 3, 3, 1280) 0
_________________________________________________________________
dense_12 (Dense) (None, 3, 3, 1024) 1311744
_________________________________________________________________
dense_13 (Dense) (None, 3, 3, 8) 8200
=================================================================
Total params: 3,577,928
Trainable params: 3,543,816
Non-trainable params: 34,112
_________________________________________________________________
plot_model()保存的模型结构图如下:
相关文章
- Tensorflow2.0 tf.data.Dataset.from_tensor_slices 自定义图像数据集 (一)
- What my daughter whispered to me that caught me by surprise
- Why communication in the workplace is key to my business
- The first thing everyone should do when they get married
- How you can be a hero and change your own community
- XREAL 宣布 5 月 30 日举行空间计算新品发布会
- 3月25日外媒科学网站摘要:首例猪肾移植成功,
- 5月21日外媒科学网站摘要:蟑螂起源于南亚,后
- 生成式AI热潮掀起“淘数据热”,背后风险有多大
- 英派斯取得VR动感单车和可摆动机架专利,摘要显
发表评论: