簡體   English   中英

如何將Tensorflow Simple Audio Recognition冷凍圖(.pb)轉換為Core ML模型?

[英]How to convert Tensorflow Simple Audio Recognition frozen graph(.pb) to Core ML model?

我一直在嘗試將Tensorflow的簡單音頻識別功能應用到iPhone應用程序中。 經過一些研究后,我發現我需要將Tensorflow的凍結圖.pb文件轉換為核心ML模型,然后在iOS應用程序中使用它。 所以我嘗試了這個示例並引用了這個轉換器。 但看起來轉換器主要用於轉換以圖像為輸入的模型。但我的模型應該能夠將音頻.wav文件作為輸入。 `

import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'my_frozen_graph.pb',
                     mlmodel_path = 'my_model.mlmodel',
                     output_feature_names = ['labels_softmax:0'],
                     class_labels = 'classes.txt'
                     )

當我嘗試使用上面的代碼將我的圖形轉換為核心ML模型時,我得到以下錯誤響應。

(env3) minimaci73$ python model.py
WARNING:root:Keras version 2.2.0 detected. Last version known to be fully compatible of Keras is 2.1.6 .
WARNING:root:TensorFlow version 1.8.0 detected. Last version known to be fully compatible is 1.5.0 .

Loading the TF graph...
Graph Loaded.
Traceback (most recent call last):
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tfcoreml/_tf_coreml_converter.py", line 204, in _convert_pb_to_mlmodel
    shape_list = shape.as_list()
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 900, in as_list
    raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "model.py", line 6, in <module>
    class_labels = 'conv_labels.txt'
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tfcoreml/_tf_coreml_converter.py", line 586, in convert
    custom_conversion_functions=custom_conversion_functions)
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tfcoreml/_tf_coreml_converter.py", line 206, in _convert_pb_to_mlmodel
    raise ValueError('Please provide the shape for the input {} through the argument \'input_name_shape_dict\''.format(input_name))
ValueError: Please provide the shape for the input wav_data:0 through the argument 'input_name_shape_dict'
(env3) minimaci73$

根據上一條錯誤消息,我需要輸入input wav_data:0的輸入形狀input wav_data:0 所以我創建了教程中提到的凍結圖的文本摘要,並查看了wav_data輸入形狀。 但輸入形狀為<unknown>

在此輸入圖像描述

我想知道是否還有其他方法可以將此文件轉換為核心ML模型? 或者有沒有辦法直接在ios swift應用程序中使用凍結圖形.pb文件?

Tensorflow實驗的Swift框架也很難記錄。 如果你們有這方面的任何好資源,請分享。

Update根據控制台日志我降級了kerastensorflow版本,並且根據Matthijs的回答我添加了input_name_shape_dict = { "import/wav_data:0" : [1, 16, 44100, 1]現在控制台顯示錯誤不同的方法

(env3) minimaci73$ python model.py

Loading the TF graph...
Traceback (most recent call last):
  File "model.py", line 7, in <module>
    class_labels = 'conv_labels.txt'
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tfcoreml/_tf_coreml_converter.py", line 586, in convert
    custom_conversion_functions=custom_conversion_functions)
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tfcoreml/_tf_coreml_converter.py", line 153, in _convert_pb_to_mlmodel
    tf.import_graph_def(gdef, name='')
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 316, in new_func
    return func(*args, **kwargs)
  File "/Users/minimaci73/anaconda3/envs/env3/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 541, in import_graph_def
    raise ValueError('No op named %s in defined operations.' % node.op)
ValueError: No op named DecodeWav in defined operations.
(env3) minimaci73$ python model.py

您可以為轉換器提供輸入形狀:

input_name_shape_dict={ "import/wav_data:0" : [1, input_height, input_width, channels] })

如果您的數據是1維的,則高度和寬度應為1,通道應為數據的長度。

您也可能遇到其他轉換錯誤,但這是第一步。 :-)(tfcoreml需要知道輸入形狀,以便它可以使用一些假數據運行模型。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM