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形式的均值文件时,

正确率基本一样(差异很小但是还是验证集合稍高)

原文地址:https://www.cnblogs.com/carle-09/p/5779178.html