0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

label studioで事前にラベリングされた状態で画像が出るようにする

Posted at

前回の記事では、YOLOのためのラベリング画像を作成する記事を書きました。以下の記事はそれに基づいて話をしています。

YOLOというフレームワークは、少ない画像でも精度の高い判定結果を出すことに優れていますが、それでもたくさん画像をラベリングする必要があることがありますよね。

そのとき、すべての画像をいちから囲んでいると、時間がかかってしょうがないことがあります。

そこで、今回はある程度事前にアノテーションをした状態で画像を出してくるようにする方法について話したいと思います。

ml-backend

こちらに公開されているlabel studio公式のバックエンドを使用します。

まずこのリポジトリをクローンします。

#cwd yolo-demo
## ソースをGit管理していない場合
git clone https://github.com/HumanSignal/label-studio-ml-backend.git

## ソースをGit管理している場合
git submodule add https://github.com/HumanSignal/label-studio-ml-backend.git

さて、ここで、前回ディレクトリのトップにコピーしたモデルをバックエンドのYOLOのディレクトリにコピーします。

#cwd yolo-demo
cp best.pt label-studio-ml-backend/label_studio_ml/examples/yolo/models/

また、label-studio-ml-backend/label_studio_ml/examples/yolo/docker-compose.yml
にあるファイルに少し変更を加えます。

docker-compose.yml
 version: "3.8"
 
 services:
   yolo:
     container_name: yolo
     image: humansignal/yolo:v0
     build:
       context: .
       args:
         TEST_ENV: ${TEST_ENV}
     environment:
       # specify these parameters if you want to use basic auth for the model server
       - BASIC_AUTH_USER=
       - BASIC_AUTH_PASS=
       # set the log level for the model server
       - LOG_LEVEL=DEBUG
       # any other parameters that you want to pass to the model server
       - ANY=PARAMETER
       # specify the number of workers and threads for the model server
       - WORKERS=1
       - THREADS=8
       # specify the model directory (likely you don't need to change this)
       - MODEL_DIR=/data/models
       - PYTHONPATH=/app
 
       # Specify the Label Studio URL and API key to access
       # uploaded, local storage and cloud storage files.
       # Do not use 'localhost' or '127.0.0.1' as it does not work within Docker containers.
       # Use prefix 'http://' or 'https://' for the URL always.
       # Determine the actual IP using 'ifconfig' (Linux/Mac) or 'ipconfig' (Windows).
       # or you can try http://host.docker.internal:<label-studio-port> if you run LS on the same machine
       - LABEL_STUDIO_HOST=${LABEL_STUDIO_HOST:-http://host.docker.internal:8080}
       - LABEL_STUDIO_API_KEY=${LABEL_STUDIO_API_KEY}
 
       # YOLO parameters
 
       # Allow to use custom `model_path` in labeling configurations
       - ALLOW_CUSTOM_MODEL_PATH=true
       # Show matplotlib debug plot for YOLO predictions
       - DEBUG_PLOT=false
       # Default score threshold, which is used to filter out low-confidence predictions,
       # you can change it in the labeling configuration using `model_score_threshold` parameter in the control tags
       - MODEL_SCORE_THRESHOLD=0.5
       # Model root directory, where the YOLO model files are stored
       - MODEL_ROOT=/app/models
     extra_hosts:
       - "host.docker.internal:host-gateway"  # for macos and unix      
     ports:
       - "9090:9090"
     volumes:
       - "./data/server:/data"
       - "./models:/app/models"
       - "./cache_dir:/app/cache_dir"
+    networks:
+     - label_link

+networks:
+ label_link:
+   name: label_link

また、yolo-demo/label-studioにあるdocker-compose.yml(Label Studioのらーバーを立ち上げるファイル)も編集します。

docker-compose.yml
 version: "3.9"

 services:
   label_studio:
     container_name: label_studio
     image: heartexlabs/label-studio:latest
     volumes:
       - label-studio-data:/label-studio/data
       - ../images/:/tmp/test_images/
     ports:
       - 8080:8080
     environment:
       - LOCAL_FILES_SERVING_ENABLED=true
+    networks:
+      - label_link

 volumes:
   label-studio-data:
+networks:
+ label_link:
+   external: true

さて、一旦、yolo-demo/label-studio/docker-compose.ymlだけ立ち上げます。

#cwd yolo-demo/label-studio
docker compose up -d

ページのトップ画面から、
image.png

左上のハンバーガーメニューをクリック、Organizationを選択。

遷移した先の画面でAPI Tokens Settingsを選択。

Legacy Tokensを有効にして保存します。

その後、右上の自分のアイコンをクリックし、Account & Settingsを選択。

左側のLegacy Tokenをクリックします。

image.png

原則としてトークンが表示された画像をこのように一般公開してはいけません。

ここで、トークンをコピーし、プロジェクトのenvファイル(.envrc, .env.local, .envなど)に配置します。

.envrc
export LABEL_STUDIO_API_KEY="YOURAPITOKEN"

YOURAPITOKENは適宜変更してください。

さて、環境変数を読み込んだら、次はml-backendのDockerを立ち上げます。

#cwd yolo-demo/label-studio-ml-backend/label_studio_ml/examples/yolo
docker compose up -d

初回はしばらくかかりますが、気長に待ちましょう。

さて、起動が終わったら、プロジェクトに行き、Settingsを開きます。

左側にあるModelを選択し、Connect Modelを選択します。

image.png

次のように保存します。

成功すると、次のように緑色でConnectedと出るはずです。

image.png

さて、一旦backendを落として、事前アノテーションをさせるためのモデルをコピーします。

#cwd yolo-demo/label-studio-ml-backend/label_studio_ml/examples/yolo
docker compose down

そして、前回の記事でプロジェクトのルートにコピーしたモデルをコピーしてきます。

#cwd yolo-demo/label-studio-ml-backend/label_studio_ml/examples/yolo
cp ../../../../best.pt models/

そして再びdocker compose up -dで立ち上げます。

ここで、少しLabel Studio側でも設定を加えます。

プロジェクトを開き、Settingsをクリックして、Labeling Interfaceを選択し、Codeに切り替えます。

image.png

ラベルのクラスによって具体的な中身は異なるかもしれませんが、大体はこのようになっていると思います。

RectangleLabelsのパラメーターにmodel_pathを追加して、best.ptとします。

image.png

Saveを押して保存しましょう。

これでまだラベリングしていない画像をアノテーションしようとすると、

image.png

次のように自分が指定したモデルで判別結果を返してくれています。

精度は今ひとつというところではありますが、いちからラベルするよりは格段に早いのではないでしょうか。

終わりに

今回も誰かのためになれば幸いです。

自分はこれの設定にかなり苦労したのでこの記事によって手っ取り早く始めることができれば嬉しいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?