Introduction
Hi everyone, as the editor of JinCloud server recommendation website, I am here to introduce a detailed example of creating loss and accuracy curve charts in Caffe using Python interface. For those who are interested, you can refer to and learn from it, and I hope it can be helpful to you. Wish you all the best in your career and get promoted and raise your salary!

Python Interface Implementation
In the training process of Caffe, if we want to know the loss and accuracy values of a certain stage and draw them with charts, we can use Python interface.
The following is the Python code:
#-*-coding:utf-8-*-
"""
Created on Tue Jul 19 16:22:22 2016
author:root
"""
import matplotlib.pyplot as plt
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
#use SGDSolver
solver=caffe.SGDSolver('/home/xxx/mnist/solver.prototxt')
#max_iter=9380, the maximum number of iterations
niter=9380
#Collect data every 100 iterations
display=100
#Each test includes solving 100 times, 10000/100
test_iter=100
#Test every 500 iterations (100 times of solving), 60000/64
test_interval=938
#Initialization
train_loss=zeros(ceil(niter*1.0/display))
test_loss=zeros(ceil(niter*1.0/test_interval))
test_acc=zeros(ceil(niter*1.0/test_interval))
#Iteration 0, not counted
solver.step(1)
#Auxiliary variables
_train_loss=0;_test_loss=0;_accuracy=0
#Solving
for it in range(niter):
#Solve once
solver.step(1)
#Train batch_size images each time
_train_loss+=solver.net.blobs['SoftmaxWithLoss1'].data
if it%display==0:
#Calculate the average train loss
train_loss[it//display]=_train_loss/display
_train_loss=0
if it%test_interval==0:
for test_it in range(test_iter):
#Test once
solver.test_nets[0].forward()
#Calculate test loss
_test_loss+=solver.test_nets[0].blobs['SoftmaxWithLoss1'].data
#Calculate test accuracy
_accuracy+=solver.test_nets[0].blobs['Accuracy1'].data
#Calculate the average test loss
test_loss[it/test_interval]=_test_loss/test_iter
#Calculate the average test accuracy
test_acc[it/test_interval]=_accuracy/test_iter
_test_loss=0
_accuracy=0
#Draw the train loss, test loss and accuracy curve
print'nplot the train loss and test accuracyn'
_,ax1=plt.subplots()
ax2=ax1.twinx()
#train loss->green
ax1.plot(display*arange(len(train_loss)),train_loss,'g')
#test loss->yellow
ax1.plot(test_interval*arange(len(test_loss)),test_loss,'y')
#test accuracy->red
ax2.plot(test_interval*arange(len(test_acc)),test_acc,'r')
ax1.set_xlabel('iteration')
ax1.set_ylabel('loss')
ax2.set_ylabel('accuracy')
plt.show()
Anaconda Library
I prefer to use Anaconda to install a series of third-party libraries for Python. Therefore, I use an online editor called Spyder, which is similar to the MATLAB page. It’s very convenient to run by simply typing the ‘spyder’ command in the terminal.
The screenshot is shown below:

Conclusion
To sum up, this article gives you a comprehensive example of creating loss and accuracy curve charts in Caffe using Python interface. Hope it can be helpful to you. Thank you for reading!
原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/6877.html
