caffe icon indicating copy to clipboard operation
caffe copied to clipboard

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

Open teknosains opened this issue 12 years ago • 23 comments

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 avatar May 16 '14 08:05 teknosains

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

teknosains avatar May 16 '14 09:05 teknosains

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.

shelhamer avatar May 22 '14 08:05 shelhamer

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.

sunbaigui avatar May 29 '14 03:05 sunbaigui

@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

teknosains avatar May 29 '14 05:05 teknosains

@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.

sunbaigui avatar May 29 '14 06:05 sunbaigui

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

teknosains avatar May 29 '14 06:05 teknosains

@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?

sunbaigui avatar May 29 '14 08:05 sunbaigui

@sunbaigui well, i never had it...

maybe you should check all installation dependencies

teknosains avatar May 29 '14 13:05 teknosains

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 https://github.com/BVLC/caffe/issues/420#issuecomment-44507488.

shelhamer avatar May 29 '14 16:05 shelhamer

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)

Yangqing avatar May 30 '14 23:05 Yangqing

@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?

sunbaigui avatar May 31 '14 07:05 sunbaigui

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.

drubiano avatar Jun 02 '14 16:06 drubiano

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.

sunbaigui avatar Jun 03 '14 02:06 sunbaigui

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

teknosains avatar Jun 24 '14 09:06 teknosains

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 https://github.com/BVLC/caffe/issues/420#issuecomment-46950311.

shelhamer avatar Jun 24 '14 13:06 shelhamer

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

teknosains avatar Jun 26 '14 05:06 teknosains

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)

Cheng-Wang avatar Aug 11 '14 06:08 Cheng-Wang

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.

AlesZurek avatar Sep 20 '14 16:09 AlesZurek

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?

sleepsophia avatar Apr 30 '15 07:04 sleepsophia

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

laotao avatar Jun 02 '15 02:06 laotao