Error while converting keras model to tensorflow saved model

Able to get keras model h5 format for masked RCNN.
I have tried 2 approaches:
1.While trying to convert keras model to tensorflow saved_model using GitHub - bendangnuksung/mrcnn_serving_ready: 🛠 Converting Mask R-CNN Keras model to Tensorflow getting error as File “main.py”, line 113, in
model.load_weights(H5_WEIGHT_PATH, by_name=True)
File “/home/ubuntu/Downloads/Blaize/mrcnn_serving_ready-master/model.py”, line 2131, in load_weights
saving.load_weights_from_hdf5_group_by_name(f, layers)
File “/home/ubuntu/Downloads/venv/lib/python3.6/site-packages/keras/engine/saving.py”, line 1328, in load_weights_from_hdf5_group_by_name
str(weight_values[i].shape) + ‘.’)
ValueError: Layer #389 (named “mrcnn_bbox_fc”), weight <tf.Variable ‘mrcnn_bbox_fc/kernel:0’ shape=(1024, 24) dtype=float32, numpy=
array([[-0.05486 , -0.03290214, 0.05897582, …, -0.05898178,
-0.06868616, 0.05374715],
[-0.06710163, -0.03682471, -0.03057443, …, -0.05611433,
-0.04561458, 0.05178914],
[-0.0041154 , -0.07344876, -0.06137543, …, 0.0011842 ,
0.04365869, -0.05199062],
…,
[ 0.06231805, -0.02443966, -0.00532094, …, -0.01833269,
-0.02245103, -0.01552512],
[-0.04047406, -0.06753345, 0.02390008, …, 0.01883602,
-0.04362615, -0.05265519],
[ 0.00530255, 0.04341973, 0.03085093, …, -0.07011634,
0.01440722, 0.02777647]], dtype=float32)> has shape (1024, 24), but the saved weight has shape (1024, 324).

2.in the second method using https://github.com/amir-abdi/keras_to_tensorflow,got error as
ValueError('Unknown ’ + printable_module_name + ': ’ + class_name)
ValueError: Unknown layer: BatchNorm

I’m not sure I understand your question correctly. Does the error occur when you are saving the keras model into .h5 file or loading it from it?
If you are loading a model from a file and see messages about unknown layers, it mean that the original model contained some custom layers. You need to import them or initialize from scratch in your new code before loading the model.

1 Like

There is a Mask-RCNN implementation in TF Model Garden as well. You can try that if you do not manage to resolve your issues with the code base you have been using.

I have a sample for Model Garden’s RetinaNet training + saving here. It’s not Mask-RCNN but also a detection model so maybe the code can be useful to you to get through the Model Garden learning curve (the links below open in Colab and TPU training in Colab works in approx. 40 min):

The main training + saving happens in 04ab_retinanet_arthropods_train.ipynb. You can reload the model to test it with https://04ac_retinanet_arthropods_predict.ipynb and the last notebook contains the data conversion pipeline, which you can adapt for your own dataset: 04aa_retinanet_arthropods_dataprep.ipynb

4 Likes

Adding up some more information for the future:

2 Likes

Thank You.Actually I wanted to convert mrcnn model to tf dialect .I downloaded model from TensorFlow Hub. and I tried adding signatures to model using import tensorflow.compat.v2 as tf
loaded_model = tf.saved_model.load(’/maskedrcnn’)
call = loaded_model.call.get_concrete_function(
tf.TensorSpec(shape=(1, 1024, 1024, 3), dtype=tf.uint8))
signatures = {‘predict’: call}
tf.saved_model.save(loaded_model,’/ex’, signatures=signatures)

I got warning as Found untraced functions such as restored_function_body, restored_function_body, restored_function_body, restored_function_body, restored_function_body while saving (showing 5 of 125). These functions will not be directly callable after loading.

try this instead:

import tensorflow as tf
import tensorflow_hub as hub

model_url = "https://tfhub.dev/tensorflow/mask_rcnn/inception_resnet_v2_1024x1024/1"
loaded_model = hub.load(model_url)
call = loaded_model.__call__.get_concrete_function(
tf.TensorSpec(shape=(1, 1024, 1024, 3), dtype=tf.uint8))
signatures = {'predict': call}
tf.saved_model.save(loaded_model,'./test', signatures=signatures)

did it help?

I used :import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

model_url = “TensorFlow Hub
imported = hub.load(model_url)

concrete_func = imported.call.get_concrete_function( tf.TensorSpec(shape=(1, 1024, 1024, 3), dtype=tf.uint8))
#concrete_func = imported.signatures[’__saved_model_init_op’]
frozen_func = convert_variables_to_constants_v2(concrete_func, lower_control_flow=False)
graph_def = frozen_func.graph.as_graph_def(add_shapes=True)
with tf.Graph().as_default() as inferred_graph:
tf.import_graph_def(graph_def, name="")

print(tf.mlir.experimental.convert_graph_def(
graph_def, pass_pipeline=‘tf-standard-pipeline’
))

mlir file got generated .and then tensorflow/bazel-bin/tensorflow/compiler/mlir/tf-opt --tf-executor-to-functional-conversion --tf-shape-inference -xla-legalize-tf --print-ir-before-all --print-ir-after-all &>sample ex.mlir
I got error as error: unknown TensorFlow type: uint8
%1106 = “tf.Placeholder”() {_output_shapes = [“tfshape$dim { size: 1 } dim { size: -1 } dim { size: -1 } dim { size: 3 }”], device = “”, dtype = “tfdtype$DT_UINT8”, name = “input_tensor”, shape = “tfshape$dim { size: 1 } dim { size: -1 } dim { size: -1 } dim { size: 3 }”} : () → tensor<1x?x?x3x!tf.uint8>

May I know where can I find the source code of the mrcnn model from TFHub. I tried converting model to to tflite using
import tensorflow as tf
import tensorflow_hub as hub

inputs = tf.keras.Input(shape=(1024, 1024, 3),batch_size=1,dtype=tf.uint8)
m_l = hub.KerasLayer(“TensorFlow Hub”)
x = m_l(inputs)
model = tf.keras.Model(inputs=inputs, outputs=x, name=“mrcnn”)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

got error as 2021-08-27 12:49:16.478141: F tensorflow/lite/toco/tooling_util.cc:2277] Check failed: array.data_type == array.final_data_type Array “input_1” has mis-matching actual and final data types (data_type=uint8, final_data_type=float).
Fatal Python error: Aborted

Hi Aruna,

the source code is at the Model Garden repo here

One question, are you planning on using this model on a mobile device? I’m asking because it might be very slow! this model is very complex and has a big input (1024x1024). It’s not really a model for on-device inference. There’re many better options (good tutorial here).

Thank you sir. Sir actually I wanted convert the model to mlir that is HLO dialect and wanted to understand how the expression operators and conditional branching , dynamic sizes are looks when lowered to mlir. Sir is HLO files available for MRCNN?.Because I tried lowering the TF-HUB model But I am not able to lower it .I am getting error got error as 2021-08-27 12:49:16.478141: F tensorflow/lite/toco/tooling_util.cc:2277] Check failed: array.data_type == array.final_data_type Array “input_1” has mis-matching actual and final data types (data_type=uint8, final_data_type=float).
Fatal Python error: Aborted

I think I understood.

I don’t think there’s a MRCNN model converted to tflite (for the reasons mentioned earlier), sorry.

Isn’t there any other model that you could use to understand the operations you want and that’s already converted to TFLite?

Sir is there a way to get rid of error 2021-08-27 12:49:16.478141: F tensorflow/lite/toco/tooling_util.cc:2277] Check failed: array.data_type == array.final_data_type Array “input_1” has mis-matching actual and final data types (data_type=uint8, final_data_type=float).
Fatal Python error: Aborted.

Thanks