Using Classifier to predict TOP-5 accuracy in PyCaffe

316 views
Skip to first unread message

Seamus Mackenzie

unread,
Feb 23, 2016, 10:50:25 AM2/23/16
to Caffe Users
Hi Guys, I am being very unsuccessful in trying to implement a validation test with Oversampling enabled. I had a look at the caffe.Classifier class and some examples which seem to point to it being able to be initialised correctly. It then fails straight after the initialization step with :-

I0223 15:24:52.030282 24764 net.cpp:286] Network initialization done.
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/lib/python2.7/dist-packages/caffe/classifier.py", line 29, in __init__
    in_ = self.inputs[0]
IndexError: list index out of range

I know this has something to do with me missing something out in interfacing with the classifier class, The code below uses a batch size of 1 in order for the classifier to oversample individually for each image and validate the accuracy



import caffe
import numpy
caffe.set_mode_gpu()

net = caffe.Classifier("/home/seamus/Downloads/BVLC_GoogLeNet/train_val.prototxt",
               "/home/seamus/Downloads/BVLC_GoogLeNet/snapshot_iter_67.caffemodel",
                image_dims=(256, 256))

               
#Declaring accuracy variable and IterationTest - This defines how many forward passes the network #should be ran through
accuracy = 0
IterationTest=50000


#Loop for running specified number of forward passes
for i in range(IterationTest):
  # one iteration
  outputs = net.predict()
  accuracy+=net.blobs['accuracy-top5'].data
  print outputs


#Prints average accuracy over specified number of passes through the network
print('========================================================================')
avgAcc=accuracy/IterationTest
print 'accuracy = ', avgAcc


Please someone help, I'm totally lost here


Thanks

Seamus


Seamus Mackenzie

unread,
Feb 23, 2016, 3:04:29 PM2/23/16
to Caffe Users
Ok Forget about the last code, I've realised net.forward() is what I should be using

Enteimport caffe
import numpy

caffe
.set_mode_gpu()


net
= caffe.Net("/home/seamus/Downloads/BVLC_GoogLeNet/train_val.prototxt",
               
"/home/seamus/Downloads/BVLC_GoogLeNet/snapshot_iter_67.caffemodel",
                caffe
.TEST)


accuracy
= 0
IterationTest = 5000



for i in range(IterationTest):
 
# one iteration

    net
.blobs['data'] = caffe.io.oversample(net.blobs['data'], "224, 224")
    outputs
= net.forward()
    net
.blobs['data'] = net.blobs['data'].reshape(len(net.blobs['data']) / 10, 10, -1))
    net
.blobs['data'] = net.blobs['data'].mean(1)

    accuracy
+=net.blobs['accuracy-top5'].data
   
print outputs

print('========================================================================')
avgAcc
=accuracy/IterationTest
print 'accuracy = ', avgAcc

Hopefully this shows what I'm trying to do, I just dont know how to run a forward pass on 10 individual blobs and then average them succesfully
Reply all
Reply to author
Forward
0 new messages