将Tensorflow(.pb)格式转换为Keras(.h5) [英] Tensorflow (.pb) format to Keras (.h5)
问题描述
我正在尝试将Tensorflow(.pb)格式的模型转换为Keras(.h5)格式,以查看事后注意的可视化效果. 我已经尝试过下面的代码.
I am trying to convert my model in Tensorflow (.pb) format to Keras (.h5) format to view post hoc attention visualisation. I have tried below code.
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)
有人可以帮我吗?这有可能吗?
Can anyone help me with this? Is this even possible?
推荐答案
在最新的Tensorflow Version (2.2)
中,当我们使用tf.keras.models.save_model
Save
模型时,该模型将成为Saved
,而不仅仅是一个pb file
但它将保存在一个文件夹中,该文件夹除了saved_model.pb
文件外,还包括Variables
文件夹和Assets
文件夹,如以下屏幕截图所示:
In the Latest Tensorflow Version (2.2)
, when we Save
the Model using tf.keras.models.save_model
, the Model will be Saved
in not just a pb file
but it will be Saved in a Folder, which comprises Variables
Folder and Assets
Folder, in addition to the saved_model.pb
file, as shown in the screenshot below:
例如,如果Model
是名称为 "Model"
的Saved
,则必须Load
使用文件夹名称模型"而不是saved_model.pb
,如下所示:
For example, if the Model
is Saved
with the Name, "Model"
, we have to Load
using the Name of the Folder, "Model", instead of saved_model.pb
, as shown below:
loaded_model = tf.keras.models.load_model('Model')
代替
loaded_model = tf.keras.models.load_model('saved_model.pb')
您可以做的另一项改变是替换
One more change you can do is to replace
tf.keras.models.save_keras_model
与
tf.keras.models.save_model
将模型从Tensorflow Saved Model Format (pb)
转换为Keras Saved Model Format (h5)
的完整工作代码如下所示:
Complete working Code to convert a Model from Tensorflow Saved Model Format (pb)
to Keras Saved Model Format (h5)
is shown below:
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
_________________________________________________________________
从以上两个Models
的Summary
可以看出,两个Models
是相同的.
As can be seen from the Summary
of both the Models
above, both the Models
are same.
如果您需要其他任何信息,请告诉我,我们将竭诚为您服务.
Please let me know if you need any other information and I will be Happy to help you.
希望这会有所帮助.学习愉快.
Hope this helps. Happy Learning.
这篇关于将Tensorflow(.pb)格式转换为Keras(.h5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!