loss_layers.hpp
#ifndef CAFFE_LOSS_LAYER_HPP_
#define CAFFE_LOSS_LAYER_HPP_
#include
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
const float kLOG_THRESHOLD = 1e-20;
/**
* @brief An interface for Layer%s that take two Blob%s as input -- usually
* (1) predictions and (2) ground-truth labels -- and output a
* singleton Blob representing the loss.
*
* LossLayers are typically only capable of backpropagating to their first input
* -- the predictions.
*/
template <typename Dtype>
class LossLayer : public Layer {
public:
//显式构造函数
explicit LossLayer(const LayerParameter& param)
: Layer(param) {}
//层配置函数
virtual void LayerSetUp(
const vector *>& bottom, const vector *>& top);
//变形函数
virtual void Reshape(
const vector *>& bottom, const vector *>& top);
//接受两个blob作为输入
virtual inline int ExactNumBottomBlobs() const { return 2; }
//指导Net为损失层自动非配单个输出blob, 损失层会将计算结果保存在这里
virtual inline bool AutoTopBlobs() const { return true; }
//只有一个输出blob
virtual inline int ExactNumTopBlobs() const { return 1; }
//通常不能对标签做反向传播,所以忽略force_backward
virtual inline bool AllowForceBackward(const int bottom_index) const {
return bottom_index != 1;
}
};
} // namespace caffe
#endif // CAFFE_LOSS_LAYER_HPP_
softmax_layer.hpp
#ifndef CAFFE_SOFTMAX_LAYER_HPP_
#define CAFFE_SOFTMAX_LAYER_HPP_
#include
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
namespace caffe {
/**
* @brief Computes the softmax function.
* TODO(dox): thorough documentation for Forward, Backward, and proto params.
*/
template <typename Dtype>
class SoftmaxLayer : public Layer {
public:
explicit SoftmaxLayer(const LayerParameter& param)
: Layer(param) {}
virtual void Reshape(const vector *>& bottom,
const vector *>& top);
virtual inline const char* type() const { return "Softmax"; } //返回 类名 字符串
virtual inline int ExactNumBottomBlobs() const { return 1; } //输入输出分别1个
virtual inline int ExactNumTopBlobs() const { return 1; }
protected:
virtual void Forward_cpu(const vector *>& bottom,
const vector *>& top);
virtual void Forward_gpu(const vector *>& bottom,
const vector *>& top);
virtual void Backward_cpu(const vector *>& top,
const vector<bool>& propagate_down, const vector *>& bottom);
virtual void Backward_gpu(const vector *>& top,
const vector<bool>& propagate_down, const vector *>& bottom);
int outer_num_;
int inner_num_;
int softmax_axis_;
Blob sum_multiplier_; //利用BLAS计算求和
Blob scale_;//临时存放中间结果
};
} // namespace caffe
#endif // CAFFE_SOFTMAX_LAYER_HPP_
softmax_layer.cpp
#include
#include
#include "caffe/layers/softmax_layer.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
//**********Reshape*************//
template <typename Dtype>
void SoftmaxLayer::Reshape(const vector *>& bottom,
const vector *>& top) {
//CanonicalAxisIndex是Blob的成员函数,设置shape_的轴,并返回该轴
//layer_param_是Layer中定义的LayerParameter类型的变量,softmax_param是SoftmaxParameter类型,沿着axis执行SoftmaxLayer,axis一般取默认值0
softmax_axis_ =
bottom[0]->CanonicalAxisIndex(this->layer_param_.softmax_param().axis());
top[0]->ReshapeLike(*bottom[0]);
vector<int> mult_dims(1, bottom[0]->shape(softmax_axis_));
//bottom[0].shape(softmax_axis_)表示第一维的大小,mult_dims此时是指1个值为bottom[0]->shape(softmax_axis_)的向量
//bottom的维度是N*channels*H*W, mult_dims就表示包含1个值为channels的容器
sum_multiplier_.Reshape(mult_dims);
//sum_multiplier_的尺寸:1*channels*1*1, 值全为1
Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
caffe_set(sum_multiplier_.count(), Dtype(1), multiplier_data);//用Dtype(1)初始化
outer_num_ = bottom[0]->count(0, softmax_axis_);//outer_num_表示N,N是指每次输入的样本数,每个bottom有N*Channel*H*W,N指的是batch_size。
inner_num_ = bottom[0]->count(softmax_axis_ + 1);//inner_num_表示H*W,count返回从第2到最后一维的维度乘积
vector<int> scale_dims = bottom[0]->shape();
scale_dims[softmax_axis_] = 1;//scale_一个中间Blob,用于hold一些临时结果,这里将它第一维大小设为1
//scale_尺寸: num*1*height*width
scale_.Reshape(scale_dims);
}
//**********Forward*************//
template <typename Dtype>
void SoftmaxLayer::Forward_cpu(const vector *>& bottom,
const vector *>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
Dtype* scale_data = scale_.mutable_cpu_data();
//channels在softmax层不再表示通道,表示最终分类的个数,softmax_axis_的值为1,bottom[0]维度是N*Channel*H*W,
int channels = bottom[0]->shape(softmax_axis_);
int dim = bottom[0]->count() / outer_num_;//dim是指bottom[0]元素个数,Channels*W*H
//先将输入拷贝到输出缓冲区
caffe_copy(bottom[0]->count(), bottom_data, top_data);
// We need to subtract the max to avoid numerical issues, compute the exp,and then normalize.
for (int i = 0; i < outer_num_; ++i) {
// initialize scale_data to the first plane
caffe_copy(inner_num_, bottom_data + i * dim, scale_data);
for (int j = 0; j < channels; j++) {
for (int k = 0; k < inner_num_; k++) {
scale_data[k] = std::max(scale_data[k],
bottom_data[i * dim + j * inner_num_ + k]);/-------------
}
}
// subtraction输出缓冲区减去最大值
//求矩阵的差放入top_data中,公式:top_data = -1*sum_multiplier_*scale_data + top_data
//sum_multiplier_是channels*1的矩阵,每个元素值为1
//scale_data是1*(N*W)的矩阵
//top_data是channels*(N*W)的矩阵
//CblasNoTrans:指不做转置
caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, channels, inner_num_,
1, -1., sum_multiplier_.cpu_data(), scale_data, 1., top_data);
// exponentiation
caffe_exp(dim, top_data, top_data);//对top_data的每个像素点做幂运算c
// sum after exp
//对top_data转置,每一列都加到一起,也就是对应像素点的channels个值相加,放到scale_data中
caffe_cpu_gemv(CblasTrans, channels, inner_num_, 1.,
top_data, sum_multiplier_.cpu_data(), 0., scale_data);
// division
//求在每一个分类里面的概率值
for (int j = 0; j < channels; j++) {
caffe_div(inner_num_, top_data, scale_data, top_data);//对每个channel除以scale_data,存到top_data里面
top_data += inner_num_;//这里的+=inner_num_是指top_data的偏移量。
}
}
}
template <typename Dtype>
void SoftmaxLayer::Backward_cpu(const vector *>& top,
const vector<bool>& propagate_down,
const vector *>& bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* top_data = top[0]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
Dtype* scale_data = scale_.mutable_cpu_data();
int channels = top[0]->shape(softmax_axis_);
int dim = top[0]->count() / outer_num_;
caffe_copy(top[0]->count(), top_diff, bottom_diff);
for (int i = 0; i < outer_num_; ++i) {
//计算top_diff和top_data的点积,然后从bottom_diff中减去该值
for (int k = 0; k < inner_num_; ++k) {
scale_data[k] = caffe_cpu_strided_dot(channels,
bottom_diff + i * dim + k, inner_num_,
top_data + i * dim + k, inner_num_);
}
// subtraction减值
caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, channels, inner_num_, 1,
-1., sum_multiplier_.cpu_data(), scale_data, 1., bottom_diff + i * dim);
}
// elementwise multiplication 逐点相乘
caffe_mul(top[0]->count(), bottom_diff, top_data, bottom_diff);
}
#ifdef CPU_ONLY
STUB_GPU(SoftmaxLayer);
#endif
INSTANTIATE_CLASS(SoftmaxLayer);
} // namespace caffe