python - 如何将 Tensorflow Simple Audio Recognition frozen graph(.pb) 转换为 Core ML 模型?

标签 python ios swift tensorflow coreml

我一直在努力实现 Tensorflow's simple audio recognition到 iphone 应用程序。经过一些研究,我发现我需要将 Tensorflow 的卡住图 .pb 文件转换为核心 ML 模型,然后在 iOS 应用程序中使用它。所以我尝试关注 this sample 和引用 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-experimental 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, channel 应为数据的长度。

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

关于python - 如何将 Tensorflow Simple Audio Recognition frozen graph(.pb) 转换为 Core ML 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53626504/

相关文章:

python - 运行我的函数时,我的 if 语句未运行

python - Django休息框架时间字段输入格式

ios - 裁剪 UIImage 没有产生预期的裁剪 - swift ?

iPhone : In-App Purchase(s) must be submitted with a new app version

ios - Swift iOS 中的实时变调

python - 如何将数组保存到文件中

python - 如何绘制一个随机三角形及其中线?

ios - 使用 ios-charts 库对条形图中的列进行分组,其中数据条目的数量可变

ios - 从 UIView 导航到 UIViewController

swift - 导航栏上后退按钮上的字体(Swift)