简体   繁体   English

在Windows上将tensorflow模型转换为coreml模型会产生问题

[英]Converting a tensorflow model to a coreml model on windows gives issues

I'm having problems with a conversion of a custom tf model. 我在转换自定义tf模型时遇到问题。 The model looks like this: 该模型如下所示: 张量流模型

and after conversion(without errors) it looks like this: 转换后(没有错误)如下所示:

coreml模型

Where is the add operator? 加法运算符在哪里?

Another issue is that the output of the conversion gave me this: 另一个问题是转换的输出给了我这个:

Core ML input(s):
[name: "x_placeholder__0"
type {
  multiArrayType {
    shape: 41
    dataType: DOUBLE
  }
}
]
Core ML output(s):
[name: "softmax_prediction__0"
type {
  multiArrayType {
    shape: 2
    dataType: DOUBLE
  }
}
]

But my model has only float values? 但是我的模型只有浮点值吗? (conversion did actually change it from float32 to float64) (转换实际上将其从float32更改为float64)

Can someone answer my questions and maybe tell me what I'm doing wrong? 有人可以回答我的问题,也许告诉我我做错了什么吗?

Thx 谢谢

A MatMul followed by an Add is the same thing as the innerProduct operator. MatMul后跟Add与innerProduct运算符相同。

Note that the innerProduct layer has a "bias". 请注意,innerProduct层具有“偏差”。 If you look at those bias values, you'll see that these are the same values as are used in your Add operator. 如果查看这些偏差值,就会发现它们与“添加”运算符中使用的值相同。 So coremltools / tf-coreml has simply combined these two operations into a single layer. 因此coremltools / tf-coreml只是将这两个操作组合到一个单独的层中。

The default datatype for MLMultiArray objects is DOUBLE. MLMultiArray对象的默认数据类型为DOUBLE。 You can change this to FLOAT, but that may not necessarily be any faster. 您可以将其更改为FLOAT,但不一定会更快。 Here's how you would do this in Python: 使用Python的方法如下:

import coremltools  
import sys  

def update_multiarray_to_float32(feature):  
    if feature.type.HasField('multiArrayType'):  
        import coremltools.proto.FeatureTypes_pb2 as _ft  
        feature.type.multiArrayType.dataType = _ft.ArrayFeatureType.FLOAT32  

if __name__ == "__main__":  
    if len(sys.argv) != 3:  
        print "USAGE: %s <input_model_path> <output_model_path>" % sys.argv[0]  
        sys.exit(1)  

    input_model_path = sys.argv[1]  
    output_model_path = sys.argv[2]  

    spec = coremltools.utils.load_spec(input_model_path)  

    for input_feature in spec.description.input:  
        update_multiarray_to_float32(input_feature)  

    for output_feature in spec.description.output:  
        update_multiarray_to_float32(output_feature)  

    coremltools.utils.save_spec(spec, output_model_path)  

Script courtesy of a friendly Apple employee (see https://forums.developer.apple.com/thread/84401 ). 脚本由友好的Apple员工提供(请参阅https://forums.developer.apple.com/thread/84401 )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM