我的實踐代碼以下:python
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
參考一:
圖片減去均值再訓練,會提升訓練速度和精度。所以,通常都會有這個操做。shell
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,修改:ui
#!/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."
這樣,均值文件就計算好了。this
參考三:google
(1) 在Caffe中做classification時常常須要使用均值文件,可是caffe本身提供的腳本只能將圖像數據轉換爲 binaryproto相似的形式 (2) 咱們在使用python接口時須要將npy形式的均值文件導入進來,而非binaryproto這樣的均值文件spa
google類如下發現可使用以下的代碼進行轉換: 代碼是我本身實際使用的,有註釋code
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形式的均值文件時,orm
正確率基本同樣(差別很小可是仍是驗證集合稍高)blog