标签:左右 比较 ack 观察 base generate erp form softmax
该工作的主要目的是为了练习运用pycaffe来进行神经网络一站式训练,并从多个角度来分析对应的结果。
目标:
测试数据集:mnist
代码:https://github.com/TiBAiL/PycaffeTrain-LeNet
环境:Ubuntu 16.04LTS训练,Windows 7+VS2013分析
关于网络架构,在caffe的训练过程中,会涉及到三种不同类型的prototxt文件,分别用于train、test(validation)以及deploy。这三种文件需要保持网络架构上的统一,方可使得程序正常工作。为了达到这一目的,可以通过程序自动生成对应的文件。这三种文件的主要区别在于输入数据层以及loss/accuracy/prob层。
与此同时,针对solver.prototxt文件,我也采用了python程序的方式进行生成。在求解过程中,为了能够统计train loss/test loss以及accuracy信息以及保存任意时刻的model参数,可以采用pycaffe提供的api接口进行处理。
代码如下:
1 import numpy as np 2 import caffe 3 from caffe import layers as L, params as P, proto, to_proto 4 5 # file path 6 root = ‘/home/your-account/DL-Analysis/‘ 7 train_list = root + ‘mnist/mnist_train_lmdb‘ 8 test_list = root + ‘mnist/mnist_test_lmdb‘ 9 10 train_proto = root + ‘mnist/LeNet/train.prototxt‘ 11 test_proto = root + ‘mnist/LeNet/test.prototxt‘ 12 13 deploy_proto = root + ‘mnist/LeNet/deploy.prototxt‘ 14 15 solver_proto = root + ‘mnist/LeNet/solver.prototxt‘ 16 17 def LeNet(data_list, batch_size, IncludeAccuracy = False, deploy = False): 18 """ 19 LeNet define 20 """ 21 22 if not(deploy): 23 data, label = L.Data(source = data_list, 24 backend = P.Data.LMDB, 25 batch_size = batch_size, 26 ntop = 2, 27 transform_param = dict(scale = 0.00390625)) 28 else: 29 data = L.Input(input_param = {‘shape‘: {‘dim‘: [64, 1, 28, 28]}}) 30 31 conv1 = L.Convolution(data, 32 kernel_size = 5, 33 stride = 1, 34 num_output = 20, 35 pad = 0, 36 weight_filler = dict(type = ‘xavier‘)) 37 38 pool1 = L.Pooling(conv1, 39 pool = P.Pooling.MAX, 40 kernel_size = 2, 41 stride = 2) 42 43 conv2 = L.Convolution(pool1, 44 kernel_size = 5, 45 stride = 1, 46 num_output = 50, 47 pad = 0, 48 weight_filler = dict(type = ‘xavier‘)) 49 50 pool2 = L.Pooling(conv2, 51 pool = P.Pooling.MAX, 52 kernel_size = 2, 53 stride = 2) 54 55 ip1 = L.InnerProduct(pool2, 56 num_output = 500, 57 weight_filler = dict(type = ‘xavier‘)) 58 59 relu1 = L.ReLU(ip1, 60 in_place = True) 61 62 ip2 = L.InnerProduct(relu1, 63 num_output = 10, 64 weight_filler = dict(type = ‘xavier‘)) 65 66 #loss = L.SoftmaxWithLoss(ip2, label) 67 68 if ( not(IncludeAccuracy) and not(deploy) ): 69 # train net 70 loss = L.SoftmaxWithLoss(ip2, label) 71 return to_proto(loss) 72 73 elif ( IncludeAccuracy and not(deploy) ): 74 # test net 75 loss = L.SoftmaxWithLoss(ip2, label) 76 Accuracy = L.Accuracy(ip2, label) 77 return to_proto(loss, Accuracy) 78 79 else: 80 # deploy net 81 prob = L.Softmax(ip2) 82 return to_proto(prob) 83 84 def WriteNet(): 85 """ 86 write proto to file 87 """ 88 89 # train net 90 with open(train_proto, ‘w‘) as file: 91 file.write( str(LeNet(train_list, 64, IncludeAccuracy = False, deploy = False)) ) 92 93 # test net 94 with open(test_proto, ‘w‘) as file: 95 file.write( str(LeNet(test_list, 100, IncludeAccuracy = True, deploy = False)) ) 96 97 # deploy net 98 with open(deploy_proto, ‘w‘) as file: 99 file.write( str(LeNet(‘not need‘, 64, IncludeAccuracy = False, deploy = True)) ) 100 101 def GenerateSolver(solver_file, train_net, test_net): 102 """ 103 generate the solver file 104 """ 105 106 s = proto.caffe_pb2.SolverParameter() 107 s.train_net = train_net 108 s.test_net.append(test_net) 109 s.test_interval = 100 110 s.test_iter.append(100) 111 s.max_iter = 10000 112 s.base_lr = 0.01 113 s.momentum = 0.9 114 s.weight_decay = 5e-4 115 s.lr_policy = ‘step‘ 116 s.stepsize = 3000 117 s.gamma = 0.1 118 s.display = 100 119 s.snapshot = 0 120 s.snapshot_prefix = ‘./lenet‘ 121 s.type = ‘SGD‘ 122 s.solver_mode = proto.caffe_pb2.SolverParameter.GPU 123 124 with open(solver_file, ‘w‘) as file: 125 file.write( str(s) ) 126 127 def Training(solver_file): 128 """ 129 training 130 """ 131 132 caffe.set_device(0) 133 caffe.set_mode_gpu() 134 solver = caffe.get_solver(solver_file) 135 #solver.solve() # solve completely 136 137 number_iteration = 10000 138 139 # collect the information 140 display = 100 141 142 # test information 143 test_iteration = 100 144 test_interval = 100 145 146 # loss and accuracy information 147 train_loss = np.zeros( np.ceil(number_iteration * 1.0 / display) ) 148 test_loss = np.zeros( np.ceil(number_iteration * 1.0 / test_interval) ) 149 test_accuracy = np.zeros( np.ceil(number_iteration * 1.0 / test_interval) ) 150 151 # tmp variables 152 _train_loss = 0; _test_loss = 0; _test_accuracy = 0; 153 154 # main loop 155 for iter in range(number_iteration): 156 solver.step(1) 157 158 # save model during training 159 if iter in [10, 30, 60, 100, 300, 600, 1000, 3000, 6000, number_iteration - 1]: 160 string = ‘lenet_iter_%(iter)d.caffemodel‘%{‘iter‘: iter} 161 solver.net.save(string) 162 163 if 0 == iter % display: 164 train_loss[iter // display] = solver.net.blobs[‘SoftmaxWithLoss1‘].data 165 166 ‘‘‘ 167 # accumulate the train loss 168 _train_loss += solver.net.blobs[‘SoftmaxWithLoss1‘].data 169 if 0 == iter % display: 170 train_loss[iter // display] = _train_loss / display 171 _train_loss = 0 172 ‘‘‘ 173 174 if 0 == iter % test_interval: 175 for test_iter in range(test_iteration): 176 solver.test_nets[0].forward() 177 _test_loss += solver.test_nets[0].blobs[‘SoftmaxWithLoss1‘].data 178 _test_accuracy += solver.test_nets[0].blobs[‘Accuracy1‘].data 179 180 test_loss[iter / test_interval] = _test_loss / test_iteration 181 test_accuracy[iter / test_interval] = _test_accuracy / test_iteration 182 _test_loss = 0 183 _test_accuracy = 0 184 185 # save for analysis 186 np.save(‘./train_loss.npy‘, train_loss) 187 np.save(‘./test_loss.npy‘, test_loss) 188 np.save(‘./test_accuracy.npy‘, test_accuracy) 189 190 if __name__ == ‘__main__‘: 191 WriteNet() 192 GenerateSolver(solver_proto, train_proto, test_proto) 193 Training(solver_proto)
利用上述代码训练出来的model进行预测,并对结果进行分析(相关分析代码参见上述链接):
标签:左右 比较 ack 观察 base generate erp form softmax
原文地址:http://www.cnblogs.com/TiBAi/p/6884033.html