LoginSignup
4
7

More than 1 year has passed since last update.

Google Colabで物体認識を簡単に試す

Last updated at Posted at 2021-06-24

Github Open In Colab
ご覧いただきありがとうございます。
Google Colaboratoryにアカウントをお持ちの方は、上の「Open in Colab」という青いボタンを押せば直接notebookをColabで開けます。ぜひ動かしてみてください。
過去の記事も含め、全てのコードをGithubで公開しています。


物体認識をpytorchで行うMMDetectionというライブラリが公開されています。今回、学習済みモデルを使ってこの物体認識をGoogle Colabで実行してみます。

まずはColabのランタイムのタイプをGPUに変更してください。
次にpipでライブラリをインストールします。ライブラリの名前はmmdetです。

!pip install mmdet
Collecting mmdet
・・・
Successfully installed mmdet-2.13.0 terminaltables-3.1.0

簡単にインストールできました。ライブラリをimportしましょう。

import mmdet
---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-2-38ab3d8819c4> in <module>()
----> 1 import mmdet


/usr/local/lib/python3.7/dist-packages/mmdet/__init__.py in <module>()
----> 1 import mmcv
      2 
      3 from .version import __version__, short_version
      4 
      5 


ModuleNotFoundError: No module named 'mmcv'



---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

駄目です。ライブラリの依存関係の設定に不備があるようです。というわけで、mmcvをインストールするのですが、実はmmcvでは上手くいきません。mmcv-fullが必要です。

mmcv-fullのインストールには時間がかかります。

%%time
!pip install mmcv-full
Collecting mmcv-full
・・・
Wall time: 10min 51s

それでは改めてmmdetをインポートします。

import mmdet

できました。

次に学習済みモデルを取得します。MMDetectionのページに様々なモデルが載っています。今回はfaster_rcnnを使いましょう。リンクを辿り、学習済みモデルのURLを調べて、wgetでファイルを取得します。

# Faster R-CNN
!wget -P /content https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
--2021-06-24 06:24:01--  https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
・・・
2021-06-24 06:24:10 (21.0 MB/s) - ‘/content/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’ saved [167287506/167287506]

次に必要なモジュールをimportします。

from mmdet.apis.inference import init_detector, inference_detector, show_result_pyplot

importできました。しかしここで困った事態に。init_detector()の引数にはconfigファイルと重みファイルを指定します。重みファイルは先ほど取得しました。ではconfigファイルは?同様にURLを指定して取得しようとしましたが、configファイルは単一ファイルではなく、複数必要でした。結局、ソースをまるごと取得したほうが簡単ですので、取得しましょう。

!git clone https://github.com/open-mmlab/mmdetection.git
%cd /content/mmdetection
Cloning into 'mmdetection'...
remote: Enumerating objects: 18606, done.
remote: Counting objects: 100% (143/143), done.
remote: Compressing objects: 100% (104/104), done.
remote: Total 18606 (delta 62), reused 86 (delta 39), pack-reused 18463
Receiving objects: 100% (18606/18606), 21.66 MiB | 29.45 MiB/s, done.
Resolving deltas: 100% (12969/12969), done.
/content/mmdetection

ソースの中のconfigファイルと先に取得した重みファイルを指定してモデルを作成します。deviceには'cuda:0'を指定します。

model = init_detector('configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py', '/content/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth', device='cuda:0')
Use load_from_local loader

ソースの中の画像を利用して物体認識を実行し、結果を表示します。

result = inference_detector(model, "demo/demo.jpg")
show_result_pyplot(model, "demo/demo.jpg", result, score_thr=0.7)
/usr/local/lib/python3.7/dist-packages/mmdet/datasets/utils.py:68: UserWarning: "ImageToTensor" pipeline is replaced by "DefaultFormatBundle" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.
  'data pipeline in your config file.', UserWarning)

1.png

あっさり成功しました。でもWarningが出ていて格好悪いです。本来、警告に従って修正すべきですが、面倒なので上辺だけ取り繕います。

import warnings
warnings.simplefilter('ignore')
result = inference_detector(model, "demo/demo.jpg")
show_result_pyplot(model, "demo/demo.jpg", result, score_thr=0.7)

2.png

次にMask R-CNNを試しましょう。こちらは物体の位置だけでなく、その領域も認識してくれます。

まずは学習済みの重みファイルを取得します。

# Mask R-CNN
!wget -P /content http://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_fpn_1x_coco/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth
--2021-06-24 06:25:13--  http://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_fpn_1x_coco/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth
Resolving download.openmmlab.com (download.openmmlab.com)... 47.88.36.78
Connecting to download.openmmlab.com (download.openmmlab.com)|47.88.36.78|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 177862517 (170M) [application/octet-stream]
Saving to: ‘/content/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth’

mask_rcnn_r50_fpn_1 100%[===================>] 169.62M  10.2MB/s    in 17s     

2021-06-24 06:25:31 (10.2 MB/s) - ‘/content/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth’ saved [177862517/177862517]

次にモデルの作成と認識実行です。

model = init_detector("configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py", "/content/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth", device="cuda:0")
result = inference_detector(model, "demo/demo.jpg")
show_result_pyplot(model, "demo/demo.jpg", result, score_thr=0.7)
Use load_from_local loader

3.png

驚くほど簡単に実行できました。

MMDetectionには他にもたくさんのモデルが公開されています。どれも今回の記事と同じ方法で実行できますので、試してみてはいかがでしょうか。

4
7
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
4
7