ตัวแบ่งส่วนรูปภาพคาดการณ์ว่าแต่ละพิกเซลของรูปภาพเชื่อมโยงกับคลาสใดคลาสหนึ่งหรือไม่ ซึ่งตรงกันข้ามกับ การตรวจจับวัตถุ ซึ่งตรวจจับวัตถุในพื้นที่สี่เหลี่ยม และ การจัดประเภทรูปภาพ ซึ่งจัดประเภทรูปภาพโดยรวม ดู ภาพรวมการแบ่งส่วนรูปภาพ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับตัวแบ่งส่วนรูปภาพ
ใช้ Task Library ImageSegmenter
API เพื่อปรับใช้ตัวแบ่งส่วนรูปภาพที่กำหนดเองหรือกลุ่มที่ได้รับการฝึกอบรมล่วงหน้าในแอปมือถือของคุณ
คุณสมบัติหลักของ ImageSegmenter API
ป้อนข้อมูลการประมวลผลภาพ รวมทั้งการหมุน การปรับขนาด และการแปลงพื้นที่สี
ป้ายกำกับสถานที่แผนที่
เอาต์พุตสองประเภท มาสก์หมวดหมู่และมาสก์ความมั่นใจ
ป้ายสีสำหรับตั้งโชว์
รุ่นตัวแบ่งรูปภาพที่รองรับ
รับประกันว่ารุ่นต่อไปนี้จะเข้ากันได้กับ ImageSegmenter
API
โมเดลแบบกำหนดเองที่ตรงตาม ข้อกำหนดความเข้ากันได้ ของโมเดล
เรียกใช้การอนุมานใน Java
ดู แอปอ้างอิงการแบ่งกลุ่มรูปภาพ สำหรับตัวอย่างวิธีใช้ ImageSegmenter
ในแอป 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
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: ติดตั้งการพึ่งพา
ไลบรารีงานรองรับการติดตั้งโดยใช้ 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)
วัตถุประสงค์ 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.OutputType.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.
มาสก์หมวดหมู่การแบ่งกลุ่มควรมีลักษณะดังนี้:
ลองใช้ เครื่องมือสาธิต CLI อย่างง่ายสำหรับ ImageSegmenter ด้วยโมเดลและข้อมูลทดสอบของคุณเอง
ข้อกำหนดความเข้ากันได้ของรุ่น
ImageSegmenter
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)
- เทนเซอร์ของขนาด
[batch x mask_height x mask_width x num_classes]
โดยที่batch
ตช์ต้องเป็น 1,mask_width
และmask_height
คือขนาดของมาสก์การแบ่งส่วนที่สร้างโดยโมเดล และnum_classes
คือจำนวนคลาสที่โมเดลรองรับ - สามารถแนบแผนที่ป้ายกำกับที่ไม่บังคับ (แต่แนะนำ) เป็น AssociatedFile-s ที่มีประเภท TENSOR_AXIS_LABELS โดยมีหนึ่งป้ายกำกับต่อบรรทัด AssociatedFile แรกดังกล่าว (ถ้ามี) ใช้เพื่อเติมฟิลด์
label
(ตั้งชื่อเป็นclass_name
ใน C++) ของผลลัพธ์ ฟิลด์display_name
ถูกกรอกจาก AssociatedFile (ถ้ามี) ซึ่ง locale ตรงกับฟิลด์display_names_locale
ของImageSegmenterOptions
ที่ใช้ ณ เวลาที่สร้าง ("en" โดยค่าเริ่มต้น เช่น ภาษาอังกฤษ) หากไม่มีสิ่งเหล่านี้ ระบบจะเติมเฉพาะฟิลด์index
ของผลลัพธ์เท่านั้น
- เทนเซอร์ของขนาด