[英]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
根据控制台日志我降级了keras
和tensorflow
版本,并且根据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.