无法将具有tf.transpose和BatchNormalization层的tf2模型转换为tf lite模型

时间:2019-10-12 03:39:58

标签: python tensorflow keras tensorflow-lite tensorflow2.0

我在本地尝试tensorflow 2.0.0,在Colab上尝试了tensorflow 2.0.0-rc2。他们俩都给了我Dimensions must match错误。

这里是重现该错误的示例代码

import tensorflow as tf

inputs = tf.keras.Input(shape=(3,3,4))
x = tf.reshape(inputs, [-1, 3, 3, 2, 4 // 2])
x = tf.transpose(x, [0, 1, 2, 4, 3])
x = tf.reshape(x, [-1, 3, 3, 4])
x = tf.keras.layers.BatchNormalization()(x)
model = tf.keras.Model(inputs=inputs, outputs=x)

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

这是来自Colab的错误:

ConverterError                            Traceback (most recent call last)
<ipython-input-70-3dd6f66ed1c5> in <module>()
      1 converter = tf.lite.TFLiteConverter.from_keras_model(model)
----> 2 tflite_model = converter.convert()

2 frames
/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/lite/python/convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str, debug_info_str, enable_mlir_converter)
    198       stdout = _try_convert_to_unicode(stdout)
    199       stderr = _try_convert_to_unicode(stderr)
--> 200       raise ConverterError("See console for info.\n%s\n%s\n" % (stdout, stderr))
    201   finally:
    202     # Must manually cleanup files.

ConverterError: See console for info.
2019-10-12 03:25:54.802429: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 8 operators, 17 arrays (0 quantized)
2019-10-12 03:25:54.802608: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 8 operators, 17 arrays (0 quantized)
2019-10-12 03:25:54.802814: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 1: 4 operators, 9 arrays (0 quantized)
2019-10-12 03:25:54.802870: F tensorflow/lite/toco/graph_transformations/propagate_fixed_sizes.cc:118] Check failed: dim_x == dim_y (2 vs. 4)Dimensions must match
Fatal Python error: Aborted

Current thread 0x00007feb48014780 (most recent call first):
  File "/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/lite/toco/python/toco_from_protos.py", line 52 in execute
  File "/tensorflow-2.0.0-rc2/python3.6/absl/app.py", line 250 in _run_main
  File "/tensorflow-2.0.0-rc2/python3.6/absl/app.py", line 299 in run
  File "/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/python/platform/app.py", line 40 in run
  File "/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/lite/toco/python/toco_from_protos.py", line 89 in main
  File "/tensorflow-2.0.0-rc2/python3.6/bin/toco_from_protos", line 10 in <module>
Aborted (core dumped)

0 个答案:

没有答案