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