automl icon indicating copy to clipboard operation
automl copied to clipboard

How to convert 'tflite' model?

Open WonTaeYeon opened this issue 6 years ago • 14 comments

Hi, Thx for your code

I tried several things to convert to the 'tflite' model.

When freeze using model_inspect.py and converted to pb file, the following error occurred :

OSError: Cannot parse file b'freeze_log_dir/saved_model.pb': Error parsing message.

When I converted to pbtxt of the custom learning model, the following error occurred:

saved_model.pbtxt': 1:1 : Message type "tensorflow.SavedModel" has no field named "node"..

How can I fix the error?

WonTaeYeon avatar Apr 06 '20 08:04 WonTaeYeon

Tensorflow Version = 2.1 Command to convert ckpts to .pb model python3 model_inspect.py --runmode=freeze --ckpt=ckpts/model.ckpt-0000.* --model_name=efficientdet-d0 --logdir=Folder_name_to_save_pb_model

Command to convert the custom Trained Model to Saved Model (make a directory name efficientdet-d0 and Move all the new trained ckpts into efficientdet-d0) python3 model_inspect.py --runmode=saved_model --ckpt_path=efficientdet-d0/ --model_name=efficientdet-d0 --saved_model_dir=eff_d0_exp_data_v2_saved_model --num_classes=1

vamshi-vk avatar Apr 06 '20 09:04 vamshi-vk

@vamshi-vk thx for your answer... but new error occurred:

WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. WARNING:tensorflow:Importing a function (MetaGraph import) with ops with custom gradients. Will likely fail if a gradient is requested. 2020-04-07 10:38:19.132517: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1 2020-04-07 10:38:19.144383: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.144837: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: pciBusID: 0000:01:00.0 name: GeForce GTX 1080 Ti computeCapability: 6.1 coreClock: 1.582GHz coreCount: 28 deviceMemorySize: 10.91GiB deviceMemoryBandwidth: 451.17GiB/s 2020-04-07 10:38:19.144963: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1 2020-04-07 10:38:19.145897: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10 2020-04-07 10:38:19.146830: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2020-04-07 10:38:19.147106: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2020-04-07 10:38:19.148069: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10 2020-04-07 10:38:19.148583: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10 2020-04-07 10:38:19.150846: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7 2020-04-07 10:38:19.150930: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.151609: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.151918: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0 2020-04-07 10:38:19.152097: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA 2020-04-07 10:38:19.175263: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3000000000 Hz 2020-04-07 10:38:19.175550: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5624c5683be0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2020-04-07 10:38:19.175575: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2020-04-07 10:38:19.175747: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.176264: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: pciBusID: 0000:01:00.0 name: GeForce GTX 1080 Ti computeCapability: 6.1 coreClock: 1.582GHz coreCount: 28 deviceMemorySize: 10.91GiB deviceMemoryBandwidth: 451.17GiB/s 2020-04-07 10:38:19.176303: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1 2020-04-07 10:38:19.176320: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10 2020-04-07 10:38:19.176336: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2020-04-07 10:38:19.176350: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2020-04-07 10:38:19.176364: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10 2020-04-07 10:38:19.176379: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10 2020-04-07 10:38:19.176393: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7 2020-04-07 10:38:19.176450: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.176970: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.177447: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0 2020-04-07 10:38:19.177480: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1 2020-04-07 10:38:19.226640: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix: 2020-04-07 10:38:19.226664: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] 0 2020-04-07 10:38:19.226670: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0: N 2020-04-07 10:38:19.226771: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.227125: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.227450: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-04-07 10:38:19.227761: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10173 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1) 2020-04-07 10:38:19.228909: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5624c5cd2b30 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices: 2020-04-07 10:38:19.228922: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1080 Ti, Compute Capability 6.1 WARNING:tensorflow:From /home/wty/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1786: calling BaseResourceVariable.init (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers. Traceback (most recent call last): File "convert.py", line 8, in tflite_model = converter.convert() File "/home/wty/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/tensorflow_core/lite/python/lite.py", line 418, in convert raise ValueError("This converter can only convert a single " ValueError: This converter can only convert a single ConcreteFunction. Converting multiple functions is under development.

WonTaeYeon avatar Apr 07 '20 01:04 WonTaeYeon

I met the same problem

sunzhe09 avatar Apr 07 '20 09:04 sunzhe09

Successful conversion to 'tflite'. Tried on Tensorflow 2.2 (installed as TF-nightly) The code is as follows.

import tensorflow as tf

saved_model_obj = tf.saved_model.load(export_dir='saved_model') print(saved_model_obj.signatures.keys())

concrete_func = saved_model_obj.signatures['serving_default'] concrete_func.inputs[0].set_shape([1, 512, 512, 3])

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) 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()

open("efficientdet.tflite", "wb").write(tflite_model)

However, there was a new problem running TF_lite on Windows :

RuntimeError: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.Node number 0 (FlexTensorArrayV3) failed to prepare.

what should I do?

WonTaeYeon avatar Apr 08 '20 00:04 WonTaeYeon

Successful conversion to 'tflite'. Tried on Tensorflow 2.2 (installed as TF-nightly) The code is as follows.

import tensorflow as tf

saved_model_obj = tf.saved_model.load(export_dir='saved_model') print(saved_model_obj.signatures.keys())

concrete_func = saved_model_obj.signatures['serving_default'] concrete_func.inputs[0].set_shape([1, 512, 512, 3])

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) 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()

open("efficientdet.tflite", "wb").write(tflite_model)

However, there was a new problem running TF_lite on Windows :

RuntimeError: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.Node number 0 (FlexTensorArrayV3) failed to prepare.

what should I do?

Thanks for sharing your code. This is because you have chosen the SELECT_TF_OPS option during the conversion. Therefore, some operators will be using TF runtime during the inference. So I guess you may need to include the necessary libraries for TF ops in the TF Lite runtime. Check this out. https://www.tensorflow.org/lite/guide/ops_select

zhedasuiyuan avatar Apr 12 '20 18:04 zhedasuiyuan

@zhedasuiyuan Thx for your answer. The link contained an answer to what I wanted. I was trying to run TF-lite in windows 10 with python. I realized that it would be difficult to run as it is still under development with the python version. I need to study how to implement it in CPP. thank you.

WonTaeYeon avatar Apr 13 '20 02:04 WonTaeYeon

Hi, I have a different issue when convert the .pb file to the tflite model, here is my code: ` import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model('saved_model')

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()

open("saved_model/converted_model.tflite", "wb").write(tflite_model) `

When I run the code, the error shows: ` Traceback (most recent call last):

File "transfer_lite.py", line 11, in

tflite_model = converter.convert()

File "/home/hulining/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/lite/python/lite.py", line 418, in convert raise ValueError("This converter can only convert a single "

ValueError: This converter can only convert a single ConcreteFunction. Converting multiple functions is under development. ` could anybody help?

Ringhu avatar Apr 13 '20 09:04 Ringhu

Try: converter = tf.lite.TFLiteConverter.from_saved_model('saved_model', signature_keys=['serving_default'])

or

converter = tf.lite.TFLiteConverter.from_saved_model('saved_model', signature_keys=['serving_base64'])

WonTaeYeon avatar Apr 13 '20 09:04 WonTaeYeon

Thanks for ur reply @WonTaeYeon , I modify the code following ur instruction. However, here is another error: `Traceback (most recent call last):

File "transfer_lite.py", line 17, in

tflite_model = converter.convert()

File "/home/hulining/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow_core/lite/python/lite.py", line 446, in convert "invalid shape '{1}'.".format(_get_tensor_name(tensor), shape_list))

ValueError: None is only supported in the 1st dimension. Tensor 'image_arrays' has invalid shape '[1, None, None, None]'. ` could u plz help me again?

Ringhu avatar Apr 13 '20 10:04 Ringhu

@Ringhu I am also looking for a solution to that error. If it's solved, I'll leave a post here.

WonTaeYeon avatar Apr 16 '20 00:04 WonTaeYeon

I can't make anything work to actually produce a TFLite file - tried all of the various options, all have various errors: `saved_model_obj = tf.saved_model.load(export_dir='savedmodel') print(saved_model_obj.signatures.keys())

concrete_func = saved_model_obj.signatures['serving_default'] concrete_func.inputs[0].set_shape([1, 512, 512, 3])

#converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) #converter = tf.lite.TFLiteConverter.from_saved_model('savedmodel', signature_keys=['serving_base64']) converter = tf.lite.TFLiteConverter.from_saved_model('savedmodel', 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()

open("efficientdet.tflite", "wb").write(tflite_model)`

===== depending on which line is used above (see # versions) get different errors. As shown above the error is the same as posted earlier: ValueError Traceback (most recent call last) in () 14 #converter.experimental_new_converter = True 15 converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS] ---> 16 tflite_model = converter.convert() 17 18 open("efficientdet.tflite", "wb").write(tflite_model)

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in convert(self) 481 "None is only supported in the 1st dimension. Tensor '{0}' has " 482 "invalid shape '{1}'.".format( --> 483 _get_tensor_name(tensor), shape_list)) 484 elif shape_list and shape_list[0] is None: 485 # Set the batch size to 1 if undefined.

ValueError: None is only supported in the 1st dimension. Tensor 'image_arrays' has invalid shape '[1, None, None, None]'.

lessw2020 avatar May 01 '20 06:05 lessw2020

other error is if you use: converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])

then this error appears: loc(callsite("strided_slice_1"("/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py":613:0) at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py":578:0 at callsite("":2:0 at callsite("/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py":2882:0 at callsite("/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py":2822:0 at callsite("/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py":2718:0 at callsite("/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py":537:0 at callsite("/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py":208:0 at callsite("/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py":399:0 at "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py":233:0)))))))))): error: 'tfl.strided_slice' op operand #0 must be tensor of 32-bit float or 32-bit integer or 64-bit integer or 8-bit integer or QI8 type or QUI8 type or 1-bit integer values, but got 'tensor<1x512x512x3x!tf.quint8>' Traceback (most recent call last): File "/usr/local/bin/toco_from_protos", line 8, in sys.exit(main()) File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/lite/toco/python/toco_from_protos.py", line 93, in main app.run(main=execute, argv=[sys.argv[0]] + unparsed) File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/usr/local/lib/python2.7/dist-packages/absl/app.py", line 300, in run _run_main(main, args) File "/usr/local/lib/python2.7/dist-packages/absl/app.py", line 251, in _run_main sys.exit(main(argv)) File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/lite/toco/python/toco_from_protos.py", line 56, in execute enable_mlir_converter) Exception: /usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py:613:7: error: 'tfl.strided_slice' op operand #0 must be tensor of 32-bit float or 32-bit integer or 64-bit integer or 8-bit integer or QI8 type or QUI8 type or 1-bit integer values, but got 'tensor<1x512x512x3x!tf.quint8>' root = load_v1_in_v2.load(export_dir, tags)

lessw2020 avatar May 01 '20 06:05 lessw2020

@lessw2020 As far as I am aware, TFLite requires the use of fixed dimensions. If you set the input shape (excl. batch size) to known dimensions then it may convert.

e.g.

model.layers[0].set_shape((None, 512, 512, 3))

sinclairnick avatar Jul 01 '20 04:07 sinclairnick

Successful conversion to 'tflite'. Tried on Tensorflow 2.2 (installed as TF-nightly) The code is as follows.

import tensorflow as tf

saved_model_obj = tf.saved_model.load(export_dir='saved_model') print(saved_model_obj.signatures.keys())

concrete_func = saved_model_obj.signatures['serving_default'] concrete_func.inputs[0].set_shape([1, 512, 512, 3])

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) 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()

open("efficientdet.tflite", "wb").write(tflite_model)

However, there was a new problem running TF_lite on Windows :

RuntimeError: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.Node number 0 (FlexTensorArrayV3) failed to prepare.

what should I do?

Have you solved this?

korabelnikov avatar Aug 06 '20 12:08 korabelnikov