1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include <algorithm>
#include <functional>
#include <utility>
#include <vector>
#include "caffe/common_layers.hpp"
namespace caffe {
template <typename Dtype>
void ArgMaxLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const ArgMaxParameter& argmax_param = this->layer_param_.argmax_param();
out_max_val_ = argmax_param.out_max_val();
top_k_ = argmax_param.top_k();
has_axis_ = argmax_param.has_axis();
CHECK_GE(top_k_, 1) << "top k must not be less than 1.";
if (has_axis_) {
axis_ = bottom[0]->CanonicalAxisIndex(argmax_param.axis());
CHECK_GE(axis_, 0) << "axis must not be less than 0.";
CHECK_LE(axis_, bottom[0]->num_axes()) <<
"axis must be less than or equal to the number of axis.";
CHECK_LE(top_k_, bottom[0]->shape(axis_))
<< "top_k must be less than or equal to the dimension of the axis.";
} else {
CHECK_LE(top_k_, bottom[0]->count(1))
<< "top_k must be less than or equal to"
" the dimension of the flattened bottom blob per instance.";
}
}
template <typename Dtype>
void ArgMaxLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
std::vector<int> shape(bottom[0]->num_axes(), 1);
if (has_axis_) {
// Produces max_ind or max_val per axis
shape = bottom[0]->shape();
shape[axis_] = top_k_;
} else {
shape[0] = bottom[0]->shape(0);
// Produces max_ind
shape[2] = top_k_;
if (out_max_val_) {
// Produces max_ind and max_val
shape[1] = 2;
}
}
top[0]->Reshape(shape);
}
template <typename Dtype>
void ArgMaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
int dim, axis_dist;
if (has_axis_) {
dim = bottom[0]->shape(axis_);
// Distance between values of axis in blob
axis_dist = bottom[0]->count(axis_) / dim;
} else {
dim = bottom[0]->count(1);
axis_dist = 1;
}
int num = bottom[0]->count() / dim;
std::vector<std::pair<Dtype, int> > bottom_data_vector(dim);
for (int i = 0; i < num; ++i) {
for (int j = 0; j < dim; ++j) {
bottom_data_vector[j] = std::make_pair(
bottom_data[(i / axis_dist * dim + j) * axis_dist + i % axis_dist], j);
}
std::partial_sort(
bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
for (int j = 0; j < top_k_; ++j) {
if (out_max_val_) {
if (has_axis_) {
// Produces max_val per axis
top_data[(i / axis_dist * top_k_ + j) * axis_dist + i % axis_dist]
= bottom_data_vector[j].first;
} else {
// Produces max_ind and max_val
top_data[2 * i * top_k_ + j] = bottom_data_vector[j].second;
top_data[2 * i * top_k_ + top_k_ + j] = bottom_data_vector[j].first;
}
} else {
// Produces max_ind per axis
top_data[(i / axis_dist * top_k_ + j) * axis_dist + i % axis_dist]
= bottom_data_vector[j].second;
}
}
}
}
INSTANTIATE_CLASS(ArgMaxLayer);
REGISTER_LAYER_CLASS(ArgMax);
} // namespace caffe
|