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

h5py生成多标签h5文件

import h5py
import numpy as np
def main():
	f = h5py.File('train00.h5', 'w')
	f.create_dataset('data', (1200, 128), dtype='f8')
	f.create_dataset('label', (1200, 4), dtype='i')

	for i in range(1200):
	    a = np.empty(128)
	    if i % 4 == 0:
	        for j in range(128):
	            a[j] = j / 128.0;
	        l = [1,0,0,0]
	    elif i % 4 == 1:
	        for j in range(128):
	            a[j] = (128 - j) / 128.0;
	        l = [1,0,1,0]
	    elif i % 4 == 2:
	        for j in range(128):
	            a[j] = (j % 6) / 128.0;
	        l = [0,1,1,0]
	    elif i % 4 == 3:
	        for j in range(128):
	            a[j] = (j % 4) * 4 / 128.0;
	        l = [1,0,1,1]
	    f['data'][i] = a
	    f['label'][i] = l
	f.close()
	with open('train.h5list','w') as f:
	    f.write('train00.h5')

	with open('test.h5list','w') as f:
	    f.write('train00.h5')

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=4,weight_filler=dict(type="xavier"))
	network.loss = L.SigmoidCrossEntropyLoss(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(int(np.ceil(niter * 1.0 / test_interval)))
	train_acc =  np.zeros(niter)

	# iteration and save the loss, accuracy
	for it in range(niter):
		solver.step(1)  
		train_loss[it] = solver.net.blobs['loss'].data
		solver.test_nets[0].forward(start='data')

		if it % test_interval == 0:
			print ('Iteration', it, 'testing...')
			correct = 0
			data = solver.test_nets[0].blobs['ip2'].data
			label = solver.test_nets[0].blobs['label'].data
			for test_it in range(100):
				solver.test_nets[0].forward()
				# Positive -> label 1, negative -> label 0
				for i in range(len(data)):
					for j in range(len(data[i])):
						if data[i][j] > 0 and label[i][j] == 1:
							correct += 1
						elif data[i][j] <= 0 and label[i][j] == 0:
							correct += 1
			test_acc[int(it / test_interval)] = correct * 1.0 / (len(data) * len(data[0]) * 100)

	#output graph
	_, ax1 = plt.subplots()
	ax2 = ax1.twinx()
	ax1.plot(np.arange(niter), train_loss)
	ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')
	ax1.set_xlabel('iteration')
	ax1.set_ylabel('train loss')
	ax2.set_ylabel('test accuracy')
	_.savefig('converge01.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、付费专栏及课程。

余额充值