引言
之所以使用 python 接口来运行 caffe 程序,其主要原因在于 python 非常容易可视化。所以推荐大家在 pycharm、jupyter notebook、spyder 等工具来运行 python 代码,这样才和它的可视化完美结合起来。同时便于读者的理解与学习。
在 caffe 训练过程中,如果我们想知道某个阶段的 loss 值和 accuracy 值,并用图表画出来,用 python 接口就对了:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
# 使用SGDSolver,即随机梯度下降算法
solver = caffe.SGDSolver('/home/xxx/mnist/solver.prototxt')
# 等价于solver文件中的max_iter,即最大解算次数
niter = 9380
# 每隔100次收集一次数据
display= 100
# 每次测试进行100次解算,10000/100
test_iter = 100
# 每500次训练进行一次测试(100次解算),60000/64
test_interval =938
#初始化
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,不计入