下载cifar10数据集
http://www.cs.toronto.edu/~kriz/cifar.html
生成lmdb文件
import os
import shutil
EXAMPLE="./cifar10"
DATA="./cifar10"
DBTYPE="lmdb"
print "Creating "+DBTYPE+"..."
path1=EXAMPLE+"\\cifar10_train_"+DBTYPE
path2=EXAMPLE+"\\cifar10_test_"+DBTYPE
if os.path.exists(path1):
shutil.rmtree(path1)
if os.path.exists(path2):
shutil.rmtree(path2)
caffe_build="D:\\software\\caffe\\bin"
cmd1=caffe_build+"\\convert_cifar_data.exe "+DATA+" "+EXAMPLE+" "+DBTYPE
print cmd1
os.system(cmd1)
print "Computing image mean..."
cmd2=caffe_build+"\\compute_image_mean.exe --backend="+DBTYPE+" "+EXAMPLE+"\\cifar10_train_"+DBTYPE+" "+EXAMPLE+"\\mean.binaryproto"
print cmd2
os.system(cmd2)
使用compute_image_mean.exe 、convert_cifar_data.exe
配置参考,https://blog.csdn.net/qq_38641985/article/details/116400423
利用caffe生成net、solver文件并训练
#coding='utf-8'
import lmdb
import caffe
from caffe.proto import caffe_pb2
from caffe import layers as L
from caffe import params as P
from matplotlib import pyplot as plt
import numpy as np
data_path = "cifar10/"
train_net_file = "auto_train.prototxt"
test_net_file = "auto_test.prototxt"
solver_file = "auto_slover.prototxt"
def net(datafile,mean_file,batch_size):
n = caffe.NetSpec()
n.data,n.label=L.Data(source=datafile, backend = P.Data.LMDB, batch_size=batch_size, ntop=2, transform_param=dict(scale=1.0/255.0, mean_file=mean_file))
n.ip1 = L.InnerProduct(n.data, num_output=200, weight_filler=dict(type='xavier'))
n.relu1 = L.ReLU(n.ip1, in_place=True)
n.ip2 = L.InnerProduct(n.relu1, num_output=10, weight_filler=dict(type='xavier'))
n.loss = L.SoftmaxWithLoss(n.ip2, n.label)
n.accu = L.Accuracy(n.ip2, n.label, include={'phase':caffe.TEST})
return n.to_proto()
def config():
s = caffe_pb2.SolverParameter()
s.train_net = train_net_file
s.test_net.append(test_net_file)
s.test_interval = 500
s.test_iter.append(100)
s.display = 500
s.max_iter = 10000
s.weight_decay = 0.005
s.base_lr = 0.1
s.lr_policy = "step"
s.gamma = 0.1
s.stepsize = 5000
s.solver_mode = caffe_pb2.SolverParameter.GPU
with open(solver_file, 'w') as f:
f.write(str(s))
def main():
with open( train_net_file, 'w') as f:
f.write(str(net(data_path+'cifar10_train_lmdb', data_path+'mean.binaryproto', 200)))
with open( test_net_file, 'w') as f:
f.write(str(net(data_path+'cifar10_test_lmdb', data_path+'mean.binaryproto', 100)))
config()
solver = caffe.get_solver(solver_file)
niter = 2001
train_loss = np.zeros(niter)
test_acc = np.zeros(niter)
for it in range(niter):
solver.step(1)
train_loss[it] = solver.net.blobs['loss'].data
test_acc[it] = solver.test_nets[0].blobs['accu'].data
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(np.arange(niter), train_loss)
ax2.plot(np.arange(niter), test_acc, 'r')
ax1.set_xlabel('iteration')
ax1.set_ylabel('train loss')
ax2.set_ylabel('test accuracy')
_.savefig('result.png')
main()