首页 > 解决方案 > TensorflowLite iOS Swift

问题描述

我需要在 iOS 中运行一个 tensorflow lite 模型,它从输入接收一个数组 (1, 4500, 1),但我不明白如何在不将其转换为数据的情况下将其发送到输入。如果我将它打印到解释器并且输入准确地告诉我我需要什么,但是当我运行代码时它会打印出 nil 输出。我在互联网指南中找到了这个:

let resultArray = (
          boundingBox: [Float](unsafeData: outputBoundingBox.data) ??
)

但是 Float unsafedata 函数告诉我它不存在。这是我的代码:

import UIKit
import TensorFlowLite
import SwiftyJSON

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        var opt1 = Interpreter.Options()
        opt1.threadCount = 2
        let model_path = Bundle.main.path(forResource: "model1", ofType: "tflite")
        let int1 = try? Interpreter(modelPath: model_path!, options: opt1)
        try? int1?.allocateTensors()

        let input = try? int1?.input(at: 0).shape

        if let filepath = Bundle.main.path(forResource: "numpytest", ofType: "txt") {
            do {
                let contents = try String(contentsOfFile: filepath)
                let data2 = Data(filepath.utf8)            
                try? int1!.copy(data2, toInputAt: 0)
                try? int1?.invoke()
                let salida = try? int1?.output(at: 0)
            } catch {
                // contents could not be loaded
            }
        } else {
            // example.txt not found!
        }
    }
}

我做错了什么,我需要添加什么?

有没有人有任何意见或建议?

问候!

标签: iosswifttensorflowtensorflow-lite

解决方案


推荐阅读