8.caffe:make_mean.sh( 數據平均化 )


個人實踐代碼如下:

 1 #!/usr/bin/env sh
 2 # Compute the mean image from the imagenet training lmdb
 3 # N.B. this is available in data/ilsvrc12
 4 
 5 EXAMPLE=/home/wp/CAFFE/caffe-master/myself/00b
 6 DATA=/home/wp/CAFFE/caffe-master/myself/00b
 7 TOOLS=build/tools
 8 
 9 $TOOLS/compute_image_mean $EXAMPLE/00b_train_lmdb \
10   $DATA/00bmean.binaryproto
11 
12 echo "Done."
13 
14 # cd CAFFE/caffe-master
15 # sh ./myself/00b/make_00b_mean.sh

參考一:
圖片減去均值再訓練,會提高訓練速度和精度。因此,一般都會有這個操作。

caffe程序提供了一個計算均值的文件compute_image_mean.cpp,我們直接使用就可以了

# sudo build/tools/compute_image_mean examples/myfile/img_train_lmdb examples/myfile/mean.binaryproto
compute_image_mean帶兩個參數,第一個參數是lmdb訓練數據位置,第二個參數設定均值文件的名字及保存路徑。 
運行成功后,會在 examples/myfile/ 下面生成一個mean.binaryproto的均值文件。

參考二:

接着,計算均值,打開make_imagenet_mean.sh,修改:

#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12

EXAMPLE=examples/imagenet
DATA=examples/imagenet 
TOOLS=build/tools

$TOOLS/compute_image_mean $EXAMPLE/mydata_train_lmdb \ #改成你的lmdb
$DATA/mydata_mean.binaryproto #生成的均值文件名,可修改

echo "Done."

 這樣,均值文件就計算好了。

參考三:

關於均值文件

(1) 在Caffe中作classification時經常需要使用均值文件,但是caffe自己提供的腳本只能將圖像數據轉換為 binaryproto類似的形式 (2) 我們在使用python接口時需要將npy形式的均值文件導入進來,而非binaryproto這樣的均值文件

均值文件形式之間的轉換

google類以下發現可以使用如下的代碼進行轉換: 代碼是我自己實際使用的,有注釋

import PIL import Image import sys import time import os import numpy as np from matplotlib import pyplot as plt start = time.time() # Make sure that caffe is on the python path caffe_root = '/home/gavinzhou/caffe-master/' sys.path.insert(0, caffe_root + 'python') import caffe # "source" is the binary file converted by the command shell # "des" is the binary file with python format converted from "source" source = caffe_root + 'gavinzhou_LAB/alexnet/GF18_mean.binaryproto' des = caffe_root + 'gavinzhou_LAB/alexnet/GF18_mean.npy' # BlobProto object blob = caffe.proto.caffe_pb2.BlobProto() data = open( source , 'rb' ).read() # parsing source data blob.ParseFromString(data) # convert to npy format arr = np.array( caffe.io.blobproto_to_array(blob) ) out = arr[0] # save the converted result np.save( des , out )

實際測試時,驗證數據集使用binaryproto形式的均值文件和測試數據集使用npy形式的均值文件時,

正確率基本一樣(差異很小但是還是驗證集合稍高)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM