SoftmaxLayer and SoftmaxwithLossLayer 代码解读

时间:2023-03-09 08:10:15
SoftmaxLayer and SoftmaxwithLossLayer 代码解读

SoftmaxLayer and SoftmaxwithLossLayer 代码解读

Wang Xiao


  先来看看 SoftmaxWithLoss 在prototext文件中的定义:

 layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "fc8"
bottom: "label"
top: "loss"
}

  再看SoftmaxWithLossLayer的.cpp文件:

  

 #include <algorithm>
#include <cfloat>
#include <vector> #include "caffe/layers/softmax_loss_layer.hpp"
#include "caffe/util/math_functions.hpp" namespace caffe { template <typename Dtype>
void SoftmaxWithLossLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
LossLayer<Dtype>::LayerSetUp(bottom, top);
LayerParameter softmax_param(this->layer_param_);
softmax_param.set_type("Softmax");
softmax_layer_ = LayerRegistry<Dtype>::CreateLayer(softmax_param);
softmax_bottom_vec_.clear();
softmax_bottom_vec_.push_back(bottom[0]); // 将bottom[0]存入softmax_bottom_vec_;
softmax_top_vec_.clear();
softmax_top_vec_.push_back(&prob_);   // 将 prob_ 存入 softmax_top_vec_;
 softmax_layer_->SetUp(softmax_bottom_vec_, softmax_top_vec_); 

 has_ignore_label_ =   // draw the parameter from layer
this->layer_param_.loss_param().has_ignore_label();
if (has_ignore_label_) {
ignore_label_ = this->layer_param_.loss_param().ignore_label();
}
if (!this->layer_param_.loss_param().has_normalization() &&
this->layer_param_.loss_param().has_normalize()) {
normalization_ = this->layer_param_.loss_param().normalize() ?
LossParameter_NormalizationMode_VALID :
LossParameter_NormalizationMode_BATCH_SIZE;
} else {
normalization_ = this->layer_param_.loss_param().normalization();
}
}

  

  接下来是对输入数据进行 reshape 操作:

  

 template <typename Dtype>
void SoftmaxWithLossLayer<Dtype>::Reshape(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
LossLayer<Dtype>::Reshape(bottom, top);
softmax_layer_->Reshape(softmax_bottom_vec_, softmax_top_vec_);
softmax_axis_ =
bottom[]->CanonicalAxisIndex(this->layer_param_.softmax_param().axis());
outer_num_ = bottom[]->count(, softmax_axis_);
inner_num_ = bottom[]->count(softmax_axis_ + );
CHECK_EQ(outer_num_ * inner_num_, bottom[]->count())
<< "Number of labels must match number of predictions; "
<< "e.g., if softmax axis == 1 and prediction shape is (N, C, H, W), "
<< "label count (number of labels) must be N*H*W, "
<< "with integer values in {0, 1, ..., C-1}.";
if (top.size() >= ) {
// softmax output
top[]->ReshapeLike(*bottom[]);
}
}