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