关闭

caffe-Python-learning-lenet-02

811人阅读 评论(0) 收藏 举报
分类:
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>

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55476次
    • 积分:973
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:4篇
    • 译文:1篇
    • 评论:35条
    最新评论