How to do Predictions in Caffe?

7,633 views
Skip to first unread message

Prabhu

unread,
Jan 28, 2015, 11:20:09 AM1/28/15
to caffe...@googlegroups.com
How to do predictions in caffe? i load grayscale images 96x96 pixels from csv file to predict.
i modified python script like this.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


# Make sure that caffe is on the python path:
#caffe_root = './caffe/'  # this file is expected to be in {caffe_root}/examples
#import sys
#sys.path.append(caffe_root + 'python')

import caffe

# Set the right path to your model definition file, pretrained model weights,
# and the image you would like to classify.
MODEL_FILE
= './facialkp.prototxt'
PRETRAINED
= './tmp_iter_1000_1.94e.caffemodel'


net
= caffe.Classifier(MODEL_FILE, PRETRAINED,
                       
None,
                       
None,
                       image_dims
=(96, 96)
           
)
caffe
.set_phase_test()
caffe
.set_mode_cpu()

df
= pd.read_csv('test.csv',header=0)

df
['Image'] = df['Image'].apply(lambda im: np.fromstring(im, sep=' ') )
X
= np.vstack (df['Image'].values) / 255
X
= X.astype(np.float32)
X
= X.reshape((-1,1,96,96))


for k in xrange(len(X)):
 input
= X[k]
 prediction
= net.predict([input])
 
print 'Predicted:', prediction



but it is not working

pbu@pbu-OptiPlex-740-Enhanced:~/Desktop$ python output.py
Traceback (most recent call last):
 
File "output.py", line 22, in <module>
    image_dims
=(96, 96)
TypeError: __init__() got multiple values for keyword argument 'image_dims'



what exactly is the problem here?


vpestret

unread,
Jan 29, 2015, 10:50:31 AM1/29/15
to caffe...@googlegroups.com
I think you should remove all two  None, from Classifier consturctor call

olddocks

unread,
Jan 29, 2015, 11:51:20 AM1/29/15
to caffe...@googlegroups.com
i changed

net = caffe.Classifier(MODEL_FILE, PRETRAINED, image_dims=(96,96) )



it still there is a problem

Traceback (most recent call last):

 
File "output.py", line 19, in <module>
    net
= caffe.Classifier(MODEL_FILE, PRETRAINED, image_dims=(96, 96) )
 
File "/home/pbu/Desktop/caffe/python/caffe/classifier.py", line 43, in __init__
   
self.crop_dims = np.array(self.blobs[self.inputs[0]].data.shape[2:])
IndexError: list index out of range

Evan Shelhamer

unread,
Jan 29, 2015, 11:57:58 AM1/29/15
to olddocks, caffe...@googlegroups.com
Either instantiate a `caffe.Net(MODEL_FILE, PRETRAINED)` and call `net.forward()` to process batch-by-batch IF you have a data layer in the model definition or instantiate a `caffe.Net` or `caffe.Classifier` and set all the preprocessing options IF you have input fields.

Note that we do have a plan to simplify IO for deployment / the interfaces, namely https://github.com/BVLC/caffe/issues/1245.

Evan Shelhamer

--
You received this message because you are subscribed to the Google Groups "Caffe Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caffe-users...@googlegroups.com.
To post to this group, send email to caffe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/caffe-users/299e89f9-00d6-43dc-b502-ed405d0b4db7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Evan Shelhamer

unread,
Jan 29, 2015, 12:09:49 PM1/29/15
to olddocks, caffe...@googlegroups.com
Oh, the list index out of range is because `net.inputs` is the list of *inputs* defined by the input fields like here: https://github.com/BVLC/caffe/blob/master/examples/mnist/lenet.prototxt#L2-L6.

You cannot combine a data layer with the Python preprocessing code. Your model has an HDF5 layer that doesn't count as a bare input. The Python preprocessing coded can be called to make a blob to give to the network, but can't be called on a blob from a data layer during forward.

Note that https://github.com/BVLC/caffe/pull/1703 with its Python layer will make it easy to do custom data transformations in combination with any data layer like HDF5 or Memory although not necessarily at full speed.

Evan Shelhamer

Prabhu

unread,
Jan 29, 2015, 12:30:28 PM1/29/15
to caffe...@googlegroups.com, old....@gmail.com
Oh, the list index out of range is because `net.inputs` is the list of *inputs* defined by the input fields like here: https://github.com/BVLC/caffe/blob/master/examples/mnist/lenet.prototxt#L2-L6.

No i dont explicity define the dims in prototxt, rather i import from hdf5. Below is my layers


name
: "FKPReg"

layers
{
  name
: "fkp"
  top
: "data"
  top
: "label"
  type
: HDF5_DATA
  hdf5_data_param
{
   source
: "train.txt"
   batch_size
: 100
 
}
    include
: { phase: TRAIN }
 
}

layers
{
  name
: "data"
  type
: HDF5_DATA
  top
: "data"
  top
: "label"
  hdf5_data_param
{
    source
: "test.txt"
    batch_size
: 100
   
 
}
  include
: { phase: TEST }
}

layers
{
  name
: "conv1"
  type
: CONVOLUTION
  bottom
: "data"
  top
: "conv1"
  convolution_param
{
    num_output
: 96
    kernel_size
: 11
    stride
: 4
 
}
}
layers
{
  name
: "relu1"
  type
: RELU
  bottom
: "conv1"
  top
: "conv1"
}
layers
{
  name
: "pool1"
  type
: POOLING
  bottom
: "conv1"
  top
: "pool1"
  pooling_param
{
    pool
: MAX
    kernel_size
: 3
    stride
: 2
 
}
}
layers
{
  name
: "norm1"
  type
: LRN
  bottom
: "pool1"
  top
: "norm1"
  lrn_param
{
    local_size
: 5
    alpha
: 0.0001
    beta
: 0.75
 
}
}
layers
{
  name
: "conv2"
  type
: CONVOLUTION
  bottom
: "norm1"
  top
: "conv2"
  convolution_param
{
    num_output
: 256
    pad
: 2
    kernel_size
: 5
   
group: 2
 
}
}
layers
{
  name
: "relu2"
  type
: RELU
  bottom
: "conv2"
  top
: "conv2"
}
layers
{
  name
: "pool2"
  type
: POOLING
  bottom
: "conv2"
  top
: "pool2"
  pooling_param
{
    pool
: MAX
    kernel_size
: 3
    stride
: 2
 
}
}
layers
{
  name
: "norm2"
  type
: LRN
  bottom
: "pool2"
  top
: "norm2"
  lrn_param
{
    local_size
: 5
    alpha
: 0.0001
    beta
: 0.75
 
}
}
layers
{
  name
: "conv3"
  type
: CONVOLUTION
  bottom
: "norm2"
  top
: "conv3"
  convolution_param
{
    num_output
: 384
    pad
: 1
    kernel_size
: 3
 
}
}
layers
{
  name
: "relu3"
  type
: RELU
  bottom
: "conv3"
  top
: "conv3"
}
layers
{
  name
: "conv4"
  type
: CONVOLUTION
  bottom
: "conv3"
  top
: "conv4"
  convolution_param
{
    num_output
: 384
    pad
: 1
    kernel_size
: 3
   
group: 2
 
}
}
layers
{
  name
: "relu4"
  type
: RELU
  bottom
: "conv4"
  top
: "conv4"
}
layers
{
  name
: "conv5"
  type
: CONVOLUTION
  bottom
: "conv4"
  top
: "conv5"
  convolution_param
{
    num_output
: 256
    pad
: 1
    kernel_size
: 3
   
group: 2
 
}
}
layers
{
  name
: "relu5"
  type
: RELU
  bottom
: "conv5"
  top
: "conv5"
}



layers
{
  name
: "ip1"
  type
: INNER_PRODUCT
  bottom
: "conv5"
  top
: "ip1"  
  inner_product_param
{
    num_output
: 30
   
 
}
}



layers
{
  name
: "loss"
  type
: HINGE_LOSS
  bottom
: "ip1"
  bottom
: "label"
  top
: "loss"
}

Prabhu

unread,
Jan 29, 2015, 12:37:45 PM1/29/15
to caffe...@googlegroups.com, old....@gmail.com
@Evan


You cannot combine a data layer with the Python preprocessing code. Your model has an HDF5 layer that doesn't count as a bare input. The Python preprocessing coded can be called to make a blob to give to the network, but can't be called on a blob from a data layer during forward.

ARe you are saying that i have to write a new layer file for this and load it to predict?? What is the difference between caffe.Classifier() and caffe.Net() ???

1. I have about 1700 grayscale images to predict all in numpy/csv format. (images are not written to disk in png)
2. i have already trained the model successfully and have two snapshotted files, .caffemodel and .solverstate



Evan Shelhamer

unread,
Jan 29, 2015, 12:47:07 PM1/29/15
to Prabhu, caffe...@googlegroups.com, olddocks

No i dont explicity define the dims in prototxt

That's exactly why you can't call `caffe.Net.preprocess()` or use the `caffe.Classifier` class. These require `input` fields -- sorry if my previous comment wasn't clear: `Net.inputs` is a list populated by the `input` fields and NOT data layers.
 
ARe you are saying that i have to write a new layer file for this and load it to predict??

No, I'm saying you have to choose between a data layer or input fields. You could just save your 1700 grayscale images, pre-process them, and save to HDF5 then call the net with your data layer. The Python layer part was just hinting at future generality -- you don't need it.

What is the difference between caffe.Classifier() and caffe.Net() ???

caffe.Net is the real interface that can be used for any Net and caffe.Classifier is a convenience class that is essentially just for our examples. Lately it seems like this confuses more than helps at this point, so new examples are coming in Feb.

Evan Shelhamer

--
You received this message because you are subscribed to the Google Groups "Caffe Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caffe-users...@googlegroups.com.
To post to this group, send email to caffe...@googlegroups.com.

Artem Yankov

unread,
Feb 27, 2015, 7:28:16 PM2/27/15
to caffe...@googlegroups.com, gosu...@gmail.com, old....@gmail.com
Evan, I'm using it as you have described (if I understood correctly).

1. loaded test (unlabeled) image set to LMDB with all the preprocessing done.
2. Added data layer to my deploy.prototxt that fetches that imageset

then:

batch = net.forward()
batch["prob"] # contains predictions for the first batch.

If I want to get predictions for all batches (say 10) I just run net.forward() 10 times.
However if I keep running net.forward() after I should have gone all batches I still keep getting some results.
The question is what are those results? I'm following the process correctly?

OCR

unread,
Nov 29, 2016, 11:07:21 AM11/29/16
to Caffe Users, gosu...@gmail.com, old....@gmail.com
I have the same question which is what happens after getting the result of all batches and continue forwarding the network? Maybe starting from the first of the LMDB?

Example: we have 100 batches each contains 10 images. What is the result of 101'th forward?

shruti sneha

unread,
Jul 17, 2017, 5:36:22 AM7/17/17
to Caffe Users
Hi,

plz replace net = caffe.Classifier(......) with 
net = caffe.Net(model_file, pretrained, caffe.TEST)

and your code....
.....
out = net.forward( )
print out['prob'].argmax()


according to your requirement. Let me know if it helps

Best Regards,
Shruti

Ibrahim hassan

unread,
Aug 26, 2017, 1:39:51 AM8/26/17
to Caffe Users
Hi shruti sneha 

I need help how can i show the probability in percentage.? my model classify correctly but it show this kind of score
4.72822614e-10   5.38280531e-10   2.85725599e-09   9.99998689e-01
   6.27359320e-09   1.31910076e-06   2.30110202e-11   5.29880445e-11
   2.35529695e-12]

i want to print percentage just like e.g 92.06% can you please help me

Hamza bekkouri

unread,
Nov 1, 2017, 7:58:40 AM11/1/17
to Caffe Users
Hello Mr Prabhu,
Please I asked for how you gathered the images into one csv file as test Data to, make prediction based on caffe Model.
thank you  
Reply all
Reply to author
Forward
0 new messages