cafffe 利用h5py生成单标签h5文件并训练

h5py生成h5文件

import h5py
import numpy as np

def net(dbfile,batch_size,mean_value=0):
	pass
	
def main():
	N= 10000
	t = np.linspace(0,2*np.pi,N)
	x1 = np.array([t,30*np.cos(t)+0.3*np.random.rand(N)])
	x2 = np.array([t,29*np.cos(t)+0.3*np.random.rand(N)])
	y1 = np.zeros((N,1))
	y2 = np.ones((N,1))

	X = np.concatenate((x1.T,x2.T)).astype("float32")
	y = np.concatenate((y1,y2)).astype("float32")

	for i in range(2):
		X[:,i]=(X[:,1]-np.min(X[:,1]))/(np.max(X[:,1])-np.min(X[:,1]))

	idx = np.arange(len(y))
	print (len(X))
	np.random.shuffle(idx)

	filenum = 10
	filelen = int(len(y)/10)
	print (filelen)


	with h5py.File("train.h5","w") as f:
		f.create_dataset("data",data=X[:int(len(idx)*0.8)])
		f.create_dataset("label",data=y[:int(len(idx)*0.8)])

	with h5py.File("test.h5","w") as f:
		f.create_dataset("data",data=X[int(len(idx)*0.8):])
		f.create_dataset("label",data=y[int(len(idx)*0.8):])

	filelist = range(filenum)
	with open("train.h5list","w")as f:
		f.write("train.h5\n")

	with open("test.h5list","w")as f:
		f.write("test.h5\n")

if __name__=="__main__":
	main()

训练数据

import caffe
from caffe import layers as L
from matplotlib import pyplot as plt
import os
import numpy as np

def change_env():
	root = os.path.dirname(__file__)
	os.chdir(root)
	print ("current work root:->",root)

def net(hdf5, batch_size):
	network = caffe.NetSpec()
	network.data,network.label = L.HDF5Data(batch_size=batch_size,source=hdf5,ntop=2)
	network.ip1  = L.InnerProduct(network.data,num_output=50,weight_filler=dict(type="xavier"))
	network.relu1 = L.ReLU(network.ip1,in_place=True)
	network.ip2  = L.InnerProduct(network.relu1,num_output=2,weight_filler=dict(type="xavier"))
	network.loss = L.SoftmaxWithLoss(network.ip2,network.label)
	network.accu = L.Accuracy(network.ip2,network.label)
	return network.to_proto()

def file_write(path=""):
	with open('auto_train01.prototxt', 'w') as f:
		f.write(str(net('train.h5list', 100)))
	with open('auto_test01.prototxt', 'w') as f:
		f.write(str(net('test.h5list', 50)))

def main():
	change_env()
	file_write()
	solver = caffe.SGDSolver('auto_solver01.prototxt')
	niter = 1001
	test_interval = 10
	train_loss = np.zeros(niter)
	test_acc =  np.zeros(niter)
	train_acc =  np.zeros(niter)

	for it in range(niter):
		solver.step(1)  
		train_loss[it] = solver.net.blobs['loss'].data
		train_acc[it] = solver.net.blobs['accu'].data
		test_acc[it] = solver.test_nets[0].blobs['accu'].data

	#output graph
	_, ax1 = plt.subplots()
	ax2 = ax1.twinx()
	ax1.plot(np.arange(niter), train_loss)
	ax2.plot(np.arange(niter), test_acc, 'r', np.arange(niter), train_acc, 'm')
	ax1.set_xlabel('iteration')
	ax1.set_ylabel('train loss')
	ax2.set_ylabel('test accuracy')
	_.savefig('converge.png')

if __name__ == '__main__':
	main()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

佐倉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值