caffe icon indicating copy to clipboard operation
caffe copied to clipboard

how to classify 1-channel input with python wrapper

Open shuokay opened this issue 12 years ago • 8 comments

I am trying to classify handwritten Chinese characters using caffe. My dataset is grayscale. I have trained my net and now I want to make predications. However I met problems when using python wrapper. For example: channel_swapper. I have been trying to make python wrapper flexible to 1-channel input, but I am not very familiar with caffe. I have found #462 , but that solution can not handle my problem completely. so, can anyone kindly tell me how to modify the python wrapper to handle 1-channel input?

shuokay avatar Jun 11 '14 10:06 shuokay

See 63c7429 for grayscale inputs in Python, and #469 for grayscale in the Caffe tools.

shelhamer avatar Jun 11 '14 17:06 shelhamer

Regarding channel_swap, just leave it unset since there's nothing to swap.

shelhamer avatar Jun 12 '14 01:06 shelhamer

Hi @shelhamer , thanks for you help. now the caffe handled grayscale images input correctly. I can create my own classifier: classifier = caffe.Classifier(my_model_file, my_pretrained_file) but, when make prediction: predictions = classifier.predict(inputs,oversample=False) there are several errors:

  1. if I unset input_scale, the wrapper complain: Traceback (most recent call last): File "pred.py", line 24, in predictions = classifier.predict(inputs,oversample=False) File "/home/gys/caffe/python/caffe/classifier.py", line 77, in predict for in_ in inputs]) File "/home/gys/caffe/python/caffe/pycaffe.py", line 260, in _Net_preprocess input_scale = self.input_scale.get(input_name) AttributeError: 'Classifier' object has no attribute 'input_scale'

2.if I set the input_scale=255: classifier = caffe.Classifier(my_model_file, my_pretrained_file,input_scale=255) the wrapper complain: Traceback (most recent call last): File "pred.py", line 24, in predictions = classifier.predict(inputs,oversample=False) File "/home/gys/caffe/python/caffe/classifier.py", line 77, in predict for in_ in inputs]) File "/home/gys/caffe/python/caffe/pycaffe.py", line 261, in _Net_preprocess channel_order = self.channel_swap.get(input_name) AttributeError: 'Classifier' object has no attribute 'channel_swap' 3. if I set channel_swap(you have tell me unset channel_swap, I just want to see what will happen), the wrapper give similar complain:AttributeError: 'Classifier' object has no attribute 'mean' It seems that I must set all parameter. I dont know what wrong with my code or my prototxt file.

here is my my_model_file: name: "LeNet-test" input: "data" input_dim: 1 input_dim: 1 input_dim: 24 input_dim: 24 layers { name: "conv1" type: CONVOLUTION bottom: "data" top: "conv1" convolution_param { num_output: 20 kernel_size: 5 stride: 1 } } layers { name: "pool1" type: POOLING bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 2 stride: 2 } } layers { name: "conv2" type: CONVOLUTION bottom: "pool1" top: "conv2" convolution_param { num_output: 50 kernel_size: 5 stride: 1 } } layers { name: "pool2" type: POOLING bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 2 stride: 2 } } layers { name: "ip1" type: INNER_PRODUCT bottom: "pool2" top: "ip1" inner_product_param { num_output: 500 } } layers { name: "relu1" type: RELU bottom: "ip1" top: "ip1" } layers { name: "ip2" type: INNER_PRODUCT bottom: "ip1" top: "ip2" inner_product_param { num_output: 3904 } } layers { name: "prob" type: SOFTMAX bottom: "ip2" top: "prob" }

shuokay avatar Jun 12 '14 10:06 shuokay

hi @shelhamer I have tested the Image Classification [notebook] example, I found if I create the net by: net = caffe.Classifier(MODEL_FILE, PRETRAINED) rather than net = caffe.Classifier(MODEL_FILE, PRETRAINED, mean_file=caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy', channel_swap=(2,1,0), input_scale=255) the python rise the same error so I think I can't omit mean_file, channel_swap and input_scale, is it right?

shuokay avatar Jun 13 '14 16:06 shuokay

Greetings,

Just a follow up question, do we need to always set mean_file, channel_swap and input_scale while extracting features in the python code?

@shuokay , Can you please tell me how did you go about solving the problem of using pre-trained Lenet model to extract features for gray scale images with a dimension of 28x28?

I am getting an error as follows:

self.blobs[in_].data[...] = blob ValueError: could not broadcast input array from shape (1,3,28,28) into shape (1,1,28,28)

Thanks in advance.

Venkatesh-Murthy avatar Jul 07 '14 08:07 Venkatesh-Murthy

I think you are using the color images, pls use grayscale images. I your are suing convert_imageset.bin to convert your image data to leveldb format, pls using the -g option.

Best regards Yushu Gao

On Mon, Jul 7, 2014 at 4:03 PM, Venkatesh-Murthy [email protected] wrote:

Greetings,

Just a follow up question, do we need to always set mean_file, channel_swap and input_scale while extracting features in the python code?

@shuokay https://github.com/shuokay , Can you please tell me how did you go about solving the problem of using pre-trained Lenet model to extract features for gray scale images with a dimension of 28x28?

I am getting an error as follows:

self.blobs[in_].data[...] = blob ValueError: could not broadcast input array from shape (1,3,28,28) into shape (1,1,28,28)

Thanks in advance.

Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/491#issuecomment-48150567.

shuokay avatar Jul 09 '14 09:07 shuokay

Thanks a lot @shuokay.

I was able to extract features for gray scale images with LeNet model but now my issue is I have all the features extracted in leveldb format, any idea how to convert it into regular format? so that I can train SVM on it.

Venkatesh-Murthy avatar Jul 24 '14 05:07 Venkatesh-Murthy

Hi friend @shuokay I have the same error as your

  1. if I unset input_scale, the wrapper complain: Traceback (most recent call last): File "pred.py", line 24, in predictions = classifier.predict(inputs,oversample=False) File "/home/gys/caffe/python/caffe/classifier.py", line 77, in predict for in_ in inputs]) File "/home/gys/caffe/python/caffe/pycaffe.py", line 260, in _Net_preprocess input_scale = self.input_scale.get(input_name) AttributeError: 'Classifier' object has no attribute 'input_scale'

2.if I set the input_scale=255: classifier = caffe.Classifier(my_model_file, my_pretrained_file,input_scale=255) the wrapper complain: Traceback (most recent call last): File "pred.py", line 24, in predictions = classifier.predict(inputs,oversample=False) File "/home/gys/caffe/python/caffe/classifier.py", line 77, in predict for in_ in inputs]) File "/home/gys/caffe/python/caffe/pycaffe.py", line 261, in _Net_preprocess channel_order = self.channel_swap.get(input_name) AttributeError: 'Classifier' object has no attribute 'channel_swap' 3. if I set channel_swap(you have tell me unset channel_swap, I just want to see what will happen), the wrapper give similar complain:AttributeError: 'Classifier' object has no attribute 'mean'

could you please tell me how did you do to solve this problem??

yinyuebabay avatar Apr 24 '16 01:04 yinyuebabay