【caffe I/O】数据读取层 代码中文注释
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】数据读取层 代码中文注释的更多相关文章
- 【caffe Net】使用举例和代码中文注释
首先是Net使用的小例子: #include <vector> #include <iostream> #include <caffe/net.hpp> using ...
- 去掉VS2010代码中文注释的红色下划线
VS2010代码中文注释出现红色下划线,代码看上去很不美观,发现是由于安装Visual Assist X插件造成的. 解决办法:打开VAX的Options对话框,取消Advanced --> U ...
- 关闭shift中英文切换 英文代码/中文注释随意切换着写。
x 背景 写代码的时候总是意外的就切成中文了,特别是代码中大小写切换的这种情况... 例如:"public static TimeZone CurrentTime..."publi ...
- 【caffe Layer】代码中文注释
src/caffe/proto/caffe.proto 中LayerParameter部分 // NOTE // Update the next available ID when you add a ...
- WIdo联网代码中文注释
代码如下 /*************************************************** 这是一个例子的dfrobot维多-无线集成物联网建兴传感器和控制节点 *产品页面及更 ...
- servlet层调用biz业务层出现浏览器 500错误,解决方法 dao数据访问层 数据库Util工具类都可能出错 通过新建一个测试类复制代码逐步测试查找出最终出错原因
package com.swift.jztk.servlet; import java.io.IOException; import javax.servlet.ServletException; i ...
- [转载]将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,解决办法
eclipse 代码中文注释乱码 求解决 将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclips ...
- caffe添加自己的数据输入层
整体思路: 阅读caffe数据输入层各个类之间的继承关系,确定当前类需要继承的父类以及所需参数的设置. 编写zzq_data.cpp 在layer_factory.cpp中完成注册: 在caffe.p ...
- php fputcsv 读取不到中文文件、数据
string setlocale(constant,location) constant 必需.规定应该设置什么地区信息. 可用的常量: LC_ALL - 包括下面的所有选项 LC_COLLATE ...
随机推荐
- Core Animation笔记(- Layer 基本属性)
一.Layer的基本属性 1. contents 图层内容默认为nil 可以指定一张图片作为内容展示 self.layerView.layer.contents = (__bridge id)imag ...
- 将Centos7的yum配置为阿里云的镜像(完美解决yum下载太慢的问题)
2017-02-17 16:02:30 张老湿 阅读数 13768 http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0. ...
- WinServer-文件共享端口
When turning on Firewall, please assure that “File and Printer Sharing” on current network profile i ...
- Linux的环境配置文件----.bashrc文件(转)
Linux的环境配置文件----.bashrc文件 .bashrc文件主要保存个人的一些个性化设置,如命令别名.路径等.也即在同一个服务器上,只对某个用户的个性化设置相关.它是一个隐藏文件,需要使 ...
- 分布式结构化存储系统-HBase应用案例
分布式结构化存储系统-HBase应用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 为了让读者更进一步了解HBase在实际生成环境中的应用方法,在董西成的书里介绍两个经典的HB ...
- XPath知识点【一】
什么是 XPath? XPath 使用路径表达式在 XML 文档中进行导航 XPath 包含一个标准函数库 XPath 是 XSLT 中的主要元素 XPath 是一个 W3C 标准 XPath 路径表 ...
- (Linux基础学习)第五章:Linux中的screen应用
第1节:安装screen1.加载系统镜像文件,因为screen的安装包在系统镜像文件中图001 2.列出系统上所有的磁盘[root@centos6 ~]# lsblk图002 3.安装screen应用 ...
- Spring+WebSocket+SockJS实现实时聊天
设计初衷是通过websocket实现网页实时通讯聊天. 工程环境:tomcat8+jdk1.7+maven+eclipse 设计思路:客户端登录网页建立socket连接,后台记录用户连接信息并做标识: ...
- Java精通并发-透过字节码理解synchronized关键字
在上一次https://www.cnblogs.com/webor2006/p/11428408.html中对于synchronized关键字的作用做了一个实例详解,下面再来看一下这个程序: 请问下, ...
- jmeter生成html报告详解
Jmeter Dashboard详解 结果面板主要分为Dashboard和Charts两部分.Dashboard对信息进行汇总展示,Charts展示更多详细指标数据. Dashboard Test a ...