tensorflow: Convert to TFLite Unexpected value for attribute 'data_format'. Expected 'NHWC'

I’m trying to convert a frozen Tensorflow graph. See model here. I found a lot related bug reports on that issue but non of them was actually solved: #30411, #7967,#24491.

System information

  • OS Platform and Distribution: Linux Ubuntu 19.04 64bit
  • TensorFlow installed from (source or binary): binary (CPU)
  • TensorFlow version (use command below): v1.14.0-rc1-22-gaf24dc9 1.14.0
  • Python version: 3.7.3

Describe the current behavior Running in Jupyter I get following error:

ConverterError: TOCO failed. See console for info.
2019-07-28 21:08:26.912035: F tensorflow/lite/toco/import_tensorflow.cc:2619] Check failed: status.ok() Unexpected value for attribute 'data_format'. Expected 'NHWC'
Fatal Python error: Aborted

Current thread 0x00007fc8ee681740 (most recent call first):
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/toco/python/toco_from_protos.py", line 33 in execute
  File "/home/paul/.local/lib/python3.7/site-packages/absl/app.py", line 251 in _run_main
  File "/home/paul/.local/lib/python3.7/site-packages/absl/app.py", line 300 in run
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 40 in run
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/toco/python/toco_from_protos.py", line 59 in main
  File "/home/paul/anaconda3/envs/openvino/bin/toco_from_protos", line 10 in <module>
Aborted (core dumped)

Running over shell:

Traceback (most recent call last):
  File "tensorflow_issue_tflite.py", line 10, in <module>
    tflite_model = converter.convert()
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/python/lite.py", line 898, in convert
    **converter_kwargs)
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/python/convert.py", line 404, in toco_convert_impl
    input_data.SerializeToString())
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/python/convert.py", line 172, in toco_convert_protos
    "TOCO failed. See console for info.\n%s\n%s\n" % (stdout, stderr))
tensorflow.lite.python.convert.ConverterError: TOCO failed. See console for info.
2019-07-28 21:35:22.220584: F tensorflow/lite/toco/import_tensorflow.cc:2619] Check failed: status.ok() Unexpected value for attribute 'data_format'. Expected 'NHWC'
Fatal Python error: Aborted

Current thread 0x00007fae1bce0740 (most recent call first):
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/toco/python/toco_from_protos.py", line 33 in execute
  File "/home/paul/.local/lib/python3.7/site-packages/absl/app.py", line 251 in _run_main
  File "/home/paul/.local/lib/python3.7/site-packages/absl/app.py", line 300 in run
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 40 in run
  File "/home/paul/anaconda3/envs/openvino/lib/python3.7/site-packages/tensorflow/lite/toco/python/toco_from_protos.py", line 59 in main
  File "/home/paul/anaconda3/envs/openvino/bin/toco_from_protos", line 10 in <module>
Aborted (core dumped)

Code to reproduce the issue

import tensorflow as tf

graph_def_file = "model_lcnn_29v2_cpu.pb"

input_arrays = ["0"]
output_arrays = ["MatMul"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("model_lcnn_29v2.tflite", "wb").write(tflite_model)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 24 (5 by maintainers)

Most upvoted comments

Awesome! We are going to support ‘nchw’ in the future, so hopefully won’t need to do this all on your own. Thanks!

Awesome! We are going to support ‘nchw’ in the future, so hopefully won’t need to do this all on your own. Thanks!

Hey, i am really excited for this. When is it going to happen?

TF Lite’s Conv/MaxPool/AvgPool operator only supports NHWC data format right now. I think your model uses NCHW format in those ops. Could you try switch your data format to ‘NHWC’?

@MaratZakirov Yes, forgot to transfer the attributes for the Conv2D layer. I added the fix to the script. As for the Dilation2D error look at the solution from https://github.com/tensorflow/tensorflow/issues/38102 maybe that helps:

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                       tf.lite.OpsSet.SELECT_TF_OPS]