caffe.proto中DataParameter部分

message DataParameter {
//输入数据使用的DB类型
enum DB {
LEVELDB = ;//使用LEVELDB
LMDB = ; //使用LMDB
}
// Specify the data source.源数据的路径
optional string source = ;
// Specify the batch size.一个批量数据包含的图片数目
optional uint32 batch_size = ;
// The rand_skip variable is for the data layer to skip a few data points
// to avoid all asynchronous sgd clients to start at the same point. The skip
// point would be set as rand_skip * rand(0,1). Note that rand_skip should not
// be larger than the number of keys in the database.
// DEPRECATED. Each solver accesses a different subset of the database.
// 随机跳过若干图片。防止SGD从同一起点开始。已弃用。
optional uint32 rand_skip = [default = ];
optional DB backend = [default = LEVELDB];//默认输入数据使用DB类型。默认LEVELDB
// DEPRECATED. See TransformationParameter. For data pre-processing, we can do
// simple scaling and subtracting the data mean, if provided. Note that the
// mean subtraction is always carried out before scaling.
// 弃用。使用TransformationParameter
optional float scale = [default = ];
optional string mean_file = ;
// DEPRECATED. See TransformationParameter. Specify if we would like to randomly
// crop an image.
optional uint32 crop_size = [default = ];
// DEPRECATED. See TransformationParameter. Specify if we want to randomly mirror
// data.
optional bool mirror = [default = false];
// Force the encoded image to have 3 color channels 强制编码图像为三通道彩色图像
optional bool force_encoded_color = [default = false];
// Prefetch queue (Increase if data feeding bandwidth varies, within the
// limit of device memory for GPU training)
// 预取队列 (在硬件设备允许的情况下,预先放到主机内存中的批量数,默认为4个batch)
optional uint32 prefetch = [default = ];
}

include/caffe/layers/base_data_layer.hpp

 #ifndef CAFFE_DATA_LAYERS_HPP_
#define CAFFE_DATA_LAYERS_HPP_ #include <vector> #include "caffe/blob.hpp"
#include "caffe/data_transformer.hpp"
#include "caffe/internal_thread.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/blocking_queue.hpp" namespace caffe { /**
* @brief Provides base for data layers that feed blobs to the Net.
*
* TODO(dox): thorough documentation for Forward and proto params.
*/
//基本数据层,派生于Layer
template <typename Dtype>
class BaseDataLayer : public Layer<Dtype> {
public:
explicit BaseDataLayer(const LayerParameter& param);
// LayerSetUp: implements common data layer setup functionality, and calls
// DataLayerSetUp to do special data layer setup for individual layer types.
// This method may not be overridden except by the BasePrefetchingDataLayer.
//通用层配置功能。之后调用DataLayerSetUp进行数据读取层的特殊配置
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {}
// Data layers have no bottoms, so reshaping is trivial.
//数据读取层没有输入Bottom Blob,变形操作不是很重要
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {}
//反向传播函数不需要做任何事情
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {} protected:
TransformationParameter transform_param_;//数据预处理变换器参数
shared_ptr<DataTransformer<Dtype> > data_transformer_;//数据预处理变换器
bool output_labels_;//是否输出标签数据
}; //批量数据,用于存放数据读取层输出
template <typename Dtype>
class Batch {
public:
Blob<Dtype> data_, label_;//两个Blob分别用来存储图片数据和标签
}; //带预取功能的数据读取层,派生于BaseDataLayer和InternalThread
template <typename Dtype>
class BasePrefetchingDataLayer :
public BaseDataLayer<Dtype>, public InternalThread {
public:
explicit BasePrefetchingDataLayer(const LayerParameter& param);
// LayerSetUp: implements common data layer setup functionality, and calls
// DataLayerSetUp to do special data layer setup for individual layer types.
// This method may not be overridden.层设置
void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top); virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);//前向
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top); protected:
virtual void InternalThreadEntry();//内部线程入口
virtual void load_batch(Batch<Dtype>* batch) = ;//载入批量数据,纯虚函数 vector<shared_ptr<Batch<Dtype> > > prefetch_;//抓取
BlockingQueue<Batch<Dtype>*> prefetch_free_;//空闲Batch队列
BlockingQueue<Batch<Dtype>*> prefetch_full_;//已加载Batch队列
Batch<Dtype>* prefetch_current_; Blob<Dtype> transformed_data_;//变换后的数据
}; } // namespace caffe #endif // CAFFE_DATA_LAYERS_HPP_

src/caffe/layers/base_data_layer.cpp

 #include <boost/thread.hpp>
#include <vector> #include "caffe/blob.hpp"
#include "caffe/data_transformer.hpp"
#include "caffe/internal_thread.hpp"
#include "caffe/layer.hpp"
#include "caffe/layers/base_data_layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/blocking_queue.hpp" namespace caffe { //构造函数。初始化Layer参数、数据变换器参数
template <typename Dtype>
BaseDataLayer<Dtype>::BaseDataLayer(const LayerParameter& param)
: Layer<Dtype>(param),
transform_param_(param.transform_param()) {
} //BaseDataLayer层设置
template <typename Dtype>
void BaseDataLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
if (top.size() == ) {//判断输出Blob数目。1则只输出data,2则输出data和label
output_labels_ = false;
} else {
output_labels_ = true;
}
//初始化数据变换器对象
data_transformer_.reset(
new DataTransformer<Dtype>(transform_param_, this->phase_));
data_transformer_->InitRand();
// The subclasses should setup the size of bottom and top
DataLayerSetUp(bottom, top);//子类负责设置Top Blob形状
} //BasePrefetchingDataLayer构造函数
template <typename Dtype>
BasePrefetchingDataLayer<Dtype>::BasePrefetchingDataLayer(
const LayerParameter& param)
: BaseDataLayer<Dtype>(param),
prefetch_(param.data_param().prefetch()),
prefetch_free_(), prefetch_full_(), prefetch_current_() {
for (int i = ; i < prefetch_.size(); ++i) {
prefetch_[i].reset(new Batch<Dtype>());
prefetch_free_.push(prefetch_[i].get());//将batch对象都放入空闲队列
}
} //BasePrefetchingDataLayer层配置函数
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
BaseDataLayer<Dtype>::LayerSetUp(bottom, top); // Before starting the prefetch thread, we make cpu_data and gpu_data
// calls so that the prefetch thread does not accidentally make simultaneous
// cudaMalloc calls when the main thread is running. In some GPUs this
// seems to cause failures if we do not so.
//在开启数据预取线程前,通过调用Blob相应函数先进行cudaMalloc,
//避免多线程情况下同时进行cudaMalloc,会导致cuda API调用失败
for (int i = ; i < prefetch_.size(); ++i) {
prefetch_[i]->data_.mutable_cpu_data();
if (this->output_labels_) {
prefetch_[i]->label_.mutable_cpu_data();
}
}
//GPU
#ifndef CPU_ONLY
if (Caffe::mode() == Caffe::GPU) {
for (int i = ; i < prefetch_.size(); ++i) {
prefetch_[i]->data_.mutable_gpu_data();
if (this->output_labels_) {
prefetch_[i]->label_.mutable_gpu_data();
}
}
}
#endif
DLOG(INFO) << "Initializing prefetch";
this->data_transformer_->InitRand();
StartInternalThread();//开启内部预取线程
DLOG(INFO) << "Prefetch initialized.";
} //内部线程入口
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::InternalThreadEntry() {
//创建CUDA Stream,非阻塞类型
#ifndef CPU_ONLY
cudaStream_t stream;
if (Caffe::mode() == Caffe::GPU) {
CUDA_CHECK(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
}
#endif try {
while (!must_stop()) {//循环载入批量数据
Batch<Dtype>* batch = prefetch_free_.pop();//得到一个空闲batch
load_batch(batch);//载入批量数据
#ifndef CPU_ONLY
if (Caffe::mode() == Caffe::GPU) {
batch->data_.data().get()->async_gpu_push(stream);
if (this->output_labels_) {
batch->label_.data().get()->async_gpu_push(stream);
}
CUDA_CHECK(cudaStreamSynchronize(stream));//同步到GPU
}
#endif
prefetch_full_.push(batch);//加入到带负载的Batch队列中
}
} catch (boost::thread_interrupted&) {//捕获到异常则退出循环
// Interrupted exception is expected on shutdown
}
#ifndef CPU_ONLY
if (Caffe::mode() == Caffe::GPU) {
CUDA_CHECK(cudaStreamDestroy(stream));//销毁CUDA Stream
}
#endif
} //前向传播函数
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
if (prefetch_current_) {
prefetch_free_.push(prefetch_current_);
}
//从带负载的Batch中取出一个Batch对象
prefetch_current_ = prefetch_full_.pop("Waiting for data");
// Reshape to loaded data.输出Top Blob根据Batch形状进行变形
top[]->ReshapeLike(prefetch_current_->data_);
top[]->set_cpu_data(prefetch_current_->data_.mutable_cpu_data());
if (this->output_labels_) {//如果需要输出便签数据
// Reshape to loaded labels.
top[]->ReshapeLike(prefetch_current_->label_);//Top Blob根据Batch中lable_形状进行变形
top[]->set_cpu_data(prefetch_current_->label_.mutable_cpu_data());
}
} #ifdef CPU_ONLY
STUB_GPU_FORWARD(BasePrefetchingDataLayer, Forward);
#endif INSTANTIATE_CLASS(BaseDataLayer);
INSTANTIATE_CLASS(BasePrefetchingDataLayer); } // namespace caffe

摘抄参看赵永科《21天实战caffe》

【caffe I/O】数据读取层 代码中文注释的更多相关文章

  1. 【caffe Net】使用举例和代码中文注释

    首先是Net使用的小例子: #include <vector> #include <iostream> #include <caffe/net.hpp> using ...

  2. 去掉VS2010代码中文注释的红色下划线

    VS2010代码中文注释出现红色下划线,代码看上去很不美观,发现是由于安装Visual Assist X插件造成的. 解决办法:打开VAX的Options对话框,取消Advanced --> U ...

  3. 关闭shift中英文切换 英文代码/中文注释随意切换着写。

    x 背景 写代码的时候总是意外的就切成中文了,特别是代码中大小写切换的这种情况... 例如:"public static TimeZone CurrentTime..."publi ...

  4. 【caffe Layer】代码中文注释

    src/caffe/proto/caffe.proto 中LayerParameter部分 // NOTE // Update the next available ID when you add a ...

  5. WIdo联网代码中文注释

    代码如下 /*************************************************** 这是一个例子的dfrobot维多-无线集成物联网建兴传感器和控制节点 *产品页面及更 ...

  6. servlet层调用biz业务层出现浏览器 500错误,解决方法 dao数据访问层 数据库Util工具类都可能出错 通过新建一个测试类复制代码逐步测试查找出最终出错原因

    package com.swift.jztk.servlet; import java.io.IOException; import javax.servlet.ServletException; i ...

  7. [转载]将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,解决办法

    eclipse 代码中文注释乱码 求解决 将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclips ...

  8. caffe添加自己的数据输入层

    整体思路: 阅读caffe数据输入层各个类之间的继承关系,确定当前类需要继承的父类以及所需参数的设置. 编写zzq_data.cpp 在layer_factory.cpp中完成注册: 在caffe.p ...

  9. php fputcsv 读取不到中文文件、数据

    string  setlocale(constant,location) constant 必需.规定应该设置什么地区信息. 可用的常量: LC_ALL - 包括下面的所有选项 LC_COLLATE ...

随机推荐

  1. linux设备模型与内核中的面向对象思想

    linux内核用C语言实现了C++面向对象的大部分特性:封装,继承,多态.在看内核的过程中,开始追寻其中的设计思想,封装.继承.多态.恰好今天又在看Linux设备模型,找了很多资料.总结如下: 1.l ...

  2. CentOS7搭建git服务器

    在CentOS7上搭建git服务器, 1.在Linux上安装git yum install -y git 验证是否安装成功,出现版本号即成功 git --version 2.创建版本库和用户 创建用户 ...

  3. Android自动化测试探索(一)adb详细介绍

    adb详细介绍 #1. 基本简介 adb,即Android Debug Bridge,它是Android开发/测试人员不可替代的强大工具 #2. Mac上安装adb 安装brew /usr/bin/r ...

  4. tengine编译安装及nginx高并发内核参数优化

    Tengine Tengine介绍 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性. Tengine的性能和稳定性已经在大型的 ...

  5. 每个程序员都应该知道延迟数—Latency Numbers Every Programmer Should Know

    每个程序员都应该知道延迟数 Latency Numbers Every Programmer Should Know https://people.eecs.berkeley.edu/~rcs/res ...

  6. Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题

    Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] ​ 总共两次询 ...

  7. P2577 [ZJOI2005]午餐[DP]

    题目描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口)不同,所以他们要吃的菜各 ...

  8. Bias vs. Variance(4)---根据是high bias还是high variance问题来判断接下来做些什么

    怎么区分哪些措施对我们有用呢?----首先根据learning curve来判断你的问题是high bias or variance 当你的算法是high bias问题时,如果你get more tr ...

  9. LINQ查询表达式(5) - LINQ Null值处理&异常处理

    查询表达式中处理Null值 此示例演示如何处理源集合中可能的 null 值. 诸如 IEnumerable<T> 等对象集合可能包含值为 null 的元素. 如果源集合为 null 或包含 ...

  10. PHP——json_encode转码保留中文

    前言 特殊的情况,特殊对待吧.转码为GBK再json_encode会报错,因为json_encode是只支持utf8的. 代码 文档 | https://www.php.net/manual/en/f ...