Fast RCNN訓練階段程式碼解析
- 首先是入口檔案trian_net.py,真正處理資料的檔案都在lib檔案裡,包括資料集製作的檔案在lib/datasets下,網路訓練測試的檔案在lib/fast_rcnn下,lib/roi_data_layer是用python實現的網路的輸入層。
parse_args函式解析輸入引數:網路引數定義,初始化模型(這兩項沒有預設值必須自己指定),顯示卡號,最大迭代次數,訓練資料位置等。
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Train a Fast R-CNN network' )
parser.add_argument('--gpu', dest='gpu_id',
help='GPU device id to use [0]',
default=0, type=int)
parser.add_argument('--solver', dest='solver',
help='solver prototxt',
default=None, type=str)
parser.add_argument('--iters' , dest='max_iters',
help='number of iterations to train',
default=40000, type=int)
parser.add_argument('--weights', dest='pretrained_model',
help='initialize with pretrained model weights',
default=None, type=str)
parser.add_argument('--cfg' , dest='cfg_file',
help='optional config file',
default=None, type=str)
parser.add_argument('--imdb', dest='imdb_name',
help='dataset to train on',
default='voc_2007_trainval', type=str)
parser.add_argument('--rand', dest='randomize',
help='randomize (do not use a fixed seed)',
action='store_true')
parser.add_argument('--set', dest='set_cfgs',
help='set config keys', default=None,
nargs=argparse.REMAINDER)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
程式入口,可以看做main函式
if __name__ == '__main__':
args = parse_args()#解析輸入引數,存入args
print('Called with args:')
print(args)
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)
if args.set_cfgs is not None:
cfg_from_list(args.set_cfgs)
print('Using config:')
pprint.pprint(cfg)
#設定caffe
if not args.randomize:
# fix the random seeds (numpy and caffe) for reproducibility
np.random.seed(cfg.RNG_SEED)
caffe.set_random_seed(cfg.RNG_SEED)
# set up caffe
caffe.set_mode_gpu()
if args.gpu_id is not None:
caffe.set_device(args.gpu_id)
#讀取訓練資料,包括訓練圖片的位置,物體gt(外接框)座標,selective search方法產生的proposal(候選框)。呼叫的是lib/datasets/factory.py中的get_imdb函式
imdb = get_imdb(args.imdb_name)
print 'Loaded dataset `{:s}` for training'.format(imdb.name)
#上步得到的資料imdb進一步製作成訓練時的資料,主要是把圖片翻轉,擴充訓練樣本.
roidb = get_training_roidb(imdb)
output_dir = get_output_dir(imdb, None)
print 'Output will be saved to `{:s}`'.format(output_dir)
#真正的訓練函式,呼叫lib/fast_rcnn/train.py中的train_net函式
train_net(args.solver, roidb, output_dir,
pretrained_model=args.pretrained_model,
max_iters=args.max_iters)
2.訓練資料讀取主函式是imdb=get_imdb(args.imdb_name)函式,在下面前輩的部落格中已經很清楚了,請參考。
http://www.cnblogs.com/louyihang-loves-baiyan/archive/2015/10/16/4885659.html
將訓練資料讀取到imdb變數中只是簡單的將資料讀入,並沒有將樣本標註為正負類,資料擴充等操作。緊接著呼叫 roidb = get_training_roidb(imdb)製作訓練資料集,實現在lib/fast_rcnn/train.py中
def get_training_roidb(imdb):
"""Returns a roidb (Region of Interest database) for use in training."""
if cfg.TRAIN.USE_FLIPPED:
print 'Appending horizontally-flipped training examples...'
imdb.append_flipped_images()#資料翻轉操作,擴充訓練資料集
print 'done'
print 'Preparing training data...'
rdl_roidb.prepare_roidb(imdb)#prepare_roidb函式中max_classes是每個proposal重合度最大的物體gt的類別,max_overlaps是最大重合度。
print 'done'
return imdb.roidb
3.lib/roi_data_layer下的網輸入層
caffe提供了python,也就是說可以用python實現某一個層,fast_rcnn就用python實現了網路的輸入層。
fast_rcnn中實現caffe支援python主要設定了兩個檔案:fast-rcnn/caffe-fast-rcnn/src/caffe.proto和fast-rcnn/caffe-fast-rcnn/include/caffe/python_layer.hpp檔案
. caffe.proto中註冊Python層引數:
optional PythonParameter python_param = 130;
........
// Message that stores parameters used by PythonLayer
message PythonParameter {
optional string module = 1;
optional string layer = 2;
// This value is set to the attribute `param_str_` of your custom
// `PythonLayer` object in Python before calling `setup()` method. This could
// be a number, a string, a dictionary in Python dict format or JSON etc. You
// may parse this string in `setup` method and use them in `forward` and
// `backward`.
optional string param_str = 3 [default = ''];
}
. python_layer.hpp標頭檔案定義了需要python實現的幾個輸入層的重要函式:setup函式,reshape函式,forward函式,backward函式。
#ifndef CAFFE_PYTHON_LAYER_HPP_
#define CAFFE_PYTHON_LAYER_HPP_
#include <boost/python.hpp>
#include <string>
#include <vector>
#include "caffe/layer.hpp"
namespace bp = boost::python;
namespace caffe {
#define PYTHON_LAYER_ERROR() { \
PyObject *petype, *pevalue, *petrace; \
PyErr_Fetch(&petype, &pevalue, &petrace); \
bp::object etype(bp::handle<>(bp::borrowed(petype))); \
bp::object evalue(bp::handle<>(bp::borrowed(bp::allow_null(pevalue)))); \
bp::object etrace(bp::handle<>(bp::borrowed(bp::allow_null(petrace)))); \
bp::object sio(bp::import("StringIO").attr("StringIO")()); \
bp::import("traceback").attr("print_exception")( \
etype, evalue, etrace, bp::object(), sio); \
LOG(INFO) << bp::extract<string>(sio.attr("getvalue")())(); \
PyErr_Restore(petype, pevalue, petrace); \
throw; \
}
template <typename Dtype>
class PythonLayer : public Layer<Dtype> {
public:
PythonLayer(PyObject* self, const LayerParameter& param)
: Layer<Dtype>(param), self_(bp::handle<>(bp::borrowed(self))) { }
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
try {
self_.attr("param_str_") = bp::str(
this->layer_param_.python_param().param_str());
#LayerSetUp函式呼叫setup函式,具體實現在lib/roi_data_layer/layer.py中的setup函式。下面reshape,forward,backward函式同理
self_.attr("setup")(bottom, top);
} catch (bp::error_already_set) {
PYTHON_LAYER_ERROR();
}
}
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
try {
self_.attr("reshape")(bottom, top);
} catch (bp::error_already_set) {
PYTHON_LAYER_ERROR();
}
}
virtual inline const char* type() const { return "Python"; }
protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
try {
#forward函式的實現在lib/roi_data_layer/layer.py中
self_.attr("forward")(bottom, top);
} catch (bp::error_already_set) {
PYTHON_LAYER_ERROR();
}
}
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
try {
#backward函式的實現在lib/roi_data_layer/layer.py中
self_.attr("backward")(top, propagate_down, bottom);
} catch (bp::error_already_set) {
PYTHON_LAYER_ERROR();
}
}
private:
bp::object self_;
};
} // namespace caffe
#endif
相關推薦
Fast RCNN訓練階段程式碼解析
首先是入口檔案trian_net.py,真正處理資料的檔案都在lib檔案裡,包括資料集製作的檔案在lib/datasets下,網路訓練測試的檔案在lib/fast_rcnn下,lib/roi_data_layer是用python實現的網路的輸入層。 p
Fast RCNN 訓練自己資料集 (1編譯配置)
FastRCNN 訓練自己資料集 (1編譯配置) FastRCNN是Ross Girshick在RCNN的基礎上增加了Multi task training整個的訓練過程和測試過程比RCNN快了許多。別的一些細節不展開,過幾天會上傳Fast RCNN的論文筆記。FastRCNN mAP效能上略有上升。Fa
Fast RCNN 訓練自己的資料集(3訓練和檢測)
1.預訓練模型介紹 首先在data目錄下,有兩個目錄就是之前在1中解壓好 fast_rcnn_models/ imagenet_models/ fast_rcnn_model資料夾下面是作者用fast rcnn訓練好的三個網路,分別對應著小、中、大型網路,大家可以試用一下這幾個網路,看一些檢測效果,
fast rcnn 訓練自己的資料集(訓練和檢測)
1.預訓練模型介紹 首先在data目錄下,有兩個目錄就是之前在1中解壓好 fast_rcnn_models/imagenet_models/ fast_rcnn_model資料夾下面是作者用fast rcnn訓練好的三個網路,分別對應著小、中、大型網路,大家可
fast-rcnn訓練實戰
這一週訓練了一個fast-rcnn網路,趁著還沒有忘記先記一筆。 關於圖片檢測detection這一類問題,隨著CNN的流行出現了許多新的方法與系統。其中RCNN就是比較出名的一個。Rcnn的論文在此 caffe裡也實現了rcnn具體的demo 在rcnn的paper
Fast RCNN 訓練自己資料集 (2修改資料讀取介面)
Fast RCNN訓練自己的資料集 (2修改讀寫介面) 這裡樓主講解了如何修改Fast RCNN訓練自己的資料集,首先請確保你已經安裝好了Fast RCNN的環境,具體的編配編制操作請參考我的上一篇文章。首先可以看到fast rcnn的工程目錄下有個Lib目錄 這裡下面存在3個目錄分別是: datase
Fast rcnn訓練
ubuntu:~/fast-rcnn$ ./tools/train_net.py --cpu 0 --solver models/CaffeNet/solver.prototxt --weights
Faster RCNN演算法訓練程式碼解析(2)
接著上篇的部落格,我們獲取imdb和roidb的資料後,就可以搭建網路進行訓練了。 我們回到trian_rpn()函式裡面,此時執行完了roidb, imdb = get_roidb(imdb_name),取得了imdb和roidb資料。 先進入第一階段的訓練: print
Faster RCNN演算法訓練程式碼解析(3)
四個層的forward函式分析: RoIDataLayer:讀資料,隨機打亂等 AnchorTargetLayer:輸出所有anchors(這裡分析這個) ProposalLayer:用產生的anchors平移整圖,裁剪出界、移除低於閾值的的anchors,排序後使用nms,返回頂部排名的anchors
【TensorFlow】多GPU訓練:示例程式碼解析
使用多GPU有助於提升訓練速度和調參效率。 本文主要對tensorflow的示例程式碼進行註釋解析:cifar10_multi_gpu_train.py 1080Ti下加速效果如下(batch=128) 單卡: 兩個GPU比單個GPU加速了近一倍 :
tensorflow+faster rcnn程式碼解析(二):anchor_target_layer、proposal_target_layer、proposal_layer
接在tensorflow+faster rcnn程式碼理解(一):構建vgg前端和RPN網路之後,對於每張輸入影象(600×800)RPN會產生17100個anchor,構建RPN後會輸出4個tensor,維度如下: rpn_cls_prob:(1,38,50,18) rpn_bbo
Faster rcnn程式碼解析
最近工作需要,對faster rcnn的程式碼進行了研究。faster rcnn中除開常規的神經網路部分之外,最終要的部分應該是資料的讀取和組織,論文中提到的anchor的生成,以及如何使用這些anc
Fast RCNN的訓練與測試
1.準備工作1.1軟體準備 首先,需要安裝Caffe和pycaffe。caffe原作者網頁://caffe.berkeleyvision.org/installation.html 注意:必須在Makefile.config配置檔案中開啟Python層支援。 #
faster RCNN(keras版本)程式碼講解(3)-訓練流程詳情
一.整體流程概述 1.輸入引數,其實輸入1個就行了(D:\tempFile\VOCdevkit),另外一個resnet權重只是為了加快訓練,如圖: 2.從VOC2007資料集中讀取資料,變成想要的資料格式 3.定義生成資料的迭代器 4.定義3個網
Fast-RCNN程式碼解讀(0)
Fast-RCNN程式碼解讀(0) 由於博主最近正在嘗試修改rbg大神的fast-rcnn程式碼,為了推進自己的學習進度,在此做一個記錄,同時也便於跟大家交流。 Fast-RCNN配置檔案解
Fast rcnn cpu 訓練自己的資料
本文介紹如何在 cpu 模式下使用 Faster RCNN demo,以及在cpu 模式下訓練自己的資料。 Install Faster-rcnn 由於 faster rcnn 依賴是基於 caffe 的,所以需要先安裝 caffe,所以前提是
語義分割丨PSPNet原始碼解析「訓練階段」
引言 之前一段時間在參與語義分割的專案,最近有時間了,正好把這段時間的所學總結一下。 在程式碼上,語義分割的框架會比目標檢測簡單很多,但其中也涉及了很多細節。在這篇文章中,我以PSPNet為例,解讀一下語義分割框架的程式碼。搞清楚一個框架後,再看別人的框架都是大同小異。 工程來自https://github.
Fast RCNN中RoI的映射關系
映射 pool .cn rop 而是 如何 ref targe 大小 在Fast RCNN中,為了減少計算量,不是進行2k次運算,而是進行了1次運算,然後在從pool5中crop出SS圖片所對應的Feature map,這裏詳細的介紹一下是如何實現的。 在CNN中下一層Fe
11月深度學習班第5課圖像物體檢測:rcnn/fast-rcnn/faster-rcnn
連接 過程 bsp reg 卷積 獨立 src 方案 技術 rcnn:看作分類問題,算法的組合:候選框+卷積提取特征+svm分類 候選框是借用外來的方案,深度學習只是用來提取特征,分類是svm算法,所以不是端到端的方案 1:邊緣策略,先根據圖像像素之間的關系聚類
Fast rcnn,Faster rcnn(RCNN改進)
src img cnblogs 改進 pool rcnn 出現 傳播 耗時 Fast RCNN: 出現原因: RCNN在對每個選擇區域都要進行前向傳播,耗時比較多 改進: 提出POIPool(興趣區域池化) 所有區域進行一次前向傳播 Fast rcnn,Faster