סיווג תמונה הוא שימוש נפוץ בלמידת מכונה כדי לזהות מה תמונה מייצגת. לדוגמה, אולי נרצה לדעת איזה סוג של חיה מופיע בתמונה נתונה. המשימה של חיזוי מה תמונה מייצגת נקראת סיווג תמונה . מסווג תמונות מאומן לזהות סוגים שונים של תמונות. לדוגמה, מודל עשוי להיות מאומן לזהות תמונות המייצגות שלושה סוגים שונים של בעלי חיים: ארנבות, אוגרים וכלבים. עיין בסקירת סיווג התמונות למידע נוסף על מסווגי תמונות.
השתמש בממשק ה-API של ספריית המשימות ImageClassifier
כדי לפרוס את סווגי התמונות המותאמים אישית שלך או אלה שהוכשרו מראש באפליקציות לנייד שלך.
תכונות עיקריות של ה-API של ImageClassifier
עיבוד תמונה של קלט, כולל סיבוב, שינוי גודל והמרת מרחב צבע.
אזור העניין של תמונת הקלט.
תווית אזור מפה.
ציון סף לסינון תוצאות.
תוצאות הסיווג העליון-k.
תווית רשימת היתרים ורשימת דחייה.
דגמי סיווג תמונות נתמכים
הדגמים הבאים מובטחים להיות תואמים ל- ImageClassifier
API.
מודלים שנוצרו על ידי TensorFlow Lite Model Maker עבור סיווג תמונות .
דגמים שנוצרו על ידי AutoML Vision Edge Image Classification .
דגמים מותאמים אישית העומדים בדרישות תאימות המודל .
הפעל הסקה ב-Java
עיין באפליקציית ההפניה לסיווג תמונה לקבלת דוגמה לשימוש ImageClassifier
באפליקציית אנדרואיד.
שלב 1: ייבא תלות Gradle והגדרות אחרות
העתק את קובץ מודל .tflite
לספריית הנכסים של מודול האנדרואיד שבו המודל יופעל. ציין שאין לדחוס את הקובץ, והוסף את ספריית 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: התקן את התלות
ספריית המשימות תומכת בהתקנה באמצעות CocoaPods. ודא ש-CocoaPods מותקן במערכת שלך. אנא עיין במדריך ההתקנה של CocoaPods לקבלת הוראות.
אנא עיין במדריך CocoaPods לפרטים על הוספת פודים לפרויקט Xcode.
הוסף את הפוד TensorFlowLiteTaskVision
ב-Podfile.
target 'MyAppWithTaskAPI' do
use_frameworks!
pod 'TensorFlowLiteTaskVision'
end
ודא שמודל .tflite
שבו תשתמש להסקת מסקנות קיים בחבילת האפליקציות שלך.
שלב 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 עם מודל ונתוני בדיקה משלך.
דרישות תאימות לדגם
ה-API ImageClassifier
מצפה למודל TFLite עם מטא נתונים חובה של מודל TFLite . ראה דוגמאות ליצירת מטא נתונים עבור מסווגי תמונות באמצעות TensorFlow Lite Metadata Writer API .
דגמי סיווג התמונות התואמים צריכים לעמוד בדרישות הבאות:
טנזור תמונה קלט (kTfLiteUInt8/kTfLiteFloat32)
- קלט תמונה בגודל
[batch x height x width x channels]
. - הסקת אצווה אינה נתמכת (
batch
נדרשת להיות 1). - רק כניסות RGB נתמכות (
channels
נדרשים להיות 3). - אם הסוג הוא kTfLiteFloat32, יש צורך לצרף את אפשרויות הנורמליזציה למטא נתונים לצורך נורמליזציה של קלט.
- קלט תמונה בגודל
טנזור ציון פלט (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
של התוצאות יתמלא.
- עם