ショートカット

MMDetectionとLabel-Studioを用いた半自動物体検出アノテーション

アノテーションデータは、時間と労力を要する作業です。この記事では、MMDetectionのRTMDetアルゴリズムとLabel-Studioソフトウェアを組み合わせて、半自動アノテーションを実行する方法を紹介します。具体的には、RTMDetを使用して画像のアノテーションを予測し、Label-Studioでアノテーションを調整します。コミュニティユーザーはこのプロセスと方法論を参照して、他の分野に適用できます。

  • RTMDet:RTMDetは、OpenMMLabによって開発され、MMDetection物体検出ツールボックスでオープンソース化された、高精度のシングルステージ物体検出アルゴリズムです。そのオープンソースライセンスはApache 2.0であり、産業ユーザーは制限なしに自由に使用できます。

  • Label Studioは、画像分類、物体検出、セグメンテーションなどの分野におけるデータセットアノテーションの機能をカバーする優れたアノテーションソフトウェアです。

この記事では、半自動アノテーションのために画像を使用します。

環境設定

まず、仮想環境を作成し、次にPyTorchとMMCVをインストールする必要があります。この記事では、PyTorchとMMCVのバージョンを指定します。次に、次の手順でMMDetection、Label-Studio、およびlabel-studio-ml-backendをインストールできます。

仮想環境を作成

conda create -n rtmdet python=3.9 -y
conda activate rtmdet

PyTorchをインストール

# Linux and Windows CPU only
pip install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html
# Linux and Windows CUDA 11.3
pip install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu113/torch_stable.html
# OSX
pip install torch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1

MMCVをインストール

pip install -U openmim
mim install "mmcv>=2.0.0"
# Installing mmcv will automatically install mmengine

MMDetectionをインストール

git clone https://github.com/open-mmlab/mmdetection
cd mmdetection
pip install -v -e .

Label-Studioとlabel-studio-ml-backendをインストール

# Installing Label-Studio may take some time, if the version is not found, please use the official source
pip install label-studio==1.7.2
pip install label-studio-ml==1.0.9

rtmdetの重みをダウンロード

cd path/to/mmetection
mkdir work_dirs
cd work_dirs
wget https://download.openmmlab.com/mmdetection/v3.0/rtmdet/rtmdet_m_8xb32-300e_coco/rtmdet_m_8xb32-300e_coco_20220719_112220-229f527c.pth

サービスの開始

RTMDetバックエンド推論サービスを開始

cd path/to/mmetection

label-studio-ml start projects/LabelStudio/backend_template --with \
config_file=configs/rtmdet/rtmdet_m_8xb32-300e_coco.py \
checkpoint_file=./work_dirs/rtmdet_m_8xb32-300e_coco_20220719_112220-229f527c.pth \
device=cpu \
--port 8003
# Set device=cpu to use CPU inference, and replace cpu with cuda:0 to use GPU inference.

RTMDetバックエンド推論サービスが開始されました。Label-Studio Webシステムで設定するには、バックエンド推論サービスとして https://127.0.0.1:8003 を使用します。

次に、Label-Studio Webサービスを開始します

label-studio start

Webブラウザーを開き、https://127.0.0.1:8080/ にアクセスして、Label-Studioインターフェースを確認します。

ユーザーを登録し、RTMDet-Semiautomatic-Labelプロジェクトを作成します。

次のコマンドを実行してサンプル猫画像をダウンロードし、[データインポート]ボタンを使用してインポートします

cd path/to/mmetection
mkdir data && cd data

wget https://download.openmmlab.com/mmyolo/data/cat_dataset.zip && unzip cat_dataset.zip

次に、[バウンディングボックス付き物体検出]テンプレートを選択します。

airplane
apple
backpack
banana
baseball_bat
baseball_glove
bear
bed
bench
bicycle
bird
boat
book
bottle
bowl
broccoli
bus
cake
car
carrot
cat
cell_phone
chair
clock
couch
cow
cup
dining_table
dog
donut
elephant
fire_hydrant
fork
frisbee
giraffe
hair_drier
handbag
horse
hot_dog
keyboard
kite
knife
laptop
microwave
motorcycle
mouse
orange
oven
parking_meter
person
pizza
potted_plant
refrigerator
remote
sandwich
scissors
sheep
sink
skateboard
skis
snowboard
spoon
sports_ball
stop_sign
suitcase
surfboard
teddy_bear
tennis_racket
tie
toaster
toilet
toothbrush
traffic_light
train
truck
tv
umbrella
vase
wine_glass
zebra

次に、上記のカテゴリをコピーしてLabel-Studioに追加し、[保存]をクリックします。

[設定]で、[モデルの追加]をクリックしてRTMDetバックエンド推論サービスを追加します。

[検証と保存]をクリックし、[ラベル付けの開始]をクリックします。

以下に示すように[接続済み]と表示された場合、バックエンド推論サービスが正常に追加されました。

半自動ラベル付けの開始

[ラベル]をクリックして、ラベル付けを開始します。

RTMDetバックエンド推論サービスが予測結果を正常に返し、画像に表示していることがわかります。ただし、猫の予測バウンディングボックスが少し大きすぎて、あまり正確ではないことに気づきました。

猫のバウンディングボックスの位置を手動で調整し、[送信]をクリックしてこの画像の注釈を完了します。

すべての画像を送信したら、[エクスポート]をクリックして、ラベル付きデータセットをCOCO形式でエクスポートします。

VS Codeを使用して解凍したフォルダーを開き、ラベル付きデータセットを確認します。これには、画像とJSON形式の注釈ファイルが含まれています。

この時点で、半自動ラベル付けは完了です。このデータセットを使用してMMDetectionでより正確なモデルをトレーニングし、このモデルを使用して新しく収集した画像で半自動ラベル付けを続けることができます。このようにして、高品質のデータセットを反復的に拡張し、モデルの精度を向上させることができます。

MMYOLOをバックエンド推論サービスとして使用する

MMYOLOでLabel-Studioを使用する場合は、バックエンド推論サービスの開始時にconfig_fileとcheckpoint_fileをMMYOLOの設定ファイルと重みファイルに置き換えることを参照できます。

cd path/to/mmetection

label-studio-ml start projects/LabelStudio/backend_template --with \
config_file= path/to/mmyolo_config.py \
checkpoint_file= path/to/mmyolo_weights.pth \
device=cpu \
--port 8003
# device=cpu is for using CPU inference. If using GPU inference, replace cpu with cuda:0.

回転物体検出とインスタンスセグメンテーションは現在開発中です。ご期待ください。