gpt4 book ai didi

python-3.x - Tensorflow (.pb) 格式到 Keras (.h5)

转载 作者:行者123 更新时间:2023-12-03 20:12:43 26 4
gpt4 key购买 nike

我正在尝试将 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)

谁能帮我这个?这甚至可能吗?

最佳答案

最新 Tensorflow Version (2.2) ,当我们Save模型使用 tf.keras.models.save_model , 型号为 Saved不仅仅是一个 pb file但它将保存在一个文件夹中,其中包含 Variables文件夹和 Assets文件夹,除了saved_model.pb文件,如下图所示:
Saved Model Folder
例如,如果 ModelSaved与名称, "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可以看出的 Models以上,都是 Models是一样的。

关于python-3.x - Tensorflow (.pb) 格式到 Keras (.h5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59375679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com