การจัดประเภทรูปภาพเป็นการใช้การเรียนรู้ของเครื่องโดยทั่วไปเพื่อระบุว่ารูปภาพนั้นสื่อถึงอะไร ตัวอย่างเช่น เราอาจต้องการทราบว่าสัตว์ชนิดใดที่ปรากฏในภาพที่กำหนด งานของการทำนายว่าภาพแสดงถึงอะไรเรียกว่า การจำแนกภาพ ลักษณนามของรูปภาพได้รับการฝึกฝนให้จดจำคลาสต่างๆ ของรูปภาพ ตัวอย่างเช่น นางแบบอาจได้รับการฝึกฝนให้จดจำภาพถ่ายที่เป็นตัวแทนของสัตว์สามประเภทที่แตกต่างกัน: กระต่าย หนูแฮมสเตอร์ และสุนัข ดูภาพ รวมการจัดประเภทรูปภาพ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับตัวแยกประเภทรูปภาพ
ใช้ Task Library ImageClassifier
API เพื่อปรับใช้ตัวแยกประเภทรูปภาพที่คุณกำหนดเองหรือตัวแยกประเภทที่ได้รับการฝึกอบรมล่วงหน้าในแอปมือถือของคุณ
คุณสมบัติที่สำคัญของ ImageClassifier API
ป้อนการประมวลผลภาพ รวมทั้งการหมุน การปรับขนาด และการแปลงพื้นที่สี
ภูมิภาคที่สนใจของภาพอินพุต
ป้ายกำกับแผนที่โลแคล
เกณฑ์คะแนนเพื่อกรองผลลัพธ์
ผลการจำแนกประเภท Top-k
ป้ายรายการที่อนุญาตและรายการที่ไม่อนุญาต
โมเดลตัวแยกประเภทรูปภาพที่รองรับ
รุ่นต่อไปนี้รับประกันว่าเข้ากันได้กับ ImageClassifier
API
โมเดลที่สร้างโดย TensorFlow Lite Model Maker สำหรับการจำแนกรูปภาพ
โมเดลที่สร้างโดย AutoML Vision Edge Image Classification
โมเดลแบบกำหนดเองที่ตรงตาม ข้อกำหนดความเข้ากันได้ของโมเดล
เรียกใช้การอนุมานใน Java
ดู แอปอ้างอิงการจัดประเภทรูปภาพ สำหรับตัวอย่างวิธีใช้ ImageClassifier
ในแอป Android
ขั้นตอนที่ 1: นำเข้าการอ้างอิง Gradle และการตั้งค่าอื่นๆ
คัดลอกไฟล์โมเดล .tflite
ไปยังไดเร็กทอรีสินทรัพย์ของโมดูล Android ที่จะเรียกใช้โมเดล ระบุว่าไม่ควรบีบอัดไฟล์ และเพิ่มไลบรารี TensorFlow Lite ลงในไฟล์ build.gradle
ของโมดูล:
android {
// Other settings
// Specify tflite file should not be compressed for the app apk
aaptOptions {
noCompress "tflite"
}
}
dependencies {
// Other dependencies
// Import the Task Vision Library dependency (NNAPI is included)
implementation 'org.tensorflow:tensorflow-lite-task-vision'
// Import the GPU delegate plugin Library for GPU inference
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
ขั้นตอนที่ 2: การใช้แบบจำลอง
// Initialization
ImageClassifierOptions options =
ImageClassifierOptions.builder()
.setBaseOptions(BaseOptions.builder().useGpu().build())
.setMaxResults(1)
.build();
ImageClassifier imageClassifier =
ImageClassifier.createFromFileAndOptions(
context, modelFile, options);
// Run inference
List<Classifications> results = imageClassifier.classify(image);
ดู ซอร์สโค้ดและ javadoc สำหรับตัวเลือกเพิ่มเติมในการกำหนดค่า ImageClassifier
เรียกใช้การอนุมานใน iOS
ขั้นตอนที่ 1: ติดตั้งการพึ่งพา
Task Library รองรับการติดตั้งโดยใช้ CocoaPods ตรวจสอบให้แน่ใจว่าได้ติดตั้ง CocoaPods ในระบบของคุณแล้ว โปรดดู คู่มือการติดตั้ง CocoaPods สำหรับคำแนะนำ
โปรดดู คู่มือ CocoaPods สำหรับรายละเอียดเกี่ยวกับการเพิ่มพ็อดในโครงการ Xcode
เพิ่มพ็อด TensorFlowLiteTaskVision
ใน Podfile
target 'MyAppWithTaskAPI' do
use_frameworks!
pod 'TensorFlowLiteTaskVision'
end
ตรวจสอบให้แน่ใจว่ามีโมเดล .tflite
ที่คุณจะใช้ในการอนุมานอยู่ใน App Bundle ของคุณ
ขั้นตอนที่ 2: การใช้แบบจำลอง
สวิฟต์
// Imports
import TensorFlowLiteTaskVision
// Initialization
guard let modelPath = Bundle.main.path(forResource: "birds_V1",
ofType: "tflite") else { return }
let options = ImageClassifierOptions(modelPath: modelPath)
// Configure any additional options:
// options.classificationOptions.maxResults = 3
let classifier = try ImageClassifier.classifier(options: options)
// Convert the input image to MLImage.
// There are other sources for MLImage. For more details, please see:
// https://developers.google.com/ml-kit/reference/ios/mlimage/api/reference/Classes/GMLImage
guard let image = UIImage (named: "sparrow.jpg"), let mlImage = MLImage(image: image) else { return }
// Run inference
let classificationResults = try classifier.classify(mlImage: mlImage)
วัตถุประสงค์ ค
// Imports
#import <TensorFlowLiteTaskVision/TensorFlowLiteTaskVision.h>
// Initialization
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"birds_V1" ofType:@"tflite"];
TFLImageClassifierOptions *options =
[[TFLImageClassifierOptions alloc] initWithModelPath:modelPath];
// Configure any additional options:
// options.classificationOptions.maxResults = 3;
TFLImageClassifier *classifier = [TFLImageClassifier imageClassifierWithOptions:options
error:nil];
// Convert the input image to MLImage.
UIImage *image = [UIImage imageNamed:@"sparrow.jpg"];
// There are other sources for GMLImage. For more details, please see:
// https://developers.google.com/ml-kit/reference/ios/mlimage/api/reference/Classes/GMLImage
GMLImage *gmlImage = [[GMLImage alloc] initWithImage:image];
// Run inference
TFLClassificationResult *classificationResult =
[classifier classifyWithGMLImage:gmlImage error:nil];
ดู ซอร์สโค้ด สำหรับตัวเลือกเพิ่มเติมในการกำหนดค่า TFLImageClassifier
เรียกใช้การอนุมานใน Python
ขั้นตอนที่ 1: ติดตั้งแพ็คเกจ pip
pip install tflite-support
ขั้นตอนที่ 2: การใช้แบบจำลอง
# Imports
from tflite_support.task import vision
from tflite_support.task import core
from tflite_support.task import processor
# Initialization
base_options = core.BaseOptions(file_name=model_path)
classification_options = processor.ClassificationOptions(max_results=2)
options = vision.ImageClassifierOptions(base_options=base_options, classification_options=classification_options)
classifier = vision.ImageClassifier.create_from_options(options)
# Alternatively, you can create an image classifier in the following manner:
# classifier = vision.ImageClassifier.create_from_file(model_path)
# Run inference
image = vision.TensorImage.create_from_file(image_path)
classification_result = classifier.classify(image)
ดู ซอร์สโค้ด สำหรับตัวเลือกเพิ่มเติมในการกำหนดค่า ImageClassifier
เรียกใช้การอนุมานใน C ++
// Initialization
ImageClassifierOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();
// Create input frame_buffer from your inputs, `image_data` and `image_dimension`.
// See more information here: tensorflow_lite_support/cc/task/vision/utils/frame_buffer_common_utils.h
std::unique_ptr<FrameBuffer> frame_buffer = CreateFromRgbRawBuffer(
image_data, image_dimension);
// Run inference
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();
ดู ซอร์สโค้ด สำหรับตัวเลือกเพิ่มเติมในการกำหนดค่า ImageClassifier
ตัวอย่างผลลัพธ์
นี่คือตัวอย่างผลการจำแนก ประเภทนก
Results:
Rank #0:
index : 671
score : 0.91406
class name : /m/01bwb9
display name: Passer domesticus
Rank #1:
index : 670
score : 0.00391
class name : /m/01bwbt
display name: Passer montanus
Rank #2:
index : 495
score : 0.00391
class name : /m/0bwm6m
display name: Passer italiae
ลองใช้ เครื่องมือสาธิต CLI อย่างง่ายสำหรับ ImageClassifier ด้วยแบบจำลองและข้อมูลการทดสอบของคุณเอง
ข้อกำหนดความเข้ากันได้ของโมเดล
ImageClassifier
API ต้องการโมเดล TFLite ที่มี ข้อมูลเมตาของโมเดล TFLite ที่บังคับ ดูตัวอย่างการสร้างข้อมูลเมตาสำหรับตัวแยกประเภทรูปภาพโดยใช้ TensorFlow Lite Metadata Writer API
โมเดลตัวแยกประเภทภาพที่เข้ากันได้ควรเป็นไปตามข้อกำหนดต่อไปนี้:
อินพุตอิมเมจเทนเซอร์ (kTfLiteUInt8/kTfLiteFloat32)
- อินพุตรูปภาพขนาด
[batch x height x width x channels]
- ไม่สนับสนุนการอนุมานแบบแบทช์ (
batch
จำเป็นต้องเป็น 1) - รองรับอินพุต RGB เท่านั้น (
channels
ต้องเป็น 3) - หากประเภทคือ kTfLiteFloat32 จำเป็นต้องแนบ NormalizationOptions กับข้อมูลเมตาสำหรับการปรับอินพุตให้เป็นมาตรฐาน
- อินพุตรูปภาพขนาด
เทนเซอร์คะแนนเอาต์พุต (kTfLiteUInt8/kTfLiteFloat32)
- ด้วย
N
คลาสและ 2 หรือ 4 มิติ เช่น[1 x N]
หรือ[1 x 1 x 1 x N]
- แมปป้ายกำกับเพิ่มเติม (แต่แนะนำ) เป็น AssociatedFile-s ที่มีประเภท TENSOR_AXIS_LABELS โดยมีหนึ่งป้ายกำกับต่อบรรทัด ดู ตัวอย่างไฟล์ฉลาก AssociatedFile แรกดังกล่าว (ถ้ามี) ใช้เพื่อเติมฟิลด์ป้าย
label
(ชื่อclass_name
ใน C++) ของผลลัพธ์ ฟิลด์display_name
ถูกเติมจาก AssociatedFile (ถ้ามี) ซึ่งโลแคลตรงกับฟิลด์display_names_locale
ของImageClassifierOptions
ที่ใช้ในขณะสร้าง ("en" ตามค่าดีฟอลต์ เช่น ภาษาอังกฤษ) หากไม่มีข้อมูลเหล่านี้ ระบบจะเติมเฉพาะฟิลด์index
ของผลลัพธ์เท่านั้น
- ด้วย