引言
最近跟小夥伴設計訓練了很多模型,我們主要通過看mAP
進行判斷這個模型的好壞,沒有將模型實際效果進行顯示觀察。這不,就想着寫個調用的程序進行顯示。
- 顯示訓練過程中的loss變化情況
- 顯示訓練過程中的lr變化情況
- 模型的inference time
- 模型效果的顯示 本來想自己寫,一看caffe裏有類似的代碼,真是太高興了,先看看caffe是怎麼做的。
Caffe Tools中的demo
caffe
caffe
可執行文件可以有不同的選項進行選擇功能,功能選擇是通過功能函數指針註冊的方式實現的,在tools/caffe.cpp
中有,其中的註冊功能部分大家有興趣可以學習一下,這塊還是很有趣的。
分析train log
在caffe/tools/extra
下有分析log的各種腳本,你可以使用gnuplot
繼續繪製,也可以採用python的matplot
-
如果想提取log的關鍵信息,可以查看
parse_log.sh
或者parse_log.py
-
如果想繪製採用繪製
python tools/extra/plot_training_log.py 2 examples/ooxx/result/result.png jobs/ResNet/VOC0712/OOXX_321x321/ResNet_VOC0712_OOXX_321x321.log
This script mainly serves as the basis of your customizations. Customization is a must. You can copy, paste, edit them in whatever way you want. Be warned that the fields in the training log may change in the future. You had better check the data files and change the mapping from field name to field index in create_field_index before designing your own plots.
Usage:
./plot_training_log.py chart_type[0-7] /where/to/save.png /path/to/first.log ...
Notes:
1. Supporting multiple logs.
2. Log file name must end with the lower-cased ".log".
Supported chart types:
0: Test accuracy vs. Iters
1: Test accuracy vs. Seconds
2: Test loss vs. Iters
3: Test loss vs. Seconds
4: Train learning rate vs. Iters
5: Train learning rate vs. Seconds
6: Train loss vs. Iters
7: Train loss vs. Seconds
顯示模型結果
- classification
./build/examples/cpp_classification/classification.bin \
models/bvlc_reference_caffenet/deploy.prototxt \
models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \
data/ilsvrc12/imagenet_mean.binaryproto \
data/ilsvrc12/synset_words.txt \
examples/images/cat.jpg
The output should look like this:
---------- Prediction for examples/images/cat.jpg ----------
0.3134 - "n02123045 tabby, tabby cat"
0.2380 - "n02123159 tiger cat"
0.1235 - "n02124075 Egyptian cat"
0.1003 - "n02119022 red fox, Vulpes vulpes"
0.0715 - "n02127052 lynx, catamount"
-
ssd_detection ssd_detection腳本
-
stairsNet detection stairnet的結果展示腳本
其中需要配置一些依賴文件信息
# caffe的root路勁
caffe_root=
labelmap_file = 'data/VOC0712/labelmap_voc.prototxt'
model_def = 'models/ResNet/VOC0712/OOXX_321x321/deploy.prototxt'
model_weights = 'models/ResNet/VOC0712/OOXX_321x321/ResNet_VOC0712_OOXX_321x321_iter_70000.caffemodel'
image_dir = "examples/ooxx/test"
save_dir = "examples/ooxx/result"
對多個snapshot模型進行打分
- 首先運行模型自帶的score腳本, 如
examples/ssd/score_ssd_pascal.py
,該腳本會調用當前最新的model進行評測,在jobs的子目錄下生成一個XXXXX_score的路徑,其中包含solver.prototxt等等文件。然後ctrl+C暫停運行。 - 運行腳本
model score script
(先去玩幾個小時,時間很漫長的...),將會在jobs的某個路徑下找到生成的各個模型對應的shell腳本和log文件。
Inference Time
- (These example calls require you complete the LeNet / MNIST example first.) time LeNet training on CPU for 10 iterations
./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10
- time LeNet training on GPU for the default 50 iterations
./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -gpu 0
- time a model architecture with the given weights on no GPU for 10 iterations
./build/tools/caffe time --model=models/ResNet/VOC0712/OOXX_321x321/deploy.prototxt --weights models/ResNet/VOC0712/OOXX_321x321/ResNet_VOC0712_OOXX_321x321_iter_115000.caffemodel --iterations 10
爲什麼要用Google Protocol Buffer序列化協議
# | protobuf | jackson | xstream | serialization | hessian2 | hessian2壓縮 | hessian 1 |
---|---|---|---|---|---|---|---|
序列化 ns | 1154 | 5421 | 92406 | 10189 | 26794 | 100766 | 29027 |
反序列化ns | 1334 | 8743 | 117329 | 64027 | 37871 | 188432 | 37596 |
bytes | 97 | 311 | 664 | 824 | 374 | 283 | 495 |
-
protobuf 不管是處理時間上,還是空間佔用上都優於現有的其他序列化方式。內存暫用是java序列化的1/9, 時間也是差了一個數量級,一次操作在1us左右。缺點:就是對象結構體有限制,只適合於內部系統使用。
-
json格式在空間佔用還是有一些優勢,是java序列化的1/2.6。序列化和反序列化處理時間上差不多,也就在5us。當然這次使用的jackson,如果使用普通的jsonlib可能沒有這樣好的性能,jsonlib估計跟java序列化差不多。
-
xml相比於java序列化來說,空間佔用上有點優勢,但不明顯。處理時間上比java序列化多了一個數量級,在100us左右。
-
以前一種的java序列化,表現得有些失望
-
hessian測試有點意外,具體序列化數據上還步入json。性能上也不如jackjson,輸得比較徹底。
-
hessian使用壓縮,雖然在字節上有20%以上的空間提升,但性能上差了4,5倍,典型的以時間換空間。總的來說還是google protobuf比較給力 以後在內部系統,數據cache存儲上可以考慮使用protobuf。跟外部系統交互上可以考慮使用json。
參考文獻
-
[estimate Inference time from average forward pass time in caffe]http://stackoverflow.com/questions/36867591/how-to-estimate-inference-time-from-average-forward-pass-time-in-caffe
-
[caffe interface manual]http://caffe.berkeleyvision.org/tutorial/interfaces.html