LoginSignup
5

More than 3 years have passed since last update.

posted at

updated at

PrototxtとCaffemodelって何だ?

Memo

Darknet/Yoloのモデルや重みデータを、prototxt、caffemodelに変換したいので調べてます。

やりたい事はつまり、Tsingjinyunの説明を引用しますと、

Darknet configuration file .cfg to the .prototxt definition in Caffe, a tool to convert the weight file .weights to .caffemodel in Caffe and a detection demo to test the converted networks.

Convert Darknet model to Caffe's

検索すると、関係するリンクが集められたサイトがあった。その中のリンクを含め片っ端から調べてみる。

Tsingjinyun/Caffe-YOLO2

This is a set of tools to convert models from Darknet to Caffe, and in particular to convert the YOLO networks for object detection. For more details, see the paper:

手当たり次第、cfgとweightsを使って実行してみた。まずcfgからprototxtへの変換。

darknet
# python create_yolo_prototxt.py ../darknet_cfg-weights/darknet.cfg darknet
WARNING: cost layer not recognized
tiny
# python create_yolo_prototxt.py ../darknet_cfg-weights/tiny.cfg tiny
WARNING: cost layer not recognized
yolov2-tiny-voc
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov2-tiny-voc.cfg yolov2-tiny-voc
WARNING: region layer not recognized
yolov2-tiny
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov2-tiny.cfg yolov2-tiny
WARNING: region layer not recognized
yolov2-voc
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov2-voc.cfg yolov2-voc
WARNING: route layer not recognized
WARNING: reorg layer not recognized
WARNING: route layer not recognized
WARNING: region layer not recognized
yolov2
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov2.cfg yolov2
WARNING: route layer not recognized
WARNING: reorg layer not recognized
WARNING: route layer not recognized
WARNING: region layer not recognized
yolov3
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov3.cfg yolov3
WARNING: shortcut layer not recognized
WARNING: shortcut layer not recognized
~
WARNING: shortcut layer not recognized
WARNING: shortcut layer not recognized
WARNING: yolo layer not recognized
WARNING: route layer not recognized
WARNING: upsample layer not recognized
WARNING: route layer not recognized
WARNING: yolo layer not recognized
WARNING: route layer not recognized
WARNING: upsample layer not recognized
WARNING: route layer not recognized
WARNING: yolo layer not recognized
results
# ls -l *.prototxt
-rw-r--r-- 1 root root  5768 Mar 22 15:13 darknet_deploy.prototxt
-rw-r--r-- 1 root root 10257 Mar 22 15:14 tiny_deploy.prototxt
-rw-r--r-- 1 root root  6027 Mar 22 13:12 yolov2-tiny-voc_deploy.prototxt
-rw-r--r-- 1 root root  6026 Mar 22 13:13 yolov2-tiny_deploy.prototxt
-rw-r--r-- 1 root root 14500 Mar 22 13:13 yolov2-voc_deploy.prototxt
-rw-r--r-- 1 root root 14500 Mar 22 13:13 yolov2_deploy.prototxt
-rw-r--r-- 1 root root 44915 Mar 22 13:13 yolov3_deploy.prototxt

できたprototxtを使い、weightsをcaffemodelへ変換。

darknet
# python create_yolo_caffemodel.py \
darknet_deploy.prototxt ../darknet_cfg-weights/darknet.weights \
darknet.caffemodel

model file is darknet_deploy.prototxt
weight file is ../darknet_cfg-weights/darknet.weights
output caffemodel file is darknet.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv7
  converting conv8
Converted 7323480 weights.
tiny
# python create_yolo_caffemodel.py \
tiny_deploy.prototxt ../darknet_cfg-weights/tiny.weights \
tiny.caffemodel

model file is tiny_deploy.prototxt
weight file is ../darknet_cfg-weights/tiny.weights
output caffemodel file is tiny.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv15
  converting conv16
Converted 1046488 weights.
yolov2-tiny-voc
# python create_yolo_caffemodel.py \
yolov2-tiny-voc_deploy.prototxt ../darknet_cfg-weights/yolov2-tiny-voc.weights \
yolov2-tiny-voc.caffemodel

model file is yolov2-tiny-voc_deploy.prototxt
weight file is ../darknet_cfg-weights/yolov2-tiny-voc.weights
output caffemodel file is yolov2-tiny-voc.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv8
  converting conv9
Converted 15867885 weights.
yolov2-tiny
# python create_yolo_caffemodel.py \
yolov2-tiny_deploy.prototxt ../darknet_cfg-weights/yolov2-tiny.weights \
yolov2-tiny.caffemodel

model file is yolov2-tiny_deploy.prototxt
weight file is ../darknet_cfg-weights/yolov2-tiny.weights
output caffemodel file is yolov2-tiny.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv8
  converting conv9
Traceback (most recent call last):
  File "create_yolo_caffemodel.py", line 115, in <module>
    main()
  File "create_yolo_caffemodel.py", line 111, in main
    convert_weights(args.model, args.yolo_weights, args.output)
  File "create_yolo_caffemodel.py", line 94, in convert_weights
    format(weights.size, count, weights.size-count))
ValueError:  Wrong number of weights: read 11237146, used 11237145 (missing 1)
yolov2-voc
# python create_yolo_caffemodel.py \
yolov2-voc_deploy.prototxt ../darknet_cfg-weights/yolov2-voc.weights \
yolov2-voc.caffemodel

model file is yolov2-voc_deploy.prototxt
weight file is ../darknet_cfg-weights/yolov2-voc.weights
output caffemodel file is yolov2-voc.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv22
  converting conv23
Traceback (most recent call last):
  File "create_yolo_caffemodel.py", line 115, in <module>
    main()
  File "create_yolo_caffemodel.py", line 111, in main
    convert_weights(args.model, args.yolo_weights, args.output)
  File "create_yolo_caffemodel.py", line 94, in convert_weights
    format(weights.size, count, weights.size-count))
ValueError:  Wrong number of weights: read 50676062, used 39502173 (missing 11173889)
yolov2-tiny
# python create_yolo_caffemodel.py \
yolov2-tiny_deploy.prototxt ../darknet_cfg-weights/yolov2-tiny.weights \
yolov2-tiny.caffemodel

model file is yolov2-tiny_deploy.prototxt
weight file is ../darknet_cfg-weights/yolov2-tiny.weights
output caffemodel file is yolov2-tiny.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv8
  converting conv9
Traceback (most recent call last):
  File "create_yolo_caffemodel.py", line 115, in <module>
    main()
  File "create_yolo_caffemodel.py", line 111, in main
    convert_weights(args.model, args.yolo_weights, args.output)
  File "create_yolo_caffemodel.py", line 94, in convert_weights
    format(weights.size, count, weights.size-count))
ValueError:  Wrong number of weights: read 11237146, used 11237145 (missing 1)
yolov2
# python create_yolo_caffemodel.py \
yolov2_deploy.prototxt ../darknet_cfg-weights/yolov2.weights \
yolov2.caffemodel

model file is yolov2_deploy.prototxt
weight file is ../darknet_cfg-weights/yolov2.weights
output caffemodel file is yolov2.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv22
  converting conv23
Traceback (most recent call last):
  File "create_yolo_caffemodel.py", line 115, in <module>
    main()
  File "create_yolo_caffemodel.py", line 111, in main
    convert_weights(args.model, args.yolo_weights, args.output)
  File "create_yolo_caffemodel.py", line 94, in convert_weights
    format(weights.size, count, weights.size-count))
ValueError:  Wrong number of weights: read 50983561, used 39809673 (missing 11173888)
yolov3
# python create_yolo_caffemodel.py \
yolov3_deploy.prototxt ../darknet_cfg-weights/yolov3.weights \
yolov3.caffemodel

model file is yolov3_deploy.prototxt
weight file is ../darknet_cfg-weights/yolov3.weights
output caffemodel file is yolov3.caffemodel
Transpose fc layers: False
  converting conv1
  converting conv2
 ~
  converting conv74
  converting conv75
Traceback (most recent call last):
  File "create_yolo_caffemodel.py", line 115, in <module>
    main()
  File "create_yolo_caffemodel.py", line 111, in main
    convert_weights(args.model, args.yolo_weights, args.output)
  File "create_yolo_caffemodel.py", line 94, in convert_weights
    format(weights.size, count, weights.size-count))
ValueError:  Wrong number of weights: read 62001758, used 61771997 (missing 229761)

できたのは、yolov2-tiny-vocだけ。

results
# ls -l *.caffemodel
-rw-r--r-- 1 root root 29296614 Mar 22 15:17 darknet.caffemodel
-rw-r--r-- 1 root root  4191062 Mar 22 15:17 tiny.caffemodel
-rw-r--r-- 1 root root 63474427 Mar 22 13:18 yolov2-tiny-voc.caffemodel

READMEに従い、できあがったprototxtとcaffemodelを使って、Caffeで動かして確認する。

# python yolo_detect.py \
 darknet_deploy.prototxt darknet.caffemodel \
 images/dog.jpg

エラーを吐いて実行できず。
このGitにある元々のファイルを使うと、tiny-yoloだけOKだった。

# python yolo_detect.py \
 prototxt_caffemodel/tiny_yolo_deploy.prototxt prototxt_caffemodel/tiny_yolo.caffemodel \
 images/dog.jpg

深追いは止めて、一先ずここまでにしておく:thermometer_face:

Pushyami/YOLOv3-Caffe

Pytorchで、スクラッチからYOLOv3を実装する方法ということらしく、「オブジェクト検出の学習に取り組むための最善の方法は、最初からアルゴリズムを自分で実装すること」と書かれていました。

Part1

YOLOのしくみ、層の説明があり、参考になるかな。

Part2

YOLOのcfg(つまりCaffeのprototxt)の簡単な説明がある。YOLOで使用されるレイヤは、Convolutional、Shortcut、Upsample、Route、YOLO(検知層)の5種類だそうな。

Part3

Pytorchでの実装に入ってくるので、ビギナーにはつらいな~:frowning2:

Part4
Part5

Jasonlovescoding/YOLOv3-Caffe

CaffeでYolov3を動かしているみたい。
prototxtとcaffemodelをGoogleDriveからダウンロードできたので、Netronで見てみると、100ぐらいの隠れ層があって、とっても深~い!Netronの表示結果は ここ をクリック。

Python3のCaffeが必要みたい、良くわからないので断念:sob:

ChenYingpeng/Caffe-YOLOv3

こちらはBaidouにprototxt/caffemodel、cfg/weightsがUpされているが、Baidouのアプリをインストールしないといけないようで、怖いから止めとこ:relaxed:

Marvis/Pytorch-Caffe-Darknet-convert

Pytorch、Caffe、DarknetのConvert、これもPytorchが要る。

# python darknet2caffe.py \
  ../darknet_Cfg-Weights/tiny-yolo-voc.cfg \
  ../darknet_Cfg-Weights/tiny-yolo-voc.weights \
  tiny-yolo-voc.prototxt tiny-yolo-voc.caffemodel
Traceback (most recent call last):
  File "darknet2caffe.py", line 6, in <module>
    from cfg import *
  File "/home/hoge/temp/darknet2caffe/Marvis_Pytorch-Caffe-Darknet/cfg.py", line 1, in <module>
    import torch
ImportError: No module named torch

仕方ない、Pytorchをインストールしてみるか:frowning2:

Ysh329/Darknet2Caffe

ysh329/darknet-to-caffe-model-convertorが元のようで、これもPytorchがいるみたい:thinking:

Ysh329/Darknet2Caffe
root@3034bee841db:~/darknet2caffe# ./convert_YOLOv2_fromDarknet2Caffe.sh 
Traceback (most recent call last):
  File "darknet2caffe.py", line 5, in <module>
    from cfg import *
  File "/root/darknet2caffe/cfg.py", line 1, in <module>
    import torch
ImportError: No module named torch
root@3034bee841db:~/darknet2caffe# 

Xingwangsfu/Caffe-YOLO

This is a caffe implementation of the YOLO

まだ試していない・・・

Lwplw/Darknet2Caffe

まだ試していない・・・

Xilinx/ML-Suite

まだ試していない・・・

Abars/Darknet2Caffe

これもPytorchが必要みたい:rolling_eyes:

Traceback (most recent call last):
  File "./darknet2caffe27.py", line 6, in <module>
    from cfg import *
  File "/home/hoge/temp/caffe-ssd/python/darknet2caffe/cfg.py", line 1, in <module>
    import torch
ImportError: No module named torch

README.mdを見てみたら書いてあった。

This repository is forked from pytorch-caffe-darknet-convert.

karolmajek/darknet

4KのVideo入力を、Tiny YOLOで認識しているらしい・・・ 4K Tiny YOLO Object Detection

prototxtとcaffemodelって何だ?

ネットワークモデルの定義、重みファイルについて・・・

Caffeの実装理解のために

Caffeの学習済みモデルの利用

株式会社グリッドの、ReNoMという機械学習フレームワークのチュートリアルページに辿り着いた。

Caffe: Tutorial: Blob, 層, そしてネット : Caffe モデルの解剖

株式会社クラスキャットの、Caffeの本家サイトのTutorial – Blobs, Layers, and Nets: anatomy of a Caffe modelを翻訳・補足したページに辿り着いた。

Caffeを通してCNNを理解する #1

Caffeを通してCNNを理解する #2

Caffeによるシーン認識(8分類問題)

Seiya.Kumadaさんのブログ

YOLOv3の使い方

Seiya.Kumadaさんのブログ

prototxtの可視化

How do you visualize neural network architectures?

に投稿されていたものの中から、

Netron

Netscope

Ubuntu16.04, Docker, Caffe

こちらへ

取り敢えず、ここまで。

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
What you can do with signing up
5