LoginSignup
15
10

More than 3 years have passed since last update.

YOLOv3を動かしてみる

Last updated at Posted at 2019-06-01

まずYOLOv3とは

物体認識モデルの一つ
他のモデルとの比較や性能、構造については

一般物体認識手法の把握
検出アルゴリズムの紹介
YOLPv3論文日本語訳
を読んでまずYOLOの概観を知る

**YOLOv3のスクラッチコード

実装してみる

開発環境

MacBook Pro(13-inch, 2017)
macOS Mojave 10.14.5
python 3.7.1

環境構築

pipenvで仮装環境を構築

仮装環境をつくりたいディレクトリに移動

$cd test_yolov3

仮装環境の構築

$pipenv install --Python 3.7.1

環境に入る+次回以降はpipfile に入ってるPackageInstall(必要ならUpdata)
http://pynote.hatenablog.com/entry/pipenv-usage

$ pipenv shell
(test_yolo) bash-3.2$
(test_yolo) bash-3.2$ pipenv install

必要なパッケージのインストール
*ここでMatplotlibのインストールでエラーが出たので、参考1,参考2
を元にmatplotlibrcのpathを調べて
vimでmatplotlibrcを編集

$python -c "import matplotlib;print(matplotlib.matplotlib_fname())"
$cd ~~~~~~~~~~~~~~~~~~/matplotlibrc
$vi matplotlibrc


backend : macosx-> backend : macosx

に変更。

その上で再度


$ pipenv install opencv-contrib-python
$ pipenv install numpy
$ pipenv install matplotlib-python

YOLOv3の設定

本家以外に色々あってややこしい。。。
1. 公式サイトのコードで認識

git clone https://github.com/pjreddie/darknet
$ cd darknet
$ make
$ wget https://pjreddie.com/media/files/yolov3.weights
$ ./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

YOLOv3とりあえず一枚画像で試す

DATAの自作
- https://qiita.com/watakandhi/items/7981f05e6cfc1b69737c
- https://nmxi.hateblo.jp/entry/2019/02/28/104546
- https://demura.net/misc/14458.html
- https://qiita.com/tk-tkhs/items/eb74ea516cadfaa202a0

  1. 次に不要なコードが省かれた Pytorch仕様のYOLOv3 を使うことに これはCOCOデータセットの学習済みモデル。後にオリジナルのデータセットを学習させることも可能
(test_yolo) $ git clone https://github.com/ayooshkathuria/pytorch-yolo-v3.git
(test_yolo) $ cd pytorch-yolo-v3
(test_yolo) $ wget https://pjreddie.com/media/files/yolov3.weights

**Mac wget -> curl https://pjreddie.com/media/files/yolov3.weights -0
とするといいそうです(私はbrew install wgetしました)

認識テスト

  • あらかじめ用意した画像データ imgsフォルダに認識させたい画像いれる。 detフォルダに認識後の画像が入る。
$ python detect.py --images imgs --det det
  • 動画認識のデモ
$python video_demo.py --video sample.mp4
  • リアルタイムで内蔵カメラで撮影した画像の認識
$python cam_demo.py

YOLOv3の編集について

どのgithubコードをメインにするかによって実行コマンドが違う。
公式のDarknetをcloneした場合、画像の書き出しや座標出力をimage.cファイルの編集により可能となるそう。
ただし、コンパイル$makeが毎度必要
https://pycarnival.com/yolo_python_opencv/

GPU使うにはmakefileを編集すればいい
これも変更したらコンパイル$makeが毎度必要

 参考にさせて頂いた資料

15
10
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
15
10