Converting binaryproto to npy

1,150 views
Skip to first unread message

Gajus Polikaitis

unread,
Sep 30, 2015, 10:29:18 PM9/30/15
to Caffe Users
Hi everyone,

I need to convert my image mean file into a .npy, but the best I've been able to find to do that is this year old Python script that I think might be outdated:

import caffe
import numpy as np
import sys


if len(sys.argv) != 3:
   print "Usage: python convert_protomean.py proto.mean out.npy"
   sys.exit()


blob = caffe.proto.caffe_pb2.BlobProto()
data = open( sys.argv[1] , 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
np.save( sys.argv[2] , out )



When I try to run it, I get the following error:


Traceback (most recent call last):
  File "python/numpy.py", line 1, in <module>
    import caffe
  File "/home/ubuntu/caffe/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver
  File "/home/ubuntu/caffe/python/caffe/pycaffe.py", line 11, in <module>
    import numpy as np
  File "/home/ubuntu/caffe/python/numpy.py", line 11, in <module>
    blob = caffe.proto.caffe_pb2.BlobProto()
AttributeError: 'module' object has no attribute 'proto'


Swapping out 'proto' for 'io' in that last line yielded essentially the same error. Can anyone help me out here?

Bartosz Ludwiczuk

unread,
Oct 1, 2015, 3:08:55 AM10/1/15
to Caffe Users
Hi, 
here is my script for converting image mean to numpy. The script is run by:
python bin_to_np.py 100 mean_file.protobin mean_file.npy
where 100- image dimmension (I assume it is square, if not you can change it in code)

# Make sure that caffe is on the python path:
caffe_root = '/home/blcv/LIB/caffe/'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
import numpy as np

#Convert mean file produced by Caffe to numpy array, assume 3 chanels
#python bin_to_npy.py dim_image_mean path_to_mean_caffe name_output

channels = 3

a = caffe.io.caffe_pb2.BlobProto()
with open(sys.argv[2],'rb') as f:
  a.ParseFromString(f.read())

means=a.data
means=np.asarray(means)
print means.shape
s = int(sys.argv[1])
means=means.reshape(channels,s,s)
np.save(sys.argv[3],means)


Gajus Polikaitis

unread,
Oct 1, 2015, 3:52:46 AM10/1/15
to Caffe Users
Hi there,

Thanks a bunch for the script. Trying yours helped me determine what was wrong with the one I was trying. I got the same error message as before when I ran yours, but it looked like it was still trying to run the other script. I think it had to do with the file name. When I got rid of my other one I ran into some other errors, including:


Traceback (most recent call last):
  File "bin_to_np.py", line 19, in <module>
    means=means.reshape(channels,s,s)
ValueError: total size of new array must be unchanged

So I commented out that line and it seemed to work. Is resizing the array very critical, or am I alright having skipped that part?
Reply all
Reply to author
Forward
0 new messages