caffe-Python-learning-lenet-02

原创 2016年08月29日 16:05:00
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

import sys
caffe_root = '../caffe/'
sys.path.insert(0, caffe_root + 'python')

import caffe

import os
os.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_pb2
s = caffe_pb2.SolverParameter()
s.random_seed = 0xCAFFE
s.train_net = train_net_path
s.test_net.append(test_net_path)
s.test_interval = 500
s.test_iter.append(100)
s.max_iter = 10000
s.type='SGD'
s.base_lr = 0.01
s.momentum = 0.9
s.weight_decay = 5e-4
s.lr_policy = 'inv'
s.gamma = 0.0001
s.power = 0.75
s.display = 1000
s.snapshot = 5000
s.snapshot_prefix = 'mnist/custom_net'
s.solver_mode = caffe_pb2.SolverParameter.GPU

with open(solver_config_path, 'w') as f:
    f.write(str(s))

#os.chdir('/home/dl/caffe/')

solver = None
solver = caffe.get_solver(solver_config_path)

niter = 250
test_interval = niter / 10
train_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>

这里写图片描述

《利用python做数据分析》第十章:时间序列分析

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline //anac...
  • qq_16735407
  • qq_16735407
  • 2016年02月01日 16:33
  • 6097

为了caffe(三)在安装完了caffe之后做python配置

为了caffe(三)在安装完了caffe之后做python配置   配置  联想笔记本G480 双显卡(intel集成显卡和N卡独立显卡GeForce610M) ubuntu14.0464位 c...
  • ddqqfree123
  • ddqqfree123
  • 2016年08月31日 20:38
  • 17764

为了caffe(七)做kaggle上的mnist

为了caffe(七)做kaggle上的mnist   因为蠢啊,因为不懂才写这么详细啊   第0步: 刚刚不能验证注册kaggle,注册需要VPN翻墙一下             第一步:  老样子...
  • ddqqfree123
  • ddqqfree123
  • 2016年08月31日 22:06
  • 13040

Matplotlib is building the font cache using fc-list. This may take a moment

/Users/pro/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplot...
  • yimingsilence
  • yimingsilence
  • 2016年10月20日 10:18
  • 3439

Matplotlib is building the font cache using fc-list. This may take a moment解决办法

在tensorflow上看到的,转过来一下 deleted ~/.cache/matplotlib/fontList.cacheran code, again the warning was...
  • c437yuyang
  • c437yuyang
  • 2017年02月04日 21:04
  • 1133

Matplotlib is building the font cache using fc-list. This may take a moment.

>>>import matplotlib.pyplot as plt /usr/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: ...
  • T_Ting555
  • T_Ting555
  • 2017年05月02日 19:55
  • 567

《利用python做数据分析》第十章:时间序列分析

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline //anac...
  • qq_16735407
  • qq_16735407
  • 2016年02月01日 16:33
  • 6097

为了caffe(三)在安装完了caffe之后做python配置

为了caffe(三)在安装完了caffe之后做python配置   配置  联想笔记本G480 双显卡(intel集成显卡和N卡独立显卡GeForce610M) ubuntu14.0464位 c...
  • ddqqfree123
  • ddqqfree123
  • 2016年08月31日 20:38
  • 17764

彻底解决matplotlib中文乱码问题

1.环境查看 a.系统版本查看 [hadoop@p168 ~]$ cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)  b.系统中...
  • dgatiger
  • dgatiger
  • 2015年12月27日 22:56
  • 24468

caffe之python接口实战 :01-learning-lenet 官方教程源码解析

本教程属于官方文档的源码解析笔记注1:本文属于caffe_root/example/下的ipynb文件的源码解析,本教程旨在通过源码注释,加速初学者的学习进程。 注2:以下解析中,未对各部分英文注释...
  • xuluohongshang
  • xuluohongshang
  • 2017年12月03日 10:43
  • 49
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:caffe-Python-learning-lenet-02
举报原因:
原因补充:

(最多只允许输入30个字)