須要的文件列表:github
1. train.list : 訓練的圖片的絕對路徑 2. test.list : 用於測試的圖片的絕對路徑 3. labels.txt : 全部的類別,一行一個類 4. voc.data : darknet配置文件,記錄相關位置信息 5. cifar.cfg : 網絡配置文件
按照如下目錄結構進行構造:網絡
VOCdevkit VOC2017 JPEGImages train test
其中訓練和測試的比例設置:ide
而後轉到JPEGImages目錄下進行如下操做:測試
find `pwd`/train -name \*.jpg > train.list find `pwd`/test -name \*.jpg > test.list
構造labels.txt文件內容code
airplane automobile bird cat deer dog frog horse ship truck
構造voc.data文件中內容:orm
classes=10 #設置的類別個數 train = data/cifar/train.list #上邊構造的訓練列表 valid = data/cifar/test.list # 上邊構造的測試列表 labels = data/cifar/labels.txt # 記錄類別 backup = backup/ #訓練的網絡文件的位置 top=2 # 計算top-n的準確率
網絡配置文件的選擇:圖片
以AlexNet爲例:ci
[net] # Training # batch=128 # subdivisions=1 # Testing batch=1 subdivisions=1 height=227 width=227 channels=3 momentum=0.9 decay=0.0005 max_crop=256 learning_rate=0.01 policy=poly power=4 max_batches=800000 angle=7 hue = .1 saturation=.75 exposure=.75 aspect=.75 [convolutional] filters=96 size=11 stride=4 pad=0 activation=relu [maxpool] size=3 stride=2 padding=0 [convolutional] filters=256 size=5 stride=1 pad=1 activation=relu [maxpool] size=3 stride=2 padding=0 [convolutional] filters=384 size=3 stride=1 pad=1 activation=relu [convolutional] filters=384 size=3 stride=1 pad=1 activation=relu [convolutional] filters=256 size=3 stride=1 pad=1 activation=relu [maxpool] size=3 stride=2 padding=0 [connected] output=4096 activation=relu [dropout] probability=.5 [connected] output=4096 activation=relu [dropout] probability=.5 [connected] output=1000 activation=linear [softmax] groups=1
git clone https://github.com/pjreddie/darknet.git cd darknet make -j4
若是有GPU而且安裝了cuda8.0和cudnn6.0,請在Makefile中進行修改,將對應的CUDA=0改成CUDA=1.
darknet cfg -- AlexNet.cfg data -- voc.data, labels.txt, train.list, test.list
其中voc.data中的內容直接指到對應的文件上。
train命令
./darknet classifier train data/voc.data cfg/AlexNet.cfg
valid命令
./darknet classifier valid data/voc.data cfg/AlexNet.cfg backup AlexNet.backup
predict命令
./darknet classifier predict data/voc.data cfg/AlexNet.cfg backup AlexNet.backup ./cat.png
終端訓練後從新訓練
./darknet classifier train data/voc.data cfg/AlexNet.cfg backup/AlexNet.backup
設置訓練使用的GPU
-gpus 0,1
數據獲取
cd data wget https://pjreddie.com/media/files/cifar.tgz tar xzf cifar.tgz cd cifar find `pwd`/train -name \*.png > train.list find `pwd`/test -name \*.png > test.list cd ../..
選擇config文件
classes=10 train = data/cifar/train.list valid = data/cifar/test.list labels = data/cifar/labels.txt backup = backup/ top=2
建立網絡配置文件
cifar_small.cfg(官方提供)
[net] batch=128 subdivisions=1 height=28 width=28 channels=3 max_crop=32 min_crop=32 hue=.1 saturation=.75 exposure=.75 learning_rate=0.1 policy=poly power=4 max_batches = 5000 momentum=0.9 decay=0.0005 [convolutional] batch_normalize=1 filters=32 size=3 stride=1 pad=1 activation=leaky [maxpool] size=2 stride=2 [convolutional] batch_normalize=1 filters=16 size=1 stride=1 pad=1 activation=leaky [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=leaky [maxpool] size=2 stride=2 [convolutional] batch_normalize=1 filters=32 size=1 stride=1 pad=1 activation=leaky [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=leaky [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=leaky [convolutional] filters=10 size=1 stride=1 pad=1 activation=leaky [avgpool] [softmax]
訓練
訓練:./darknet classifier train cfg/cifar.data cfg/cifar_small.cfg
valid: ./darknet classifier valid cfg/cifar.data cfg/cifar_small.cfg backup/cifar_small.backup