Caffe源碼解析4: Data_layer

轉載請注明出處,樓燚(yì)航的blog,http://home.cnblogs.com/louyihang-loves-baiyan/

data_layer應該是網絡的最底層,主要是將數據送給blob進入到net中,在data_layer中存在多個跟data_layer相關的類

  • BaseDataLayer
  • BasePrefetchingDataLayer
  • DataLayer
  • DummyDataLayer
  • HDF5DataLayer
  • HDF5OutputLayer
  • ImageDataLayer
  • MemoryDataLayer
  • WindowDataLayer
  • Batch

這里首先說明一下這幾個類之間的區別。
首先Layer是基類,這個之前就已經提到過了。其次看HDF5相關的類有兩個,一個是HDF5DataLayer,另一個是HDF5OutputLayer,主要是基于HDF5數據格式的讀取和存儲

留意到這個data_layer的頭文件還include了不少頭文件

#include <string>
#include <utility>
#include <vector>
#include "hdf5.h"#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/data_reader.hpp"
#include "caffe/data_transformer.hpp"
#include "caffe/filler.hpp"
#include "caffe/internal_thread.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/blocking_queue.hpp"
#include "caffe/util/db.hpp"

hdf5就是之前說到的一種主要用于科學數據記錄、能自我描述的數據格式。
還有幾個跟data相關的頭文件比如data_read.hpp,data_transformer.hpp
其中data_reader主要是負責數據的讀取,傳送到data layer中。并且對于每一個source,都會開一一起獨立的reading thread讀取線程,幾十有多個solver在并行的跑。比如在多GPU訓練的時候,可以保證對于數據庫的讀取是順序的

data_transformer.hpp里面的DataTransformer這個類,這個類我們要關注一下,這個類主要能對input data 執一些預處理操作,比如縮放、鏡像、減去均值。同時還支持一些隨機的操作。
其核心的函數如下,這里總共有5個常在的Transform函數,其中所有函數的第二部分是相同的,都是一個目標blob,而輸入根據輸入的情況可以有所選擇,可以是blob,也可以是opencv的mat 結構,或者proto中定義的datum結構。

void Transform(const Datum& datum, Blob<Dtype>* transformed_blob);
void Transform(const vector<Datum> & datum_vector, Blob<Dtype>* transformed_blob);
void Transform(const vector<cv::Mat> & mat_vector, Blob<Dtype>* transformed_blob);
void Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob);
void Transform(Blob<Dtype>* input_blob, Blob<Dtype>* transformed_blob);

TransformationParameter是該類構造器中需要傳入的一些變形參數,相關的操作定義在proto中,摘錄如下,可以看到總共有sacle,mirror,crop_size,mean_file,mean_value,force_color,force_grey共7個相關操作

message TransformationParameter {optional float scale = 1 [default = 1];optional bool mirror = 2 [default = false];optional uint32 crop_size = 3 [default = 0];optional string mean_file = 4;repeated float mean_value = 5;optional bool force_color = 6 [default = false];optional bool force_gray = 7 [default = false];
}

首先對于dat_layer,里面根據繼承關系最后的幾個子類分別是ImageDataLayer,DataLayer,WindowDataLayer,MemoryDataLayer,HDF5以及Dummy這里暫時先不做分析。
其實最重要的就是類面的layerSetup.首先我們來看DataLayer的DataLayerSetUp

void DataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {const int batch_size = this->layer_param_.data_param().batch_size();//獲得相應的datum,用來初始化top blobDatum& datum = *(reader_.full().peek());//使用data_transformer 來計算根據datum的期望blob的shapevector<int> top_shape = this->data_transformer_->InferBlobShape(datum);this->transformed_data_.Reshape(top_shape);//首先reshape top[0],再根據batch的大小進行預取top_shape[0] = batch_size;top[0]->Reshape(top_shape);for (int i = 0; i < this->PREFETCH_COUNT; ++i) {this->prefetch_[i].data_.Reshape(top_shape);}LOG(INFO) << "output data size: " << top[0]->num() << ","<< top[0]->channels() << "," << top[0]->height() << ","<< top[0]->width();// 同樣reshape label的blob的shapeif (this->output_labels_) {vector<int> label_shape(1, batch_size);top[1]->Reshape(label_shape);for (int i = 0; i < this->PREFETCH_COUNT; ++i) {this->prefetch_[i].label_.Reshape(label_shape);}}
}

MemoryDataLayer

void MemoryDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {//直接通過memory_data_param類設置layer的相關參數batch_size_ = this->layer_param_.memory_data_param().batch_size();channels_ = this->layer_param_.memory_data_param().channels();height_ = this->layer_param_.memory_data_param().height();width_ = this->layer_param_.memory_data_param().width();size_ = channels_ * height_ * width_;CHECK_GT(batch_size_ * size_, 0) <<"batch_size, channels, height, and width must be specified and"" positive in memory_data_param";//這里跟datalayer一樣都是先設置top[0],然后對label進行reshapevector<int> label_shape(1, batch_size_);top[0]->Reshape(batch_size_, channels_, height_, width_);top[1]->Reshape(label_shape);added_data_.Reshape(batch_size_, channels_, height_, width_);added_label_.Reshape(label_shape);data_ = NULL;labels_ = NULL;added_data_.cpu_data();added_label_.cpu_data();
}

ImageDataLayer,它的DataLayerSetUP函數

void ImageDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {const int new_height = this->layer_param_.image_data_param().new_height();const int new_width  = this->layer_param_.image_data_param().new_width();const bool is_color  = this->layer_param_.image_data_param().is_color();string root_folder = this->layer_param_.image_data_param().root_folder();CHECK((new_height == 0 && new_width == 0) ||(new_height > 0 && new_width > 0)) << "Current implementation requires ""new_height and new_width to be set at the same time.";//讀取圖像文件和相應的labelconst string& source = this->layer_param_.image_data_param().source();LOG(INFO) << "Opening file " << source;std::ifstream infile(source.c_str());string filename;int label;while (infile >> filename >> label) {lines_.push_back(std::make_pair(filename, label));}if (this->layer_param_.image_data_param().shuffle()) {// randomly shuffle dataLOG(INFO) << "Shuffling data";const unsigned int prefetch_rng_seed = caffe_rng_rand();prefetch_rng_.reset(new Caffe::RNG(prefetch_rng_seed));ShuffleImages();}LOG(INFO) << "A total of " << lines_.size() << " images.";lines_id_ = 0;//check是否需要隨機跳過一些圖像if (this->layer_param_.image_data_param().rand_skip()) {unsigned int skip = caffe_rng_rand() %this->layer_param_.image_data_param().rand_skip();LOG(INFO) << "Skipping first " << skip << " data points.";CHECK_GT(lines_.size(), skip) << "Not enough points to skip";lines_id_ = skip;}//使用Opencv來讀進圖像,然后使用它初始化相應的top blobcv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first,new_height, new_width, is_color);CHECK(cv_img.data) << "Could not load " << lines_[lines_id_].first;//這里的步驟和上面相同,使用transformer來做reshapevector<int> top_shape = this->data_transformer_->InferBlobShape(cv_img);this->transformed_data_.Reshape(top_shape);//之后部分跟前面差不多,初始化top[0]const int batch_size = this->layer_param_.image_data_param().batch_size();CHECK_GT(batch_size, 0) << "Positive batch size required";top_shape[0] = batch_size;for (int i = 0; i < this->PREFETCH_COUNT; ++i) {this->prefetch_[i].data_.Reshape(top_shape);}top[0]->Reshape(top_shape);LOG(INFO) << "output data size: " << top[0]->num() << ","<< top[0]->channels() << "," << top[0]->height() << ","<< top[0]->width();//reshape labelvector<int> label_shape(1, batch_size);top[1]->Reshape(label_shape);for (int i = 0; i < this->PREFETCH_COUNT; ++i) {this->prefetch_[i].label_.Reshape(label_shape);}
}

WindowDataLayer的DataLayerSetUp,這個函數標比較長,我只列出了其中主要的部分,之前的Image相當于是已經剪裁過的一個圖像,也就是說你的目標基本上是充棉了整個畫面,而Window File是用于原始圖的,也就是說有background和object,這個window file 的格式如下

window_file format
repeated:# image_indeximg_path (abs path)channelsheightwidthnum_windowsclass_index overlap x1 y1 x2 y2
//讀取每一個box
int num_windows;
infile >> num_windows;
const float fg_threshold =this->layer_param_.window_data_param().fg_threshold();
const float bg_threshold =this->layer_param_.window_data_param().bg_threshold();
for (int i = 0; i < num_windows; ++i) {int label, x1, y1, x2, y2;float overlap;infile >> label >> overlap >> x1 >> y1 >> x2 >> y2;vector<float> window(WindowDataLayer::NUM);window[WindowDataLayer::IMAGE_INDEX] = image_index;window[WindowDataLayer::LABEL] = label;window[WindowDataLayer::OVERLAP] = overlap;window[WindowDataLayer::X1] = x1;window[WindowDataLayer::Y1] = y1;window[WindowDataLayer::X2] = x2;window[WindowDataLayer::Y2] = y2;// add window to foreground list or background list// read each box
int num_windows;
infile >> num_windows;
const float fg_threshold =this->layer_param_.window_data_param().fg_threshold();
const float bg_threshold =this->layer_param_.window_data_param().bg_threshold();
for (int i = 0; i < num_windows; ++i) {int label, x1, y1, x2, y2;float overlap;infile >> label >> overlap >> x1 >> y1 >> x2 >> y2;vector<float> window(WindowDataLayer::NUM);window[WindowDataLayer::IMAGE_INDEX] = image_index;window[WindowDataLayer::LABEL] = label;window[WindowDataLayer::OVERLAP] = overlap;window[WindowDataLayer::X1] = x1;window[WindowDataLayer::Y1] = y1;window[WindowDataLayer::X2] = x2;window[WindowDataLayer::Y2] = y2;//首先計算得到overlap,根據Overlap與fg_threshold的比較載添加到fg的list中if (overlap >= fg_threshold) {int label = window[WindowDataLayer::LABEL];CHECK_GT(label, 0);fg_windows_.push_back(window);label_hist.insert(std::make_pair(label, 0));label_hist[label]++;} else if (overlap < bg_threshold) {// background window, force label and overlap to 0window[WindowDataLayer::LABEL] = 0;window[WindowDataLayer::OVERLAP] = 0;bg_windows_.push_back(window);label_hist[0]++;}
}
=-if (overlap >= fg_threshold) {int label = window[WindowDataLayer::LABEL];CHECK_GT(label, 0);fg_windows_.push_back(window);label_hist.insert(std::make_pair(label, 0));label_hist[label]++;} else if (overlap < bg_threshold) {//background的label和overlap都是0window[WindowDataLayer::LABEL] = 0;window[WindowDataLayer::OVERLAP] = 0;bg_windows_.push_back(window);label_hist[0]++;}
}..............
for (map<int, int>::iterator it = label_hist.begin();it != label_hist.end(); ++it) {LOG(INFO) << "class " << it->first << " has " << label_hist[it->first]<< " samples";}LOG(INFO) << "Amount of context padding: "<< this->layer_param_.window_data_param().context_pad();LOG(INFO) << "Crop mode: "<< this->layer_param_.window_data_param().crop_mode();//這里之后的步驟就差不多了,同樣是對transform的一些操作const int crop_size = this->transform_param_.crop_size();CHECK_GT(crop_size, 0);const int batch_size = this->layer_param_.window_data_param().batch_size();top[0]->Reshape(batch_size, channels, crop_size, crop_size);for (int i = 0; i < this->PREFETCH_COUNT; ++i)this->prefetch_[i].data_.Reshape(batch_size, channels, crop_size, crop_size);LOG(INFO) << "output data size: " << top[0]->num() << ","<< top[0]->channels() << "," << top[0]->height() << ","<< top[0]->width();// 對label進行reshapevector<int> label_shape(1, batch_size);top[1]->Reshape(label_shape);for (int i = 0; i < this->PREFETCH_COUNT; ++i) {this->prefetch_[i].label_.Reshape(label_shape);}//做減均值的操作has_mean_file_ = this->transform_param_.has_mean_file();has_mean_values_ = this->transform_param_.mean_value_size() > 0;if (has_mean_file_) {const string& mean_file =this->transform_param_.mean_file();LOG(INFO) << "Loading mean file from: " << mean_file;BlobProto blob_proto;ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);data_mean_.FromProto(blob_proto);}if (has_mean_values_) {CHECK(has_mean_file_ == false) <<"Cannot specify mean_file and mean_value at the same time";for (int c = 0; c < this->transform_param_.mean_value_size(); ++c) {mean_values_.push_back(this->transform_param_.mean_value(c));}CHECK(mean_values_.size() == 1 || mean_values_.size() == channels) <<"Specify either 1 mean_value or as many as channels: " << channels;if (channels > 1 && mean_values_.size() == 1) {// Replicate the mean_value for simplicityfor (int c = 1; c < channels; ++c) {mean_values_.push_back(mean_values_[0]);}}}

轉載于:https://www.cnblogs.com/louyihang-loves-baiyan/p/5153155.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458362.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458362.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458362.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

理解C++中拷貝構造函數

拷貝構造函數的功能是用一個已有的對象來初始化一個被創建的同樣對象&#xff0c;是一種特殊的構造函數&#xff0c;具有一般構造函數的所有特性&#xff0c;當創建一個新對象的時候系統會自動調用它&#xff1b;其形參是本類對象的引用&#xff0c;它的特殊功能是將參數代表的…

IDEA mybatis-generator-maven-plugin 插件的使用

2019獨角獸企業重金招聘Python工程師標準>>> pom.xml中添加插件 <plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuratio…

python優秀網友學習筆記推薦

AstralWindMr.Seven 轉載于:https://www.cnblogs.com/migongci0412/p/5154892.html

深入理解CRITICAL_SECTION

摘要臨界區是一種防止多個線程同時執行一個特定代碼節的機制&#xff0c;這一主題并沒有引起太多關注&#xff0c;因而人們未能對其深刻理解。在需要跟蹤代碼中的多線程處理的性能時&#xff0c;對 Windows 中臨界區的深刻理解非常有用。本文深入研究臨界區的原理&#xff0c;以…

webpack進階之插件篇

上一篇博客講解了webpack環境的基本&#xff0c;這一篇講解一些更深入的內容和開發技巧。基本環境搭建就不展開講了 一、插件篇 1. 自動補全css3前綴 autoprefixer 官方是這樣說的&#xff1a;Parse CSS and add vendor prefixes to CSS rules using values from the Can I Use…

QT:QObject 簡單介紹

QObject 是所有Qt對象的基類。QObject 是Qt模塊的核心。它的最主要特征是關于對象間無縫通信的機制&#xff1a;信號與槽。 使用connect()建立信號到槽的連接&#xff0c;使用disconnect()銷毀連接&#xff0c;使用blockSignals()暫時阻塞信號以避免無限通知循環&#xff0c;使…

利用malloc定義數組

使用malloc方法時&#xff0c;應導入文件 #include<malloc.h> 1.利用malloc定義一維數組 int *num (int *)malloc(sizeof(int)*8); // 定義一個一維數組有8個元素&#xff0c;等價于 int num[8]; 2.利用malloc定義二維數組 int **num &#xff08; int **&#xff09…

C++中基類的析構函數為什么要用virtual虛析構函數

from&#xff1a;https://blog.csdn.net/iicy266/article/details/11906457知識背景要弄明白這個問題&#xff0c;首先要了解下C中的動態綁定。 關于動態綁定的講解&#xff0c;請參閱&#xff1a; C中的動態類型與動態綁定、虛函數、多態實現 正題直接的講&#xff0c;C中基類…

第二章 Python基本元素:數字、字符串和變量

Python有哪些內置的數據類型&#xff1a; True False #布爾型 42 100000000 #整型 3.14159 1.0e8 #浮點型 abcdes #字符串 2.1 變量、名字和對象 python中統一的形式是什么&#xff1f; 對象&#xff0c;所有的對象都是以對象的形式存在…

Mac - 設置NSButton 的背景色

- (void)drawRect:(NSRect)dirtyRect {[super drawRect:dirtyRect];[[NSColor clearColor] setFill];NSRectFill(self.bounds);self.wantsLayer YES;self.layer.cornerRadius 8;self.layer.masksToBounds YES; } 轉載于:https://www.cnblogs.com/741162830qq/p/5157046.html…

C++中static關鍵字作用總結

from&#xff1a;https://www.cnblogs.com/songdanzju/p/7422380.html1.先來介紹它的第一條也是最重要的一條&#xff1a;隱藏。&#xff08;static函數&#xff0c;static變量均可&#xff09; 當同時編譯多個文件時&#xff0c;所有未加static前綴的全局變量和函數都具有全局…

C Primer Plus 第7章 C控制語句:分支和跳轉 7.4 一個統計字數的程序

2019獨角獸企業重金招聘Python工程師標準>>> 首先&#xff0c;這個程序應該逐個讀取字符&#xff0c;并且應該有些方法判斷何時停止&#xff1b;第二&#xff0c;它應該能夠識別并統計下列單位&#xff1a;字符、行和單詞。下面是偽代碼描述&#xff1a; read a cha…

深入理解extern用法

from&#xff1a;https://blog.csdn.net/z702143700/article/details/46805241一、 extern做變量聲明 l 聲明extern關鍵字的全局變量和函數可以使得它們能夠跨文件被訪問。 我們一般把所有的全局變量和全局函數的實現都放在一個*.cpp文件里面&#xff0c;然后用一個同名的*.h文…

收集整理的非常有用的PHP函數

為什么80%的碼農都做不了架構師&#xff1f;>>> 1、PHP加密解密 2、PHP生成隨機字符串 3、PHP獲取文件擴展名&#xff08;后綴&#xff09; 4、PHP獲取文件大小并格式化 5、PHP替換標簽字符 6、PHP列出目錄下的文件名 7、PHP獲取當前頁面URL 8、PHP強制下載文件 9、…

進程間的通信方式——pipe(管道)

from&#xff1a;https://blog.csdn.net/skyroben/article/details/715133851.進程間通信每個進程各自有不同的用戶地址空間,任何一個進程的全局變量在另一個進程中都看不到&#xff0c;所以進程之間要交換數據必須通過內核,在內核中開辟一塊緩沖區,進程A把數據從用戶空間拷到內…

bash中(),{},(()),[],[[]]的區別

前言:在bash中遇到各種括號&#xff0c;同時在進行字符數值比較判定時&#xff0c;總是不斷出現問題&#xff0c;于是通過參考《advanced bash-scripting guide》&#xff0c;同時在centos 6.7版本上進行測試&#xff0c;現況總結如下。如有紕漏&#xff0c;望指正。一.()一個命…

多進程和多線程之間的通信方式及通信實現步驟小結

進程間通信方式 # 管道( pipe )&#xff1a;管道是一種半雙工的通信方式&#xff0c;數據只能單向流動&#xff0c;而且只能在具有親緣關系的進程間使用。進程的親緣關系通常是指父子進程關系。 # 有名管道 (namedpipe) &#xff1a; 有名管道也是半雙工的通信方式&#xff0c;…

highcharts 顯示網格

2019獨角獸企業重金招聘Python工程師標準>>> xAxis: { gridLineColor: #197F07, gridLineWidth: 1 }, yAxis: { gridLineColor: #197F07, gridLineWidth: 2 }, 轉載于:https://my.oschina.net/LingBlog/blog/697885

Cheat—— 給Linux初學者和管理員一個終極命令行備忘單

編譯自&#xff1a;http://www.tecmint.com/cheat-command-line-cheat-sheet-for-linux-users/作者&#xff1a; Avishek Kumar原創&#xff1a;LCTT https://linux.cn/article-3760-1.html譯者&#xff1a; su-kaiyao原文稍有改動 當你不確定你所運行的命令&#xff0c;尤其是…

串口操作之API篇 CreateFile

CreateFile http://bbs.fishc.com/thread-72944-1-1.html(出處: 魚C論壇) ------------------------------------------------------------------------CreateFile用于打開串口,如果操作成功,返回一個句柄.1 function CreateFile(lpFileName: PChar; dwDesiredAccess, dwShareM…