Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert own dataset mean to numpy succeed but error when running the prediction. #420

Closed
teknosains opened this issue May 16, 2014 · 23 comments

Comments

@teknosains
Copy link

Using this script, i successfully converty the binaryproto mean into numpy
import sys
from caffe.proto import caffe_pb2
from convert import blobproto_to_array
import numpy as np

blob = caffe_pb2.BlobProto()
data = open("../../examples/test/my_mean.binaryproto", "rb").read()
blob.ParseFromString(data)
nparray = blobproto_to_array(blob)
f = file("my_mean.npy","wb")
np.save(f,nparray)
f.close()

but when I run the prediction i got this error

Traceback (most recent call last):
File "tester.py", line 23, in
scores = net.predict(caffe_root + 'examples/test/'+filename)
File "/home/ubuntu/caffe/caffe-dev/python/caffe/imagenet/wrapper.py", line 85, in predict
input_blob = [prepare_image(filename, self._center_only)]
File "/home/ubuntu/caffe/caffe-dev/python/caffe/imagenet/wrapper.py", line 65, in prepare_image
img_reshape -= IMAGENET_MEAN
ValueError: operands could not be broadcast together with shapes (256,256,3) (1,3,256,256) (256,256,3)

The prediction is just fine when using ilsvrc12 mean, but its error when i run my own mean from my dataset

Thx

@teknosains
Copy link
Author

I change wrapper.py , on prepare_image function

the_mean = IMAGENET_MEAN.reshape(256,256,3)

im not sure it is correct or not...but it works

@shelhamer
Copy link
Member

The image mean doesn't actually vary much pixel-to-pixel and channel-wise means work almost equally well so it doesn't matter too much.

At any rate, try it with the new python interface in the latest master. It's a bit more batteries-included.

@sunbaigui
Copy link

Hi @shelhamer ,
when I run @codetrash 's solution, I've occurred the same error, @codetrash just change the code in wrapper.py to "the_mean = IMAGENET_MEAN.reshape(256,256,3)". Does it right or not? I am new to caffe, it's a little bit hard for me to understand your last comment.
Is there any new features in master to support this kind convert? Looking forward to your early reply.

@teknosains
Copy link
Author

@sunbaigui are u using your own generated dataset from your own images ?

I had those error until i fix it by reshaping the mean IMAGENET_MEAN.reshape(256,256,3) .

but when i used the latest branch master, i did not get those errors anymore

@sunbaigui
Copy link

@codetrash which latest branch master do you really use, I am using the version download at 24 April, can you kindly give me your email or facebook? I'd like to ask you a few questions.

@teknosains
Copy link
Author

@sunbaigui im using master committed this month. here's my facebook : budyks

@sunbaigui
Copy link

@codetrash when I run it under the latest branch master, another error occurred below:
AttributeError: 'module' object has no attribute 'BufferedIOBase'

Have you met this error before?

@teknosains
Copy link
Author

@sunbaigui well, i never had it...

maybe you should check all installation dependencies

@shelhamer
Copy link
Member

Don't import/run Caffe inside the wrapper directory
CAFFE_ROOT/python/caffe. The caffe.io submodule is conflicting with the
python standard library io module because caffe/io.py is in the same dir
and had precedence.

Note your PYTHONPATH should point to the parent dir of caffe (python/caffe
that is) and not the module dir itself.

Le jeudi 29 mai 2014, sunbaigui [email protected] a écrit :

@codetrash https://github.com/codetrash when I run it under the latest
branch master, another error occurred below:
AttributeError: 'module' object has no attribute 'BufferedIOBase'

Have you met this error before?


Reply to this email directly or view it on GitHub
#420 (comment).

@Yangqing
Copy link
Member

If you would like to inspect the image mean, don't do reshape (this will mess up with the order). What you will need to do is

mean_channel_last = IMAGENET_MEAN[0].swapaxes(0,1).swapaxes(1,2)

so that you get a correct 3-channel mean file. You can then use e.g. pyplot to show the mean (It's a pretty boring, gray-ish image)

@sunbaigui
Copy link

@Yangqing actually, I just wanna generate corresponding npy format mean file from binaryproto.
I've generated the npy format mean file by using function below:
def convert(binaryproto_file_path, npy_file_path):
blob = caffe_pb2.BlobProto()
data = open(binaryproto_file_path, "rb").read()
blob.ParseFromString(data)
nparray = blobproto_to_array(blob)
try:
f = file(npy_file_path,"wb")
np.save(f,nparray)
f.close()
except:
print "save nparray to file %s error!" % npy_file_path

But when I run apdated wrapper.py to extract feature, there's an error occurred in code line:
img_reshape -= IMAGENET_MEAN

Error: img_reshape -= IMAGENET_MEAN
ValueError: operands could not be broadcast together with shapes (256,256,3) (1,3,256,256) (256,256,3)

So how should I modify this code line to make it right?

@drubiano
Copy link

drubiano commented Jun 2, 2014

Hi, I am working with this file as well. To fix the error "ValueError: operands could not be broadcast together with shapes" I simply changed the blobproto_to_array by removing the blob.num argument from the reshape. I made it into a new function as well so it does not mess with anything anywhere else.

@sunbaigui
Copy link

thanks to @drubiano
suddenly, I got @Yangqing 's idea, I am agree with @drubiano ,I've put the nparray tranfer code from @Yangqing in convert_binaryproto2npy.py file to make sure it does not mess with anything anywhere else.

@teknosains
Copy link
Author

I convert the mean binaryproto to npy and test it but error raised :

    Traceback (most recent call last):
   File "classify.py", line 143, in <module>
    main(sys.argv)
  File "classify.py", line 99, in main
   input_scale=args.input_scale, channel_swap=channel_swap)
  File "/home/ubuntu/Documents/caffe-master2/python/caffe/classifier.py", line 34, in __init__
  self.set_mean(self.inputs[0], mean_file)
    File "/home/ubuntu/Documents/caffe-master2/python/caffe/pycaffe.py", line 200, in _Net_set_mean
mean = caffe.io.resize_image(normal_mean.transpose((1,2,0)),
 ValueError: axes don't match array

until i tried @drubiano suggestion to remove blob.num :

   '''return np.array(blob.data).reshape(
            blob.num, blob.channels, blob.height, blob.width)'''
   #blob.num removeed
    return np.array(blob.data).reshape(blob.channels, blob.height, blob.width)

and it works, any idea what happened ? @sunbaigui @shelhamer @Yangqing

NOTE : the error occurred when im using the latest caffe (branch master) , with caffe-dev (branch dev) it works just fine

@shelhamer
Copy link
Member

The mean is for a single input, not an entire batch, so it should only have
dimensions K x H x W. Technically, this could be a blob of dimensions 1 x K
x H x W but as a coding convenience it is three dimensional.

On Tue, Jun 24, 2014 at 5:30 PM, Budy [email protected] wrote:

I convert the binaryproto to npy and test it but error raised :

Traceback (most recent call last):

File "classify.py", line 143, in
main(sys.argv)
File "classify.py", line 99, in main
input_scale=args.input_scale, channel_swap=channel_swap)
File "/home/ubuntu/Documents/caffe-master2/python/caffe/classifier.py", line 34, in init
self.set_mean(self.inputs[0], mean_file)
File "/home/ubuntu/Documents/caffe-master2/python/caffe/pycaffe.py", line 200, in _Net_set_mean
mean = caffe.io.resize_image(normal_mean.transpose((1,2,0)),
ValueError: axes don't match array

until i tried @drubiano https://github.com/drubiano suggestion to
remove blob.num :

'''return np.array(blob.data).reshape(
blob.num, blob.channels, blob.height, blob.width)'''
#blob.num removeed
return np.array(blob.data).reshape(blob.channels, blob.height, blob.width)

and it works, any idea what happened ? @sunbaigui
https://github.com/sunbaigui @shelhamer https://github.com/shelhamer
@Yangqing https://github.com/Yangqing


Reply to this email directly or view it on GitHub
#420 (comment).

@teknosains
Copy link
Author

@shelhamer u mean the Converter code (proto mean to npy) for single input only ?

@Cheng-Wang
Copy link

hello, I have similar error, how to fix it ? I tried those methods, but doesn't work, thanks!

811 08:28:01.393484 20592 net.cpp:74] Creating Layer prob
I0811 08:28:01.393498 20592 net.cpp:84] prob <- fc8
I0811 08:28:01.393509 20592 net.cpp:110] prob -> prob
I0811 08:28:01.393525 20592 net.cpp:125] Top shape: 10 10 1 1 (100)
I0811 08:28:01.393537 20592 net.cpp:151] prob needs backward computation.
I0811 08:28:01.393548 20592 net.cpp:162] This network produces output prob
I0811 08:28:01.393561 20592 net.cpp:173] Collecting Learning Rate and Weight Decay.
I0811 08:28:01.393580 20592 net.cpp:166] Network initialization done.
I0811 08:28:01.393594 20592 net.cpp:167] Memory required for Data 41943640
Traceback (most recent call last):
File "cheng_feature_visualization.py", line 26, in
scores = net.predict(caffe_root + 'examples/cheng_test/n00453396_157.jpg')
File "../caffe1/python/caffe/imagenet/wrapper.py", line 84, in predict
input_blob = [prepare_image(filename, self._center_only)]
File "../caffe1/python/caffe/imagenet/wrapper.py", line 64, in prepare_image
img_reshape -= IMAGENET_MEAN
ValueError: operands could not be broadcast together with shapes (256,256,3) (3,256,256) (256,256,3)

@AlesZurek
Copy link

I got the same problem
I convert the mean binaryproto to npy.
And when I want to use it in caffe.Classifier() I got error:
"axes don't match array"
Is the solution to remove blob.num still actual or is there any other better solution?
I have got actual (2014-09-20) version of master branch.

@sleepsophia
Copy link

I convert the mean binaryproto to npy.
And when I want to use it in caffe.Classifier() I got error:
"mean shape incompatible with input shape"
and when i remove blob.num ,i got the same error,is there any solution?

@laotao
Copy link

laotao commented Jun 2, 2015

@sunbaigui Where did you put convert_binaryproto2npy.py? Can you share it?

@adnan15110
Copy link

you can use the following code to convert between .binaryproto to .npy:

print "Usage: python convert_protomean.py proto.mean out.npy"
blob = caffe.proto.caffe_pb2.BlobProto()
BINARY_PROTO_FILE_NAME  ='image_mean.binaryproto'
BINARY_PROTO_FILE_PATH  = os.path.join(os.getcwd(),BINARY_PROTO_FILE_NAME)
NPY_FILE_NAME  ='image_mean.npy'
NPY_FILE_PATH  = os.path.join(os.getcwd(),NPY_FILE_NAME)

data = open( BINARY_PROTO_FILE_PATH, 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
np.save( NPY_FILE_PATH , out )

@SongEsther
Copy link

@sleepsophia Hi, I get the same problem with you ,have you already solved it ?

@SongEsther
Copy link

@Yangqing Could you help me? I gengrate my own mean.npy and follow http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb . But when I run the below:
load the mean ImageNet image (as distributed with Caffe) for subtraction
mu = np.load(caffe_root + 'python/caffe/imagenet/mean.npy')
mu = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values
print 'mean-subtracted values:', zip('BGR', mu)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR

my problem:
mean-subtracted values: [('B', array([ 211.32064002, 220.19243949, 226.30641338, 228.79393605,
229.45166288, 228.21699033, 225.68690327, 222.26552255,
218.41735295, 214.56371852, 210.7109702 , 207.73977334,
205.35947091, 203.82138116, 203.28422383, 203.61476408,
205.16159385, 207.72180993, 211.22096089, 215.12253734,
219.32449286, 223.22309222, 226.59998049, 228.92342867,
230.11787251, 229.7456883 , 224.77160536, 218.04600089]))]
ValueError Traceback (most recent call last)
in ()
9
10 transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
---> 11 transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel
12 transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
13 transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR
/home/siat/caffe-master/python/caffe/io.py in set_mean(self, in_, mean)
257 raise ValueError('Mean shape invalid')
258 if ms != self.inputs[in_][1:]:
--> 259 raise ValueError('Mean shape incompatible with input shape.')
260 self.mean[in_] = mean
261
ValueError: Mean shape incompatible with input shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests