Caffe: Problem saving mean image as binaryproto in python

Created on 5 Dec 2016  路  3Comments  路  Source: BVLC/caffe

I'm trying to save my mean image as a binaryproto file using Python. I'm using the following code

blob = caffe.io.array_to_blobproto(np.asarray([mean_img]))
binaryproto_file = open(binary_file, 'wb+')
binaryproto_file.write(blob.SerializeToString())
binaryproto_file.close())

where the mean_image shape is [3,227,227]. I followed the suggestion in this post: https://groups.google.com/forum/#!searchin/caffe-users/binaryproto|sort:relevance/caffe-users/6eK459nwvMQ/Zx35GSbDWG8J

However, when I try to read the file I get the following error:
F1205 14:04:29.775773 2752512 data_transformer.cpp:257] Check failed: img_channels == data_mean_.channels() (3 vs. 227)

I'm using the code in the fine-tuning jupyter notebook that comes with caffe and my mean_image file as input for transform_param:

transform_param = dict(mirror=train, crop_size=227, mean_file='my_mean_image.binaryproto')

My caffe was compiled in mac os El Capitan using Anaconda.

Would anybody know how to fix this?

Thank you!

Most helpful comment

It worked!
Instead of using
blob = caffe.io.array_to_blobproto(np.asarray([mean_img]))
I used

    blob = caffe.io.array_to_blobproto(np.asarray(mean_img.reshape([1,3,227,227])))
    binaryproto_file = open(binary_file, 'wb+')
    binaryproto_file.write(blob.SerializeToString())
    binaryproto_file.close()

All 3 comments

Try to reshape mean_image to be [1,3,277,277].

Can you double check the shape of blob?

It worked!
Instead of using
blob = caffe.io.array_to_blobproto(np.asarray([mean_img]))
I used

    blob = caffe.io.array_to_blobproto(np.asarray(mean_img.reshape([1,3,227,227])))
    binaryproto_file = open(binary_file, 'wb+')
    binaryproto_file.write(blob.SerializeToString())
    binaryproto_file.close()

I had grayscale data cifar10 in png format using ImageData data reader, and had the same problem. I had to reshape the mean to [1,1,28,28]. Tried both [1,28,28] and [28,28,1] and neither worked. The original shape is just [28,28].
The code should check for len(blob.shape) and implicitly reshape.


Not fully related to this issue: I also tried using HDF5Data data layer. That one does not have any is_color and if I add my [28,28] images in it I got some cuda error about (3 vs. 0) after some trial I figured the shape should have been [1,28,28]. I wonder if there is any rule to these dimensions? and if they may change in the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

malreddysid picture malreddysid  路  3Comments

greatgao picture greatgao  路  3Comments

sdemyanov picture sdemyanov  路  3Comments

lixin7895123 picture lixin7895123  路  3Comments

vladislavdonchev picture vladislavdonchev  路  3Comments