21天实战caffe(4)数据结构 blob

namespace caffe {

//变维函数,将(num,channels,height,width)参数转换维vector<int>,然后调用重载的变维函数void Blob<Dtype>::Reshape(const vector<int>& shape)

template <typename Dtype>

void Blob<Dtype>::Reshape(const int num, const int channels, const int height,

const int width) {

vector<int> shape(4);

shape[0] = num;

shape[1] = channels;

shape[2] = height;

shape[3] = width;

Reshape(shape);

}

//真正变维函数

template <typename Dtype>

void Blob<Dtype>::Reshape(const vector<int>& shape) {

CHECK_LE(shape.size(), kMaxBlobAxes);//保证vector维度<=kMaxBlobAxes

count_ = 1;//用于计算元素总数 = num*channels*height*width

shape_.resize(shape.size()); // 成员变量维度也被重置

if (!shape_data_ || shape_data_->size() < shape.size() * sizeof(int)) {

shape_data_.reset(new SyncedMemory(shape.size() * sizeof(int)));

}

int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());

for (int i = 0; i < shape.size(); ++i) {

CHECK_GE(shape[i], 0);

if (count_ != 0) {

CHECK_LE(shape[i], INT_MAX / count_) << "blob size exceeds INT_MAX";

}

count_ *= shape[i];

shape_[i] = shape[i];

shape_data[i] = shape[i];

}

if (count_ > capacity_) {

capacity_ = count_;

data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));

diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));

}

}

template <typename Dtype>

void Blob<Dtype>::Reshape(const BlobShape& shape) {

CHECK_LE(shape.dim_size(), kMaxBlobAxes);

vector<int> shape_vec(shape.dim_size());

for (int i = 0; i < shape.dim_size(); ++i) {

shape_vec[i] = shape.dim(i);

}

Reshape(shape_vec);

}

template <typename Dtype>

void Blob<Dtype>::ReshapeLike(const Blob<Dtype>& other) {

Reshape(other.shape());

}

template <typename Dtype>

Blob<Dtype>::Blob(const int num, const int channels, const int height,

const int width)

// capacity_ must be initialized before calling Reshape

: capacity_(0) {

Reshape(num, channels, height, width);

}

template <typename Dtype>

Blob<Dtype>::Blob(const vector<int>& shape)

// capacity_ must be initialized before calling Reshape

: capacity_(0) {

Reshape(shape);

}

template <typename Dtype>

const int* Blob<Dtype>::gpu_shape() const {

CHECK(shape_data_);

return (const int*)shape_data_->gpu_data();

}

template <typename Dtype>

const Dtype* Blob<Dtype>::cpu_data() const {

CHECK(data_);

return (const Dtype*)data_->cpu_data();

}

template <typename Dtype>

void Blob<Dtype>::set_cpu_data(Dtype* data) {

CHECK(data);

// Make sure CPU and GPU sizes remain equal

size_t size = count_ * sizeof(Dtype);

if (data_->size() != size) {

data_.reset(new SyncedMemory(size));

diff_.reset(new SyncedMemory(size));

}

data_->set_cpu_data(data);

}

template <typename Dtype>

const Dtype* Blob<Dtype>::gpu_data() const {

CHECK(data_);

return (const Dtype*)data_->gpu_data();

}

template <typename Dtype>

void Blob<Dtype>::set_gpu_data(Dtype* data) {

CHECK(data);

// Make sure CPU and GPU sizes remain equal

size_t size = count_ * sizeof(Dtype);

if (data_->size() != size) {

data_.reset(new SyncedMemory(size));

diff_.reset(new SyncedMemory(size));

}

data_->set_gpu_data(data);

}

template <typename Dtype>

const Dtype* Blob<Dtype>::cpu_diff() const {

CHECK(diff_);

return (const Dtype*)diff_->cpu_data();

}

template <typename Dtype>

const Dtype* Blob<Dtype>::gpu_diff() const {

CHECK(diff_);

return (const Dtype*)diff_->gpu_data();

}

template <typename Dtype>

Dtype* Blob<Dtype>::mutable_cpu_data() {

CHECK(data_);

return static_cast<Dtype*>(data_->mutable_cpu_data());

}

template <typename Dtype>

Dtype* Blob<Dtype>::mutable_gpu_data() {

CHECK(data_);

return static_cast<Dtype*>(data_->mutable_gpu_data());

}

template <typename Dtype>

Dtype* Blob<Dtype>::mutable_cpu_diff() {

CHECK(diff_);

return static_cast<Dtype*>(diff_->mutable_cpu_data());

}

template <typename Dtype>

Dtype* Blob<Dtype>::mutable_gpu_diff() {

CHECK(diff_);

return static_cast<Dtype*>(diff_->mutable_gpu_data());

}

template <typename Dtype>

void Blob<Dtype>::ShareData(const Blob& other) {

CHECK_EQ(count_, other.count());

data_ = other.data();

}

template <typename Dtype>

void Blob<Dtype>::ShareDiff(const Blob& other) {

CHECK_EQ(count_, other.count());

diff_ = other.diff();

}

// The "update" method is used for parameter blobs in a Net, which are stored

// as Blob<float> or Blob<double> -- hence we do not define it for

// Blob<int> or Blob<unsigned int>.

template <> void Blob<unsigned int>::Update() { NOT_IMPLEMENTED; }

template <> void Blob<int>::Update() { NOT_IMPLEMENTED; }

template <typename Dtype>

void Blob<Dtype>::Update() {

// We will perform update based on where the data is located.

switch (data_->head()) {

case SyncedMemory::HEAD_AT_CPU:

// perform computation on CPU

caffe_axpy<Dtype>(count_, Dtype(-1),

static_cast<const Dtype*>(diff_->cpu_data()),

static_cast<Dtype*>(data_->mutable_cpu_data()));

break;

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

// perform computation on GPU

caffe_gpu_axpy<Dtype>(count_, Dtype(-1),

static_cast<const Dtype*>(diff_->gpu_data()),

static_cast<Dtype*>(data_->mutable_gpu_data()));

#else

NO_GPU;

#endif

break;

default:

LOG(FATAL) << "Syncedmem not initialized.";

}

}

template <> unsigned int Blob<unsigned int>::asum_data() const {

NOT_IMPLEMENTED;

return 0;

}

template <> int Blob<int>::asum_data() const {

NOT_IMPLEMENTED;

return 0;

}

template <typename Dtype>

Dtype Blob<Dtype>::asum_data() const {

if (!data_) { return 0; }

switch (data_->head()) {

case SyncedMemory::HEAD_AT_CPU:

return caffe_cpu_asum(count_, cpu_data());

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

{

Dtype asum;

caffe_gpu_asum(count_, gpu_data(), &asum);

return asum;

}

#else

NO_GPU;

#endif

case SyncedMemory::UNINITIALIZED:

return 0;

default:

LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();

}

return 0;

}

template <> unsigned int Blob<unsigned int>::asum_diff() const {

NOT_IMPLEMENTED;

return 0;

}

template <> int Blob<int>::asum_diff() const {

NOT_IMPLEMENTED;

return 0;

}

template <typename Dtype>

Dtype Blob<Dtype>::asum_diff() const {

if (!diff_) { return 0; }

switch (diff_->head()) {

case SyncedMemory::HEAD_AT_CPU:

return caffe_cpu_asum(count_, cpu_diff());

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

{

Dtype asum;

caffe_gpu_asum(count_, gpu_diff(), &asum);

return asum;

}

#else

NO_GPU;

#endif

case SyncedMemory::UNINITIALIZED:

return 0;

default:

LOG(FATAL) << "Unknown SyncedMemory head state: " << diff_->head();

}

return 0;

}

template <> unsigned int Blob<unsigned int>::sumsq_data() const {

NOT_IMPLEMENTED;

return 0;

}

template <> int Blob<int>::sumsq_data() const {

NOT_IMPLEMENTED;

return 0;

}

template <typename Dtype>

Dtype Blob<Dtype>::sumsq_data() const {

Dtype sumsq;

const Dtype* data;

if (!data_) { return 0; }

switch (data_->head()) {

case SyncedMemory::HEAD_AT_CPU:

data = cpu_data();

sumsq = caffe_cpu_dot(count_, data, data);

break;

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

data = gpu_data();

caffe_gpu_dot(count_, data, data, &sumsq);

#else

NO_GPU;

#endif

break;

case SyncedMemory::UNINITIALIZED:

return 0;

default:

LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();

}

return sumsq;

}

template <> unsigned int Blob<unsigned int>::sumsq_diff() const {

NOT_IMPLEMENTED;

return 0;

}

template <> int Blob<int>::sumsq_diff() const {

NOT_IMPLEMENTED;

return 0;

}

template <typename Dtype>

Dtype Blob<Dtype>::sumsq_diff() const {

Dtype sumsq;

const Dtype* diff;

if (!diff_) { return 0; }

switch (diff_->head()) {

case SyncedMemory::HEAD_AT_CPU:

diff = cpu_diff();

sumsq = caffe_cpu_dot(count_, diff, diff);

break;

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

diff = gpu_diff();

caffe_gpu_dot(count_, diff, diff, &sumsq);

break;

#else

NO_GPU;

#endif

case SyncedMemory::UNINITIALIZED:

return 0;

default:

LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();

}

return sumsq;

}

template <> void Blob<unsigned int>::scale_data(unsigned int scale_factor) {

NOT_IMPLEMENTED;

}

template <> void Blob<int>::scale_data(int scale_factor) {

NOT_IMPLEMENTED;

}

template <typename Dtype>

void Blob<Dtype>::scale_data(Dtype scale_factor) {

Dtype* data;

if (!data_) { return; }

switch (data_->head()) {

case SyncedMemory::HEAD_AT_CPU:

data = mutable_cpu_data();

caffe_scal(count_, scale_factor, data);

return;

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

data = mutable_gpu_data();

caffe_gpu_scal(count_, scale_factor, data);

return;

#else

NO_GPU;

#endif

case SyncedMemory::UNINITIALIZED:

return;

default:

LOG(FATAL) << "Unknown SyncedMemory head state: " << data_->head();

}

}

template <> void Blob<unsigned int>::scale_diff(unsigned int scale_factor) {

NOT_IMPLEMENTED;

}

template <> void Blob<int>::scale_diff(int scale_factor) {

NOT_IMPLEMENTED;

}

template <typename Dtype>

void Blob<Dtype>::scale_diff(Dtype scale_factor) {

Dtype* diff;

if (!diff_) { return; }

switch (diff_->head()) {

case SyncedMemory::HEAD_AT_CPU:

diff = mutable_cpu_diff();

caffe_scal(count_, scale_factor, diff);

return;

case SyncedMemory::HEAD_AT_GPU:

case SyncedMemory::SYNCED:

#ifndef CPU_ONLY

diff = mutable_gpu_diff();

caffe_gpu_scal(count_, scale_factor, diff);

return;

#else

NO_GPU;

#endif

case SyncedMemory::UNINITIALIZED:

return;

default:

LOG(FATAL) << "Unknown SyncedMemory head state: " << diff_->head();

}

}

template <typename Dtype>

bool Blob<Dtype>::ShapeEquals(const BlobProto& other) {

if (other.has_num() || other.has_channels() ||

other.has_height() || other.has_width()) {

// Using deprecated 4D Blob dimensions --

// shape is (num, channels, height, width).

// Note: we do not use the normal Blob::num(), Blob::channels(), etc.

// methods as these index from the beginning of the blob shape, where legacy

// parameter blobs were indexed from the end of the blob shape (e.g., bias

// Blob shape (1 x 1 x 1 x N), IP layer weight Blob shape (1 x 1 x M x N)).

return shape_.size() <= 4 &&

LegacyShape(-4) == other.num() &&

LegacyShape(-3) == other.channels() &&

LegacyShape(-2) == other.height() &&

LegacyShape(-1) == other.width();

}

vector<int> other_shape(other.shape().dim_size());

for (int i = 0; i < other.shape().dim_size(); ++i) {

other_shape[i] = other.shape().dim(i);

}

return shape_ == other_shape;

}

template <typename Dtype>

void Blob<Dtype>::CopyFrom(const Blob& source, bool copy_diff, bool reshape) {

if (source.count() != count_ || source.shape() != shape_) {

if (reshape) {

ReshapeLike(source);

} else {

LOG(FATAL) << "Trying to copy blobs of different sizes.";

}

}

switch (Caffe::mode()) {

case Caffe::GPU:

if (copy_diff) {

caffe_copy(count_, source.gpu_diff(),

static_cast<Dtype*>(diff_->mutable_gpu_data()));

} else {

caffe_copy(count_, source.gpu_data(),

static_cast<Dtype*>(data_->mutable_gpu_data()));

}

break;

case Caffe::CPU:

if (copy_diff) {

caffe_copy(count_, source.cpu_diff(),

static_cast<Dtype*>(diff_->mutable_cpu_data()));

} else {

caffe_copy(count_, source.cpu_data(),

static_cast<Dtype*>(data_->mutable_cpu_data()));

}

break;

default:

LOG(FATAL) << "Unknown caffe mode.";

}

}

template <typename Dtype>

void Blob<Dtype>::FromProto(const BlobProto& proto, bool reshape) {

if (reshape) {

vector<int> shape;

if (proto.has_num() || proto.has_channels() ||

proto.has_height() || proto.has_width()) {

// Using deprecated 4D Blob dimensions --

// shape is (num, channels, height, width).

shape.resize(4);

shape[0] = proto.num();

shape[1] = proto.channels();

shape[2] = proto.height();

shape[3] = proto.width();

} else {

shape.resize(proto.shape().dim_size());

for (int i = 0; i < proto.shape().dim_size(); ++i) {

shape[i] = proto.shape().dim(i);

}

}

Reshape(shape);

} else {

CHECK(ShapeEquals(proto)) << "shape mismatch (reshape not set)";

}

// copy data

Dtype* data_vec = mutable_cpu_data();

if (proto.double_data_size() > 0) {

CHECK_EQ(count_, proto.double_data_size());

for (int i = 0; i < count_; ++i) {

data_vec[i] = proto.double_data(i);

}

} else {

CHECK_EQ(count_, proto.data_size());

for (int i = 0; i < count_; ++i) {

data_vec[i] = proto.data(i);

}

}

if (proto.double_diff_size() > 0) {

CHECK_EQ(count_, proto.double_diff_size());

Dtype* diff_vec = mutable_cpu_diff();

for (int i = 0; i < count_; ++i) {

diff_vec[i] = proto.double_diff(i);

}

} else if (proto.diff_size() > 0) {

CHECK_EQ(count_, proto.diff_size());

Dtype* diff_vec = mutable_cpu_diff();

for (int i = 0; i < count_; ++i) {

diff_vec[i] = proto.diff(i);

}

}

}

template <>

void Blob<double>::ToProto(BlobProto* proto, bool write_diff) const {

proto->clear_shape();

for (int i = 0; i < shape_.size(); ++i) {

proto->mutable_shape()->add_dim(shape_[i]);

}

proto->clear_double_data();

proto->clear_double_diff();

const double* data_vec = cpu_data();

for (int i = 0; i < count_; ++i) {

proto->add_double_data(data_vec[i]);

}

if (write_diff) {

const double* diff_vec = cpu_diff();

for (int i = 0; i < count_; ++i) {

proto->add_double_diff(diff_vec[i]);

}

}

}

template <>

void Blob<float>::ToProto(BlobProto* proto, bool write_diff) const {

proto->clear_shape();

for (int i = 0; i < shape_.size(); ++i) {

proto->mutable_shape()->add_dim(shape_[i]);

}

proto->clear_data();

proto->clear_diff();

const float* data_vec = cpu_data();

for (int i = 0; i < count_; ++i) {

proto->add_data(data_vec[i]);

}

if (write_diff) {

const float* diff_vec = cpu_diff();

for (int i = 0; i < count_; ++i) {

proto->add_diff(diff_vec[i]);

}

}

}

INSTANTIATE_CLASS(Blob);

template class Blob<int>;

template class Blob<unsigned int>;

}  // namespace caffe

时间: 2024-08-05 04:28:06

21天实战caffe(4)数据结构 blob的相关文章

《深度学习-21天实战Caffe》高清带标签完整PDF版下载

近期做深度学习的项目用到了Caffe框架,需要系统地学习一下,特别是源码.经同事地推荐,了解熟悉了一本经典地好书 -- <深度学习-21天实战Caffe>,现在发现一个可以下载高清完整PDF版本地链接,比一般的都清晰,现在发出来这个下载链接. 百度云盘下载链接:<深度学习-21天实战Caffe> 这本高清的书基本长这个样子: 本书一共分为21天的知识点,在内容上分为上篇--<初见>.中篇--<热恋>.下篇--<升华>.作者以一种生动有趣的组织语言

21天实战caffe笔记_第二天

1 传统机器学习 传统机器学习:通过人工设计特征提取器,将原始数据转化为合适的中间表示形式或者特征向量,利用学习系统(通常为分类器)可以对输入模式进行检测或者分类.流程如下: 传统机器学习的局限在于需要人工设计特征提取器,而且要求较高.而深度学习则不需要,可以由机器自动学习获取,适应性较强. 2 从表示学习到深度学习 表示学习:原始数据->自动发现用于检测和分类的表示,如下图 : 深度学习:是一种多层表示学习方法,用简单的非线性模块构建而成:这些模块将上一层表示(从原始数据开始)转化为更高层.更

21天实战caffe(3)数据结构

caffe中一个CNN模型由Net表示,Net由多个Layer堆叠而成. caffe的万丈高楼(Net)是由图纸(prototxt),用blob这些砖块筑成一层层(Layer),最后通过SGD方法(Solver)进行简装修(train).精装修(finetune)实现的. Blob Blob在内存中表示为4维数组,维度从低到高为(width_,height_,channels_,num_).用于存储数据(data)或权值增量(diff). Blob中封装里SyncedMemory类.Bolob作

【21天实战Caffe】学习笔记(一)Ubuntu16.04+Caffe环境搭建

安装前准备工作: sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install --no-install-recommends libboost-all-dev sudo apt-get install libatlas-base-dev sudo apt-get install th

21天实战caffe笔记_第三天

1 深度学习工具汇总 (1)  caffe : 由BVLC开发的基于C++/CUDA/Python实现的卷积神经网络,提供了面向命令行.Matlab和Python的绑定接口.特性如下: A 实现了前馈卷积神经网络(CNN),不是递归网络结构(RNN) : B 速度快,利用MKL/OpenBLAS.cuBlas计算库,支持GPU加速 ; C 适合特征提取,实际上适合做二维图像数据的特征提取 ; caffe其他特性: A 完全开源,遵循BSD-2协议 ; B 提供了一整套工具集,可用于模型训练.预测

21天实战caffe(1)

convolutionLayer:卷积层实现 InnerProductLayer:全连接实现 CPR值 早发现就好了,论文都写完了又发现了.. 如果上层传过来的特征图是20*12*12,本层卷积层大小为50*5*5,本层输出50*8*8,那么单样本前向传播计算量为: calculations(MAC)=5*5*8*8*20*50=1600 000MAC 参数数量: params=50*5*5*20=25000 那么CPR: CPR=calculations/params=1600 000/250

21天学习caffe(二)

本文大致记录使用caffe的一次完整流程 Process 1 下载mnist数据集(数据量很小),解压放在data/mnist文件夹中:2 运行create_mnist.sh,生成lmdb格式的数据(data+label):$CAFFEROOT/build/tools/convert_imageset 可以用来做把原始图片转换为LevelDB或者 Lmdb格式. 3 运行build/tools/caffe train --solver=examples/mnist/lenet_solver.pr

【caffe】基本数据结构blob

blob是caffe中的基本数据结构,简单理解就是一个"4维数组".但是,这个4维数组有什么意义?BTW,TensorFlow这款google出的框架,带出了tensor(张量)的概念.虽然是数学概念,个人还是倾向于简单理解为"多维数组",那么放在这里, caffe的blob就相当于一个特殊的tensor了.而矩阵就是二维的张量.anyway,看看blob的4个维度都代表什么: num: 图像数量 channel:通道数量 width:图像宽度 height:图像高

2017.2.21 activiti实战--第七章--Activiti与容器集成

学习资料:<Activiti实战> 第七章 Activiti与容器集成 本章讲解activiti-spring可以做的事情,如何与现有系统集成,包含bean的注入.统一事务管理等. 7.1 流程引擎工厂 7.1.1 ProcessEngine 创建processEngine的方法有三种: 1 通过配置文件 2 测试中通过ActivitiRule 3 通过ProcessEngines类 7.1.2 ProcessEngineFactory 与spring集成的目的有两个: 1 通过spring统