基础概念
blob的reshape
reshape接收的是一个vector的参数,目的是要给这个blob分配相应的内存空间。这个vector参数,表示了不同的维度,一般就是N*C*H*W,如[0]表示N,[1]表示C,等。
直观的解析就是:有N张图,每张图有C个channel,每张图的大小是H*W,如果每个像素都是float类型,那么,
计算总共有多少像素(元素)的公式就是:N*C*H*W,就是blob的count的值。
计算用来存储这批数据的blob所需的内存的大小为:N*C*H*W*sizeof(float)。
相关源码:
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);
count_ = 1;
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]; //累计count的数量,记住是N*C*H*W这样计算总共有多少元素
shape_[i] = shape[i]; //同时记住各维度的大小
shape_data[i] = shape[i];
}
if (count_ > capacity_) {
capacity_ = count_;
data_.reset(new SyncedMemory(capacity_ * sizeof(Dtype))); //如果reshape的大小比以前的大,要重新分配空间
diff_.reset(new SyncedMemory(capacity_ * sizeof(Dtype)));
}
}
blob的count函数
caffe中表示数据都是以N*C*H*W这4个维度来表示的,count函数用来计算某些范围的大小。
/**
* @brief Compute the volume of a slice; i.e., the product of dimensions
* among a range of axes.
*
* @param start_axis The first axis to include in the slice.
*
* @param end_axis The first axis to exclude from the slice.
*/
inline int count(int start_axis, int end_axis) const {
CHECK_LE(start_axis, end_axis);
CHECK_GE(start_axis, 0);
CHECK_GE(end_axis, 0);
CHECK_LE(start_axis, num_axes());
CHECK_LE(end_axis, num_axes());
int count = 1;
for (int i = start_axis; i < end_axis; ++i) {
count *= shape(i);
}
return count;
}
caffe源码中的accuracy_layer.cpp的相关梳理
首先明确accuracy层的功能。
accuracy层是通过对比预测的结果与输入的label,通过统计预测正确的数量与总共要预测的数量的比值得到的。
accuracy层需要两个输入源,一个是经过网络预测的数值,另一个是最开始输入的label至,分别对应了bottom[0]和bottom[1].
对于一批输入N*C*H*W来说,图像表示为:
AccuracyLayer的Reshape函数分析
template <typename Dtype>
void AccuracyLayer<Dtype>::Reshape(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
CHECK_LE(top_k_, bottom[0]->count() / bottom[1]->count())
<< "top_k must be less than or equal to the number of classes.";
label_axis_ =
bottom[0]->CanonicalAxisIndex(this->layer_param_.accuracy_param().axis()); //label_axis默认是1(查看caffe.probo文件)
outer_num_ = bottom[0]->count(0, label_axis_); //结合上图,该outer_num是N
inner_num_ = bottom[0]->count(label_axis_ + 1); //结合上图,该inner_num是H*W
CHECK_EQ(outer_num_ * inner_num_, bottom[1]->count())
<< "Number of labels must match number of predictions; "
<< "e.g., if label 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}.";
//上面的check表示的意思为:
//先结合上图理解outer_num与inner_num的数量,以及bottom[1]的结构。
//如果C表示的是预测的种类的数量(就是label有多少种),那么label的数量有多少?
//就是N*H*W,一个输入是H*W大,对于H*W的每个元素都有一个label值,所以,一个输入就会有H*W个label,
//N个输入(一批),就会有N*H*W个label数量。
//很多网络最后一层都是全连接层,即一张h*w的图片进去,到最后就成为了一列单个的元素,N张图进去就是变成N列(计算上来说),
//所以一般输入给accuracy层的bottom[0]都是N*C*1*1.
//对于分类来说,这个特别好理解,每张图片对应一个label,那么就会有N个label
vector<int> top_shape(0); // Accuracy is a scalar; 0 axes.
top[0]->Reshape(top_shape);
if (top.size() > 1) {
// Per-class accuracy is a vector; 1 axes.
vector<int> top_shape_per_class(1);
top_shape_per_class[0] = bottom[0]->shape(label_axis_);
top[1]->Reshape(top_shape_per_class);
nums_buffer_.Reshape(top_shape_per_class);
}
}
AccuracyLayer的Forward_cpu函数分析
template <typename Dtype>
void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
Dtype accuracy = 0;
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* bottom_label = bottom[1]->cpu_data();
const int dim = bottom[0]->count() / outer_num_;
const int num_labels = bottom[0]->shape(label_axis_);
vector<Dtype> maxval(top_k_+1);
vector<int> max_id(top_k_+1);
if (top.size() > 1) {
caffe_set(nums_buffer_.count(), Dtype(0), nums_buffer_.mutable_cpu_data());
caffe_set(top[1]->count(), Dtype(0), top[1]->mutable_cpu_data());
}
int count = 0;
for (int i = 0; i < outer_num_; ++i) {
for (int j = 0; j < inner_num_; ++j) {
const int label_value =
static_cast<int>(bottom_label[i * inner_num_ + j]);
if (has_ignore_label_ && label_value == ignore_label_) {
continue;
}
if (top.size() > 1) ++nums_buffer_.mutable_cpu_data()[label_value];
DCHECK_GE(label_value, 0);
DCHECK_LT(label_value, num_labels);
// Top-k accuracy
std::vector<std::pair<Dtype, int> > bottom_data_vector;
for (int k = 0; k < num_labels; ++k) {
bottom_data_vector.push_back(std::make_pair(
bottom_data[i * dim + k * inner_num_ + j], k));
}
std::partial_sort(
bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
// check if true label is in top k predictions
for (int k = 0; k < top_k_; k++) {
if (bottom_data_vector[k].second == label_value) {
++accuracy;
if (top.size() > 1) ++top[1]->mutable_cpu_data()[label_value];
break;
}
}
++count;
}
}
// LOG(INFO) << "Accuracy: " << accuracy;
top[0]->mutable_cpu_data()[0] = accuracy / count;
if (top.size() > 1) {
for (int i = 0; i < top[1]->count(); ++i) {
top[1]->mutable_cpu_data()[i] =
nums_buffer_.cpu_data()[i] == 0 ? 0
: top[1]->cpu_data()[i] / nums_buffer_.cpu_data()[i];
}
}
// Accuracy layer should not be used as a loss function.
}