ผสานรวมตัวแยกประเภทรูปภาพ

การจัดประเภทรูปภาพเป็นการใช้งานทั่วไปของแมชชีนเลิร์นนิงเพื่อระบุว่า โดยใช้รูปภาพแทน ตัวอย่างเช่น เราอาจต้องการทราบว่าแสดงสัตว์ประเภทใด ในภาพหนึ่งๆ งานที่ต้องคาดคะเนสิ่งที่รูปภาพแสดงเรียกว่าอะไร การจำแนกประเภทรูปภาพ ตัวแยกประเภทรูปภาพได้รับการฝึกให้จดจำ คลาสของรูปภาพ เช่น โมเดลอาจได้รับการฝึกให้จดจำรูปภาพ ที่แสดงสัตว์ 3 ประเภท คือ กระต่าย แฮมสเตอร์ และสุนัข โปรดดู เวลา ตัวอย่างการจัดประเภทรูปภาพ เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับตัวแยกประเภทรูปภาพ

ใช้ API ของไลบรารีงาน ImageClassifier เพื่อทำให้อิมเมจที่กำหนดเองใช้งานได้ ตัวแยกประเภทหรือตัวแยกประเภทที่ฝึกไว้แล้วล่วงหน้าลงในแอปบนอุปกรณ์เคลื่อนที่ของคุณ

คุณลักษณะที่สำคัญของ ImageClassifier API

  • การประมวลผลรูปภาพที่ป้อน รวมถึงการหมุน การปรับขนาด และพื้นที่สี Conversion

  • ภูมิภาคที่สนใจของรูปภาพอินพุต

  • ติดป้ายกำกับภาษาบนแผนที่

  • เกณฑ์คะแนนเพื่อกรองผลลัพธ์

  • ผลลัพธ์การจัดประเภทที่สำคัญ

  • ติดป้ายกำกับรายการที่อนุญาตและรายการที่ปฏิเสธ

รูปแบบตัวแยกประเภทรูปภาพที่รองรับ

รุ่นต่อไปนี้ได้รับการรับประกันว่าใช้งานร่วมกับ ImageClassifier ได้ API

เรียกใช้การอนุมานใน Java

โปรดดู แอปอ้างอิงการจัดประเภทรูปภาพ สำหรับตัวอย่างวิธีใช้ ImageClassifier ในแอป Android

ขั้นตอนที่ 1: นำเข้าการอ้างอิง Gradle และการตั้งค่าอื่นๆ

คัดลอกไฟล์โมเดล .tflite ไปยังไดเรกทอรี Asset ของโมดูล 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
    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: ติดตั้งทรัพยากร Dependency

ไลบรารีงานรองรับการติดตั้งโดยใช้ CocoaPods ตรวจสอบว่า CocoaPods ติดตั้งอยู่ในระบบของคุณ โปรดดู คู่มือการติดตั้ง CocoaPods สำหรับคำแนะนำ

โปรดดู คู่มือ CocoaPods สำหรับ รายละเอียดการเพิ่มพ็อดลงในโปรเจ็กต์ Xcode

เพิ่มพ็อด TensorFlowLiteTaskVision ใน Podfile

target 'MyAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskVision'
end

ตรวจสอบว่าโมเดล .tflite ที่คุณจะใช้สำหรับการอนุมานมีอยู่ใน App Bundle ของคุณ

ขั้นตอนที่ 2: การใช้โมเดล

Swift

// 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)

Objective C

// 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

โมเดลตัวแยกประเภทรูปภาพที่ใช้งานร่วมกันได้ควรเป็นไปตามข้อกำหนดต่อไปนี้

  • Tensor รูปภาพอินพุต (kTfLiteUInt8/kTfLiteFloat32)

    • อินพุตภาพขนาด [batch x height x width x channels]
    • ไม่รองรับการอนุมานแบบกลุ่ม (batch ต้องเป็น 1)
    • รองรับอินพุต RGB เท่านั้น (channels ต้องเป็น 3)
    • หากประเภทคือ kTfLiteFloat32 จะต้องระบุ BottomizationOptions ซึ่งแนบมากับข้อมูลเมตา เพื่อการปรับอินพุตให้เป็นมาตรฐาน
  • tensor คะแนนเอาต์พุต (kTfLiteUInt8/kTfLiteFloat32)

    • ที่มีคลาส N และมิติข้อมูล 2 หรือ 4 รายการ นั่นคือ [1 x N] หรือ [1 x 1 x 1 x N]
    • แมปป้ายกำกับที่ไม่บังคับ (แต่แนะนำ) เป็น AssociatedFile-s พร้อมประเภท TENSOR_AXIS_LABELS ที่มีป้ายกำกับ 1 รายการต่อบรรทัด โปรดดู example label file ไฟล์ AssociatedFile แรก (หากมี) จะใช้กรอกในช่อง label (ชื่อว่า class_name ใน C++) ของผลลัพธ์ ช่อง display_name ได้รับมาจาก AssociatedFile (หากมี) ซึ่งมีภาษาตรงกับ ฟิลด์ display_names_locale ของ ImageClassifierOptions ใช้ที่ เวลาที่สร้าง ("en" โดยค่าเริ่มต้น เช่น ภาษาอังกฤษ) ถ้าไม่มีลิงก์ที่กล่าวมา พร้อมใช้งาน ระบบจะเติมเฉพาะฟิลด์ index ของผลลัพธ์