问题
Tensorflow Version: 2.2.0
OS: Windows 10
I am trying to convert a saved_model.pb to a tflite file.
Here is the code I am running:
import tensorflow as tf
# Convert
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir='C:\Data\TFOD\models\ssd_mobilenet_v2_quantized')
tflite_model = converter.convert()
fo = open("model.tflite", "wb")
fo.write(tflite_model)
fo.close
This code gives an error while converting:
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\convert.py", line 196, in toco_convert_protos
model_str = wrap_toco.wrapped_toco_convert(model_flags_str,
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\wrap_toco.py", line 32, in wrapped_toco_convert
return _pywrap_toco_api.TocoConvert(
Exception: <unknown>:0: error: loc("Func/StatefulPartitionedCall/input/_0"): requires all operands and results to have compatible element types
<unknown>:0: note: loc("Func/StatefulPartitionedCall/input/_0"): see current operation: %1 = "tf.Identity"(%arg0) {device = ""} : (tensor<1x?x?x3x!tf.quint8>) -> tensor<1x?x?x3xui8>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Data/TFOD/convert.py", line 13, in <module>
tflite_model = converter.convert()
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\lite.py", line 1076, in convert
return super(TFLiteConverterV2, self).convert()
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\lite.py", line 899, in convert
return super(TFLiteFrozenGraphConverterV2,
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\lite.py", line 629, in convert
result = _toco_convert_impl(
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\convert.py", line 569, in toco_convert_impl
data = toco_convert_protos(
File "C:\Users\Mr.Ace\AppData\Roaming\Python\Python38\site-packages\tensorflow\lite\python\convert.py", line 202, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc("Func/StatefulPartitionedCall/input/_0"): requires all operands and results to have compatible element types
<unknown>:0: note: loc("Func/StatefulPartitionedCall/input/_0"): see current operation: %1 = "tf.Identity"(%arg0) {device = ""} : (tensor<1x?x?x3x!tf.quint8>) -> tensor<1x?x?x3xui8>
回答1:
Tensorflow provides a python file called export_tflite_ssd_graph.py
in the model/object_detection
folder which can be used for converting your saved model into tflite format.
This is the GitHub link to the file. It is downloaded when you download the models directory.
How you can use it:
python object_detection/export_tflite_ssd_graph.py \
--pipeline_config_path path/to/ssd_mobilenet.config \
--trained_checkpoint_prefix path/to/model.ckpt \
--output_directory path/to/exported_model_directory
The expected output would be in the directory
path/to/exported_model_directory (which is created if it does not exist)
with contents:
- tflite_graph.pbtxt
- tflite_graph.pb
For complete usage, you can read the comments inside the file.
回答2:
Ok, I finally resolved it!
What I did is use tf-nightly and use the following Python Script:
import tensorflow as tf
saved_model_dir = "C:/Data/TFOD/models/ssd_mobilenet_v2_quantized/tflite"
converter = tf.lite.TFLiteConverter.from_saved_model(
saved_model_dir, signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
fo = open(
"C:/Data/TFOD/models/ssd_mobilenet_v2_quantized/tflite/model.tflite", "wb")
fo.write(tflite_model)
fo.close
This fixes the problem and you can convert to .tflite
来源:https://stackoverflow.com/questions/63652692/converting-saved-model-pb-to-model-tflite