【问题标题】:coremltools produces a lot of inputs for xgboost modelcoremltools 为 xgboost 模型生成大量输入
【发布时间】:2021-09-13 21:57:57
【问题描述】:

我正在使用 python 在 coremltools 工具中进行从 xgboost 到 coreml 的转换,但它为 coreml 模型生成 n 个单独的输入(而不是单个输入数组)。

我该如何解决?

【问题讨论】:

    标签: xgboost coreml coremltools


    【解决方案1】:

    上周我遇到了同样的问题,但我找不到合适的解决方案来解决这个问题。我相信当我写这篇文章的时候,没有办法正确处理大量的输入。

    在我的例子中,模型预计会有超过 1000 个输入(命名为 f0f1f2,...),因为我扁平化了一些图像特征来训练提升算法。我发现唯一的解决方法是编写一个简短的 python sn-p 来生成要复制/粘贴到 XCode 中的字符串。例如,在我的例子中,包含 CoreML 模型输入的数组被命名为 stackedFeatures,并且如前所述,这些特征被称为 f0f1f2、...

    
    def save_swift_input_str(model):
        path_to_save_txt = os.path.join(path_to_save_model, "predict_args.txt")
        text_file = open(path_to_save_txt, "w")
        feature_names = model.feature_names
        feature_list_swift_name = "stackedFeatures["
        for idx, feature_name in enumerate(feature_names):
            text_file.write('{}: {},'.format(feature_name, feature_list_swift_name + str(idx) + "]"))
        text_file.close()
    
    

    然后通过将此文件的内容复制/粘贴到处理模型输入的类中:

    
    let modelInput = ModelInput(f0: stackedFeatures[0],f1: stackedFeatures[1],f2: stackedFeatures[2],f3: stackedFeatures[3],f4: stackedFeatures[4],f5: stackedFeatures[5],f6: stackedFeatures[6],f7: stackedFeatures[7],f8: stackedFeatures[8], ...)
    
    

    我可以使用以下代码在我的设备上使用 CoreML 运行预测:

    
    guard let modelOutput = try? model.prediction(input: model_input) else {
                                fatalError("Unexpected runtime error.")
                            }
    
    

    我希望这会有所帮助!如果有人找到更优雅的解决方案来解决这个问题,我也会很好奇!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2013-11-26
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多