Python版本:Python2.7
mnist数据集
博客来源:http://blog.csdn.net/c406495762/article/details/70306550
如何编译caffe的Python接口就不多说了
下面的代码可以一次生成Lenet网络训练所需的train.prototxt和test.prototxt,还有solver.prototxt
代码:
import caffe def create_net(lmdb, mean_file, batch_size, include_acc=False): net = caffe.NetSpec() net.data, net.label = caffe.layers.Data(source=lmdb, backend=caffe.params.Data.LMDB, batch_size=batch_size, ntop=2, transform_param = dict(mean_file=mean_file,scale= 0.00390625)) net.conv1 = caffe.layers.Convolution(net.data, num_output=20,kernel_size=5,weight_filler={"type": "xavier"},bias_filler={"type": "constant"}) net.pool1 = caffe.layers.Pooling(net.conv1, pool=caffe.params.Pooling.MAX, kernel_size=2, stride=2) net.conv2 = caffe.layers.Convolution(net.pool1, kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'),bias_filler={"type": "constant"}) net.pool2 = caffe.layers.Pooling(net.conv2, pool=caffe.params.Pooling.MAX, kernel_size=2, stride=2) net.fc1 = caffe.layers.InnerProduct(net.pool2, num_output=500,weight_filler=dict(type='xavier'),bias_filler={"type": "constant"}) net.fc_add1 = caffe.layers.InnerProduct(net.fc1, num_output=500,weight_filler=dict(type='xavier'),bias_filler={"type": "constant"}) net.fc_add2 = caffe.layers.InnerProduct(net.fc_add1, num_output=500,weight_filler=dict(type='xavier'),bias_filler={"type": "constant"}) net.relu1 = caffe.layers.ReLU(net.fc_add2, in_place=True) net.drop3 = caffe.layers.Dropout(net.fc_add2, in_place=True) net.fc2 = caffe.layers.InnerProduct(net.fc_add2, num_output=10,weight_filler=dict(type='xavier')) net.loss = caffe.layers.SoftmaxWithLoss(net.fc2, net.label) if include_acc: net.acc = caffe.layers.Accuracy(net.fc2, net.label) return str(net.to_proto()) return str(net.to_proto())def write_net(mean_file,train_proto, train_lmdb, test_proto, val_lmdb): with open(train_proto, 'w') as f: f.write(str(create_net(train_lmdb,mean_file,batch_size = 64))) with open(test_proto, 'w') as f: f.write(str(create_net(val_lmdb,mean_file,batch_size = 100, include_acc = True)))def write_sovler(my_project_root, solver_proto, train_proto, test_proto): sovler_string = caffe.proto.caffe_pb2.SolverParameter() sovler_string.train_net = train_proto sovler_string.test_net.append(test_proto) sovler_string.test_iter.append(100) sovler_string.test_interval = 938 sovler_string.base_lr = 0.01 sovler_string.momentum = 0.9 sovler_string.weight_decay = 5e-4 sovler_string.lr_policy = 'step' sovler_string.stepsize = 3000 sovler_string.gamma = 0.1 sovler_string.display = 20 sovler_string.max_iter = 9380 sovler_string.snapshot = 938 sovler_string.snapshot_prefix = my_project_root + 'mnist/model/mnist' sovler_string.solver_mode = caffe.proto.caffe_pb2.SolverParameter.GPU with open(solver_proto, 'w') as f: f.write(str(sovler_string))if __name__ == '__main__': my_project_root = "F:/python/make_prototxt/" train_lmdb = my_project_root + "mnist/data/mnist_train_lmdb" val_lmdb = my_project_root + "mnist/data/mnist_test_lmdb" train_proto = my_project_root + "mnist/train.prototxt" test_proto = my_project_root + "mnist/test.prototxt" solver_proto = my_project_root + "mnist/solver.prototxt" mean_file = my_project_root + "mnist/data/trainMean.binaryproto" write_net(mean_file,train_proto, train_lmdb, test_proto, val_lmdb) print "生成train.prototxt test.prototxt成功" write_sovler(my_project_root, solver_proto, train_proto, test_proto) print "生成solver.prototxt成功"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
运行结果:
生成的train.prototxt
layer { name: "data" type: "Data" top: "data" top: "label" transform_param { scale: 0.00390625 mean_file: "F:/python/make_prototxt/mnist/data/trainMean.binaryproto" } data_param { source: "F:/python/make_prototxt/mnist/data/mnist_train_lmdb" batch_size: 64 backend: LMDB }}layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" convolution_param { num_output: 20 kernel_size: 5 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 2 stride: 2 }}layer { name: "conv2" type: "Convolution" bottom: "pool1" top: "conv2" convolution_param { num_output: 50 pad: 0 kernel_size: 5 stride: 1 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 2 stride: 2 }}layer { name: "fc1" type: "InnerProduct" bottom: "pool2" top: "fc1" inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "fc_add1" type: "InnerProduct" bottom: "fc1" top: "fc_add1" inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "fc_add2" type: "InnerProduct" bottom: "fc_add1" top: "fc_add2" inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "relu1" type: "ReLU" bottom: "fc_add2" top: "fc_add2"}layer { name: "drop3" type: "Dropout" bottom: "fc_add2" top: "fc_add2"}layer { name: "fc2" type: "InnerProduct" bottom: "fc_add2" top: "fc2" inner_product_param { num_output: 10 weight_filler { type: "xavier" } }}layer { name: "loss" type: "SoftmaxWithLoss" bottom: "fc2" bottom: "label" top: "loss"}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
生成的test.prototxt
layer { name: "data" type: "Data" top: "data" top: "label" transform_param { scale: 0.00390625 mean_file: "F:/python/make_prototxt/mnist/data/trainMean.binaryproto" } data_param { source: "F:/python/make_prototxt/mnist/data/mnist_test_lmdb" batch_size: 100 backend: LMDB }}layer { name: "conv1" type: "Convolution" bottom: "data" top: "conv1" convolution_param { num_output: 20 kernel_size: 5 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "pool1" type: "Pooling" bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 2 stride: 2 }}layer { name: "conv2" type: "Convolution" bottom: "pool1" top: "conv2" convolution_param { num_output: 50 pad: 0 kernel_size: 5 stride: 1 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "pool2" type: "Pooling" bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 2 stride: 2 }}layer { name: "fc1" type: "InnerProduct" bottom: "pool2" top: "fc1" inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "fc_add1" type: "InnerProduct" bottom: "fc1" top: "fc_add1" inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "fc_add2" type: "InnerProduct" bottom: "fc_add1" top: "fc_add2" inner_product_param { num_output: 500 weight_filler { type: "xavier" } bias_filler { type: "constant" } }}layer { name: "relu1" type: "ReLU" bottom: "fc_add2" top: "fc_add2"}layer { name: "drop3" type: "Dropout" bottom: "fc_add2" top: "fc_add2"}layer { name: "fc2" type: "InnerProduct" bottom: "fc_add2" top: "fc2" inner_product_param { num_output: 10 weight_filler { type: "xavier" } }}layer { name: "loss" type: "SoftmaxWithLoss" bottom: "fc2" bottom: "label" top: "loss"}layer { name: "acc" type: "Accuracy" bottom: "fc2" bottom: "label" top: "acc"}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
生成的solver.prototxt
train_net: "F:/python/make_prototxt/mnist/train.prototxt"test_net: "F:/python/make_prototxt/mnist/test.prototxt"test_iter: 100test_interval: 938base_lr: 0.01display: 20max_iter: 9380lr_policy: "step"gamma: 0.1momentum: 0.9weight_decay: 0.0005stepsize: 3000snapshot: 938snapshot_prefix: "F:/python/make_prototxt/mnist/model/mnist"solver_mode: GPU
接下来就可以训练了