Caffe使用教程

2022-02-28 03:01:57 字數 3809 閱讀 9229

caffe官網:

1 #include "

caffe/caffe.hpp

"2 #include

3 #include 4

using

namespace

caffe;56

char *proto = "

h:\\models\\caffe\\deploy.prototxt

"; /*

載入caffenet的配置

*/7 phase phase = test; /*

or train */8

caffe::set_mode(caffe::cpu);9//

caffe::set_mode(caffe::gpu);

10//

caffe::setdevice(0);

1112

//! note: 後文所有提到的net,都是這個net

13 boost::shared_ptr< net > net(new caffe::net(proto, phase));

1

char *model = "

h:\\models\\caffe\\bvlc_reference_caffenet.caffemodel

";

2 net->copytrainedlayersfrom(model);

1

char *mean_file = "

h:\\models\\caffe\\imagenet_mean.binaryproto";

2 blobimage_mean;

3blobproto blob_proto;

4const

float *mean_ptr;

5 unsigned int

num_pixel;67

bool succeed = readprotofrombinaryfile(mean_file, &blob_proto);8if

(succeed)

9

1

//! note: data_ptr指向已經處理好(去均值的,符合網路輸入影象的長寬和batch size)的資料

2void caffe_forward(boost::shared_ptr< net > & net, float *data_ptr)

318 net->forwardprefilled();

19 }

1

//! note: net的blob是指,每個層的輸出資料,即feature maps2//

char *query_blob_name = "conv1";

3 unsigned int get_blob_index(boost::shared_ptr< net > & net, char *query_blob_name)413

}14 log(fatal) << "

unknown blob name:

"<15 }

1

//! note: 根據caffenet的deploy.prototxt檔案,該net共有15個blob,從data一直到prob

2char *query_blob_name = "

conv1

"; /*

data, conv1, pool1, norm1, fc6, prob, etc

*/3 unsigned int blob_id =get_blob_index(net, query_blob_name);

45 boost::shared_ptrfloat> > blob = net->blobs()[blob_id];

6 unsigned int num_data = blob->count(); /*

nchw=10x96x55x55 */7

const

float *blob_ptr = (const

float *) blob->cpu_data();

1

//! note: layer包括神經網路所有層,比如,caffenet共有23層2//

char *query_layer_name = "conv1";

3 unsigned int get_layer_index(boost::shared_ptr< net > & net, char *query_layer_name)413

}14 log(fatal) << "

unknown layer name:

"<15 }

1

//! note: 不同於net的blob是feature maps,layer的blob是指conv和fc等層的weight和bias

2char *query_layer_name = "

conv1";

3const

float *weight_ptr, *bias_ptr;

4 unsigned int layer_id =get_layer_index(net, query_layer_name);

5 boost::shared_ptrfloat> > layer = net->layers()[layer_id];

6 std::vectorfloat> >> blobs = layer->blobs();

7if (blobs.size() > 0)8

1213

//! note: 訓練模式下,讀取指定layer的梯度資料,與此相似,唯一的區別是將cpu_data改為cpu_diff

1

const

float* data_ptr; /*

指向待寫入資料的指標, 源資料指標*/2

float* weight_ptr = null; /*

指向網路中某層權重的指標,目標資料指標

*/3 unsigned int data_size; /*

待寫入的資料量 */4

char *layer_name = "

conv1

"; /*

需要修改的layer名字 */5

6 unsigned int layer_id =get_layer_index(net, query_layer_name);

7 boost::shared_ptrfloat> > blob = net->layers()[layer_id]->blobs()[0];8

9 check(data_size == blob->count());

10switch

(caffe::mode())

1121 caffe_copy(blob->count(), data_ptr, weight_ptr);

2223

//! note: 訓練模式下,手動修改指定layer的梯度資料,與此相似

24//

mutable_cpu_data改為mutable_cpu_diff,mutable_gpu_data改為mutable_gpu_diff

1

char* weights_file = "

bvlc_reference_caffenet_new.caffemodel";

2netparameter net_param;

3 net->toproto(&net_param, false

);4 writeprototobinaryfile(net_param, weights_file);

Caffe使用教程

by shicai yang 星空下的巫師 on 2015 08 06 include caffe caffe.hpp include include using namespace caffe char proto h models caffe deploy.prototxt 載入caffenet...

caffe教程翻譯 Loss

與絕大多數的機器學習引擎一樣,caffe是由乙個lossfunction 損失函式 驅動的。loss function也稱為 error function,cost function或者objective function。loss function利用模型中的引數 比如模型中網路的weights ...

Caffe簡明教程1 Caffe簡介

您可以檢視所有文章的索引 caffe簡明教程0 文章列表 caffe是乙個很常用的深度學習框架,官網 在我個人的經歷中,經常遇到 作者在學術 中使用caffe來作為實驗框架。目前,caffe由伯克利ai研究所 bair 以及開源社群成員進行開發和維護。其原始作者是賈揚清。另外,當選擇乙個框架時,我們...