Tflite TOCO转换因K.random_normal(shape =(batch,dim))而失败

时间:2019-05-27 07:22:31

标签: python tensorflow keras

我正在使用tensorflow lite的toco_convert做一些旧工作。 这些是我因以下命令而遇到的错误。

toco\
--graph_def_file=6-graphmh-55epoc.pb \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--output_file=/leaves.tflite \
--inference_type=FLOAT \
--input_type=FLOAT \
--input_arrays=ImageTensor \
--output_arrays=SemanticPredictions \
--input_shapes=1,113,3 \

我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\ash\AppData\Local\Continuum\anaconda3\envs\diec\Scripts\toco_from_protos-script.py", line 10, in <module>
    sys.exit(main())
  File "C:\Users\ash\AppData\Local\Continuum\anaconda3\envs\diec\lib\site-packages\tensorflow\lite\toco\python\toco_from_protos.py", line 59, in main
    app.run(main=execute, argv=[sys.argv[0]] + unparsed)
  File "C:\Users\ash\AppData\Local\Continuum\anaconda3\envs\diec\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
    _sys.exit(main(argv))
  File "C:\Users\ash\AppData\Local\Continuum\anaconda3\envs\diec\lib\site-packages\tensorflow\lite\toco\python\toco_from_protos.py", line 33, in execute
    output_str = tensorflow_wrap_toco.TocoConvert(model_str, toco_str, input_str)
Exception: We are continually in the process of adding support to TensorFlow Lite for more ops. It would be helpful if you could inform us of how this conversion went by opening a github issue at https://github.com/tensorflow/tensorflow/issues/new?template=40-tflite-op-request.md
 and pasting the following:

Some of the operators in the model are not supported by the standard TensorFlow Lite runtime. If those are native TensorFlow operators, you might be able to use the extended runtime by passing --enable_select_tf_ops, or by setting target_ops=TFLITE_BUILTINS,SELECT_TF_OPS when calling tf.lite.TFLiteConverter(). Otherwise, if you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.lite.TFLiteConverter(). Here is a list of builtin operators you are using: ADD, EXP, FULLY_CONNECTED, LOGISTIC, MUL. Here is a list of operators for which you will need custom implementations: RandomStandardNormal.

我知道tf.lite.toco_convert被描述了,但是我需要它来做旧作品。 我认为这是由于以下几行 epsilon = K.random_normal(shape=(batch, dim)) return z_mean + K.exp(0.5 * z_log_var) * epsilon。 我不确定为什么会这样,因为旧的tensorflow keras.backend具有random_normal属性。 如果您可以调试或指向一些资源来规避此错误,将会很有帮助。

2 个答案:

答案 0 :(得分:0)

从这里找到解决方案:https://www.tensorflow.org/lite/guide/ops_select

import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                        tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

这是因为RandomStandardNormal不是TensorFlow lite的一部分,所以我们需要使用tf.lite.OpsSet来包含它

答案 1 :(得分:0)

推荐方法:

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                        tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

转换:

toco\
--graph_def_file=6-graphmh-55epoc.pb \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--output_file=/leaves.tflite \
--inference_type=FLOAT \
--input_type=FLOAT \
--input_arrays=ImageTensor \
--output_arrays=SemanticPredictions \
--input_shapes=1,256,3 \