本文在博客:
http://www.cnblogs.com/frombeijingwithlove/p/5314042.html
的基础上进行,主要记录在windows-caffe下如何操作以及注意事项。
一、博客中提到的Python代码生成训练测试图片,执行比较缓慢,需要一段时间
二、准备好训练数据、网络配置文件和solver文件之后,在windows-caffe下,训练命令是:
bin\caffe.exe train -solver xxxx\xxxx\solver.prototxt
注意:原博客中 solver.prototxt文件,需要注释掉两条语句
snapshot: 1000
snapshot_prefix: "./example_ising"
否则运行报错
若出现其他错误,很大可能是路径配置出现问题,请查看自己相关文件的路径是否设置正确
三、如何依据训练Log文件,画出Iteration和loss的相关曲线
windows-caffe中,训练信息保存在 log文件夹下,其中记录了训练过程中,网络的配置、训练迭代次数、loss数据等信息。
如我的log文件为 “INFO2016-11-23T10-03-22.txt”
格式如下
利用Python,从上述文件中提取出Iteration和loss数值,首先存放在“Iteration_loss.txt”中,格式如下:
在分别提取Iteration和loss数值,分别放在“Iteration.txt”和“loss.txt”中,最后读取数据,进行绘图
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 23 12:56:29 2016
@author: Steven
"""
import sys
import re
import math
import numpy as np
import matplotlib.pyplot as plt
filename1='INFO2016-11-23T10-03-22.txt'
filename2='Iteration_loss.txt'
filename3='Iteration.txt'
filename4='loss.txt'
with open(filename1, 'r') as f1:
with open (filename2,'w') as f2:
for line in f1:
if 'Iteration' in line and 'loss' in line:
f2.write(line)
with open(filename2, 'r') as f2:
with open (filename3,'w') as f3:
with open (filename4,'w') as f4:
for line in f2:
#f4.write(line.split(" ")[-1])
#f3.write(line.split(" ")[-4])
a=re.split(', | ',line)
f4.write(a[-1])
f3.write(a[-4]+'\n')
X=[]
Y=[]
with open(filename3, 'r') as f3:
for line in f3:
X.append(float(line))
with open(filename4, 'r') as f4:
for line in f4:
Y.append(float(line))
plt.plot(X,Y,color="red",linewidth=2)
plt.xlabel("Iteration")
plt.ylabel("loss")
PS:
另外参考博客:
http://blog.csdn.net/u011762313/article/details/48215725
利用该博客的代码,我目前画出了训练时的误差和迭代次数之间的曲线
要运行该代码,需要首先将路径修改为你的solver.prototxt所在文件夹的前一个文件路径下
例如,我首先在DOS里进入路径
D:\caffe-windows-master\caffe-windows-master
然后再执行命令
python xxx\xxx\xx.py
xx.py文件就是上述博客的代码 ,我将其中train loss除外的代码注释掉
solver = caffe.SGDSolver('regression_test\solver.prototxt')
整体代码如下:
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 23 19:20:54 2016
@author: Steven
"""
# -*- coding: utf-8 -*-
# 导入绘图库
from pylab import *
import matplotlib.pyplot as plt
# 导入"咖啡"
import caffe
# 设置为gpu模式
caffe.set_device(0)
caffe.set_mode_gpu()
# 使用SGDSolver,即随机梯度下降算法
solver = caffe.SGDSolver('regression_test\solver.prototxt')
# 等价于solver文件中的max_iter,即最大解算次数
niter = 10000
# 每隔100次收集一次数据
display_iter = 100
# 每次测试进行100次解算,根据test用例数量和batch_size得出
test_iter = 100
# 每500次训练进行一次测试(100次解算),根据train用例数量和batch_size得出
test_interval = 500
# train loss
train_loss = zeros(ceil(niter * 1.0 / display_iter))
# test loss
test_loss = zeros(ceil(niter * 1.0 / test_interval))
# test accuracy
test_acc = zeros(ceil(niter * 1.0 / test_interval))
# iteration 0,不计入
solver.step(1)
# 辅助变量
_train_loss = 0; _test_loss = 0; _accuracy = 0
# 进行解算
for it in range(niter):
# 进行一次解算
solver.step(1)
# 计算train loss
_train_loss += solver.net.blobs['loss'].data
if it % display_iter == 0:
# 计算平均train loss
train_loss[it // display_iter] = _train_loss / display_iter
_train_loss = 0
'''if it % test_interval == 0:
for test_it in range(test_iter):
# 进行一次测试
solver.test_nets[0].forward()
# 计算test loss
_test_loss += solver.test_nets[0].blobs['loss'].data
# 计算test accuracy
_accuracy += solver.test_nets[0].blobs['accuracy'].data
# 计算平均test loss
test_loss[it / test_interval] = _test_loss / test_iter
# 计算平均test accuracy
_accuracy += solver.test_nets[0].blobs['accuracy'].data
# 计算平均test loss
test_loss[it / test_interval] = _test_loss / test_iter
# 计算平均test accuracy
test_acc[it / test_interval] = _accuracy / test_iter
_test_loss = 0
_accuracy = 0
'''
# 绘制train loss、test loss和accuracy曲线
print '\nplot the train loss and test accuracy\n'
#ax1 = plt.subplots()
#ax2 = ax1.twinx()
# train loss -> 绿色
plt.plot(display_iter * arange(len(train_loss)), train_loss, 'g')
#test loss -> 黄色
#ax1.plot(test_interval * arange(len(test_loss)), test_loss, 'y')
# test accuracy -> 红色
#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()
两种方法基本相同