画像セグメンタは、画像の各ピクセルが特定のクラスに関連付けられているかどうかを予測します。これは、矩形の領域内の物体を検出する物体検出、および画像全体を分類する画像分類とは対照的です。画像セグメンタの詳細については、画像セグメンテーションの概要をご覧ください。
Task Library ImageSegmenterAPI を使用して、カスタム画像セグメンタまたは事前トレーニングされたものをモバイルアプリにデプロイします。
ImageSegmenter API の主な機能
回転、サイズ変更、色空間変換などの入力画像処理。
マップロケールのラベル付け。
カテゴリマスクと信頼マスクの 2 つの出力型。
表示用の色付きラベル。
サポートされている画像セグメンタモデル
次のモデルは、ImageSegmenter API との互換性が保証されています。
モデルの互換性要件を満たすカスタムモデル。
Java で推論を実行する
Android アプリケーションでImageSegmenterを使用する方法の例については、画像セグメンテーションリファレンスアプリを参照してください。
ステップ 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'
}
注:Android Gradle プラグインのバージョン 4.1 以降、.tflite はデフォルトで noCompress リストに追加され、上記の aaptOptions は不要になりました。
ステップ 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: 依存関係をインストールする
タスクライブラリは、CocoaPods を使用したインストールをサポートしています。CocoaPods がシステムにインストールされていることを確認してください。手順については、CocoaPods インストールガイドを参照してください。
ポッドを Xcode に追加する詳細な方法については、CocoaPods ガイドを参照してください。
Podfile に TensorFlowLiteTaskVision ポッドを追加します。
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.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を構成するその他のオプションについては、ソースコードをご覧ください。
結果の例
これは、TensorFlow Hub で利用可能な一般的なセグメンテーションモデルである deeplab_v3 のセグメンテーション結果の例です。

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.
セグメンテーションカテゴリマスクは次のようになります。

独自のモデルとテストデータを使用して、シンプルな ImageSegmenter 向け CLI デモツールをお試しください。
モデルの互換性要件
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は、モデルでサポートされているクラスの数です。 - TENSOR_AXIS_LABELS 型の AssociatedFile ラベルマップ (オプションですが推薦されます)。1 行に 1 つのラベルが含まれます。最初の AssociatedFile (存在する場合) は、結果の
labelフィールド (C++ではclass_nameと名付けられています) を入力ために使用されます。display_nameフィールドは、AssociatedFile (存在する場合) から入力されます。そのロケールは、作成時に使用されるImageSegmenterOptionsのdisplay_names_localeフィールドと一致します (デフォルトでは「en (英語)」)。これらのいずれも使用できない場合、結果のindexフィールドのみが入力されます。
- テンソルサイズは