Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add #7066

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

add #7066

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,12 @@ proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
$(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
$(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
@ echo PROTOC $<
$(Q)protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
$(Q)/usr/bin/protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<

$(PY_PROTO_BUILD_DIR)/%_pb2.py : $(PROTO_SRC_DIR)/%.proto \
$(PY_PROTO_INIT) | $(PY_PROTO_BUILD_DIR)
@ echo PROTOC \(python\) $<
$(Q)protoc --proto_path=src --python_out=python $<
$(Q)/usr/bin/protoc --proto_path=src --python_out=python $<

$(PY_PROTO_INIT): | $(PY_PROTO_BUILD_DIR)
touch $(PY_PROTO_INIT)
Expand Down
1 change: 1 addition & 0 deletions examples/mnist/lenet_solver.prototxt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU
debug_info: true
19 changes: 11 additions & 8 deletions include/caffe/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const int kMaxBlobAxes = 32;


namespace caffe {

/**
Expand All @@ -20,6 +21,8 @@ namespace caffe {
*
* TODO(dox): more thorough description.
*/

//存储基础数据结构的模板类
template <typename Dtype>
class Blob {
public:
Expand Down Expand Up @@ -49,9 +52,9 @@ class Blob {
* propagate the new input shape to higher layers.
*/
void Reshape(const vector<int>& shape);
void Reshape(const BlobShape& shape);
void ReshapeLike(const Blob& other);
inline string shape_string() const {
void Reshape(const BlobShape& shape); // NCHW
void ReshapeLike(const Blob& other);
inline string shape_string() const {
ostringstream stream;
for (int i = 0; i < shape_.size(); ++i) {
stream << shape_[i] << " ";
Expand Down Expand Up @@ -267,12 +270,12 @@ class Blob {
bool ShapeEquals(const BlobProto& other);

protected:
shared_ptr<SyncedMemory> data_;
shared_ptr<SyncedMemory> diff_;
shared_ptr<SyncedMemory> shape_data_;
shared_ptr<SyncedMemory> data_; //参数
shared_ptr<SyncedMemory> diff_; //梯度
shared_ptr<SyncedMemory> shape_data_; //shape(NCHW)
vector<int> shape_;
int count_;
int capacity_;
int count_; //reshape后的数据块的大小
int capacity_; //当前数据块的大小

DISABLE_COPY_AND_ASSIGN(Blob);
}; // class Blob
Expand Down
4 changes: 4 additions & 0 deletions include/caffe/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace gflags = google;
#endif // GFLAGS_GFLAGS_H_

// Disable the copy and assignment operator for a class.
// 禁止某个类通过构造函数直接初始化另一个类
#define DISABLE_COPY_AND_ASSIGN(classname) \
private:\
classname(const classname&);\
Expand All @@ -43,6 +44,7 @@ private:\
template class classname<float>; \
template class classname<double>

// 初始化GPU的前向传播函数
#define INSTANTIATE_LAYER_GPU_FORWARD(classname) \
template void classname<float>::Forward_gpu( \
const std::vector<Blob<float>*>& bottom, \
Expand All @@ -61,6 +63,8 @@ private:\
const std::vector<bool>& propagate_down, \
const std::vector<Blob<double>*>& bottom)


// 初始化GPU的前向反向传播函数
#define INSTANTIATE_LAYER_GPU_FUNCS(classname) \
INSTANTIATE_LAYER_GPU_FORWARD(classname); \
INSTANTIATE_LAYER_GPU_BACKWARD(classname)
Expand Down
2 changes: 2 additions & 0 deletions include/caffe/data_transformer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DataTransformer {
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See data_layer.cpp for an example.
*/
// 对Datum的数据进行变换,放入到transformed_blob中
void Transform(const Datum& datum, Blob<Dtype>* transformed_blob);

/**
Expand All @@ -47,6 +48,7 @@ class DataTransformer {
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See memory_layer.cpp for an example.
*/
// 对Datum容器的数据进行变换翻入到transformed_blob
void Transform(const vector<Datum> & datum_vector,
Blob<Dtype>* transformed_blob);

Expand Down
4 changes: 3 additions & 1 deletion include/caffe/internal_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ class InternalThread {
protected:
/* Implement this method in your subclass
with the code you want your thread to run. */
// 定义了一个虚函数,要求继承该类的必须要实现
virtual void InternalThreadEntry() {}

/* Should be tested when running loops to exit when requested. */
// 在当请求退出的时候应该调用该函数
bool must_stop();

private:
void entry(int device, Caffe::Brew mode, int rand_seed,
int solver_count, int solver_rank, bool multiprocess);

// 内部的成员变量
shared_ptr<boost::thread> thread_;
};

Expand Down
23 changes: 19 additions & 4 deletions include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/math_functions.hpp"

/**
* 相关的类包括:
* layer.hpp 抽象的基类
* common_layers.hpp
* data_layers.hpp
* loss_layers.hpp
* neuron_layers.hpp
* vision_layers.hpp
*/

/**
Forward declare boost::thread instead of including boost/thread.hpp
to avoid a boost/NVCC issues (#1009, #1010) on OSX.
Expand All @@ -37,6 +47,7 @@ class Layer {
* to SetUp(), where the dimensions of the bottom blobs are provided to the
* layer.
*/

explicit Layer(const LayerParameter& param)
: layer_param_(param) {
// Set phase and copy blobs (if there are any).
Expand All @@ -48,7 +59,7 @@ class Layer {
blobs_[i]->FromProto(layer_param_.blobs(i));
}
}
}
} //用protobuf 传入的参数对blobs_ 做初始化,blobs_ 是一个vector 存放指向Blob类的智能指针
virtual ~Layer() {}

/**
Expand All @@ -67,9 +78,9 @@ class Layer {
void SetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
CheckBlobCounts(bottom, top);
LayerSetUp(bottom, top);
Reshape(bottom, top);
SetLossWeights(top);
LayerSetUp(bottom, top);
Reshape(bottom, top); // 调用Reshape函数为top blob分配合适大小的存储空间
SetLossWeights(top); //为每个top blob设置损失权重乘子,非LossLayer为的top blob其值为零
}

/**
Expand All @@ -88,6 +99,8 @@ class Layer {
* <code>Reshape</code>, which will be called before the forward pass to
* adjust the top blob sizes.
*/

// 网络构建时初始化层和层的连接
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {}

Expand Down Expand Up @@ -123,6 +136,7 @@ class Layer {
*
* Your layer should implement Forward_cpu and (optionally) Forward_gpu.
*/
// 网络数据前向传递,给定bottom输入数据,计算输出到top
inline Dtype Forward(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);

Expand All @@ -147,6 +161,7 @@ class Layer {
*
* Your layer should implement Backward_cpu and (optionally) Backward_gpu.
*/
// 网络误差反向传递,给定top的梯度,计算bottom的梯度并存储到bottom blob
inline void Backward(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom);
Expand Down
15 changes: 13 additions & 2 deletions include/caffe/layer_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ namespace caffe {
template <typename Dtype>
class Layer;

/**
* 主要负责Layer的注册,已经注册完的Layer在运行时可以通过传递一个LayerParameter给CreaterLayer函数的方式来调用
*/
template <typename Dtype>
class LayerRegistry {
public:
// 函数指针Creator,返回的是Layer<Dtype>类型的指针
typedef shared_ptr<Layer<Dtype> > (*Creator)(const LayerParameter&);
// CreatorRegistry是字符串与对应的Creator的映射
typedef std::map<string, Creator> CreatorRegistry;

static CreatorRegistry& Registry() {
Expand All @@ -64,6 +69,7 @@ class LayerRegistry {
}

// Adds a creator.
// 给定类型,以及函数指针,加入一个Creator到注册表
static void AddCreator(const string& type, Creator creator) {
CreatorRegistry& registry = Registry();
CHECK_EQ(registry.count(type), 0)
Expand All @@ -72,6 +78,7 @@ class LayerRegistry {
}

// Get a layer using a LayerParameter.
//给定层的类型,创建层
static shared_ptr<Layer<Dtype> > CreateLayer(const LayerParameter& param) {
if (Caffe::root_solver()) {
LOG(INFO) << "Creating layer " << param.name();
Expand All @@ -80,6 +87,7 @@ class LayerRegistry {
CreatorRegistry& registry = Registry();
CHECK_EQ(registry.count(type), 1) << "Unknown layer type: " << type
<< " (known types: " << LayerTypeListString() << ")";
// 调用对应的层的Creator函数
return registry[type](param);
}

Expand All @@ -96,6 +104,7 @@ class LayerRegistry {
private:
// Layer registry should never be instantiated - everything is done with its
// static variables.
// 禁止实例化,因为该类都是静态函数,所以是私有的
LayerRegistry() {}

static string LayerTypeListString() {
Expand All @@ -112,13 +121,15 @@ class LayerRegistry {
}
};


// 自己定义层的注册器,以供后面的宏进行使用
template <typename Dtype>
class LayerRegisterer {
public:
// 层的注册器的构造函数
LayerRegisterer(const string& type,
shared_ptr<Layer<Dtype> > (*creator)(const LayerParameter&)) {
// LOG(INFO) << "Registering layer type: " << type;
LOG(INFO) << "Registering layer type: " << type;
// 还是调用的层注册表中的加入Creator函数加入注册表
LayerRegistry<Dtype>::AddCreator(type, creator);
}
};
Expand Down
7 changes: 7 additions & 0 deletions include/caffe/layers/base_data_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ class BaseDataLayer : public Layer<Dtype> {
// 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.
// 该函数只能被BasePrefetchingDataLayer层进行重载
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.
// 数据层是没有输入的(即bottoms),所以reshape只是形式
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {}

Expand All @@ -38,7 +40,10 @@ class BaseDataLayer : public Layer<Dtype> {
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}

protected:
// 对输入的数据进行变换的参数,这其中包括是否需要mirror,是否需要crop
// 是否需要减去meanfile,是否需要scale
TransformationParameter transform_param_;
// 实际执行数据变换类的指针(一个Transform函数加上参数即可完成对数据的变换,参数是数据哈)
shared_ptr<DataTransformer<Dtype> > data_transformer_;
bool output_labels_;
};
Expand All @@ -49,6 +54,7 @@ class Batch {
Blob<Dtype> data_, label_;
};

// 是预取层的基类
template <typename Dtype>
class BasePrefetchingDataLayer :
public BaseDataLayer<Dtype>, public InternalThread {
Expand All @@ -67,6 +73,7 @@ class BasePrefetchingDataLayer :

protected:
virtual void InternalThreadEntry();
// 多了load_batch函数,该函数是纯虚函数,继承该函数的类都需要实现的
virtual void load_batch(Batch<Dtype>* batch) = 0;

vector<shared_ptr<Batch<Dtype> > > prefetch_;
Expand Down
1 change: 1 addition & 0 deletions include/caffe/layers/data_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace caffe {

// DataLayer才是主角,继承自BasePrefetchingDataLayer
template <typename Dtype>
class DataLayer : public BasePrefetchingDataLayer<Dtype> {
public:
Expand Down
25 changes: 19 additions & 6 deletions include/caffe/syncedmem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

#include "caffe/common.hpp"

/**
* SyncedMemory 是caffe中用来管理内存分配和CPU、GPU数据及同步的类,只服务于Blob类
* 目的是为了屏蔽上层代码对不同硬件设备的内存分配的感知,同时隐藏了CPU和GPU之间的同步过程。
* 同时,SyncedMemory实现时,采用的是 “lazy”的模式,就是内存的实际申请时机是在第一次使用时进行的。
*
*/
namespace caffe {

// If CUDA is available and in GPU mode, host memory will be allocated pinned,
Expand Down Expand Up @@ -76,14 +82,21 @@ class SyncedMemory {
private:
void check_device();

void to_cpu();
void to_gpu();
void to_cpu(); //CPU数据指针
void to_gpu(); //GPU数据指针
void* cpu_ptr_;
void* gpu_ptr_;
size_t size_;
SyncedHead head_;
bool own_cpu_data_;
bool cpu_malloc_use_cuda_;
size_t size_; //当前 SyncedMemory需要维护的数据个数
/**
* 当第一次调用to_cpu(),head_处于UNINITIALIZED状态,那么系统会调用CPU的申请内存的方式去获得内存区域,之后设置 head_ = HEAD_AT_CPU,
* 如果中间过程没有GPU设备则不会有状态变动,如果中间有代码调用了 to_gpu() ,则会发现 head_处于 HEAD_AT_CPU 状态,此时会调用同步函数,将数据从CPU同步到GPU,
* 之后如果又回到CPU上,则同样会发现 head_ 处于HEAD_AT_GPU的状态,那么又会调用相应的同步代码,将数据同步回CPU,
* 通过 head_这样一个状态参数屏蔽了GPU和CPU间的申请和切换的不同。
*/
SyncedHead head_; //当前 SyncedMemory处于的状态
// *own_cpu_data_和own_gpu_data_这两个变量,这两个变量主要是用来记录是否使用了共享的数据还是自己的数据
bool own_cpu_data_;
bool cpu_malloc_use_cuda_; //是否使用cuda
bool own_gpu_data_;
int device_;

Expand Down
1 change: 1 addition & 0 deletions include/caffe/util/device_alternate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void classname<Dtype>::funcname##_##gpu(const vector<Blob<Dtype>*>& top, \

#else // Normal GPU + CPU Caffe.

#include<cuda.h>
#include <cublas_v2.h>
#include <cuda.h>
#include <cuda_runtime.h>
Expand Down
1 change: 1 addition & 0 deletions src/caffe/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void Blob<Dtype>::ShareDiff(const Blob& other) {
template <> void Blob<unsigned int>::Update() { NOT_IMPLEMENTED; }
template <> void Blob<int>::Update() { NOT_IMPLEMENTED; }

// 该函数用于参数blob的更新(weight,bias 等减去对应的导数)
template <typename Dtype>
void Blob<Dtype>::Update() {
// We will perform update based on where the data is located.
Expand Down
Loading