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
原创粉丝点击