Tensorflow-Lite:从tf.Session转换导出GraphDef失败

时间:2019-06-28 09:14:28

标签: android python tensorflow tensorflow-lite

我想将我的张量流频谱图会话转换为.tflite文件,以便在Android中使用。 我尝试遵循TFLite official example,但在转换过程中失败。 请给我一些修复此错误的建议。非常感谢!

import tensorflow as tf
from tensorflow.signal import stft

# Waveform placeholder
waveform = tf.placeholder(name="waveform", dtype=tf.float32, shape=(4000))

# Compute the spectrogram and get the asolute values
stfts = stft(waveform, frame_length=256, frame_step=64, fft_length=256)
spectrograms = tf.abs(stfts)   

def TF_spectrogram(audio):
    # Run the spectrogram session
    with tf.Session() as sess:
        # Run the computation graph and save the png encoded image to a file
        spectrogram = sess.run(spectrograms, feed_dict={waveform: audio}) 
        print("The Spectrogram size (shape) is : " + str(spectrogram.shape) + ", type : " + str(type(spectrogram)))  
        return spectrogram

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    converter = tf.lite.TFLiteConverter.from_session(sess, [waveform], [spectrograms])
    tflite_model = converter.convert()
    open("spectrogram.tflite", "wb").write(tflite_model)

我从Jupyter Notebook收到错误消息

---------------------------------------------------------------------------
ConverterError                            Traceback (most recent call last)
<ipython-input-22-a94c03f6a5ae> in <module>
      2     sess.run(tf.global_variables_initializer())
      3     converter = tf.lite.TFLiteConverter.from_session(sess, [waveform], [spectrograms])
----> 4     tflite_model = converter.convert()
      5     open("converted_model.tflite", "wb").write(tflite_model)

F:\Anaconda3\envs\tf_py36\lib\site-packages\tensorflow\lite\python\lite.py in convert(self)
    896           input_tensors=self._input_tensors,
    897           output_tensors=self._output_tensors,
--> 898           **converter_kwargs)
    899     else:
    900       result = _toco_convert_graph_def(

F:\Anaconda3\envs\tf_py36\lib\site-packages\tensorflow\lite\python\convert.py in toco_convert_impl(input_data, input_tensors, output_tensors, *args, **kwargs)
    402   data = toco_convert_protos(model_flags.SerializeToString(),
    403                              toco_flags.SerializeToString(),
--> 404                              input_data.SerializeToString())
    405   return data
    406 

F:\Anaconda3\envs\tf_py36\lib\site-packages\tensorflow\lite\python\convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str)
    170       stderr = _try_convert_to_unicode(stderr)
    171       raise ConverterError(
--> 172           "TOCO failed. See console for info.\n%s\n%s\n" % (stdout, stderr))
    173   finally:
    174     # Must manually cleanup files.

ConverterError: TOCO failed. See console for info.
2019-06-28 17:04:12.512364: I tensorflow/lite/toco/import_tensorflow.cc:1336] Converting unsupported operation: RFFT
2019-06-28 17:04:12.512817: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "NoOp" device_type: "CPU"') for unknown op: NoOp
2019-06-28 17:04:12.513084: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "NoOp" device_type: "GPU"') for unknown op: NoOp
2019-06-28 17:04:12.513269: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_HostRecv" device_type: "GPU" host_memory_arg: "tensor"') for unknown op: _HostRecv
2019-06-28 17:04:12.513564: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_Send" device_type: "CPU"') for unknown op: _Send
2019-06-28 17:04:12.513819: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_HostRecv" device_type: "CPU"') for unknown op: _HostRecv
2019-06-28 17:04:12.514068: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_Send" device_type: "GPU"') for unknown op: _Send
2019-06-28 17:04:12.514254: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_Recv" device_type: "CPU"') for unknown op: _Recv
2019-06-28 17:04:12.514431: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_HostSend" device_type: "GPU" host_memory_arg: "tensor"') for unknown op: _HostSend
2019-06-28 17:04:12.514637: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_Recv" device_type: "GPU"') for unknown op: _Recv
2019-06-28 17:04:12.514803: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "_HostSend" device_type: "CPU"') for unknown op: _HostSend
2019-06-28 17:04:12.515019: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "WrapDatasetVariant" device_type: "CPU"') for unknown op: WrapDatasetVariant
2019-06-28 17:04:12.515234: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "WrapDatasetVariant" device_type: "GPU" host_memory_arg: "input_handle" host_memory_arg: "output_handle"') for unknown op: WrapDatasetVariant
2019-06-28 17:04:12.515498: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "UnwrapDatasetVariant" device_type: "CPU"') for unknown op: UnwrapDatasetVariant
2019-06-28 17:04:12.515718: E tensorflow/core/framework/op_kernel.cc:1426] OpKernel ('op: "UnwrapDatasetVariant" device_type: "GPU" host_memory_arg: "input_handle" host_memory_arg: "output_handle"') for unknown op: UnwrapDatasetVariant
2019-06-28 17:04:12.516175: I tensorflow/lite/toco/import_tensorflow.cc:1385] Unable to determine output type for op: RFFT
2019-06-28 17:04:12.516336: I tensorflow/lite/toco/import_tensorflow.cc:1336] Converting unsupported operation: ComplexAbs
2019-06-28 17:04:12.516712: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 7 operators, 17 arrays (0 quantized)
2019-06-28 17:04:12.517018: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 7 operators, 17 arrays (0 quantized)
2019-06-28 17:04:12.517417: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 1: 7 operators, 16 arrays (0 quantized)
2019-06-28 17:04:12.517682: F tensorflow/lite/toco/graph_transformations/propagate_fixed_sizes.cc:118] Check failed: dim_x == dim_y (64 vs. 256)Dimensions must match
Fatal Python error: Aborted

Current thread 0x000033dc (most recent call first):
  File "f:\anaconda3\envs\tf_py36\lib\site-packages\tensorflow\lite\toco\python\toco_from_protos.py", line 33 in execute
  File "f:\anaconda3\envs\tf_py36\lib\site-packages\absl\app.py", line 251 in _run_main
  File "f:\anaconda3\envs\tf_py36\lib\site-packages\absl\app.py", line 300 in run
  File "f:\anaconda3\envs\tf_py36\lib\site-packages\tensorflow\python\platform\app.py", line 40 in run
  File "f:\anaconda3\envs\tf_py36\lib\site-packages\tensorflow\lite\toco\python\toco_from_protos.py", line 59 in main
  File "F:\Anaconda3\envs\tf_py36\Scripts\toco_from_protos.exe\__main__.py", line 9 in <module>
  File "f:\anaconda3\envs\tf_py36\lib\runpy.py", line 85 in _run_code
  File "f:\anaconda3\envs\tf_py36\lib\runpy.py", line 193 in _run_module_as_main

我想通过tf.signal.stft函数制作自定义频谱图库,并保存到.tflite文件中以便在Android设备中使用。 我再也找不到有关TFLite转换器的教程,只有正式文档。?请帮助我解决此错误,非常感谢! ?

0 个答案:

没有答案