Tensorflow 2.0 freeze_graph failing on tf.keras saved model - tensorflow2.0

I'm converting a trained tf.keras model into tf frozen model to use with c++ api. I'm running into error while freezing the model on TF 2.0
model_path = '/home/Desktop/model.hdf5'
model = tf.keras.models.load_model(model_path)
tf.keras.experimental.export_savedmodel(model,newdir)
After this a variables folder with files [checkpoint,variables.data-00000-of-00001,variables.index], saved_model.pb and assests folder created in newdir.
I am trying to use saved_model.pb and variables.data-00000-of-00001 files to get single .pb frozen_graph
python /home/tensorflow/python/tools/freeze_graph.py --input_graph=/home/Desktop/tf_models/saved_model.pb --input_checkpoint=/home/Desktop/tf_models/variables/variables.data-00000-of-00001 --output_graph=/home/Desktop/tf_models/frozen_graph.pb --output_node_names=classes --input_binary=true
I expected a single frozen .pb file but instead running into error like below:
Traceback (most recent call last): File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py",
line 492, in run_main() File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py",
line 489, in run_main app.run(main=my_main, argv=[sys.argv[0]] +
unparsed) File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/platform/app.py",
line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/absl/app.py",
line 300, in run
_run_main(main, args) File "/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/absl/app.py",
line 251, in _run_main sys.exit(main(argv)) File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py",
line 488, in my_main = lambda unused_args: main(unused_args, flags)
File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py",
line 382, in main flags.saved_model_tags, checkpoint_version) File
"/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py",
line 341, in freeze_graph input_graph_def =
_parse_input_graph_proto(input_graph, input_binary) File "/home/vsrira10/anaconda2/envs/tf2/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py",
line 252, in _parse_input_graph_proto
input_graph_def.ParseFromString(f.read())
google.protobuf.message.DecodeError: Error parsing message
I'm open to suggestions alternate to using freeze_graph script. Thanks.

Related

How can make inference graph file using models/research/object_detection/export_inference_graph.py

python : 3.9.13
tensorflow : 2.9.1
I am making a custom dataset with 'tensorflow object detection'
The saved_model.pb file was generated by trainning with the FastRCNN dataset.
I took this file and applied it to Nuclio (Serverless function framework) but failed
It seems to have apply inference graph file type
I find export util pythonf file in models/research/object_detection directory "export_inference_graph.py"
But this file not working .
This is error message
Traceback (most recent call last):
File "export_inference_graph.py", line 211, in <module>
tf.app.run()
File "/home/namu/.local/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/home/namu/.local/lib/python3.8/site-packages/absl/app.py", line 308, in run
_run_main(main, args)
File "/home/namu/.local/lib/python3.8/site-packages/absl/app.py", line 254, in _run_main
sys.exit(main(argv))
File "export_inference_graph.py", line 199, in main
exporter.export_inference_graph(
File "/home/namu/myspace/data/models/research/object_detection/exporter.py", line 618, in export_inference_graph
_export_inference_graph(
File "/home/namu/myspace/data/models/research/object_detection/exporter.py", line 521, in _export_inference_graph
profile_inference_graph(tf.get_default_graph())
File "/home/namu/myspace/data/models/research/object_detection/exporter.py", line 649, in profile_inference_graph
contrib_tfprof.model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS)
NameError: name 'contrib_tfprof' is not defined
I knew from Google that this did not work on tensorflow 2.x
https://medium.com/#sebastingarcaacosta/how-to-export-a-tensorflow-2-x-keras-model-to-a-frozen-and-optimized-graph-39740846d9eb
I am working on it by referring to the above site
But
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
import numpy as np
#path of the directory where you want to save your model
frozen_out_path = "/home/namu/myspace/data/models/export_graph"
# name of the .pb file
frozen_model = "frozen_graph"
model = tf.keras.models.load_model('/home/namu/myspace/data/models/train_pb/saved_model') # tf_saved_model load
# model = tf.saved_model.load('/home/namu/myspace/data/models/train_pb/saved_model')
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
When i execute this code , error occurs
ValueError: Unable to create a Keras model from SavedModel at /home/namu/myspace/data/models/train_pb/saved_model. This SavedModel was exported with `tf.saved_model.save`, and lacks the Keras metadata file. Please save your Keras model by calling `model.save`or `tf.keras.models.save_model`. Note that you can still load this SavedModel with `tf.saved_model.load`.
How can i create inference graph pb file.

OSError: SavedModel file does not exist, with Tensorflow Lite

I am trying to convert a Keras model found here to TFlite using following snippet
import tensorflow as tf
import os.path as path
pwd = path.dirname(__file__)
model_path = pwd+"/models/old-models/"
print("PWD: ", model_path)
converter = tf.lite.TFLiteConverter.from_saved_model(model_path)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
But I am getting following error
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/<PyCharm installed directory>/ch-0/211.6693.23/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/<PyCharm installed directory>/ch-0/211.6693.23/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/<Path to project>/myproject/convert_to_tflite.py", line 7, in <module>
converter = tf.lite.TFLiteConverter.from_saved_model(model_path)
File "/<path to miniconda>/miniconda3/envs/myproject/lib/python3.8/site-packages/tensorflow/lite/python/lite.py", line 1069, in from_saved_model
saved_model = _load(saved_model_dir, tags)
File "/<path to miniconda>/miniconda3/envs/myproject/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 859, in load
return load_internal(export_dir, tags, options)["root"]
File "/<path to miniconda>/miniconda3/envs/myproject/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 871, in load_internal
loader_impl.parse_saved_model_with_debug_info(export_dir))
File "/<path to miniconda>/miniconda3/envs/myproject/lib/python3.8/site-packages/tensorflow/python/saved_model/loader_impl.py", line 56, in parse_saved_model_with_debug_info
saved_model = _parse_saved_model(export_dir)
File "/<path to miniconda>/miniconda3/envs/myproject/lib/python3.8/site-packages/tensorflow/python/saved_model/loader_impl.py", line 111, in parse_saved_model
raise IOError("SavedModel file does not exist at: %s/{%s|%s}" %
OSError: SavedModel file does not exist at: /<path to my project>/models/old-models/{saved_model.pbtxt|saved_model.pb}
I renamed the model file extension from .mlmodel to .pb, since it seems file extension need to be .pb or .pbtxt. I am sure that path to the model directory is correct.
I am using tensorflow~=2.4.1
According to https://apple.github.io/coremltools/coremlspecification, .mlmodel file extension represents a format of CoreML model. Renaming the file extension does not change the given model format to the other model format.
If you would like to generate TensorFlow Lite models, please take a look at the pre-trained model section at the guide page, https://www.tensorflow.org/lite/guide/get_started#1_choose_a_model.

object detection using tensorflow 1.8

(tensorflow2) C:\tensorflow2\models\research\object_detection>python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config
WARNING:tensorflow:From C:\Users\Nischay\Anaconda3\envs\tensorflow2\lib\site-packages\tensorflow\python\platform\app.py:124: main (from __main__) is deprecated and will be removed in a future version.
Instructions for updating:
Use object_detection/model_main.py.
WARNING:tensorflow:From C:\tensorflow1\models\research\object_detection\legacy\trainer.py:266: create_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Please switch to tf.train.create_global_step
WARNING:tensorflow:num_readers has been reduced to 1 to match input file shards.
Traceback (most recent call last):
File "train.py", line 184, in <module>
tf.app.run()
File "C:\Users\Nischay\Anaconda3\envs\tensorflow2\lib\site-packages\tensorflow\python\platform\app.py", line 124, in run
_sys.exit(main(argv))
File "C:\Users\Nischay\Anaconda3\envs\tensorflow2\lib\site-packages\tensorflow\python\util\deprecation.py", line 136, in new_func
return func(*args, **kwargs)
File "train.py", line 180, in main
graph_hook_fn=graph_rewriter_fn)
File "C:\tensorflow1\models\research\object_detection\legacy\trainer.py", line 291, in train
clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue])
File "C:\tensorflow1\models\research\slim\deployment\model_deploy.py", line 193, in create_clones
outputs = model_fn(*args, **kwargs)
File "C:\tensorflow1\models\research\object_detection\legacy\trainer.py", line 204, in _create_losses
prediction_dict = detection_model.predict(images, true_image_shapes)
File "C:\tensorflow1\models\research\object_detection\meta_architectures\faster_rcnn_meta_arch.py", line 821, in predict
prediction_dict = self._predict_first_stage(preprocessed_inputs)
File "C:\tensorflow1\models\research\object_detection\meta_architectures\faster_rcnn_meta_arch.py", line 872, in _predict_first_stage
image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs)
File "C:\tensorflow1\models\research\object_detection\meta_architectures\faster_rcnn_meta_arch.py", line 1250, in _extract_rpn_feature_maps
feature_map_shape[2])]))
File "C:\tensorflow1\models\research\object_detection\core\anchor_generator.py", line 103, in generate
anchors_list = self._generate(feature_map_shape_list, **params)
File "C:\tensorflow1\models\research\object_detection\anchor_generators\grid_anchor_generator.py", line 111, in _generate
with tf.init_scope():
AttributeError: module 'tensorflow' has no attribute 'init_scope'
For tensorflow 1.8 cuda 9.0 cuDNN 7.4.2 , getting error while training the model.
Tensorflow 1.8 does not define a function called init_scope(). You can have a look at all the functions available in API r1.8. Consider upgrading to a later version of Tensorflow like API r1.13 where init_scope() is defined.

Tensorflow object detection: export inference graph

I have an error in exporting an inference graph and I searched for hours but can't find a solution; this is my error:
Command:
python export_inference_graph.py --input_type image_tensor --pipeline_config_path training faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training "model.ckpt-2950" --output_directory export
Error Traceback:
Traceback (most recent call last):
File "export_inference_graph.py", line 151, in
tf.app.run()
File "C:\Users\OctaNet\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
_sys.exit(main(argv))
File "export_inference_graph.py", line 135, in main
text_format.Merge(f.read(), pipeline_config)
File "C:\Users\OctaNet\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 125, in read
self._preread_check()
File "C:\Users\OctaNet\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 85, in _preread_check
compat.as_bytes(self.__name), 1024 * 512, status)
File "C:\Users\OctaNet\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in exit
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.UnknownError: NewRandomAccessFile failed to Create/Open: training : Acc\udce8s refus\udce9.
; Input/output error
I guess you need remove : from 'training : Acc\udce8s refus\udce9'
The --pipeline_config_path flag has been set to training faster_rcnn_inception_v2_pets.config which is not a valid path due to the space character following the word training.
Perhaps user11095295 intended to write training/faster_rcnn_inception_v2_pets.config.

how to convert csv file to tfrecord

I am actually working with miniconda and tensorflow and trying to train a model for object detection and I'm facing a problem when running generate_ tfrecord.py to convert csv to tfrecord the error is:
"generate_tfrecord.py", line 90, in <module>
tf.app.run()
I used this link
https://github.com/datitran/raccoon_dataset/blob/master/generate_tfrecord.py
(tensorflow) C:\Users\OctaNet\Miniconda3\envs\tensorflow\models\research\object_detection>python generate_tfrecord.py --csv_input=images/train.csv --output_path=data/train.record --image_dir=images/train/
Iterating train
Traceback (most recent call last):
File "generate_tfrecord.py", line 90, in <module>
tf.app.run()
File "C:\Users\OctaNet\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
_sys.exit(main(argv))
File "generate_tfrecord.py", line 80, in main
tf_example = create_tf_example(group, path)
File "generate_tfrecord.py", line 67, in create_tf_example
'image/object/class/label': dataset_util.int64_list_feature(classes),
File "C:\Users\OctaNet\Miniconda3\envs\tensorflow\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\utils\dataset_util.py", line 26, in int64_list_feature
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
TypeError: None has type NoneType, but expected one of: int, long