How to convert pb file to h5?

My tf version is 2.18 that doesn’t support pb file.
So i searched on google and then wrote these code.

import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image

pb_model_dir = "model_data/"
h5_model = "./saved_model.h5"

# Loading the Tensorflow Saved Model (PB)
model = tf.saved_model.load(pb_model_dir)

# Saving the Model in H5 Format
tf.keras.models.save_model(model, h5_model)

But there is a error

 AttributeError: '_UserObject' object has no attribute 'get_config'

I tried tf.keras.models.load_model too but the there is also a error

File format not supported: filepath=model_data/. Keras 3 only supports V3 `.keras` files and legacy H5 format files (`.h5` extension). Note that the legacy SavedModel format is not supported by `load_model()` in Keras 3. In order to reload a TensorFlow SavedModel as an inference-only layer in Keras 3, use `keras.layers.TFSMLayer(model_data/, call_endpoint='serving_default')` (note that your `call_endpoint` might have a different name).

Many people seem to solve this problem by using one of above ways, but it didn’t work at my environment. How to convert pb file to h5??

see if this documentation helps you

check the regular conversion script header section

1 Like