2

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
#.... ? 
Kev1n91
  • 3,553
  • 8
  • 46
  • 96

2 Answers2

1

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())
Kev1n91
  • 3,553
  • 8
  • 46
  • 96
0

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?

Community
  • 1
  • 1
Jayant Agrawal
  • 749
  • 8
  • 15
  • Is this much faster than the proposed answer? Cause otherwise it would just create an overhead. – Kev1n91 Jan 06 '17 at 20:32
  • Generally, people create an lmdb database for training, in that case it is easier to use compute_image_mean tool. But, you are right, if an lmdb database is not required, then the proposed answer would work faster. – Jayant Agrawal Jan 07 '17 at 16:19
  • I do not think that people generally create databases for training data. It is up to the implementation itself. If you look at the model zoo there is no tendency in what is used most. I guess that a textfile makes it easier to manage pre computational image processing steps – Kev1n91 Jan 07 '17 at 16:53