How to use binaryproto file in Python

1,978 views
Skip to first unread message

Daniela G

unread,
May 11, 2016, 9:09:12 AM5/11/16
to Caffe Users
Hello,

I'm following this example since I'm trying to test my data with a trained net, using the deploy.prototxt file, in Python.
Since I can't declare the mean.binaryproto file in deploy.prototxt, I need to load it in Python to transform my data.

I tried to use the following code but when I run it, the Spyder IDE crashes.

MEAN_FILE = '../examples/test/mean.binaryproto'

# Open mean.binaryproto file
blob
= caffe.proto.caffe_pb2.BlobProto()
data
= open(MEAN_FILE , 'rb').read()
blob
.ParseFromString(data)
mean_arr
= caffe.io.blobproto_to_array(blob)


Is there any other possibility to use the mean file?

Thank you

SRQ

unread,
May 11, 2016, 9:16:15 AM5/11/16
to Caffe Users
Change

mean_arr = caffe.io.blobproto_to_array(blob)

to

mean_arr = np.array(caffe.io.blobproto_to_array(blob))

Maybe this will work.

Daniela G

unread,
May 11, 2016, 9:37:42 AM5/11/16
to Caffe Users
The problem remains :/
It doesn't give me any error message, the IDE just stops working.

SRQ

unread,
May 11, 2016, 9:39:56 AM5/11/16
to Caffe Users
write it in a local editor and manually compile if that is an option.

Daniela G

unread,
May 11, 2016, 11:01:06 AM5/11/16
to Caffe Users
It works through command line. Thank you.
I just don't get why Spyder stops working :/

Erdem Akagündüz

unread,
May 23, 2017, 3:52:51 AM5/23/17
to Caffe Users
Hello,

I know this topic is a bit old but I need help on the same issue. 

I need to convert the attached binaryproto to a MATLAB readable format.

I am not a python/caffe user yet but trying to be one soon. I have an urgency. Would any of you be kind convert the attached binaryproto to a mat file. I think the below code is sufficient.

thanks in advance

Erdem

caffe_root = '/.../'  # 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
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( '/.../mean.binaryproto', 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
np.save('/.../mean' , out )     #first,save it to *.npy,then convert it to *.mat
import scipy.io as sio
data=np.load('/.../mean.npy')
sio.savemat('/.../data_mean.mat',{'data':data})   
mean.binaryproto
Reply all
Reply to author
Forward
0 new messages