PrototxtとCaffemodelって何だ？

#Memo#

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

やりたい事はつまり、[Tsingjinyun](https://github.com/tsingjinyun/caffe-yolov2)の説明を引用しますと、

「_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](https://libs.garden/python/similar/ysh329/darknet2caffe)#

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

###[Tsingjinyun/Caffe-YOLO2](https://github.com/tsingjinyun/caffe-yolov2)###

_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への変換。


```shell-session:darknet
# python create_yolo_prototxt.py ../darknet_cfg-weights/darknet.cfg darknet
WARNING: cost layer not recognized
```

```shell-session:tiny
# python create_yolo_prototxt.py ../darknet_cfg-weights/tiny.cfg tiny
WARNING: cost layer not recognized
```

```shell-session:yolov2-tiny-voc
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov2-tiny-voc.cfg yolov2-tiny-voc
WARNING: region layer not recognized
```
```shell-session:yolov2-tiny
# python create_yolo_prototxt.py ../darknet_cfg-weights/yolov2-tiny.cfg yolov2-tiny
WARNING: region layer not recognized
```
```shell-session: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
```
```shell-session: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
```
```shell-session: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
```
```shell-session: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へ変換。

```shell-session: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.
````
```shell-session: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.
```
```shell-session: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.
```
```shell-session: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)
```
```shell-session: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)
```
```shell-session: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)
```
```shell-session: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)
```
```shell-session: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だけ。

```shell-session: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で動かして確認する。

```shell-session
# python yolo_detect.py \
　darknet_deploy.prototxt darknet.caffemodel \
　images/dog.jpg
```
エラーを吐いて実行できず。
このGitにある元々のファイルを使うと、tiny-yoloだけOKだった。

```shell-session
# python yolo_detect.py \
　prototxt_caffemodel/tiny_yolo_deploy.prototxt　prototxt_caffemodel/tiny_yolo.caffemodel \
　images/dog.jpg
```

<img src="https://qiita-image-store.s3.amazonaws.com/0/75262/5b6addb7-bfba-f8f2-0c51-6a8a591cf8ae.png" width="360">

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

###[Pushyami/YOLOv3-Caffe](https://github.com/pushyami/yolov3-caffe)###

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

#####[Part1](https://blog.paperspace.com/how-to-implement-a-yolo-object-detector-in-pytorch/)#####

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

#####[Part2](https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch-part-2/)#####

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

#####[Part3](https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch-part-3/)#####

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

#####[Part4](https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch-part-4/)######

#####[Part5](https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch-part-5/)#####

###[Jasonlovescoding/YOLOv3-Caffe](https://github.com/jasonlovescoding/YOLOv3-caffe)###

CaffeでYolov3を動かしているみたい。
prototxtとcaffemodelをGoogleDriveからダウンロードできたので、Netronで見てみると、100ぐらいの隠れ層があって、とっても深～い！Netronの表示結果は　<a href="https://qiita-image-store.s3.amazonaws.com/0/75262/52434c63-15e8-423a-6a9f-1d61b114dc48.png">ここ</a>　をクリック。

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

###[ChenYingpeng/Caffe-YOLOv3](https://github.com/ChenYingpeng/caffe-yolov3)###

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

###[Marvis/Pytorch-Caffe-Darknet-convert](https://github.com/marvis/pytorch-caffe-darknet-convert)###
Pytorch、Caffe、DarknetのConvert、これもPytorchが要る。

```shell-session
# 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](https://github.com/ysh329/darknet2caffe)###

[ysh329/darknet-to-caffe-model-convertor](https://github.com/ysh329/darknet-to-caffe-model-convertor/)が元のようで、これもPytorchがいるみたい:thinking:

```shell-session: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](https://github.com/xingwangsfu/caffe-yolo)###

This is a caffe implementation of the YOLO

まだ試していない・・・

###[Lwplw/Darknet2Caffe](https://github.com/lwplw/darknet2caffe)###

まだ試していない・・・

###[Xilinx/ML-Suite](https://github.com/Xilinx/ml-suite/tree/master/apps/yolo)###

まだ試していない・・・

###[Abars/Darknet2Caffe](https://github.com/abars/darknet2caffe)###

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

```shell-session
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](https://github.com/karolmajek/darknet)###

4KのVideo入力を、Tiny YOLOで認識しているらしい・・・　[4K Tiny YOLO Object Detection](https://www.youtube.com/watch?v=I4nfzaF1_QY)



#prototxtとcaffemodelって何だ？#

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

###[Caffeの実装理解のために](https://qiita.com/Hiroki11x/items/7017ac0c03df8011b53c)###

###[Caffeの学習済みモデルの利用](https://www.renom.jp/ja/notebooks/tutorial/image_processing/caffe2renom/notebook.html)###
<a href="https://gridpredict.jp/">株式会社グリッド</a>の、<a href="https://www.renom.jp/ja/info/about/index.html">ReNoM</a>という機械学習フレームワークのチュートリアルページに辿り着いた。

###[Caffe: Tutorial: Blob, 層, そしてネット : Caffe モデルの解剖]()###
<a href="http://torch.classcat.com">株式会社クラスキャット</a>の、Caffeの本家サイトの*[Tutorial – Blobs, Layers, and Nets: anatomy of a Caffe model](http://caffe.berkeleyvision.org/tutorial/net_layer_blob.html)*を翻訳・補足したページに辿り着いた。


###[Caffeを通してCNNを理解する #1](http://mugichoko.hatenablog.com/entry/2017/11/22/183200)###
###[Caffeを通してCNNを理解する #2](http://mugichoko.hatenablog.com/entry/2017/11/24/222459)###

###[Caffeによるシーン認識(8分類問題)](http://seiya-kumada.blogspot.com/2015/07/caffe.html)###

Seiya.Kumadaさんのブログ

###[YOLOv3の使い方](https://orizuru.io/blog/machine-learning/yolov3/)###

Seiya.Kumadaさんのブログ

#prototxtの可視化#

####[How do you visualize neural network architectures?](https://datascience.stackexchange.com/questions/12851/how-do-you-visualize-neural-network-architectures)####
に投稿されていたものの中から、

####[Netron](https://github.com/lutzroeder/Netron)####

<img src="https://qiita-image-store.s3.amazonaws.com/0/75262/6ca7e69d-85cd-4d52-83a1-bf6f6415a7a7.png" width="640">


####[Netscope](https://ethereon.github.io/netscope/quickstart.html)####

<img src="https://qiita-image-store.s3.amazonaws.com/0/75262/34138719-65b3-0bf9-a0e3-fddb6e18d84d.png" width="280">

#Ubuntu16.04, Docker, Caffe#
[こちらへ](https://qiita.com/yka2ki/items/3d966feea2d4716a7bfc#ubuntu1604%E3%81%AEdocker%E3%81%A7caffe%E3%82%92%E5%8B%95%E3%81%8B%E3%81%97%E3%81%A6%E3%81%BF%E3%82%8B)

取り敢えず、ここまで。