遇到的问题:
由于我是anaconda+系统默认的python2.7.6,因此会找不到protobuf的安装包,即使protobuf已经通过源码编译了,那么此时就应该通过anaconda的强大的命令:
conda install protobuf来解决这个问题,所以基本上不要用系统自带的python包了,就用anaconda就可以了。
前提是已经编译好了pycaffe接口
具体命令是:
cd ~/caffe
python python/draw_net.py(这个是用来画神经网络图的脚本) .prototxt 生成的图片名称(这个图片的生成路径是caffe_home)
eg: python python/draw_net.py examples/cifar10/cifar10_quick.prototxt my_net.png
命令说明:
1)draw_net.py用来绘制网络图)
2)examples/cifar10/XXX.prototxt表示需要绘制的prototxt的文件
3)my_net.png,这个也可以保存到其他路径当中,在这里我就把该png图片生成到了当前路径下面
4)还有一个关于生成图的方向的选项:
--rankdir
一共有四种可能性
LR:默认方向,表示从左到右
RL:从右到左
TB:top->bottom,从上到下
BT:bottom->top,从下到上
此时就会生成在caffe_home下面生成my_net.png的神经网络图片
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
利用pycaffe写train_loss和accuracy的图片,并进行保存
# -*- coding: utf-8 -*- __author__ = 'xuy' import matplotlib.pyplot as plt import os import numpy as np import sys caffe_root='/home/xuy/caffe' sys.path.append(caffe_root+'python') sys.path.append('/usr/lib/python2.7/dist-packages/') os.chdir(caffe_root)#指定当前默认路径是caffe_root import caffe USE_GPU=True#默认使用gpu if USE_GPU: caffe.set_device(0) caffe.set_mode_gpu() else: caffe.set_mode_cpu() solver = caffe.SGDSolver('examples/mnist/lenet_solver.prototxt') niter =1000#迭代次数是1000 test_interval = 200#测试是每200次测试一次 train_loss = np.zeros(niter)#初始化train_loss test_loss=np.zeros(niter)#进行初始化测试的loss test_acc = np.zeros(int(np.ceil(niter / test_interval)))#计算准确率 # 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 solver.test_nets[0].forward(start='conv1') if it % test_interval == 0: acc=solver.test_nets[0].blobs['accuracy'].data print 'Iteration', it, 'testing...','accuracy:',acc test_acc[it // test_interval] = acc print test_acc _, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(np.arange(niter), train_loss,'g') ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r') ax1.set_xlabel('iteration') ax1.set_ylabel('train loss') ax2.set_ylabel('test accuracy') plt.savefig('/home/xuy/桌面/loss_train.png')#这个一定要写到plt.show()之前 plt.show()
--------------------------以上是一种方法,还有一种是通过获取caffe的训练日志,利用正则表达式来绘制loss_accuracy图像的-----------------------
1)首先,讲一下如何获取日志信息
solution:运用tee命令
eg:./build/tools/caffe train \--solver=models/bvlc_reference_caffenet/solver.prototxt2>&1| tee yourpath/caffe.log
1. tee的意思就是命令行信息重定向的命令
2. 2>&1的意思是错误的信息也当成标准信息输出,这样能够保证输出log信息的完整性。
这是2>&1的详细解释:
File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).
Here is one way to remember this construct (although it is not entirely accurate): at first, 2>1
may look like a good way to redirect stderr to stdout. However, it will actually be interpreted as "redirect stderr to a file named 1
". &
indicates that what follows is a file descriptor and not a filename. So the construct becomes: 2>&1
.