我正在尝试将Tensorflow(.pb)格式的模型转换为Keras(.h5)格式,以查看事后注意的可视化。 我已经尝试过下面的代码。
file_pb = "/test.pb"
file_h5 = "/test.h5"
loaded_model = tf.keras.models.load_model(file_pb)
tf.keras.models.save_keras_model(loaded_model, file_h5)
loaded_model_from_h5 = tf.keras.models.load_model(file_h5)
有人可以帮我吗?这有可能吗?
答案 0 :(得分:4)
在最新的Tensorflow Version (2.2)
中,当我们使用Save
tf.keras.models.save_model
进行建模时,该模型将不仅是Saved
中的pb file
,而且还会是除了Variables
文件外,还保存在一个文件夹中,该文件夹包括Assets
文件夹和saved_model.pb
文件夹,如以下屏幕截图所示:
例如,如果Model
是名称为 Saved
的"Model"
,则我们必须使用文件夹名称Load
,而不是saved_model.pb
的“模型”,如下所示:
loaded_model = tf.keras.models.load_model('Model')
代替
loaded_model = tf.keras.models.load_model('saved_model.pb')
您可以做的另一项改变是替换
tf.keras.models.save_keras_model
与
tf.keras.models.save_model
将模型从Tensorflow Saved Model Format (pb)
转换为Keras Saved Model Format (h5)
的完整工作代码如下所示:
import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image
New_Model = tf.keras.models.load_model('Dogs_Vs_Cats_Model') # Loading the Tensorflow Saved Model (PB)
print(New_Model.summary())
New_Model.summary
命令的输出为:
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 148, 148, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 72, 72, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 34, 34, 128) 73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 128) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 15, 15, 128) 147584
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 128) 0
_________________________________________________________________
flatten (Flatten) (None, 6272) 0
_________________________________________________________________
dense (Dense) (None, 512) 3211776
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 3,453,121
Trainable params: 3,453,121
Non-trainable params: 0
_________________________________________________________________
None
继续执行代码:
# Saving the Model in H5 Format and Loading it (to check if it is same as PB Format)
tf.keras.models.save_model(New_Model, 'New_Model.h5') # Saving the Model in H5 Format
loaded_model_from_h5 = tf.keras.models.load_model('New_Model.h5') # Loading the H5 Saved Model
print(loaded_model_from_h5.summary())
命令print(loaded_model_from_h5.summary())
的输出如下所示:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 148, 148, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 74, 74, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 72, 72, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 36, 36, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 34, 34, 128) 73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 17, 17, 128) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 15, 15, 128) 147584
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 128) 0
_________________________________________________________________
flatten (Flatten) (None, 6272) 0
_________________________________________________________________
dense (Dense) (None, 512) 3211776
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 3,453,121
Trainable params: 3,453,121
Non-trainable params: 0
_________________________________________________________________
从以上两个Summary
的{{1}}可以看出,两个Models
是相同的。
如果您需要其他任何信息,请告诉我,我们将竭诚为您服务。
希望这会有所帮助。学习愉快。