ผสานรวมตัวแบ่งกลุ่มรูปภาพ

ตัวจัดกลุ่มรูปภาพจะคาดการณ์ว่าแต่ละพิกเซลของรูปภาพเชื่อมโยงกับ ชั้นเรียนบางชั้น ซึ่งตรงข้ามกับการตรวจจับวัตถุ ซึ่งจะตรวจหาวัตถุใน พื้นที่สี่เหลี่ยมผืนผ้า และการจัดประเภทรูปภาพ ซึ่งจะจำแนกประเภทโดยรวม รูปภาพ ดูภาพรวมการแบ่งกลุ่มรูปภาพ ตัวอย่าง เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับตัวแบ่งกลุ่มรูปภาพ

ใช้ API ของไลบรารีงาน ImageSegmenter เพื่อทำให้กลุ่มรูปภาพที่กำหนดเองใช้งานได้ หรือที่ฝึกไว้แล้วล่วงหน้า ลงในแอปบนอุปกรณ์เคลื่อนที่ของคุณ

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

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

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

  • เอาต์พุต 2 ประเภท ได้แก่ มาสก์หมวดหมู่ และมาสก์ความเชื่อมั่น

  • ป้ายกำกับสีสำหรับวัตถุประสงค์ในการแสดงผล

โมเดลการแบ่งกลุ่มรูปภาพที่รองรับ

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

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

ดูข้อมูลอ้างอิงการแบ่งกลุ่มรูปภาพ แอป สำหรับตัวอย่างวิธีใช้ ImageSegmenter ในแอป 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
ImageSegmenterOptions options =
    ImageSegmenterOptions.builder()
        .setBaseOptions(BaseOptions.builder().useGpu().build())
        .setOutputType(OutputType.CONFIDENCE_MASK)
        .build();
ImageSegmenter imageSegmenter =
    ImageSegmenter.createFromFileAndOptions(context, modelFile, options);

// Run inference
List<Segmentation> results = imageSegmenter.segment(image);

โปรดดูซอร์สโค้ดและ Javadoc เพื่อดูตัวเลือกเพิ่มเติมในการกำหนดค่า ImageSegmenter

เรียกใช้การอนุมานใน 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: "deeplabv3",
                                            ofType: "tflite") else { return }

let options = ImageSegmenterOptions(modelPath: modelPath)

// Configure any additional options:
// options.outputType = OutputType.confidenceMasks

let segmenter = try ImageSegmenter.segmenter(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: "plane.jpg"), let mlImage = MLImage(image: image) else { return }

// Run inference
let segmentationResult = try segmenter.segment(mlImage: mlImage)

Objective C

// Imports
#import <TensorFlowLiteTaskVision/TensorFlowLiteTaskVision.h>

// Initialization
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"deeplabv3" ofType:@"tflite"];

TFLImageSegmenterOptions *options =
    [[TFLImageSegmenterOptions alloc] initWithModelPath:modelPath];

// Configure any additional options:
// options.outputType = TFLOutputTypeConfidenceMasks;

TFLImageSegmenter *segmenter = [TFLImageSegmenter imageSegmenterWithOptions:options
                                                                      error:nil];

// Convert the input image to MLImage.
UIImage *image = [UIImage imageNamed:@"plane.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
TFLSegmentationResult *segmentationResult =
    [segmenter segmentWithGMLImage:gmlImage error:nil];

ดูแหล่งที่มา โค้ด เพื่อดูตัวเลือกเพิ่มเติมในการกำหนดค่า TFLImageSegmenter

เรียกใช้การอนุมานใน 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)
segmentation_options = processor.SegmentationOptions(
    output_type=processor.SegmentationOptions.output_type.CATEGORY_MASK)
options = vision.ImageSegmenterOptions(base_options=base_options, segmentation_options=segmentation_options)
segmenter = vision.ImageSegmenter.create_from_options(options)

# Alternatively, you can create an image segmenter in the following manner:
# segmenter = vision.ImageSegmenter.create_from_file(model_path)

# Run inference
image_file = vision.TensorImage.create_from_file(image_path)
segmentation_result = segmenter.segment(image_file)

ดูแหล่งที่มา โค้ด เพื่อดูตัวเลือกเพิ่มเติมในการกำหนดค่า ImageSegmenter

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

// Initialization
ImageSegmenterOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<ImageSegmenter> image_segmenter = ImageSegmenter::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 SegmentationResult result = image_segmenter->Segment(*frame_buffer).value();

ดูแหล่งที่มา โค้ด เพื่อดูตัวเลือกเพิ่มเติมในการกำหนดค่า ImageSegmenter

ตัวอย่างผลการแข่ง

นี่เป็นตัวอย่างผลการแบ่งกลุ่มของ deeplab_v3 โมเดลการแบ่งกลุ่มลูกค้าทั่วไปที่มีอยู่ใน TensorFlow Hub

เครื่องบิน

Color Legend:
 (r: 000, g: 000, b: 000):
  index       : 0
  class name  : background
 (r: 128, g: 000, b: 000):
  index       : 1
  class name  : aeroplane

# (omitting multiple lines for conciseness) ...

 (r: 128, g: 192, b: 000):
  index       : 19
  class name  : train
 (r: 000, g: 064, b: 128):
  index       : 20
  class name  : tv
Tip: use a color picker on the output PNG file to inspect the output mask with
this legend.

มาสก์หมวดหมู่การแบ่งกลุ่มควรมีลักษณะดังนี้

segmentation-output

ลองใช้เครื่องมือสาธิต CLI สำหรับ ImageSegmenter กับโมเดลและข้อมูลทดสอบของคุณเอง

ข้อกำหนดความเข้ากันได้กับรุ่น

ImageSegmenter API จำเป็นต้องมีโมเดล TFLite ที่มีโมเดล TFLite ที่จำเป็น ข้อมูลเมตา ดูตัวอย่างการสร้างข้อมูลเมตาสำหรับรูปภาพ การแบ่งกลุ่มที่ใช้ผู้เขียนข้อมูลเมตาของ TensorFlow Lite API

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

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

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