整合影像分segmenter

影像分segmenter 會預測影像的每個像素是否與特定類別相關聯。這與物件偵測 (偵測矩形區域中的物件) 和影像分類 (分類整體影像) 不同。如需影像分segmenter 的詳細資訊,請參閱影像分segmentation 總覽

使用 Task Library ImageSegmenter API,將您的自訂影像分segmenter 或預先訓練的分segmenter 部署到您的行動應用程式中。

ImageSegmenter API 的主要功能

  • 輸入影像處理,包括旋轉、調整大小和色彩空間轉換。

  • 標籤地圖語言環境。

  • 兩種輸出類型:類別遮罩和信賴度遮罩。

  • 用於顯示用途的彩色標籤。

支援的影像分segmenter 模型

下列模型保證與 ImageSegmenter API 相容。

在 Java 中執行推論

如需如何在 Android 應用程式中使用 ImageSegmenter 的範例,請參閱影像分Segmentation 參考應用程式

步驟 1:匯入 Gradle 依附元件和其他設定

.tflite 模型檔案複製到將執行模型的 Android 模組的 assets 目錄。指定檔案不應壓縮,並將 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);

如需設定 ImageSegmenter 的更多選項,請參閱原始碼和 javadoc

在 iOS 中執行推論

步驟 1:安裝依附元件

Task Library 支援使用 CocoaPods 安裝。請確認您的系統上已安裝 CocoaPods。如需操作說明,請參閱CocoaPods 安裝指南

如需將 Pod 新增至 Xcode 專案的詳細資訊,請參閱CocoaPods 指南

在 Podfile 中新增 TensorFlowLiteTaskVision Pod。

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

請確認您將用於推論的 .tflite 模型位於您的應用程式套件中。

步驟 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 上提供的通用分segmentation 模型) 的分segmentation 結果範例。

plane

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 類別遮罩應如下所示

segmentation-output

使用您自己的模型和測試資料試用適用於 ImageSegmenter 的簡易CLI 示範工具

模型相容性需求

ImageSegmenter API 需要具有強制性 TFLite 模型中繼資料的 TFLite 模型。如需使用 TensorFlow Lite Metadata Writer API 為影像分segmenter 建立中繼資料的範例,請參閱範例。

  • 輸入影像張量 (kTfLiteUInt8/kTfLiteFloat32)

    • 大小為 [batch x height x width x channels] 的影像輸入。
    • 不支援批次推論 (需要 batch 為 1)。
    • 僅支援 RGB 輸入 (需要 channels 為 3)。
    • 如果類型為 kTF LiteFloat32,則需要將 NormalizationOptions 附加至中繼資料以進行輸入正規化。
  • 輸出遮罩張量:(kTfLiteUInt8/kTfLiteFloat32)

    • 大小為 [batch x mask_height x mask_width x num_classes] 的張量,其中 batch 必須為 1,mask_widthmask_height 是模型產生的分Segmentation 遮罩維度,而 num_classes 是模型支援的類別數量。
    • 選用 (但建議使用) 標籤地圖可做為 AssociatedFile (類型為 TENSOR_AXIS_LABELS) 附加,每行包含一個標籤。第一個這類 AssociatedFile (如果有的話) 用於填寫結果的 label 欄位 (在 C++ 中命名為 class_name)。display_name 欄位從 AssociatedFile (如果有的話) 填寫,其語言環境符合建立時使用的 ImageSegmenterOptionsdisplay_names_locale 欄位 (預設為「en」,即英文)。如果這些都不可用,則只會填寫結果的 index 欄位。