简体   繁体   English

如何在 TensorFlow 2.0 中冻结 keras model? (专门将一个保存的model格式冻结为.pb格式)

[英]How to freeze a keras model in TensorFlow 2.0? (specifically freeze a saved model format to .pb format)

Can any one please explain the procedure to freeze a keras model (saved model format) into.pb format in TensorFlow 2?谁能解释一下将 keras model(保存的 model 格式)冻结为 Z0734DD6997310708 的.pb 格式的过程? Created a sample mobilenet keras model and saved it to the disk in saved model format创建了一个示例 mobilenet keras model 并将其以保存的 model 格式保存到磁盘

import tensorflow as tf
#Tensorflow version: 2.7.0
model = tf.keras.applications.mobilenet.MobileNet(
        include_top=True, 
        weights='imagenet', 
        input_tensor=None, 
        pooling=None,
        classes=1000
)
tf.keras.models.save_model( 
        model, 
        *path*, 
        overwrite=True, 
        include_optimizer=True, 
        save_format='pb', 
        signatures=None 
)

Then in another file, I need to load the model and freeze it to a.pb format然后在另一个文件中,我需要加载 model 并将其冻结为 a.pb 格式

import tensorflow as tf
#Tensorflow version: 2.7.0
model = tf.keras.models.load_model( *path* )

############################################
# Freeze the model to a .pb format
############################################

With the advancement of tensorflow 2, freeze model has changed to saved model where instead of a single.pb file(graphdef), you now have saved model: With the advancement of tensorflow 2, freeze model has changed to saved model where instead of a single.pb file(graphdef), you now have saved model:

  1. Weights重量
  2. GraphDef (.pb)图形定义 (.pb)
import tensorflow as tf
pretrained_model = tf.keras.applications.MobileNet()
mobilenet_save_path = 'weights/mobilenet'

# Save to saved model
tf.saved_model.save(pretrained_model, mobilenet_save_path)

Note: Saved model format is faster and produce the exact same results注意:保存的 model 格式更快并产生完全相同的结果

How to use saved model如何使用已保存的 model

import tensorflow as tf
model = tf.saved_model.load('weights/mobilenet/')

# Grab this function to run saved model
infer = model.signatures['serving_default'] 

image = 'something.jpg'
img = tf.io.decode_jpeg(tf.io.read_file(image))
img_pre = tf.cast(img, tf.float32) 
img_pre = (img_pre / 127.5) - 1
img_pre = tf.image.resize(img_pre, [224, 224])
img_pre = tf.expand_dims(img_pre, axis=0)
preds = infer(img_pre)['outputs']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Tensorflow中如何冻结已保存的模型 - In Tensorflow how to freeze saved model keras.models.save_model 中的 saved_model.pb 是否与 tensorflow freeze_graph output.pb 文件相同? - Is saved_model.pb from keras.models.save_model the same with tensorflow freeze_graph output .pb file? 如何将具有自定义 keras 层(.h5)的 keras 模型冻结到张量流图(.pb)? - How to freeze a keras model with custom keras layers(.h5) to tensorflow graph(.pb)? Convert a Tensorflow model in SavedModel format (.pb file) saved with tf.saved_model.save to a Keras model (.h5 file) - Convert a Tensorflow model in SavedModel format (.pb file) saved with tf.saved_model.save to a Keras model (.h5 file) 如何冻结张量流模型? - how to freeze the tensorflow model? Tensorflow 2.0 将 keras 模型转换为 .pb 文件 - Tensorflow 2.0 Convert keras model to .pb file 如何加载 keras model 另存为.pb - How to load a keras model saved as .pb 如何以.pb格式保存模型,然后将其加载以在Tensorflow中进行推理? - How to save model in .pb format and then load it for inference in Tensorflow? 无法将自定义模型保存为 .pb 格式(tensorflow 2.1.0) - Unable to save the custom model to .pb format (tensorflow 2.1.0) 如何将 .pb 文件转换为 .h5。 (Tensorflow 模型到 keras) - How to convert .pb file to .h5. (Tensorflow model to keras)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM