如果您的应用使用自定义TensorFlow Lite模型,您可以使用 Firebase ML 来部署您的模型。通过使用 Firebase 部署模型,您可以减少应用的初始下载大小并更新应用的 ML 模型,而无需发布应用的新版本。而且,通过远程配置和 A/B 测试,您可以为不同的用户组动态地提供不同的模型。
先决条件
MLModelDownloader
库仅适用于 Swift。- TensorFlow Lite 只能在使用 iOS 9 及更新版本的设备上运行。
TensorFlow Lite 模型
TensorFlow Lite 模型是经过优化以在移动设备上运行的 ML 模型。要获取 TensorFlow Lite 模型:
在你开始之前
要将 TensorFlowLite 与 Firebase 一起使用,您必须使用 CocoaPods,因为 TensorFlowLite 目前不支持使用 Swift 包管理器进行安装。有关如何安装MLModelDownloader
的说明,请参阅CocoaPods 安装指南。
安装后,导入 Firebase 和 TensorFlowLite 以使用它们。
迅速
import FirebaseMLModelDownloader
import TensorFlowLite
1. 部署你的模型
使用 Firebase 控制台或 Firebase Admin Python 和 Node.js SDK 部署您的自定义 TensorFlow 模型。请参阅部署和管理自定义模型。
将自定义模型添加到 Firebase 项目后,您可以使用您指定的名称在应用中引用该模型。您可以随时部署新的 TensorFlow Lite 模型,并通过调用getModel()
将新模型下载到用户的设备上(见下文)。
2. 将模型下载到设备并初始化 TensorFlow Lite 解释器
要在您的应用中使用您的 TensorFlow Lite 模型,请首先使用 Firebase ML SDK 将最新版本的模型下载到设备中。要开始模型下载,请调用模型下载器的getModel()
方法,指定上传模型时指定的名称、是否要始终下载最新模型以及允许下载的条件。
您可以从三种下载行为中进行选择:
下载类型 | 描述 |
---|---|
localModel | 从设备获取本地模型。如果没有可用的本地模型,则其行为类似于latestModel 。如果您对检查模型更新不感兴趣,请使用此下载类型。例如,您正在使用远程配置来检索模型名称,并且您总是以新名称上传模型(推荐)。 |
localModelUpdateInBackground | 从设备获取本地模型并开始在后台更新模型。如果没有可用的本地模型,则其行为类似于latestModel 。 |
latestModel | 获取最新型号。如果本地模型是最新版本,则返回本地模型。否则,请下载最新型号。在下载最新版本之前,此行为将被阻止(不推荐)。仅在您明确需要最新版本的情况下使用此行为。 |
您应该禁用与模型相关的功能(例如,灰显或隐藏部分 UI),直到您确认模型已下载。
迅速
let conditions = ModelDownloadConditions(allowsCellularAccess: false)
ModelDownloader.modelDownloader()
.getModel(name: "your_model",
downloadType: .localModelUpdateInBackground,
conditions: conditions) { result in
switch (result) {
case .success(let customModel):
do {
// Download complete. Depending on your app, you could enable the ML
// feature, or switch from the local model to the remote model, etc.
// The CustomModel object contains the local path of the model file,
// which you can use to instantiate a TensorFlow Lite interpreter.
let interpreter = try Interpreter(modelPath: customModel.path)
} catch {
// Error. Bad model file?
}
case .failure(let error):
// Download was unsuccessful. Don't enable ML features.
print(error)
}
}
许多应用程序在其初始化代码中启动下载任务,但您可以在需要使用模型之前的任何时候这样做。
3. 对输入数据进行推理
获取模型的输入和输出形状
TensorFlow Lite 模型解释器将一个或多个多维数组作为输入并生成输出。这些数组包含byte
、 int
、 long
或float
值。在将数据传递给模型或使用其结果之前,您必须知道模型使用的数组的数量和维度(“形状”)。
如果您自己构建了模型,或者模型的输入和输出格式已记录在案,您可能已经拥有此信息。如果您不知道模型输入和输出的形状和数据类型,您可以使用 TensorFlow Lite 解释器来检查您的模型。例如:
Python
import tensorflow as tf interpreter = tf.lite.Interpreter(model_path="your_model.tflite") interpreter.allocate_tensors() # Print input shape and type inputs = interpreter.get_input_details() print('{} input(s):'.format(len(inputs))) for i in range(0, len(inputs)): print('{} {}'.format(inputs[i]['shape'], inputs[i]['dtype'])) # Print output shape and type outputs = interpreter.get_output_details() print('\n{} output(s):'.format(len(outputs))) for i in range(0, len(outputs)): print('{} {}'.format(outputs[i]['shape'], outputs[i]['dtype']))
示例输出:
1 input(s): [ 1 224 224 3] <class 'numpy.float32'> 1 output(s): [1 1000] <class 'numpy.float32'>
运行解释器
确定模型输入和输出的格式后,获取输入数据并对数据执行任何必要的转换,以获得模型正确形状的输入。例如,如果您的模型处理图像,并且您的模型具有[1, 224, 224, 3]
浮点值的输入尺寸,则您可能必须将图像的颜色值缩放到浮点范围,如下例所示:
迅速
let image: CGImage = // Your input image
guard let context = CGContext(
data: nil,
width: image.width, height: image.height,
bitsPerComponent: 8, bytesPerRow: image.width * 4,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
) else {
return false
}
context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height))
guard let imageData = context.data else { return false }
var inputData = Data()
for row in 0 ..< 224 {
for col in 0 ..< 224 {
let offset = 4 * (row * context.width + col)
// (Ignore offset 0, the unused alpha channel)
let red = imageData.load(fromByteOffset: offset+1, as: UInt8.self)
let green = imageData.load(fromByteOffset: offset+2, as: UInt8.self)
let blue = imageData.load(fromByteOffset: offset+3, as: UInt8.self)
// Normalize channel values to [0.0, 1.0]. This requirement varies
// by model. For example, some models might require values to be
// normalized to the range [-1.0, 1.0] instead, and others might
// require fixed-point values or the original bytes.
var normalizedRed = Float32(red) / 255.0
var normalizedGreen = Float32(green) / 255.0
var normalizedBlue = Float32(blue) / 255.0
// Append normalized values to Data object in RGB order.
let elementSize = MemoryLayout.size(ofValue: normalizedRed)
var bytes = [UInt8](repeating: 0, count: elementSize)
memcpy(&bytes, &normalizedRed, elementSize)
inputData.append(&bytes, count: elementSize)
memcpy(&bytes, &normalizedGreen, elementSize)
inputData.append(&bytes, count: elementSize)
memcpy(&ammp;bytes, &normalizedBlue, elementSize)
inputData.append(&bytes, count: elementSize)
}
}
然后,将输入的NSData
复制到解释器并运行它:
迅速
try interpreter.allocateTensors()
try interpreter.copy(inputData, toInputAt: 0)
try interpreter.invoke()
您可以通过调用解释器的output(at:)
方法来获取模型的输出。您如何使用输出取决于您使用的模型。
例如,如果您正在执行分类,作为下一步,您可能会将结果的索引映射到它们所代表的标签:
迅速
let output = try interpreter.output(at: 0)
let probabilities =
UnsafeMutableBufferPointer<Float32>.allocate(capacity: 1000)
output.data.copyBytes(to: probabilities)
guard let labelPath = Bundle.main.path(forResource: "retrained_labels", ofType: "txt") else { return }
let fileContents = try? String(contentsOfFile: labelPath)
guard let labels = fileContents?.components(separatedBy: "\n") else { return }
for i in labels.indices {
print("\(labels[i]): \(probabilities[i])")
}
附录:模型安全
无论您如何使 TensorFlow Lite 模型可用于 Firebase ML,Firebase ML 都会以标准序列化 protobuf 格式将它们存储在本地存储中。
理论上,这意味着任何人都可以复制您的模型。然而,在实践中,大多数模型都是特定于应用程序的,并且被优化混淆了,其风险类似于竞争对手反汇编和重用代码的风险。不过,在您的应用程序中使用自定义模型之前,您应该意识到这种风险。