简体   繁体   English

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

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

I have been trying to implement the Tensorflow's simple audio recognition to an iphone app. 我一直在尝试将Tensorflow的简单音频识别功能应用到iPhone应用程序中。 After some research i found that i need to convert the Tensorflow's frozen graph .pb file to core ML model and then use it in the iOS apps. 经过一些研究后,我发现我需要将Tensorflow的冻结图.pb文件转换为核心ML模型,然后在iOS应用程序中使用它。 So i tried following this sample and referenced this convertor. 所以我尝试了这个示例并引用了这个转换器。 But it looks like the convertor was mainly written to convert models that take image as input.But My model should be able to take audio .wav files as input. 但看起来转换器主要用于转换以图像为输入的模型。但我的模型应该能够将音频.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'
                     )

When i try to convert my graph to core ML model using the above code i get the following error response. 当我尝试使用上面的代码将我的图形转换为核心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$

As per the last error message I need to enter the input shape of the input wav_data:0 . 根据上一条错误消息,我需要输入input wav_data:0的输入形状input wav_data:0 So i created the text summary of the frozen graph as mentioned in the tutorial and looked up on the wav_data input shape. 所以我创建了教程中提到的冻结图的文本摘要,并查看了wav_data输入形状。 But the input shape is <unknown> for this. 但输入形状为<unknown>

在此输入图像描述

I would like to know if there are any other methods to convert this file into core ML model? 我想知道是否还有其他方法可以将此文件转换为核心ML模型? or Is there any way to use the frozen graph .pb file directly in an ios swift app ? 或者有没有办法直接在ios swift应用程序中使用冻结图形.pb文件?

The Tensorflow-experimental Swift framework is also poorly documented. Tensorflow实验的Swift框架也很难记录。 If you guys have any good resource on this, please share. 如果你们有这方面的任何好资源,请分享。

Update As per the console log i downgraded the keras and tensorflow versions and also as per Matthijs answer i have added the input_name_shape_dict = { "import/wav_data:0" : [1, 16, 44100, 1] Now the console shows the error in different way 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

You can supply an input shape to the converter: 您可以为转换器提供输入形状:

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

If your data is 1-dimensional, then the height and width should be 1 and channels should be the length of the data. 如果您的数据是1维的,则高度和宽度应为1,通道应为数据的长度。

You may run into other conversion errors too, but this is the first step. 您也可能遇到其他转换错误,但这是第一步。 :-) (tfcoreml needs to know the input shape so that it can run the model with some fake data.) :-)(tfcoreml需要知道输入形状,以便它可以使用一些假数据运行模型。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将预训练的 tensorflow pb 冻结图转换为可修改的 h5 keras 模型? - How to convert a pretrained tensorflow pb frozen graph into a modifiable h5 keras model? 如何将在 tensorflow 2 中训练的模型转换为 tensorflow 1 冻结图 - How can I convert a model trained in tensorflow 2 to a tensorflow 1 frozen graph 如何将基于 tensorflow 构建的修改模型(用于迁移学习,in.ckpt 格式)导出到冻结图(.pb 文件) - How to export a modified model(used for transfer learning, in .ckpt format) built on tensorflow to a frozen graph(.pb file) TensorFlow:有没有办法将冻结图转换为检查点模型? - TensorFlow: Is there a way to convert a frozen graph into a checkpoint model? 将元数据添加到 tensorflow 冻结图 pb - Add metadata to tensorflow frozen graph pb Tensorflow:如何将冻结模型转换为已保存模型 - Tensorflow: how to convert a frozen model to saved model 如何将冻结图转换为 TensorFlow lite - How to convert frozen graph to TensorFlow lite 将冻结的图从.pb转换为.onnx - Convert frozen graph from .pb to .onnx 导入一个简单的Tensorflow frozen_model.pb文件并在C ++中进行预测 - Import a simple Tensorflow frozen_model.pb file and make prediction in C++ 如何在其他PC上使用Tensorflow(GPU)的Frozen_inference_graph.pb文件? - How to use frozen_inference_graph.pb file of tensorflow (GPU) at some other PC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM