คำว่า อนุมาน หมายถึงกระบวนการดำเนินการโมเดล TensorFlow Lite บนอุปกรณ์เพื่อทำการคาดคะเนตามข้อมูลอินพุต ในการอนุมานด้วยโมเดล TensorFlow Lite คุณต้องเรียกใช้ผ่าน ล่าม ล่าม TensorFlow Lite ได้รับการออกแบบมาให้มีความบางและรวดเร็ว ล่ามใช้การเรียงลำดับกราฟแบบคงที่และตัวจัดสรรหน่วยความจำแบบกำหนดเอง (ไดนามิกน้อยกว่า) เพื่อให้แน่ใจว่ามีการโหลด การเริ่มต้น และเวลาแฝงในการดำเนินการน้อยที่สุด
หน้านี้อธิบายวิธีเข้าถึงล่าม TensorFlow Lite และทำการอนุมานโดยใช้ C++, Java และ Python รวมถึงลิงก์ไปยังทรัพยากรอื่นๆ สำหรับแต่ละ แพลตฟอร์มที่รองรับ
แนวคิดที่สำคัญ
การอนุมาน TensorFlow Lite มักทำตามขั้นตอนต่อไปนี้:
กำลังโหลดโมเดล
คุณต้องโหลดโมเดล
.tflite
ลงในหน่วยความจำ ซึ่งมีกราฟการดำเนินการของโมเดลการแปลงข้อมูล
ข้อมูลอินพุตดิบสำหรับโมเดลโดยทั่วไปไม่ตรงกับรูปแบบข้อมูลอินพุตที่โมเดลคาดหวัง ตัวอย่างเช่น คุณอาจต้องปรับขนาดรูปภาพหรือเปลี่ยนรูปแบบรูปภาพเพื่อให้เข้ากันได้กับโมเดล
การอนุมานการวิ่ง
ขั้นตอนนี้เกี่ยวข้องกับการใช้ TensorFlow Lite API เพื่อดำเนินการกับโมเดล มันเกี่ยวข้องกับสองสามขั้นตอน เช่น การสร้างล่าม และการจัดสรรเทนเซอร์ ตามที่อธิบายไว้ในส่วนต่อไปนี้
ล่ามเอาท์พุท
เมื่อคุณได้รับผลลัพธ์จากการอนุมานแบบจำลอง คุณต้องตีความเทนเซอร์อย่างมีความหมายซึ่งเป็นประโยชน์ในแอปพลิเคชันของคุณ
ตัวอย่างเช่น โมเดลอาจส่งคืนเฉพาะรายการความน่าจะเป็น การทำแผนที่ความน่าจะเป็นกับหมวดหมู่ที่เกี่ยวข้องและนำเสนอต่อผู้ใช้ปลายทางของคุณนั้นขึ้นอยู่กับคุณ
แพลตฟอร์มที่รองรับ
API การอนุมาน TensorFlow มีให้สำหรับแพลตฟอร์มมือถือ/ฝังตัวทั่วไปส่วนใหญ่ เช่น Android , iOS และ Linux ในภาษาการเขียนโปรแกรมหลายภาษา
ในกรณีส่วนใหญ่ การออกแบบ API สะท้อนให้เห็นความต้องการประสิทธิภาพมากกว่าการใช้งานง่าย TensorFlow Lite ได้รับการออกแบบมาเพื่อการอนุมานที่รวดเร็วบนอุปกรณ์ขนาดเล็ก ดังนั้นจึงไม่น่าแปลกใจที่ API จะพยายามหลีกเลี่ยงสำเนาที่ไม่จำเป็นโดยเสียความสะดวก ในทำนองเดียวกัน ความสอดคล้องกับ TensorFlow API ไม่ใช่เป้าหมายที่ชัดเจนและคาดว่าจะมีความแตกต่างระหว่างภาษา
ในไลบรารีทั้งหมด TensorFlow Lite API ช่วยให้คุณสามารถโหลดโมเดล ฟีดอินพุต และดึงเอาต์พุตการอนุมานได้
แพลตฟอร์ม Android
บน Android การอนุมาน TensorFlow Lite สามารถทำได้โดยใช้ Java หรือ C++ API Java API ให้ความสะดวกและสามารถใช้ได้โดยตรงภายในคลาสกิจกรรม Android ของคุณ C++ API มีความยืดหยุ่นและความเร็วมากกว่า แต่อาจต้องเขียน JNI wrappers เพื่อย้ายข้อมูลระหว่างเลเยอร์ Java และ C++
ดูรายละเอียดเกี่ยวกับการใช้ C++ และ Java ด้านล่าง หรือทำตาม Android Quickstart เพื่อดูบทแนะนำและโค้ดตัวอย่าง
เครื่องสร้างโค้ด wrapper ของ Android TensorFlow Lite
สำหรับโมเดล TensorFlow Lite ที่ปรับปรุงด้วย ข้อมูลเมตา นักพัฒนาสามารถใช้ตัวสร้างโค้ดแรปเปอร์ของ TensorFlow Lite Android เพื่อสร้างโค้ดแรปเปอร์เฉพาะของแพลตฟอร์ม รหัส wrapper ไม่จำเป็นต้องโต้ตอบโดยตรงกับ ByteBuffer
บน Android นักพัฒนาสามารถโต้ตอบกับโมเดล TensorFlow Lite กับวัตถุประเภทเช่น Bitmap
และ Rect
ได้ สำหรับข้อมูลเพิ่มเติม โปรดดูที่ เครื่องสร้างโค้ด wrapper ของ Android TensorFlow Lite
แพลตฟอร์ม iOS
บน iOS TensorFlow Lite สามารถใช้ได้กับไลบรารี iOS ดั้งเดิมที่เขียนด้วย Swift และ Objective-C คุณยังสามารถใช้ C API ได้โดยตรงในโค้ด Objective-C
ดูรายละเอียดเกี่ยวกับการใช้ Swift , Objective-C และ C API ด้านล่าง หรือทำตาม iOS Quickstart เพื่อดูบทแนะนำและโค้ดตัวอย่าง
แพลตฟอร์มลินุกซ์
บนแพลตฟอร์ม Linux (รวมถึง Raspberry Pi ) คุณสามารถเรียกใช้การอนุมานโดยใช้ TensorFlow Lite API ที่มีอยู่ใน C++ และ Python ดังที่แสดงในส่วนต่อไปนี้
การวิ่งโมเดล
การรันโมเดล TensorFlow Lite มีขั้นตอนง่ายๆ ไม่กี่ขั้นตอน:
- โหลดโมเดลลงในหน่วยความจำ
- สร้าง
Interpreter
ตามแบบจำลองที่มีอยู่ - ตั้งค่าเทนเซอร์อินพุต (เลือกปรับขนาดเทนเซอร์อินพุตหากไม่ต้องการขนาดที่กำหนดไว้ล่วงหน้า)
- เรียกใช้การอนุมาน
- อ่านค่าเทนเซอร์เอาต์พุต
ส่วนต่อไปนี้จะอธิบายว่าขั้นตอนเหล่านี้สามารถทำได้ในแต่ละภาษาอย่างไร
โหลดและรันโมเดลใน Java
แพลตฟอร์ม: Android
Java API สำหรับการรันการอนุมานด้วย TensorFlow Lite ได้รับการออกแบบมาเพื่อใช้กับ Android เป็นหลัก ดังนั้นจึงมีให้ใช้งานเป็นการพึ่งพาไลบรารี Android: org.tensorflow:tensorflow-lite
ใน Java คุณจะใช้คลาส Interpreter
เพื่อโหลดโมเดลและการอนุมานโมเดลของไดรฟ์ ในหลายกรณี นี่อาจเป็น API เดียวที่คุณต้องการ
คุณสามารถเริ่มต้น Interpreter
โดยใช้ไฟล์ .tflite
:
public Interpreter(@NotNull File modelFile);
หรือด้วย MappedByteBuffer
:
public Interpreter(@NotNull MappedByteBuffer mappedByteBuffer);
ในทั้งสองกรณี คุณต้องระบุโมเดล TensorFlow Lite ที่ถูกต้อง มิฉะนั้น API จะส่ง IllegalArgumentException
หากคุณใช้ MappedByteBuffer
เพื่อเริ่มต้น Interpreter
จะต้องไม่เปลี่ยนแปลงตลอดอายุของ Interpreter
วิธีที่ต้องการในการอนุมานบนโมเดลคือการใช้ลายเซ็น - มีให้สำหรับโมเดลที่แปลงโดยเริ่มต้น Tensorflow 2.5
try (Interpreter interpreter = new Interpreter(file_of_tensorflowlite_model)) {
Map<String, Object> inputs = new HashMap<>();
inputs.put("input_1", input1);
inputs.put("input_2", input2);
Map<String, Object> outputs = new HashMap<>();
outputs.put("output_1", output1);
interpreter.runSignature(inputs, outputs, "mySignature");
}
เมธอด runSignature
รับสามอาร์กิวเมนต์:
อินพุต : แมปสำหรับอินพุตจากชื่ออินพุตในลายเซ็นไปยังออบเจ็กต์อินพุต
ผลลัพธ์ : แผนที่สำหรับการแมปเอาต์พุตจากชื่อเอาต์พุตในลายเซ็นไปยังข้อมูลเอาต์พุต
ชื่อลายเซ็น [ไม่บังคับ]: ชื่อลายเซ็น (สามารถเว้นว่างไว้ได้หากรุ่นมีลายเซ็นเดียว)
อีกวิธีหนึ่งในการรันการอนุมานเมื่อโมเดลไม่มีลายเซ็นที่กำหนดไว้ เพียงเรียก Interpreter.run()
ตัวอย่างเช่น:
try (Interpreter interpreter = new Interpreter(file_of_a_tensorflowlite_model)) {
interpreter.run(input, output);
}
เมธอด run()
รับเพียงอินพุตเดียวและส่งคืนเอาต์พุตเพียงเอาต์พุตเดียว ดังนั้นหากโมเดลของคุณมีหลายอินพุตหรือหลายเอาต์พุต ให้ใช้:
interpreter.runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs);
ในกรณีนี้ แต่ละรายการใน inputs
สอดคล้องกับเทนเซอร์อินพุต และ map_of_indices_to_outputs
จับคู่ดัชนีของเทนเซอร์เอาต์พุตกับข้อมูลเอาต์พุตที่เกี่ยวข้อง
ในทั้งสองกรณี ดัชนีเทนเซอร์ควรสอดคล้องกับค่าที่คุณให้กับตัว แปลง TensorFlow Lite เมื่อคุณสร้างโมเดล โปรดทราบว่าลำดับของเทนเซอร์ใน input
ต้องตรงกับลำดับที่กำหนดให้กับตัวแปลง TensorFlow Lite
คลาส Interpreter
ยังมีฟังก์ชันที่สะดวกสำหรับคุณในการรับดัชนีของโมเดลอินพุตหรือเอาต์พุตโดยใช้ชื่อการทำงาน:
public int getInputIndex(String opName);
public int getOutputIndex(String opName);
หาก opName
ไม่ใช่การดำเนินการที่ถูกต้องในโมเดล มันจะส่ง IllegalArgumentException
ระวังด้วยว่า Interpreter
เป็นเจ้าของทรัพยากร เพื่อหลีกเลี่ยงการรั่วไหลของหน่วยความจำ ทรัพยากรจะต้องถูกปล่อยหลังจากใช้งานโดย:
interpreter.close();
สำหรับโปรเจ็กต์ตัวอย่างที่มี Java โปรดดู ตัวอย่างการจัดหมวดหมู่อิมเมจ Android
ชนิดข้อมูลที่รองรับ (ใน Java)
ในการใช้ TensorFlow Lite ชนิดข้อมูลของเทนเซอร์อินพุตและเอาต์พุตต้องเป็นหนึ่งในประเภทพื้นฐานต่อไปนี้:
-
float
-
int
-
long
-
byte
รองรับประเภท String
เช่นกัน แต่มีการเข้ารหัสที่แตกต่างจากประเภทดั้งเดิม โดยเฉพาะอย่างยิ่ง รูปร่างของสตริง เทนเซอร์จะกำหนดจำนวนและการจัดเรียงของสตริงในเทนเซอร์ โดยแต่ละองค์ประกอบจะเป็นสตริงที่มีความยาวผันแปรได้ ในแง่นี้ ขนาด (ไบต์) ของเทนเซอร์ไม่สามารถคำนวณจากรูปร่างและประเภทเพียงอย่างเดียว ดังนั้นจึงไม่สามารถระบุสตริงเป็นอาร์กิวเมนต์ ByteBuffer
แบบแบนเดียวได้
หากมีการใช้ข้อมูลประเภทอื่น รวมทั้งชนิดบรรจุกล่อง เช่น Integer
และ Float
IllegalArgumentException
จะถูกส่งออกไป
อินพุต
อินพุตแต่ละรายการควรเป็นอาร์เรย์หรืออาร์เรย์หลายมิติของประเภทพื้นฐานที่รองรับ หรือ ByteBuffer
ดิบที่มีขนาดเหมาะสม ถ้าอินพุตเป็นอาร์เรย์หรืออาร์เรย์หลายมิติ เทนเซอร์อินพุตที่เกี่ยวข้องจะถูกปรับขนาดโดยปริยายตามขนาดของอาร์เรย์ ณ เวลาอนุมาน หากอินพุตเป็น ByteBuffer ผู้เรียกควรปรับขนาดเทนเซอร์อินพุตที่เกี่ยวข้องด้วยตนเองก่อน (ผ่าน Interpreter.resizeInput()
) ก่อนรันการอนุมาน
เมื่อใช้ ByteBuffer
คุณต้องการใช้บัฟเฟอร์ไบต์โดยตรง เนื่องจากจะทำให้ Interpreter
หลีกเลี่ยงการคัดลอกที่ไม่จำเป็น หาก ByteBuffer
เป็นบัฟเฟอร์ไบต์โดยตรง ลำดับของ ByteOrder.nativeOrder() ต้องเป็น ByteOrder.nativeOrder()
หลังจากที่ใช้สำหรับการอนุมานแบบจำลองแล้ว จะต้องไม่เปลี่ยนแปลงจนกว่าการอนุมานแบบจำลองจะเสร็จสิ้น
ผลลัพธ์
แต่ละเอาต์พุตควรเป็นอาร์เรย์หรืออาร์เรย์หลายมิติของประเภทพื้นฐานที่รองรับ หรือ ByteBuffer ที่มีขนาดเหมาะสม โปรดทราบว่าบางรุ่นมีเอาต์พุตแบบไดนามิก โดยที่รูปร่างของเทนเซอร์เอาต์พุตอาจแตกต่างกันไปตามอินพุต ไม่มีวิธีตรงไปตรงมาในการจัดการกับ Java inference API ที่มีอยู่ แต่ส่วนขยายที่วางแผนไว้จะทำให้สิ่งนี้เป็นไปได้
โหลดและเรียกใช้โมเดลใน Swift
แพลตฟอร์ม: iOS
Swift API มีอยู่ใน TensorFlowLiteSwift
Pod จาก Cocoapods
ก่อนอื่น คุณต้องนำเข้าโมดูล TensorFlowLite
import TensorFlowLite
// Getting model path
guard
let modelPath = Bundle.main.path(forResource: "model", ofType: "tflite")
else {
// Error handling...
}
do {
// Initialize an interpreter with the model.
let interpreter = try Interpreter(modelPath: modelPath)
// Allocate memory for the model's input `Tensor`s.
try interpreter.allocateTensors()
let inputData: Data // Should be initialized
// input data preparation...
// Copy the input data to the input `Tensor`.
try self.interpreter.copy(inputData, toInputAt: 0)
// Run inference by invoking the `Interpreter`.
try self.interpreter.invoke()
// Get the output `Tensor`
let outputTensor = try self.interpreter.output(at: 0)
// Copy output to `Data` to process the inference results.
let outputSize = outputTensor.shape.dimensions.reduce(1, {x, y in x * y})
let outputData =
UnsafeMutableBufferPointer<Float32>.allocate(capacity: outputSize)
outputTensor.data.copyBytes(to: outputData)
if (error != nil) { /* Error handling... */ }
} catch error {
// Error handling...
}
โหลดและรันโมเดลใน Objective-C
แพลตฟอร์ม: iOS
Objective-C API มีอยู่ใน TensorFlowLiteObjC
Pod จาก Cocoapods
ก่อนอื่น คุณต้องนำเข้าโมดูล TensorFlowLite
@import TensorFlowLite;
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"model"
ofType:@"tflite"];
NSError *error;
// Initialize an interpreter with the model.
TFLInterpreter *interpreter = [[TFLInterpreter alloc] initWithModelPath:modelPath
error:&error];
if (error != nil) { /* Error handling... */ }
// Allocate memory for the model's input `TFLTensor`s.
[interpreter allocateTensorsWithError:&error];
if (error != nil) { /* Error handling... */ }
NSMutableData *inputData; // Should be initialized
// input data preparation...
// Get the input `TFLTensor`
TFLTensor *inputTensor = [interpreter inputTensorAtIndex:0 error:&error];
if (error != nil) { /* Error handling... */ }
// Copy the input data to the input `TFLTensor`.
[inputTensor copyData:inputData error:&error];
if (error != nil) { /* Error handling... */ }
// Run inference by invoking the `TFLInterpreter`.
[interpreter invokeWithError:&error];
if (error != nil) { /* Error handling... */ }
// Get the output `TFLTensor`
TFLTensor *outputTensor = [interpreter outputTensorAtIndex:0 error:&error];
if (error != nil) { /* Error handling... */ }
// Copy output to `NSData` to process the inference results.
NSData *outputData = [outputTensor dataWithError:&error];
if (error != nil) { /* Error handling... */ }
การใช้ C API ในโค้ด Objective-C
ปัจจุบัน Objective-C API ไม่รองรับผู้รับมอบสิทธิ์ หากต้องการใช้ผู้รับมอบสิทธิ์ด้วยโค้ด Objective-C คุณต้องเรียกใช้ C API พื้นฐานโดยตรง
#include "tensorflow/lite/c/c_api.h"
TfLiteModel* model = TfLiteModelCreateFromFile([modelPath UTF8String]);
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
// Create the interpreter.
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
// Allocate tensors and populate the input tensor data.
TfLiteInterpreterAllocateTensors(interpreter);
TfLiteTensor* input_tensor =
TfLiteInterpreterGetInputTensor(interpreter, 0);
TfLiteTensorCopyFromBuffer(input_tensor, input.data(),
input.size() * sizeof(float));
// Execute inference.
TfLiteInterpreterInvoke(interpreter);
// Extract the output tensor data.
const TfLiteTensor* output_tensor =
TfLiteInterpreterGetOutputTensor(interpreter, 0);
TfLiteTensorCopyToBuffer(output_tensor, output.data(),
output.size() * sizeof(float));
// Dispose of the model and interpreter objects.
TfLiteInterpreterDelete(interpreter);
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);
โหลดและเรียกใช้โมเดลใน C++
แพลตฟอร์ม: Android, iOS และ Linux
ใน C ++ โมเดลจะถูกเก็บไว้ในคลาส FlatBufferModel
มันห่อหุ้มโมเดล TensorFlow Lite และคุณสามารถสร้างได้สองสามวิธี ขึ้นอยู่กับตำแหน่งที่เก็บโมเดล:
class FlatBufferModel {
// Build a model based on a file. Return a nullptr in case of failure.
static std::unique_ptr<FlatBufferModel> BuildFromFile(
const char* filename,
ErrorReporter* error_reporter);
// Build a model based on a pre-loaded flatbuffer. The caller retains
// ownership of the buffer and should keep it alive until the returned object
// is destroyed. Return a nullptr in case of failure.
static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
const char* buffer,
size_t buffer_size,
ErrorReporter* error_reporter);
};
ตอนนี้คุณมีโมเดลเป็นอ็อบเจ็กต์ FlatBufferModel
แล้ว คุณสามารถดำเนินการกับ Interpreter
ได้ FlatBufferModel
เดียวสามารถใช้พร้อมกันโดย Interpreter
มากกว่าหนึ่งตัว
ส่วนสำคัญของ Interpreter
API แสดงอยู่ในข้อมูลโค้ดด้านล่าง ควรสังเกตว่า:
- เทนเซอร์จะแสดงด้วยจำนวนเต็ม เพื่อหลีกเลี่ยงการเปรียบเทียบสตริง (และการพึ่งพาไลบรารีสตริงแบบตายตัว)
- ล่ามจะต้องไม่สามารถเข้าถึงได้จากเธรดที่เกิดขึ้นพร้อมกัน
- การจัดสรรหน่วยความจำสำหรับเทนเซอร์อินพุตและเอาต์พุตต้องถูกทริกเกอร์โดยการเรียก
AllocateTensors()
ทันทีหลังจากปรับขนาดเทนเซอร์
การใช้งาน TensorFlow Lite ที่ง่ายที่สุดกับ C++ มีลักษณะดังนี้:
// Load the model
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(filename);
// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
// Resize input tensors, if desired.
interpreter->AllocateTensors();
float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.
interpreter->Invoke();
float* output = interpreter->typed_output_tensor<float>(0);
สำหรับโค้ดตัวอย่างเพิ่มเติม โปรดดูที่ minimal.cc
และ label_image.cc
โหลดและเรียกใช้โมเดลใน Python
แพลตฟอร์ม: Linux
Python API สำหรับการรันการอนุมานมีให้ในโมดูล tf.lite
ซึ่งส่วนใหญ่คุณต้องการเพียง tf.lite.Interpreter
เพื่อโหลดโมเดลและเรียกใช้การอนุมาน
ตัวอย่างต่อไปนี้แสดงวิธีใช้ล่าม Python เพื่อโหลดไฟล์ .tflite
และรันการอนุมานด้วยข้อมูลอินพุตแบบสุ่ม:
ขอแนะนำให้ใช้ตัวอย่างนี้หากคุณกำลังแปลงจาก SavedModel ด้วย SignatureDef ที่กำหนดไว้ มีจำหน่ายตั้งแต่ TensorFlow 2.5
class TestModel(tf.Module):
def __init__(self):
super(TestModel, self).__init__()
@tf.function(input_signature=[tf.TensorSpec(shape=[1, 10], dtype=tf.float32)])
def add(self, x):
'''
Simple method that accepts single input 'x' and returns 'x' + 4.
'''
# Name the output 'result' for convenience.
return {'result' : x + 4}
SAVED_MODEL_PATH = 'content/saved_models/test_variable'
TFLITE_FILE_PATH = 'content/test_variable.tflite'
# Save the model
module = TestModel()
# You can omit the signatures argument and a default signature name will be
# created with name 'serving_default'.
tf.saved_model.save(
module, SAVED_MODEL_PATH,
signatures={'my_signature':module.add.get_concrete_function()})
# Convert the model using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
tflite_model = converter.convert()
with open(TFLITE_FILE_PATH, 'wb') as f:
f.write(tflite_model)
# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(TFLITE_FILE_PATH)
# There is only 1 signature defined in the model,
# so it will return it by default.
# If there are multiple signatures then we can pass the name.
my_signature = interpreter.get_signature_runner()
# my_signature is callable with input as arguments.
output = my_signature(x=tf.constant([1.0], shape=(1,10), dtype=tf.float32))
# 'output' is dictionary with all outputs from the inference.
# In this case we have single output 'result'.
print(output['result'])
อีกตัวอย่างหนึ่งหากโมเดลไม่มีการกำหนด SignatureDefs
import numpy as np
import tensorflow as tf
# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Test the model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)
อีกทางเลือกหนึ่งในการโหลดโมเดลเป็นไฟล์ .tflite
ที่แปลงไว้ล่วงหน้า คุณสามารถรวมโค้ดของคุณกับ TensorFlow Lite Converter Python API ( tf.lite.TFLiteConverter
) ได้ ซึ่งทำให้คุณสามารถแปลงโมเดล TensorFlow ของคุณเป็นรูปแบบ TensorFlow Lite จากนั้น เรียกใช้การอนุมาน:
import numpy as np
import tensorflow as tf
img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
const = tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
val = img + const
out = tf.identity(val, name="out")
# Convert to TF Lite format
with tf.Session() as sess:
converter = tf.lite.TFLiteConverter.from_session(sess, [img], [out])
tflite_model = converter.convert()
# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
# Continue to get tensors and so forth, as shown above...
สำหรับโค้ดตัวอย่าง Python เพิ่มเติม โปรดดู label_image.py
การดำเนินงานที่รองรับ
TensorFlow Lite รองรับชุดย่อยของการดำเนินการ TensorFlow โดยมีข้อจำกัดบางประการ สำหรับรายการการดำเนินการและข้อจำกัดทั้งหมด โปรดดูที่ หน้า TF Lite Ops