Hi, guys
I was trying to convert custom trained yolov5s model to tensorflow model for only predict.
First, converting yolov5s to onnx model was successful by running export.py, and to tensorflow representation too.
Pb folder created, and there are assets(but just empty folder), variables folder and saved_model.pb file.
With them, I used tf.keras.models.load_model, the type of model was _UserObject.
I cannot use summary, predict because the model is _UserObject.
Hi @human Can you share some of the code here, so the community can try to debug it with you? So the issue is, based on your description, is that, after the onnx-tf conversion, you can’t use either tf.keras.Model’s summary or predict.
Okay, so there are two flavors of saved_model: “vanilla” and “keras”.
Vanilla only has the basic TensorFlow constructs (functions, variables).
The “keras” flavored ones also have all the metadata required to rebuild the keras objects.
It can’t automatically uncompile the low-level representation up into a higher-level keras representation.
tf.keras.models.load_model should be printing a warning that there’s no keras metadata available in that saved_model. IIRC, future versions of tensorflow will fail if you use tf.keras.models.load_model on a saved_model that doesn’t have it.
Use tf.saved_model.load. It will return the same _UserObect. Inspect that that to find your functions. does it have a .signatures attirbute?
I am facing the same issue after converting onnx model to tensorflow, and trying to get model.summary(), it doesn’t work:
import onnx
from onnx_tf.backend import prepare
Load the ONNX model
onnx_model = onnx.load(“model_1.onnx”)
tf_rep = prepare(onnx_model) # prepare tf representation
tf_rep.export_graph(“./model_1”) # export the model as .pb
Load tf model and use
from tensorflow import keras
keras_model = keras.models.load_model(‘model_1’)
keras_model.summary()
…
There are many problems and solutions on this thread.
Issues should be specific and reproducible.
Many of the issues stem from the fact that there are two flavors of saved-model: “generic” and “keras”.
Model.save() will save a “keras” flavored model. tf.keras.models.load_model will load it back as a keras model. Standard keras methods like summary and perdict will all work.
tf.saved_model.save OTOH will save a generic model. tf.saved_model.load will load it back either type, as a generic _UserObject model.
If you load a generic _UserObject model It will only have two sets of methods available:
The tf.function methods that were attached to the model before you saved it
AttributeError: '_UserObject' object has no attribute 'summary'
It looks like both keras.models.load_model will also load either flavor, and just fall-back to the non-keras _UserObject if the saved_model directory doesn’t contain the keras metadata required to load it as a keras.Model