如何将Tensorflow Simple Audio Recognition冻结图(.pb)转换为Core ML模型?

时间:2018-12-05 06:37:48

标签: python ios swift tensorflow coreml

我一直在尝试将Tensorflow's simple audio recognition实施到iPhone应用程序中。经过一些研究,我发现我需要将Tensorflow的冻结图.pb文件转换为核心ML模型,然后在iOS应用中使用它。因此,我尝试了以下this示例并引用了this转换器。但是看起来转换器主要是为转换将图像作为输入的模型而编写的,但是我的模型应该能够将音频.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的输入形状。因此,如本教程中所述,我创建了冻结图的文本摘要,并查看了wav_data输入形状。但是为此,输入形状为<unknown>

enter image description here

我想知道是否还有其他方法可以将此文件转换为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

1 个答案:

答案 0 :(得分:2)

您可以向转换器提供输入形状:

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

如果您的数据是一维的,则高度和宽度应为1,通道应为数据的长度。

您也可能会遇到其他转换错误,但这是第一步。 :-)(tfcoreml需要知道输入形状,以便它可以使用一些假数据运行模型。)

相关问题