Tensorflow (.pb) 格式到 Keras (.h5)

Posted

技术标签:

【中文标题】Tensorflow (.pb) 格式到 Keras (.h5)【英文标题】:Tensorflow (.pb) format to Keras (.h5) 【发布时间】:2020-04-10 00:59:31 【问题描述】:

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

谁能帮我解决这个问题?这甚至可能吗?

【问题讨论】:

【参考方案1】:

在最新的Tensorflow Version (2.2)中,当我们Save模型使用tf.keras.models.save_model时,模型将不仅仅是pb file中的Saved,而是保存在一个文件夹中,其中包含Variables文件夹和Assets文件夹,另外还有saved_model.pb文件,如下截图所示:

例如,如果ModelSaved,名称为"Model",我们必须使用文件夹名称“Model”而不是Load 987654335@,如下图:

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
_________________________________________________________________

​ 从上面ModelsSummary可以看出,Models都是一样的。

【讨论】:

我在保存模型时遇到了这个错误 AttributeError: '_UserObject' object has no attribute '_is_graph_network' 这就像一个梦想成真的约定,但我得到了这个错误:'_UserObject'对象没有属性'summary'。我在 Tensorflow 版本 (2.3) 上运行。 我得到了这个 AttributeError: 'AutoTrackable' object has no attribute '_is_graph_network' 我收到此错误:AttributeError: 'AutoTrackable' object has no attribute 'Summary'?

以上是关于Tensorflow (.pb) 格式到 Keras (.h5)的主要内容,如果未能解决你的问题,请参考以下文章

Tensorflow (.pb) 格式到 Keras (.h5)

转换TensorFlow 的PB 模型到 UFF格式

TensorFlow 自定义模型导出:将 .ckpt 格式转化为 .pb 格式

保存tensorflow模型为pb文件

如何将 Keras .h5 导出到 tensorflow .pb?

机器学习:我选kera