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)
- Caffe 实例笔记 3 Brewing Logistic Regression then Going Deeper
- caffe学习笔记9 -- Brewing Logistic Regression then Going Deeper
- (已翻译)caffe官方例程之hdf5_classification(Brewing Logistic Regression then Going Deeper)
- going deeper with convolutions笔记
- Going deeper with convolutions笔记
- 《Going Deeper with Convolutions》笔记
- Going Deeper into Regression Analysis with Assumptions, Plots & Solutions
- (GoogLeNet)Going deeper with convolutions笔记
- 《Going Deeper With Convolution》学习笔记
- 论文笔记:going deeper with convolutions
- 论文笔记:Going deeper with convolution
- 论文笔记 | Going deeper with convolutions
- 论文笔记《Going deeper with convolutions》1409
- Going Deeper with Convolutions学习笔记
- Going Deeper with Convolutions 阅读笔记
- googlenet笔记(Going Deeper with Convolutions)
- logistic regression 学习笔记
- Logistic Regression笔记
- Android入门--style.xml设置和应用
- Activity生命周期、启动方式及常见Activity
- Two Sum
- 深度学习与自然语言处理(2)_斯坦福cs224d Lecture 2
- 近期学习css+javascript
- Caffe 实例笔记 3 Brewing Logistic Regression then Going Deeper
- Labview实现简单知乎日报客户端
- 《2016互联网趋势报告》解读(一)
- (转载)NSUserDefaults 保存数据
- Eclipse 导入 Makefiles 项目
- WordPress日记——集成markdown
- 漫话机器学习大数据那些事儿
- MySQL Order by 语句用法与优化详解
- 【一天一道LeetCode】#90. Subsets II