LoginSignup
6
4

More than 5 years have passed since last update.

Windows10 に Pytorch をインストールして yolo v3 を動かす

Posted at

はじめに

pytorch は Preferred Networks社が開発したchainerから2017年2月にPython用として派生したディープラーニング用のライブラリです。コミュニティが非常に活発で多くの研究者が利用しはじめているため、新しい論文がは発表されると凄まじい勢いでアルゴリズムが実装されているようです。そのため色んな課題で悩んでいる場合、pytorchベースで論文が実装されているコードを参考することで解決できる可能性があるかもしれません。

インストール

下記URL にアクセスし自分の環境に合わせてクリックするとコマンドが表示されます。
私の環境はWindows で パッケージマネージャは pip 、python 3.6.4、cuda9.1 でインストールしてみます。

getstarted_pytorch.png

Run this command: に以下が表示されていますのでこれをコマンドプロンプトから実行します。
pip3 install http://download.pytorch.org/whl/cu91/torch-0.4.0-cp36-cp36m-win_amd64.whl
pip3 install torchvision

コマンドの実行

表示されているコマンドを実行します。
私の環境ではpip3 を pip にマッピングしているので下記のコマンドで実行します。

C:\>pip install http://download.pytorch.org/whl/cu91/torch-0.4.0-cp36-cp36m-win_amd64.whl
Collecting torch==0.4.0 from http://download.pytorch.org/whl/cu91/torch-0.4.0-cp36-cp36m-win_amd64.whl
  Downloading http://download.pytorch.org/whl/cu91/torch-0.4.0-cp36-cp36m-win_amd64.whl (607.0MB)
    100% |████████████████████████████████| 607.0MB 5.7MB/s
notebook 5.4.0 requires ipykernel, which is not installed.
jupyter 1.0.0 requires ipykernel, which is not installed.
jupyter-console 5.2.0 requires ipykernel, which is not installed.
ipywidgets 7.1.1 requires ipykernel>=4.5.1, which is not installed.
Installing collected packages: torch
Successfully installed torch-0.4.0

C:\>pip install torchvision
Collecting torchvision
  Downloading https://files.pythonhosted.org/packages/ca/0d/f00b2885711e08bd71242ebe7b96561e6f6d01fdb4b9dcf4d37e2e13c5e1/torchvision-0.2.1-py2.py3-none-any.whl (54kB)
    100% |████████████████████████████████| 61kB 3.9MB/s
Requirement already satisfied: six in c:\anaconda3\lib\site-packages (from torchvision) (1.11.0)
Requirement already satisfied: pillow>=4.1.1 in c:\anaconda3\lib\site-packages (from torchvision) (5.0.0)
Requirement already satisfied: torch in c:\anaconda3\lib\site-packages (from torchvision) (0.4.0)
Requirement already satisfied: numpy in c:\anaconda3\lib\site-packages (from torchvision) (1.14.2)
notebook 5.4.0 requires ipykernel, which is not installed.
jupyter 1.0.0 requires ipykernel, which is not installed.
jupyter-console 5.2.0 requires ipykernel, which is not installed.
ipywidgets 7.1.1 requires ipykernel>=4.5.1, which is not installed.
Installing collected packages: torchvision
Successfully installed torchvision-0.2.1

C:\>

Successfully が表示されていれば完了です。
すげー簡単!!!利用者が増えるのもわかります。

pytorch 版 yolo v3 の取得

Darknet Yolo V3 の pytorch版を下記より yolo v3 を取得して動かしてみましょう。

C:\github>git clone https://github.com/ayooshkathuria/pytorch-yolo-v3.git
Cloning into 'pytorch-yolo-v3'...
remote: Counting objects: 433, done.
remote: Total 433 (delta 0), reused 0 (delta 0), pack-reused 433 eceiving objects: 100% (433/433), 2.18 MiB | 393.00 KiBReceiving objects: 10
0% (433/433), 2.38 MiB | 341.00 KiB/s, done.
Resolving deltas: 100% (260/260), done.

C:\github>

weightsファイルの取得

ちゃんと認識できるか確かめるために weithts ファイルを本家のサイトより取得します。

をダウンロードしてpytorch 版 yolo v3のフォルダに格納します。

私の環境では
C:\github\pytorch-yolo-v3\yolov3.weights

yolo v3 の実行

C:\github\pytorch-yolo-v3>python detect.py --images imgs --det det
Loading network.....
Network successfully loaded
C:\github\pytorch-yolo-v3\imgs\dog.jpg predicted in  0.047 seconds
Objects Detected:    bicycle truck dog
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\eagle.jpg predicted in  0.047 seconds
Objects Detected:    bird
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\giraffe.jpg predicted in  0.047 seconds
Objects Detected:    zebra giraffe giraffe
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\herd_of_horses.jpg predicted in  0.047 seconds
Objects Detected:    horse horse horse horse
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\img1.jpg predicted in  0.047 seconds
Objects Detected:    person dog
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\img2.jpg predicted in  0.047 seconds
Objects Detected:    train
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\img3.jpg predicted in  0.047 seconds
Objects Detected:    car car car car car car car truck traffic light
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\img4.jpg predicted in  0.047 seconds
Objects Detected:    chair chair chair clock
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\messi.jpg predicted in  0.057 seconds
Objects Detected:    person person person sports ball
----------------------------------------------------------
C:\github\pytorch-yolo-v3\imgs\person.jpg predicted in  0.041 seconds
Objects Detected:    person dog horse
----------------------------------------------------------

SUMMARY
----------------------------------------------------------
Task                     : Time Taken (in seconds)

Reading addresses        : 0.000
Loading batch            : 1.728
Detection (11 images)    : 0.539
Output Processing        : 0.000
Drawing Boxes            : 0.031
Average time_per_img     : 0.209
----------------------------------------------------------

C:\github\pytorch-yolo-v3>

と表示されました。

物体検知した結果はdetディレクトリに保存されるハズですが・・・
保存されていませんでした。

フォルダの属性を見ると読み取り専用となっています。なんでやろ?:thinking:

動画入力

マックスくんとキーラちゃんの動画をリアルタイム認識させてみましょう。

ツイートの動画を保存する方法は適当にぐぐってください。:stuck_out_tongue_winking_eye:
C:\github\pytorch-yolo-v3> 以下に max_kun.mp4 として動画を保存します。

で、

C:\github\pytorch-yolo-v3>python video_demo.py --video max_kun.mp4

を実行すると・・・

not_detect_max_kun.png

さすがボルゾイです。:sunglasses:
たまに、馬と認識されますが、ほとんど犬と認識してくれます。人とサーフボードも認識してくれます。サーフボードではなくてSUPの板だけどね!:stuck_out_tongue_closed_eyes:

detect_max_kun.png

カメラ入力

cam_demo.py を実行するとエラーが発生します。

C:\github\pytorch-yolo-v3>python cam_demo.py
Traceback (most recent call last):
  File "cam_demo.py", line 123, in <module>
    im_dim = im_dim.cuda()
NameError: name 'im_dim' is not defined

cam_demo.py の 119 行目がコメントアウトされているので有効にするとカメラ入力が有効になります。

            im_dim = torch.FloatTensor(dim).repeat(1,2)                        

いかがでしょうか?

今週もワンコの画像みて頑張りましょう。:heart_eyes:

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