caffe-Python-learning-lenet-02
来源:互联网 发布:乖戾知乎 编辑:程序博客网 时间:2024/06/12 13:42
import numpy as npimport matplotlib.pyplot as plt%matplotlib inlineplt.rcParams['figure.figsize'] = (10, 10)plt.rcParams['image.interpolation'] = 'nearest'plt.rcParams['image.cmap'] = 'gray'import syscaffe_root = '../caffe/'sys.path.insert(0, caffe_root + 'python')import caffeimport osos.chdir(caffe_root + 'examples/')from caffe import layers as L, params as P
/home/dl/anaconda2/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')../caffe/python/caffe/pycaffe.py:13: RuntimeWarning: to-Python converter for boost::shared_ptr<caffe::Net<float> > already registered; second conversion method ignored. from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \../caffe/python/caffe/pycaffe.py:13: RuntimeWarning: to-Python converter for boost::shared_ptr<caffe::Blob<float> > already registered; second conversion method ignored. from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \../caffe/python/caffe/pycaffe.py:13: RuntimeWarning: to-Python converter for boost::shared_ptr<caffe::Solver<float> > already registered; second conversion method ignored. from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
caffe.set_device(0)caffe.set_mode_gpu()#你好train_net_path = 'mnist/custom_auto_train.prototxt'test_net_path = 'mnist/custom_auto_test.prototxt'solver_config_path = 'mnist/custom_auto_solver.prototxt'#1. 定义一个线性 net#def custom_net_linear(lmdb, batch_size):# n = caffe.NetSpec()# n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB,# source=lmdb, transform_param=dict(scale=1./255), ntop=2)# n.score = L.InnerProduct(n.data, num_output=10, weight_filler=dict(type='xavier'))# n.loss = L.SoftmaxWithLoss(n.score, n.label)# return n.to_proto()#with open(train_net_path, 'w') as f:# f.write(str(custom_net_linear('mnist/mnist_train_lmdb', 64)))#with open(test_net_path, 'w') as f:# f.write(str(custom_net_linear('mnist/mnist_test_lmdb', 100)))def custom_net_cnn(lmdb, batch_size): n = caffe.NetSpec() n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdb, transform_param=dict(scale=1./255), ntop=2) n.conv1 = L.Convolution(n.data, kernel_size=5, num_output=20, weight_filler=dict(type='xavier')) n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX) n.conv2 = L.Convolution(n.pool1, kernel_size=5, num_output=50, weight_filler=dict(type='xavier')) n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX) n.fc1 = L.InnerProduct(n.pool2, num_output=500, weight_filler=dict(type='xavier')) n.relu1 = L.ReLU(n.fc1, in_place=True) #L.Sigmoid n.score = L.InnerProduct(n.relu1, num_output=10, weight_filler=dict(type='xavier')) n.loss = L.SoftmaxWithLoss(n.score, n.label) return n.to_proto()with open(train_net_path, 'w') as f: f.write(str(custom_net_cnn('mnist/mnist_train_lmdb', 64)))with open(test_net_path, 'w') as f: f.write(str(custom_net_cnn('mnist/mnist_test_lmdb', 100)))
from caffe.proto import caffe_pb2s = caffe_pb2.SolverParameter()s.random_seed = 0xCAFFEs.train_net = train_net_paths.test_net.append(test_net_path)s.test_interval = 500s.test_iter.append(100)s.max_iter = 10000s.type='SGD's.base_lr = 0.01s.momentum = 0.9s.weight_decay = 5e-4s.lr_policy = 'inv's.gamma = 0.0001s.power = 0.75s.display = 1000s.snapshot = 5000s.snapshot_prefix = 'mnist/custom_net's.solver_mode = caffe_pb2.SolverParameter.GPUwith open(solver_config_path, 'w') as f: f.write(str(s))
#os.chdir('/home/dl/caffe/')solver = Nonesolver = caffe.get_solver(solver_config_path)niter = 250test_interval = niter / 10train_loss = np.zeros(niter)test_acc = np.zeros(int(np.ceil(niter / test_interval)))for it in range(niter): solver.step(1) train_loss[it] = solver.net.blobs['loss'].data #test_acc[it] = solver.net_nets[0].blobs['accuracy'].data if it % test_interval == 0: print('Iteration', it, 'testing...') correct = 0 for test_it in range(100): solver.test_nets[0].forward() correct += np.sum(solver.test_nets[0].blobs['score'].data.argmax(1) == solver.test_nets[0].blobs['label'].data) test_acc[it // test_interval] = correct / 1e4_, ax1 = plt.subplots()ax2 = ax1.twinx()ax1.plot(np.arange(niter), train_loss)#ax2.plot(np.arange(niter), test_acc, 'r')ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')ax1.set_xlabel('iterations')ax1.set_ylabel('train loss')ax2.set_ylabel('test accuracy')ax2.set_title('Test accuracy :{:.2f} '.format(test_acc[-1]))
('Iteration', 0, 'testing...')('Iteration', 25, 'testing...')('Iteration', 50, 'testing...')('Iteration', 75, 'testing...')('Iteration', 100, 'testing...')('Iteration', 125, 'testing...')('Iteration', 150, 'testing...')('Iteration', 175, 'testing...')('Iteration', 200, 'testing...')('Iteration', 225, 'testing...')<matplotlib.text.Text at 0x7f7285120ed0>
0 0
- caffe-Python-learning-lenet-02
- 【caffe】Caffe的Python接口-官方教程-01-learning-Lenet-详细说明(含代码)
- 使用 Caffe Python 编写 LeNet
- caffe之python接口实战 :01-learning-lenet 官方教程源码解析
- Learning Caffe(1)---Training LeNet on MNIST with Caffe
- lenet and caffe-lenet
- caffe:cpu模式下运行01-learning-lenet.ipynb
- python-caffe接口学习(Solving in Python with LeNet)
- caffe示例实现之10LeNet的python接口
- caffe学习笔记8-- Python solving with LeNet
- LeNet Tutorial with Caffe
- Caffe之LeNet分析
- 01-learning-lenet.ipynb
- 01-learning-lenet.ipynb
- Caffe上LeNet模型理解
- caffe学习2-LeNet网络
- caffe中的 AlexNet,LeNet,CaffeNet
- (Caffe,LeNet)反向传播
- 模仿 Android支付宝咻一咻功能实现
- 在main方法中启动TestNG测试时build path的问题
- 如何让div在整个屏幕中垂直居中
- Leetcode 391. Perfect Rectangle 完美矩形 解题报告
- 浏览器主页锁定无敌解决方案
- caffe-Python-learning-lenet-02
- 搭建virtualbox + kgdb内核调试环境
- 使用git从Coding.net上同步代码到Eclipse报错 Internal error; consult Eclipse error log
- 二十二、图文混合排版
- (五)2.3 查看索引使用情况
- leetcode 手机九宫格输入字母组合
- 利用redis + lua解决抢红包高并发的问题
- Parse JSON into ordered data-structure (in python)
- php完全手册中文版本