caffe 利用cifar10进行训练

下载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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值