introduction
One 、 Binary format mean calculation
Two 、python Mean value calculation of format
introductionAfter subtracting the mean from the picture , And then training and testing , It improves speed and accuracy . therefore , Generally, this operation can be found in various models .
So how does this mean come from , It's actually the average of all the training samples , After the calculation , Save as an average file , In future tests , You can directly use this mean to subtract , There is no need to recalculate the test image .
One 、 Binary format mean calculationcaffe The mean data format used in is binaryproto, The author provides us with a file for calculating the mean value compute_image_mean.cpp, Put it in caffe In the root directory tools In the folder .
The compiled executable is placed in build/tools/ below , We can just call it directly
# sudo build/tools/compute_image_mean examples/mnist/mnist_train_lmdb examples/mnist/mean.binaryproto
With two parameters :
The first parameter :examples/mnist/mnist_train_lmdb, Indicates the data for which the mean value needs to be calculated , The format is lmdb Training data .
The second parameter :examples/mnist/mean.binaryproto, The calculated results are saved in a file .
Two 、python Mean value calculation of formatIf we want to use python Interface , Or we need to do feature visualization , Maybe we'll use python Format mean file . First , We use it lmdb Formatted data , Calculate the mean value of binary format , then , To convert python Mean of format .
We can write a python Script to achieve :
#!/usr/bin/env pythonimport numpy as npimport sys,caffeif len(sys.argv)!=3: print "Usage: python convert_mean.py mean.binaryproto mean.npy" sys.exit()blob = caffe.proto.caffe_pb2.BlobProto()bin_mean = open( sys.argv[1] , 'rb' ).read()blob.ParseFromString(bin_mean)arr = np.array( caffe.io.blobproto_to_array(blob) )npy_mean = arr[0]np.save( sys.argv[2] , npy_mean )
Save this script as convert_mean.py
The invocation format is :
# sudo python convert_mean.py mean.binaryproto mean.npy
Among them mean.binaryproto Is the binary mean value calculated in the previous steps .
mean.npy That's what we need python Mean of format .
That's all python Format Caffe Picture data mean calculation learning details , More about python Format Caffe For information on mean value calculation, please pay attention to other relevant articles on software development network !