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. CSS两列布局

    方法1:左边设置绝对定位,右边设置左外边距,大小和左边的宽度相等 //CSS部分: .contain{ position :relative; height: 300px; } .left{ posi ...

  2. 将集群WEB节点静态数据迁移到共享存储器(LNMP环境)

    系统版本:Centos 6.5 机器及IP规划如下: 192.168.0.117  MySQL 192.168.0.118  nginx+php 192.168.0.123  nfs ①在NFS机器上 ...

  3. Android笔记(四十八) Android中的资源访问——SDCard

    访问存储在SD卡中的文件 使用 Environment.getExternalStorageState(); 判断是否存在内存卡 使用 Environment.getExternalStorageDi ...

  4. jeecg的开发api接口之旅(http)

    一.接口测试工具 1.postman下载地址:https://download.csdn.net/download/qq_35792159/11898005 2.谷歌浏览器插件:https://www ...

  5. 服务发现:Zookeeper vs etcd vs Consul_转

    转自:https://mp.weixin.qq.com/s?__biz=MzA5OTAyNzQ2OA==&mid=208173179&idx=1&sn=392c17b136c2 ...

  6. [bluez] linux下蓝牙鼠标的延迟问题

    引言 现在的便携设备,接口越来越少了.所以我们没有理由不用蓝牙鼠标.高大上也不贵. 蓝牙4.0之前,蓝牙设备的问题是特别费电.蓝牙4.0之后省电的要命,我的上一个鼠标Microsoft Designe ...

  7. python的time模块和datetime模块

    1. 将当前时间转成字符串 strftime 方法,并输出 import datetime # 获取当前时间 datetime.datetime.now() print(datetime.dateti ...

  8. Luogu-P1450 [HAOI2008]硬币购物-完全背包+容斥定理

    Luogu-P1450 [HAOI2008]硬币购物-完全背包+容斥定理 [Problem Description] 略 [Solution] 上述题目等价于:有\(4\)种物品,每种物品有\(d_i ...

  9. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

  10. HTML5 Canvas 绘制图片不显示的问题

    问题: 慕名赶来,却一脚踩空,低头一看,地上一个大坑. 事情是这样的,在我看完w3c的介绍和很有说服力和教学力的demo后,本着实践出真知的思想决定上手一试,这一试不要紧~ 我按照流水线工程铺设以下几 ...