Caffe 实例笔记 3 Brewing Logistic Regression then Going Deeper

来源:互联网 发布:linux 重启jenkins 编辑:程序博客网 时间:2024/06/03 17:10

From now on, I will try to write some blogs in English to improve my English writing skills. If there is anything wrong in the blogs ,please let me know. Thanks.
In this example, we will
1. use caffe as a generic SGD optimizer to train logistic regression ,
2. and then we will add layers to improve accuracy.

1 classify with sklearn

shutil - Utility functions for copying and archiving files and directory trees.

Note the following function when generate a binary classification:

sklearn.datasets.make_classification(...)sklearn.cross_validation.train_test_split(X,y)ind=np.random.permutation(X.shape[0])[:1000]df=pd.DataFrame(X[ind])_=pd.scatter_matrix(df,figsize=(9,9),diagonal='kde',marker='o',s=40,alpha=0.4,c=y[ind])#alpha=transparencyclf = sklearn.linear_model.SGDClassifier(...)clf.fit(X, y)yt_pred = clf.predict(Xt)print('Accuracy: {:.3f}'.format(sklearn.metrics.accuracy_score(yt, yt_pred)))

we can get the result like this:
accuracy:0.772
accuracy:0.772
accuracy:0.772
accuracy:0.772
1 loops, best of 3: 443 ms per loop

2 use caffe

2.1 save dataset to HDF5

?

dirname=os.path.abspath('/home/beatree/caffe-rc3/examples/hdf5_classification/data')if not os.path.exists(dirname):    os.makedirs(dirname)train_filename=os.path.join(dirname,'train.h5')test_filename=os.path.join(dirname,'test.h5')with h5py.File(train_filename,'w')as f:    f['data']=X    f['label']=y.astype(np.float32)with open(os.path.join(dirname,'train.txt'),'w') as f:    f.write(train_filename+'\n')#why list the same data file twice???    f.write(train_filename+'\n')comp_kwargs={'compression':'gzip','compression_opts':1}with h5py.File(test_filename,'w')as f:     f.create_dataset('data',data=Xt,**comp_kwargs)    f.create_dataset('label',data=yt.astype(np.float32),**comp_kwargs)with open (os.path.join(dirname,'test.txt'),'w')as f:    f.write(test_filename+'\n')

2.2 prottobuf model(shallow)

from caffe import layers as Lfrom caffe import params as Pdef logreg(hdf5,batch_size):    n=caffe.NetSpec()    n.data,n.label=L.HDF5Data(batch_size=batch_size,source=hdf5,ntop=2)    n.ip1=L.InnerProduct(n.data,num_output=2,weight_filler=dict(type='xavier'))    n.accuracy=L.Accuracy(n.ip1,n.label)    n.loss=L.SoftmaxWithLoss(n.ip1,n.label)    return n.to_proto()train_net_path='/home/beatree/caffe-rc3/examples/hdf5_classification/logreg_auto_train.prototxt'test_net_path='/home/beatree/caffe-rc3/examples/hdf5_classification/logreg_auto_test.prototxt'with open(train_net_path,'w')as f:    f.write(str(logreg('/home/beatree/caffe-rc3/examples/hdf5_classification/data/train.txt',10)))with open(test_net_path,'w')as f:    f.write(str(logreg('/home/beatree/caffe-rc3/examples/hdf5_classification/data/test.txt',10)))

solver.prototxt

from caffe.proto import caffe_pb2def solver(train_net_path,test_net_path):    s= caffe_pb2.SolverParameter()    s.train_net= train_net_path    s.test_net.append(test_net_path)    s.test_interval=1000    s.test_iter.append(250)    s.max_iter=10000    s.base_lr=0.01    s.lr_policy='step'    s.gamma=0.1    s.stepsize=5000    s.momentum=0.9    s.weight_decay=5e-4    s.display=1000    s.snapshot=10000    s.snapshot_prefix='/home/beatree/caffe-rc3/examples/hdf5_classification/data/train'    s.solver_mode=caffe_pb2.SolverParameter.CPU    return ssolver_path='/home/beatree/caffe-rc3/examples/hdf5_classification/logreg_solver.protxt'with open (solver_path,'w') as f:    f.write(str(solver(train_net_path,test_net_path)))
%%timeitcaffe.set_mode_cpu()solver=caffe.get_solver(solver_path)solver.solve()accuracy=0batch_size=solver.test_nets[0].blobs['data'].numtest_iters=int(len(Xt)/batch_size)for i in range (test_iters):    solver.test_nets[0].forward()    accuracy+=solver.test_nets[0].blobs['accuracy'].dataaccuracy/=test_itersprint ('accuracy:{:.3}'.format(accuracy))

accuracy:0.771
accuracy:0.771
10 loops, best of 3: 156 ms per loop

2.3 deeper model

from caffe import layers as Lfrom caffe import params as Pdef nonlinear_net(hdf5,batch_size):    n=caffe.NetSpec()    n.data,n.label=L.HDF5Data(batch_size=batch_size,source=hdf5,ntop=2)    n.ip1=L.InnerProduct(n.data,num_output=40,weight_filler=dict(type='xavier'))    n.relu1=L.ReLU(n.ip1,in_place=True)    n.ip2=L.InnerProduct(n.relu1,num_output=2,weight_filler=dict(type='xavier'))    n.accuracy=L.Accuracy(n.ip2,n.label)    n.loss=L.SoftmaxWithLoss(n.ip2,n.label)    return n.to_proto()train_net_path='/home/beatree/caffe-rc3/examples/hdf5_classification/nonlinear_auto_train.prototxt'test_net_path='/home/beatree/caffe-rc3/examples/hdf5_classification/nonlinear_auto_test.prototxt'with open(train_net_path,'w')as f:    f.write(str(nonlinear_net('/home/beatree/caffe-rc3/examples/hdf5_classification/data/train.txt',10)))with open(test_net_path,'w')as f:    f.write(str(nonlinear_net('/home/beatree/caffe-rc3/examples/hdf5_classification/data/test.txt',10)))solver_path='/home/beatree/caffe-rc3/examples/hdf5_classification/nonliear_logreg_solver.prototxt'with open (solver_path,'w') as f:    f.write(str(solver(train_net_path,test_net_path)))
%%timeitcaffe.set_mode_cpu()solver=caffe.get_solver(solver_path)solver.solve()accuracy=0batch_size=solver.test_nets[0].blobs['data'].numtest_iters=int(len(Xt)/batch_size)for i in range (test_iters):    solver.test_nets[0].forward()    accuracy+=solver.test_nets[0].blobs['accuracy'].dataaccuracy/=test_itersprint ('acc:{:.3}'.format(accuracy))

acc:0.827
acc:0.826
acc:0.828
acc:0.829
1 loops, best of 3: 254 ms per loop

we get a higher accuracy!
finally,if you want to clean up the dir

shutil.remtree(dirname)
0 0
原创粉丝点击