1 #ifndef CAFFE_ACCURACY_LAYER_HPP_
2 #define CAFFE_ACCURACY_LAYER_HPP_
6 #include "caffe/blob.hpp"
7 #include "caffe/layer.hpp"
8 #include "caffe/proto/caffe.pb.h"
10 #include "caffe/layers/loss_layer.hpp"
15 * @brief Computes the classification accuracy for a one-of-many
16 * classification task.
18 template <typename Dtype>
19 class AccuracyLayer : public Layer<Dtype> {
22 * @param param provides AccuracyParameter accuracy_param,
23 * with AccuracyLayer options:
24 * - top_k (\b optional, default 1).
25 * Sets the maximum rank @f$ k @f$ at which a prediction is considered
26 * correct. For example, if @f$ k = 5 @f$, a prediction is counted
27 * correct if the correct label is among the top 5 predicted labels.
29 explicit AccuracyLayer(const LayerParameter& param)
30 : Layer<Dtype>(param) {}
31 virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
32 const vector<Blob<Dtype>*>& top);
33 virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
34 const vector<Blob<Dtype>*>& top);
36 virtual inline const char* type() const { return "Accuracy"; }
37 virtual inline int ExactNumBottomBlobs() const { return 2; }
39 // If there are two top blobs, then the second blob will contain
40 // accuracies per class.
41 virtual inline int MinTopBlobs() const { return 1; }
42 virtual inline int MaxTopBlobs() const { return 2; }
46 * @param bottom input Blob vector (length 2)
47 * -# @f$ (N \times C \times H \times W) @f$
48 * the predictions @f$ x @f$, a Blob with values in
49 * @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of
50 * the @f$ K = CHW @f$ classes. Each @f$ x_n @f$ is mapped to a predicted
51 * label @f$ \hat{l}_n @f$ given by its maximal index:
52 * @f$ \hat{l}_n = \arg\max\limits_k x_{nk} @f$
53 * -# @f$ (N \times 1 \times 1 \times 1) @f$
54 * the labels @f$ l @f$, an integer-valued Blob with values
55 * @f$ l_n \in [0, 1, 2, ..., K - 1] @f$
56 * indicating the correct class label among the @f$ K @f$ classes
57 * @param top output Blob vector (length 1)
58 * -# @f$ (1 \times 1 \times 1 \times 1) @f$
59 * the computed accuracy: @f$
60 * \frac{1}{N} \sum\limits_{n=1}^N \delta\{ \hat{l}_n = l_n \}
62 * \delta\{\mathrm{condition}\} = \left\{
64 * 1 & \mbox{if condition} \\
65 * 0 & \mbox{otherwise}
69 virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
70 const vector<Blob<Dtype>*>& top);
73 /// @brief Not implemented -- AccuracyLayer cannot be used as a loss.
74 virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
75 const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
76 for (int i = 0; i < propagate_down.size(); ++i) {
77 if (propagate_down[i]) { NOT_IMPLEMENTED; }
81 int label_axis_, outer_num_, inner_num_;
85 /// Whether to ignore instances with a certain label.
86 bool has_ignore_label_;
87 /// The label indicating that an instance should be ignored.
89 /// Keeps counts of the number of samples per class.
90 Blob<Dtype> nums_buffer_;
95 #endif // CAFFE_ACCURACY_LAYER_HPP_