How to create mean.binaryproto for caffe out of a text file for data?

4.2k Views Asked by At

I am using two text files where each has paths to my validation / training images.

I now want to create a mean.binaryproto out of these images to give into my input layer. However, I only found examples where this is done using a leveldb input layer. I can create my own mean image easily using a python script, but I have no idea how to continue after this, so to say how to write the image as a binaryproto at the end of my script. Are there any pointers?

from PIL import Image
import numpy as np;

#Create mean image function
def create_mean(list_of_images):

    for i in range(0,len(list_of_images)):
        print list_of_images[i]
        if i == 0:
            n = np.int32(Image.open(list_of_images[i]));
        else:
            n = n +   np.int32(Image.open(list_of_images[i]));

    return np.uint8(np.double(n)/len(list_of_images))


#paths out of textfile,here to simplify as an array , usually comes out of a txt file 
#but that's not the issue
list_imgs = ['out.tiff','out2.tiff' ]


avg_img  = create_mean(list_imgs)



#Now how to write this into the needed .binaryproto
#.... ? 
2

There are 2 best solutions below

0
On BEST ANSWER

Since the Average Picture is given in a numpy array the caffe function can be used to write as .binaryproto

import caffe

blob = caffe.io.array_to_blobproto( avg_img)
with open( mean.binaryproto, 'wb' ) as f :
    f.write( blob.SerializeToString())
3
On

You can first convert your images to generate an lmdb database using the convert_imageset tool. Then, using that and the compute_image_mean tool you can generate a mean.binaryproto file.

See my answer to: How to convert .npy file into .binaryproto?