YOLO v3による顔検出：02.Darknetで学習

# 概要
Darknet YOLO v3をWIDER FACEデータセットで学習させてweightを作成
weightとYOLO v3ネットワークを使って、KerasにコンバートしたYOLO v3モデルを構築
Keras YOLO v3モデルで顔検出
[過去に構築したモデル](https://qiita.com/search?utf8=%E2%9C%93&sort=&q=user%3Aha9kberry+tag%3AUTKFace)を使って、**検出した顔画像から性別・人種・年齢を予測**

これらのタスクを分割して掲載
- [YOLO v3による顔検出：01.データセット準備]
(https://qiita.com/ha9kberry/items/1bc113cfcd9892a9ddbd)
- YOLO v3による顔検出：02.Darknetで学習
- [YOLO v3による顔検出：03.Kerasで予測]
(https://qiita.com/ha9kberry/items/f75bd95fb2322c00af10)



# Darknet

## インストール
AWS EC2で実行

```
# Darknetをclone
$ git clone https://github.com/AlexeyAB/darknet.git
$ cd darknet

# Makefile編集（GPU=1、CUDNN=1に変更）
$ vi Makefile

# コンパイル
$ make

# 実行
$ ./darknet

# 下記が出力されればOK
usage: ./darknet <function>
```

## weightをダウンロード
Imagenetでの学習済みのweightをダウンロード

```
$ wget https://pjreddie.com/media/files/darknet53.conv.74
```

## 設定ファイル作成

### obj.data
```:cfg/obj.data
classes = 1
train  = face/train.txt
valid  = face/test.txt
names = cfg/obj.names
backup = backup/
```

classes：クラス数
train：trainデータ画像のパス一覧ファイル
valid：testデータ画像のパス一覧ファイル
names：クラス名一覧ファイル
backup：学習後のweightファイル保存先

### obj.name
```:cfg/obj.name
face
```

## yolov3.cfg編集
Line 3：コメントアウト
Line 4：コメントアウト
Line 6：batch=24に設定
Line 7：subdivisions=8に設定

```:cfg/yolov3.cfg
[net]
# Testing
# batch=1
# subdivisions=1
# Training
batch=24
subdivisions=8
...
```
**filters=(classes+5)*3**
Line 603：filters=18に設定
Line 610：classes=1に設定
Line 689：filters=18に設定
Line 696：classes=1に設定
Line 776：filters=18に設定
Line 783：classes=1に設定

```:cfg/yolov3.cfg
...
[convolutional]
size=1
stride=1
pad=1
filters=18
activation=linear


[yolo]
mask = 0,1,2
anchors = 10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326
classes=1
num=9
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1
```

## ディレクトリ構成
前回作成したデータセット（face）をdarknet直下に格納

```
darknet/
  ┣ backup/
  ┣ cfg/
  ┃  ┣ yolov3.cfg
  ┃  ┣ obj.data
  ┃  ┣ obj.name
  ┃  ...
  ┣ face/
  ...
  ┣ darknet
  ┣ Makefile
  ┣ darknet53.conv.74
  ...
```




## モデル学習
```
$./darknet detector train cfg/obj.data cfg/yolov3.cfg darknet53.conv.74
```
100iterationsごとにweight保存

```:backup/
yolov3_100.weights
yolov3_200.weights
...
yolov3_1100.weights
yolov3_1200.weights
```





avg lossに変化が見られなくなったので強制終了

```
...
1224: 8.434164, 7.634709 avg loss, 0.001000 rate, 6.211914 seconds, 78336 images
Loaded: 0.000082 seconds
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000022, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.830334, Class: 0.998110, Obj: 0.722482, No Obj: 0.001276, .5R: 1.000000, .75R: 1.000000,  count: 3
Region 106 Avg IOU: 0.351877, Class: 0.997746, Obj: 0.343769, No Obj: 0.007085, .5R: 0.271186, .75R: 0.000000,  count: 59
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000028, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.673452, Class: 0.981980, Obj: 0.384832, No Obj: 0.001454, .5R: 1.000000, .75R: 0.400000,  count: 5
Region 106 Avg IOU: 0.457167, Class: 0.973023, Obj: 0.306883, No Obj: 0.004004, .5R: 0.431034, .75R: 0.120690,  count: 58
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000028, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.694572, Class: 0.989655, Obj: 0.299587, No Obj: 0.001351, .5R: 1.000000, .75R: 0.333333,  count: 6
Region 106 Avg IOU: 0.572213, Class: 0.993285, Obj: 0.408671, No Obj: 0.001393, .5R: 0.714286, .75R: 0.285714,  count: 14
Region 82 Avg IOU: 0.677525, Class: 0.994951, Obj: 0.047155, No Obj: 0.001469, .5R: 1.000000, .75R: 0.500000,  count: 2
Region 94 Avg IOU: 0.736714, Class: 0.972360, Obj: 0.352567, No Obj: 0.000589, .5R: 1.000000, .75R: 0.000000,  count: 1
Region 106 Avg IOU: 0.558755, Class: 0.990499, Obj: 0.605316, No Obj: 0.002516, .5R: 0.615385, .75R: 0.076923,  count: 13
Region 82 Avg IOU: 0.769287, Class: 0.998308, Obj: 0.925071, No Obj: 0.001063, .5R: 1.000000, .75R: 1.000000,  count: 1
Region 94 Avg IOU: 0.804348, Class: 0.989604, Obj: 0.487220, No Obj: 0.000655, .5R: 1.000000, .75R: 1.000000,  count: 2
Region 106 Avg IOU: 0.407566, Class: 0.990047, Obj: 0.292463, No Obj: 0.001540, .5R: 0.571429, .75R: 0.142857,  count: 7
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000577, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.768268, Class: 0.997666, Obj: 0.782844, No Obj: 0.000980, .5R: 1.000000, .75R: 0.500000,  count: 2
Region 106 Avg IOU: 0.289653, Class: 0.995889, Obj: 0.679247, No Obj: 0.002009, .5R: 0.090909, .75R: 0.000000,  count: 11
Region 82 Avg IOU: 0.674502, Class: 0.999339, Obj: 0.973705, No Obj: 0.001093, .5R: 1.000000, .75R: 0.000000,  count: 1
Region 94 Avg IOU: 0.780698, Class: 0.998117, Obj: 0.617814, No Obj: 0.000402, .5R: 1.000000, .75R: 1.000000,  count: 1
Region 106 Avg IOU: 0.427390, Class: 0.997754, Obj: 0.424934, No Obj: 0.005473, .5R: 0.433333, .75R: 0.000000,  count: 90
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000295, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.651136, Class: 0.995051, Obj: 0.272277, No Obj: 0.000337, .5R: 1.000000, .75R: 0.000000,  count: 2
Region 106 Avg IOU: 0.363869, Class: 0.997585, Obj: 0.295904, No Obj: 0.005900, .5R: 0.303030, .75R: 0.015152,  count: 66
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000098, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.681434, Class: 0.998474, Obj: 0.503257, No Obj: 0.000774, .5R: 1.000000, .75R: 0.333333,  count: 3
Region 106 Avg IOU: 0.327224, Class: 0.994823, Obj: 0.376327, No Obj: 0.005351, .5R: 0.285714, .75R: 0.017857,  count: 56
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000022, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.660394, Class: 0.996295, Obj: 0.817968, No Obj: 0.000580, .5R: 1.000000, .75R: 0.000000,  count: 2
Region 106 Avg IOU: 0.311842, Class: 0.996340, Obj: 0.242430, No Obj: 0.002912, .5R: 0.325000, .75R: 0.025000,  count: 40
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000030, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.664672, Class: 0.994279, Obj: 0.249362, No Obj: 0.000237, .5R: 1.000000, .75R: 0.000000,  count: 1
Region 106 Avg IOU: 0.406206, Class: 0.988949, Obj: 0.262028, No Obj: 0.003189, .5R: 0.416667, .75R: 0.125000,  count: 24
Region 82 Avg IOU: 0.652377, Class: 0.988994, Obj: 0.037485, No Obj: 0.000304, .5R: 1.000000, .75R: 0.500000,  count: 2
Region 94 Avg IOU: 0.797908, Class: 0.985404, Obj: 0.156164, No Obj: 0.001240, .5R: 1.000000, .75R: 1.000000,  count: 2
Region 106 Avg IOU: 0.235209, Class: 0.997958, Obj: 0.445427, No Obj: 0.003285, .5R: 0.181818, .75R: 0.000000,  count: 55
Region 82 Avg IOU: 0.481452, Class: 0.951541, Obj: 0.001648, No Obj: 0.000034, .5R: 0.500000, .75R: 0.000000,  count: 2
Region 94 Avg IOU: 0.810755, Class: 0.987223, Obj: 0.831186, No Obj: 0.001385, .5R: 1.000000, .75R: 1.000000,  count: 2
Region 106 Avg IOU: 0.390754, Class: 0.997696, Obj: 0.284987, No Obj: 0.007060, .5R: 0.333333, .75R: 0.043011,  count: 93
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000029, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.722319, Class: 0.998787, Obj: 0.840885, No Obj: 0.000482, .5R: 1.000000, .75R: 0.000000,  count: 1
Region 106 Avg IOU: 0.371164, Class: 0.994526, Obj: 0.339715, No Obj: 0.003740, .5R: 0.285714, .75R: 0.085714,  count: 35
Region 82 Avg IOU: -nan, Class: -nan, Obj: -nan, No Obj: 0.000032, .5R: -nan, .75R: -nan,  count: 0
Region 94 Avg IOU: 0.593241, Class: 0.994242, Obj: 0.096606, No Obj: 0.000800, .5R: 0.750000, .75R: 0.250000,  count: 4
Region 106 Avg IOU: 0.549288, Class: 0.996601, Obj: 0.382373, No Obj: 0.001924, .5R: 0.500000, .75R: 0.312500,  count: 16
Region 82 Avg IOU: 0.618548, Class: 0.974710, Obj: 0.048116, No Obj: 0.000174, .5R: 1.000000, .75R: 0.000000,  count: 2
Region 94 Avg IOU: 0.734757, Class: 0.999906, Obj: 0.973093, No Obj: 0.000690, .5R: 1.000000, .75R: 0.000000,  count: 1
Region 106 Avg IOU: 0.717950, Class: 0.995706, Obj: 0.715195, No Obj: 0.002873, .5R: 0.916667, .75R: 0.583333,  count: 12
```
まだまだYOLOを把握しきれていない
今後、論文などを参考に深掘りしていきたい

今回の学習で生成されたweightを使って、Keras YOLO v3モデルを作成し、モデル予測する