こんにちは!みやしんです。
今回はDetectron2を使った物体検出・セグメンテーション・骨格検出をご紹介します!
物体検出はPythonの醍醐味の1つ!
上手くできるととても楽しいと思います。
是非試してみてくださいね🤗
この記事で出来る事
今回は下記4パターンを実際にやってみたいと思います。
・Object Detection
・Instance Segmentation
・Keypoint Detection
・Panoptic Segmentation
Detectron2とは
・Facebook AI Researchが開発
・PyTorchベースの物体検出 / セグメンテーション
・多数のモデルを公開
・Google Colab上でも基本的なコード付きで公開
・Githubで公開、誰でも使用可能
Detectron2のダウンロード
下記のGithubから入手できます。
detectron2-mainフォルダをダウンロードできます。
フォルダの中はこんな感じです。
PC環境
【OS】Windows
【Python】3.10.6
ライブラリのインストール
Pytorch
自分のPC環境に合ったPyTorchをインストールします。
PyTorch
https://pytorch.org/
自分の場合は、nvcc -Vコマンドでcudaのバージョンを確認してcuda_11.7でしたので、cudaに合ったPyTorchをインストールしています。
nvcc -V
PyTorchインストール
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
その他のライブラリをインストール
fvcore
cloudpickle
omegaconf
pycocotools
fairscale
timm
opencv-python
requirements.txt にまとめておくと楽ですね!
その場合は、
pip install -r requirements.txt
でインストールできます。
環境構築ができましたら早速始めていきましょう!
Detectron2 Model Zoo and Baselines (学習済みモデル)
下記に今回使う学習済みモデルがまとめられています。
Detectron2 Model Zoo and Baselines
https://github.com/facebookresearch/detectron2/blob/main/MODEL_ZOO.md
以下で使っていきます。
COCO Instance Segmentation
コマンドは以下のページを参考にしています。
demo.py と predictor.pyを移動
demoフォルダに入っている「demo.py」と 「predictor.py」をdetectron2-mainフォルダの直下に移動させます。
detectron2-mainフォルダの直下に保存
画像ファイルを保存
今回、処理をしたい画像もdetectron2-mainフォルダの直下に保存しましょう。
今回はmessi.jpgを準備しました。
messi.jpg
detectron2-mainフォルダの直下に保存
コマンド実行
フォルダーをdetectron2-mainに移動
cd detectron2-main
コマンド実行
python demo.py --config-file ./configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input messi.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
設定ファイル:mask_rcnn_R_50_FPN_3x.yaml
画像:messi.jpg
モデル:model_final_f10217.pkl
コマンドの簡易解説
実行結果
COCO Person Keypoint Detection
コマンド実行
コマンドの考え方はCOCO-InstanceSegmentationと同じです。
python demo.py --config-file ./configs/COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml --input messi.jpg --opts MODEL.WEIGHTS detectron2://COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x/138363331/model_final_997cc7.pkl
設定ファイル:keypoint_rcnn_R_101_FPN_3x.yaml
画像:messi.jpg
モデル:model_final_997cc7.pkl
実行結果
COCO Panoptic Segmentation
コマンド実行
python demo.py --config-file ./configs/COCO-PanopticSegmentation/panoptic_fpn_R_50_3x.yaml --input messi.jpg --opts MODEL.WEIGHTS detectron2://COCO-PanopticSegmentation/panoptic_fpn_R_50_3x/139514569/model_final_c10459.pkl
設定ファイル:panoptic_fpn_R_50_3x.yaml
画像:messi.jpg
モデル:model_final_c10459.pkl
実行結果
COCO Object Detection
コマンド実行
python demo.py --config-file ./configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml --input messi.jpg --opts MODEL.WEIGHTS detectron2://COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl
設定ファイル:faster_rcnn_R_50_FPN_3x.yaml
画像:messi.jpg
モデル:model_final_280758.pkl
実行結果
オプション機能
Webカメラで実行
--input file名 を 「--webcam」 に変更
ビデオで実行
--input file名 を 「--video-input videoファイル名」 に変更
cpuで実行
--opts の後に 「MODEL.DEVICE cpu」 を追加
例)
python demo.py --config-file ./configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --webcam --opts MODEL.DEVICE cpu MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
output (画像、ビデオ)
「--output ファイル名」 を追加
例)
python demo.py --config-file ./configs/COCO-PanopticSegmentation/panoptic_fpn_R_50_3x.yaml --input messi.jpg --output output.jpg --opts MODEL.WEIGHTS detectron2://COCO-PanopticSegmentation/panoptic_fpn_R_50_3x/139514569/model_final_c10459.pkl
エラー対応
ValueError: RGBA values should be within 0-1 range
エラー内容を見るとcolors.pyというファイルの395行目で起こっていました。
今回は、エラーが発生する部分を応急処置的にコメントアウトで対応しました。
その他の画像
参考にその他の処理した画像を載せておきます。
コメント