2

I'm using this code to load my net:

net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                   mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),
                   channel_swap=(2,1,0),
                   raw_scale=255,
                   image_dims=(256, 256))

I have doubts on three lines.

1- mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)

What is mean? Should I use this mean value or another? And if yes, where can I get custom mean value? I'm using a custom dataset.

2- channel_swap=(2,1,0)

What channel_swap means? And again, should I use this value or an custom value?

And the last

3- raw_scale=255

What is raw_scale? And what value should I use?

I'm using Cohn Kanade dataset. All images are 64x64 and in grayscale.

Carlos Porta
  • 1,224
  • 5
  • 19
  • 31

2 Answers2

4

The channel_swap is to reverse RGB into BGR, which is apparently necessary if you use a reference image net model, based on a comment in [1]. In your case the images are greyscale, so you probably do not have three channels. You might need to set it to (0, 0, 0), but even that might not help (I am unsure on the exact implementation of channel_swap). If that does not help, the simplest solution might be to preprocess you data by splitting every pixel into three values (RGB) with equal values. After that you might drop channel_swap altogether, because your channels have the same value, and swapping them is a no-op.

Mean is what will be subtracted from your input data to center it. (Remember that neural networks need the data to have zero mean, while the input images usually have positive mean, hence the need of the subtraction). The mean you subtract should be the same that was used for training, so using mean from the file associated with the model is correct. I am not sure, however, on whether you should call .mean(1) on it -- did you get that line from some example? If yes, then it is most likely the correct thing to do.

raw_scale is a scale of your input data. The model expects pixels to be normalized, so if your input data has values between 0 and 255, then raw_scale set to 255 is correct. If your data has values between 0 and 1, then raw_scale should be set to 1.

Finally, based on my understanding of the comment in [2] you do not need to provide image_dims

[1] https://github.com/BVLC/caffe/blob/master/python/caffe/io.py#L204

[2] https://github.com/BVLC/caffe/blob/master/python/caffe/classifier.py#L18

Ishamael
  • 12,583
  • 4
  • 34
  • 52
  • Thank you, I get this code on this [site](http://www.cc.gatech.edu/~zk15/deep_learning/classify_test.py). I not using imaginet, I just searched on google for 'caffe predict from pretrained model'. So, I have to change mean to (0,0,0), not use channel_swap and raw_scale to 1/126. Because 126 is my dataset grayscale mean. I'm right? – Carlos Porta Nov 17 '15 at 19:47
  • If 126 is your mean, then `raw_scale` is 255 (raw_scale is the range, not mean). For mean you need to use the mean of the dataset that was used to train the model you use. What exact model do you use? For `channel_swap`, in your case you do not need it, that's correct. – Ishamael Nov 17 '15 at 20:16
  • Ishamael, could you, please, explain me what do you mean for "What exact model do you use?". I did not understand it. Caffe and convnet are really recent to me. – Carlos Porta Nov 18 '15 at 00:15
  • What is the value of `MODEL_FILE` and `PRETRAINED_FILE`? – Ishamael Nov 18 '15 at 00:25
  • This is my [model_file](https://www.dropbox.com/s/s6hpibcd09iv4aw/ck.caffemodel?dl=0) – Carlos Porta Nov 18 '15 at 00:41
  • I'm in trouble to create my deploy file. I already removed the data layer, but I still get an error. [This is my train.prototxt file](https://bpaste.net/show/920701b6ca01). Could you help me to solve my problem or should I ask a new question? – Carlos Porta Nov 18 '15 at 00:43
  • It is always preferred to create a new question for the new problem, so that other people also see it, and can answer using answers instead of comments. It also helps those who find these questions later, since it makes both questions and answers more consistent with each other. – Ishamael Nov 18 '15 at 00:48
  • Ok, so like I said, that was my model_file. I'll create a new question and, after I resolve my deploy question, I'll ask here again. Ok? Could I post my new question link here? – Carlos Porta Nov 18 '15 at 01:10
  • Ishamael, [this is my question](http://stackoverflow.com/questions/33770190/how-to-create-caffe-deploy-from-train-prototxt) – Carlos Porta Nov 18 '15 at 01:22
2

I agree on comments of @Ishamael on channel_swap and mean. I just wanted to add further clarification on raw_scale. Assuming that images are loaded with caffe.io.load_image, values are always in the range of 0 to 1 [1]. Just to note that:

While Python represents images in [0, 1], certain Caffe models CaffeNet and AlexNet represent images in [0, 255] so the raw_scale these models must be 255.

And I think it's wise to check the input image values prior to feeding to the data layer of network in order to choose appropriate raw_scale.

Thank you.

[1] https://github.com/BVLC/caffe/blob/master/python/caffe/io.py#L224

accssharma
  • 157
  • 1
  • 10