【问题标题】:Converting a tensorflow model to a coreml model on windows gives issues在 windows 上将 tensorflow 模型转换为 coreml 模型会出现问题
【发布时间】:2019-04-02 05:37:12
【问题描述】:

我在转换自定义 tf 模型时遇到问题。 该模型如下所示:

转换后(没有错误)它看起来像这样:

加法运算符在哪里?

另一个问题是转换的输出给了我这个:

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
  }
}
]

但我的模型只有浮点值? (转换确实将它从 float32 更改为 float64)

有人可以回答我的问题并告诉我我做错了什么吗?

谢谢

【问题讨论】:

    标签: ios tensorflow coreml coremltools


    【解决方案1】:

    MatMul 后跟 Add 与 innerProduct 运算符相同。

    请注意,innerProduct 层有一个“偏差”。如果您查看这些偏差值,您会发现这些值与 Add 运算符中使用的值相同。所以 coremltools / tf-coreml 只是简单地将这两个操作合并到一个层中。

    MLMultiArray 对象的默认数据类型是 DOUBLE。您可以将其更改为 FLOAT,但这可能不一定更快。以下是您在 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)  
    

    脚本由一位友好的 Apple 员工提供(请参阅 https://forums.developer.apple.com/thread/84401)。

    【讨论】:

    • kk 谢谢澄清。我感觉合理。知道为什么我的转换模型没有产生任何输出吗?
    • “没有产生任何输出”是什么意思?输出是否全为零?
    猜你喜欢
    • 2018-06-14
    • 2020-08-25
    • 2018-09-12
    • 2019-12-18
    • 1970-01-01
    • 2019-05-14
    • 2017-11-12
    • 2017-11-23
    • 2017-12-09
    相关资源
    最近更新 更多