8.caffe:make_mean.sh( 数据平均化 )

我的实践代码以下: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

相关文章
相关标签/搜索