Search code examples
python-2.7deep-learningcaffepycaffe

Calling particular function at each input on testing or on prediction?


I want to call a function for every input in test time for MNIST dataset. For example, if the test data in MNIST is x1, ... , xn for every input xi at test time I want to call the function. I modified the code in "http://nbviewer.ipython.org/github/BVLC/caffe/blob/tutorial/examples/01-learning-lenet.ipynb" such that I comment the line " #solver.step(1) # SGD by Caffe" becuase I do not want any training of the network. But I think this is not right way as the accuracy decrease comparing to the orginal code. Any help please for how to implement it.

The code which I think once can change.

%%time
niter = 200
test_interval = 25
# losses will also be stored in the log
train_loss = zeros(niter)
test_acc = zeros(int(np.ceil(niter / test_interval)))
output = zeros((niter, 8, 10))
# the main solver loop
for it in range(niter):
    solver.step(1)  # SGD by Caffe

# store the train loss
#train_loss[it] = solver.net.blobs['loss'].data

# store the output on the first test batch
# (start the forward pass at conv1 to avoid loading new data)
#solver.test_nets[0].forward(start='conv1')
output[it] = solver.test_nets[0].blobs['ip2'].data[:8]

# run a full test every so often
# (Caffe can also do this for us and write to a log, but we show here
#  how to do it directly in Python, where more complicated things are easier.)
if it % test_interval == 0:
    print 'Iteration', it, 'testing...'
    correct = 0
    for test_it in range(100):
        solver.test_nets[0].forward()
        correct += sum(solver.test_nets[0].blobs['ip2'].data.argmax(1)
                       == solver.test_nets[0].blobs['label'].data)
    test_acc[it // test_interval] = correct / 1e4

Solution

  • You can add a python Layer for include { phase: TEST } only that will transform your test inputs.