公共最终类
口译
驱动程序类,通过 TensorFlow Lite 推动模型推断。
注意:如果您不需要访问任何“实验性”以下 API 功能,建议使用 InterpreterApi 和 InterpreterFactory,而不是直接使用 Interpreter。
Interpreter
封装了预训练的 TensorFlow Lite 模型,其中操作
以进行模型推断。
例如,如果模型仅接受一个输入并仅返回一个输出:
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.run(input, output);
}
如果模型接受多个输入或输出:
Object[] inputs = {input0, input1, ...};
Map<Integer, Object> map_of_indices_to_outputs = new HashMap<>();
FloatBuffer ith_output = FloatBuffer.allocateDirect(3 * 2 * 4); // Float tensor, shape 3x2x4.
ith_output.order(ByteOrder.nativeOrder());
map_of_indices_to_outputs.put(i, ith_output);
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs);
}
如果模型采用或生成字符串张量:
String[] input = {"foo", "bar"}; // Input tensor shape is [2].
String[][] output = new String[3][2]; // Output tensor shape is [3, 2].
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.runForMultipleInputsOutputs(input, output);
}
请注意,形状 [] 和形状 [1] 是有区别的。对于标量字符串张量 输出:
String[] input = {"foo"}; // Input tensor shape is [1].
ByteBuffer outputBuffer = ByteBuffer.