繁体   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