@@ -16,7 +16,7 @@ template <typename Dtype>
16
16
class ArgMaxLayerTest : public CPUDeviceTest <Dtype> {
17
17
protected:
18
18
ArgMaxLayerTest ()
19
- : blob_bottom_(new Blob<Dtype>(10 , 20 , 1 , 1 )),
19
+ : blob_bottom_(new Blob<Dtype>(10 , 10 , 20 , 20 )),
20
20
blob_top_ (new Blob<Dtype>()),
21
21
top_k_(5 ) {
22
22
Caffe::set_random_seed (1701 );
@@ -55,6 +55,43 @@ TYPED_TEST(ArgMaxLayerTest, TestSetupMaxVal) {
55
55
EXPECT_EQ (this ->blob_top_ ->channels (), 2 );
56
56
}
57
57
58
+ TYPED_TEST (ArgMaxLayerTest, TestSetupAxis) {
59
+ LayerParameter layer_param;
60
+ ArgMaxParameter* argmax_param = layer_param.mutable_argmax_param ();
61
+ argmax_param->set_axis (0 );
62
+ ArgMaxLayer<TypeParam> layer (layer_param);
63
+ layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
64
+ EXPECT_EQ (this ->blob_top_ ->shape (0 ), argmax_param->top_k ());
65
+ EXPECT_EQ (this ->blob_top_ ->shape (1 ), this ->blob_bottom_ ->shape (0 ));
66
+ EXPECT_EQ (this ->blob_top_ ->shape (2 ), this ->blob_bottom_ ->shape (2 ));
67
+ EXPECT_EQ (this ->blob_top_ ->shape (3 ), this ->blob_bottom_ ->shape (3 ));
68
+ }
69
+
70
+ TYPED_TEST (ArgMaxLayerTest, TestSetupAxisNegativeIndexing) {
71
+ LayerParameter layer_param;
72
+ ArgMaxParameter* argmax_param = layer_param.mutable_argmax_param ();
73
+ argmax_param->set_axis (-2 );
74
+ ArgMaxLayer<TypeParam> layer (layer_param);
75
+ layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
76
+ EXPECT_EQ (this ->blob_top_ ->shape (0 ), this ->blob_bottom_ ->shape (0 ));
77
+ EXPECT_EQ (this ->blob_top_ ->shape (1 ), this ->blob_bottom_ ->shape (1 ));
78
+ EXPECT_EQ (this ->blob_top_ ->shape (2 ), argmax_param->top_k ());
79
+ EXPECT_EQ (this ->blob_top_ ->shape (3 ), this ->blob_bottom_ ->shape (3 ));
80
+ }
81
+
82
+ TYPED_TEST (ArgMaxLayerTest, TestSetupAxisMaxVal) {
83
+ LayerParameter layer_param;
84
+ ArgMaxParameter* argmax_param = layer_param.mutable_argmax_param ();
85
+ argmax_param->set_axis (2 );
86
+ argmax_param->set_out_max_val (true );
87
+ ArgMaxLayer<TypeParam> layer (layer_param);
88
+ layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
89
+ EXPECT_EQ (this ->blob_top_ ->shape (0 ), this ->blob_bottom_ ->shape (0 ));
90
+ EXPECT_EQ (this ->blob_top_ ->shape (1 ), this ->blob_bottom_ ->shape (1 ));
91
+ EXPECT_EQ (this ->blob_top_ ->shape (2 ), argmax_param->top_k ());
92
+ EXPECT_EQ (this ->blob_top_ ->shape (3 ), this ->blob_bottom_ ->shape (3 ));
93
+ }
94
+
58
95
TYPED_TEST (ArgMaxLayerTest, TestCPU) {
59
96
LayerParameter layer_param;
60
97
ArgMaxLayer<TypeParam> layer (layer_param);
@@ -112,6 +149,7 @@ TYPED_TEST(ArgMaxLayerTest, TestCPUTopK) {
112
149
layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
113
150
layer.Forward (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
114
151
// Now, check values
152
+ const TypeParam* bottom_data = this ->blob_bottom_ ->cpu_data ();
115
153
int max_ind;
116
154
TypeParam max_val;
117
155
int num = this ->blob_bottom_ ->num ();
@@ -121,10 +159,10 @@ TYPED_TEST(ArgMaxLayerTest, TestCPUTopK) {
121
159
EXPECT_LE (this ->blob_top_ ->data_at (i, 0 , 0 , 0 ), dim);
122
160
for (int j = 0 ; j < this ->top_k_ ; ++j) {
123
161
max_ind = this ->blob_top_ ->data_at (i, 0 , j, 0 );
124
- max_val = this -> blob_bottom_ -> data_at (i, max_ind, 0 , 0 ) ;
162
+ max_val = bottom_data[i * dim + max_ind] ;
125
163
int count = 0 ;
126
164
for (int k = 0 ; k < dim; ++k) {
127
- if (this -> blob_bottom_ -> data_at (i, k, 0 , 0 ) > max_val) {
165
+ if (bottom_data[i * dim + k] > max_val) {
128
166
++count;
129
167
}
130
168
}
@@ -142,6 +180,7 @@ TYPED_TEST(ArgMaxLayerTest, TestCPUMaxValTopK) {
142
180
layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
143
181
layer.Forward (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
144
182
// Now, check values
183
+ const TypeParam* bottom_data = this ->blob_bottom_ ->cpu_data ();
145
184
int max_ind;
146
185
TypeParam max_val;
147
186
int num = this ->blob_bottom_ ->num ();
@@ -152,10 +191,10 @@ TYPED_TEST(ArgMaxLayerTest, TestCPUMaxValTopK) {
152
191
for (int j = 0 ; j < this ->top_k_ ; ++j) {
153
192
max_ind = this ->blob_top_ ->data_at (i, 0 , j, 0 );
154
193
max_val = this ->blob_top_ ->data_at (i, 1 , j, 0 );
155
- EXPECT_EQ (this -> blob_bottom_ -> data_at (i, max_ind, 0 , 0 ) , max_val);
194
+ EXPECT_EQ (bottom_data[i * dim + max_ind] , max_val);
156
195
int count = 0 ;
157
196
for (int k = 0 ; k < dim; ++k) {
158
- if (this -> blob_bottom_ -> data_at (i, k, 0 , 0 ) > max_val) {
197
+ if (bottom_data[i * dim + k] > max_val) {
159
198
++count;
160
199
}
161
200
}
@@ -164,5 +203,93 @@ TYPED_TEST(ArgMaxLayerTest, TestCPUMaxValTopK) {
164
203
}
165
204
}
166
205
206
+ TYPED_TEST (ArgMaxLayerTest, TestCPUAxis) {
207
+ LayerParameter layer_param;
208
+ ArgMaxParameter* argmax_param = layer_param.mutable_argmax_param ();
209
+ argmax_param->set_axis (0 );
210
+ ArgMaxLayer<TypeParam> layer (layer_param);
211
+ layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
212
+ layer.Forward (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
213
+ // Now, check values
214
+ int max_ind;
215
+ TypeParam max_val;
216
+ std::vector<int > shape = this ->blob_bottom_ ->shape ();
217
+ for (int i = 0 ; i < shape[1 ]; ++i) {
218
+ for (int j = 0 ; j < shape[2 ]; ++j) {
219
+ for (int k = 0 ; k < shape[3 ]; ++k) {
220
+ max_ind = this ->blob_top_ ->data_at (0 , i, j, k);
221
+ max_val = this ->blob_bottom_ ->data_at (max_ind, i, j, k);
222
+ EXPECT_GE (max_ind, 0 );
223
+ EXPECT_LE (max_ind, shape[0 ]);
224
+ for (int l = 0 ; l < shape[0 ]; ++l) {
225
+ EXPECT_LE (this ->blob_bottom_ ->data_at (l, i, j, k), max_val);
226
+ }
227
+ }
228
+ }
229
+ }
230
+ }
231
+
232
+ TYPED_TEST (ArgMaxLayerTest, TestCPUAxisTopK) {
233
+ LayerParameter layer_param;
234
+ ArgMaxParameter* argmax_param = layer_param.mutable_argmax_param ();
235
+ argmax_param->set_axis (2 );
236
+ argmax_param->set_top_k (this ->top_k_ );
237
+ ArgMaxLayer<TypeParam> layer (layer_param);
238
+ layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
239
+ layer.Forward (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
240
+ // Now, check values
241
+ int max_ind;
242
+ TypeParam max_val;
243
+ std::vector<int > shape = this ->blob_bottom_ ->shape ();
244
+ for (int i = 0 ; i < shape[0 ]; ++i) {
245
+ for (int j = 0 ; j < shape[1 ]; ++j) {
246
+ for (int k = 0 ; k < shape[3 ]; ++k) {
247
+ for (int m = 0 ; m < this ->top_k_ ; ++m) {
248
+ max_ind = this ->blob_top_ ->data_at (i, j, m, k);
249
+ max_val = this ->blob_bottom_ ->data_at (i, j, max_ind, k);
250
+ EXPECT_GE (max_ind, 0 );
251
+ EXPECT_LE (max_ind, shape[2 ]);
252
+ int count = 0 ;
253
+ for (int l = 0 ; l < shape[2 ]; ++l) {
254
+ if (this ->blob_bottom_ ->data_at (i, j, l, k) > max_val) {
255
+ ++count;
256
+ }
257
+ }
258
+ EXPECT_EQ (m, count);
259
+ }
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ TYPED_TEST (ArgMaxLayerTest, TestCPUAxisMaxValTopK) {
266
+ LayerParameter layer_param;
267
+ ArgMaxParameter* argmax_param = layer_param.mutable_argmax_param ();
268
+ argmax_param->set_axis (-1 );
269
+ argmax_param->set_top_k (this ->top_k_ );
270
+ argmax_param->set_out_max_val (true );
271
+ ArgMaxLayer<TypeParam> layer (layer_param);
272
+ layer.SetUp (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
273
+ layer.Forward (this ->blob_bottom_vec_ , this ->blob_top_vec_ );
274
+ // Now, check values
275
+ TypeParam max_val;
276
+ std::vector<int > shape = this ->blob_bottom_ ->shape ();
277
+ for (int i = 0 ; i < shape[0 ]; ++i) {
278
+ for (int j = 0 ; j < shape[1 ]; ++j) {
279
+ for (int k = 0 ; k < shape[2 ]; ++k) {
280
+ for (int m = 0 ; m < this ->top_k_ ; ++m) {
281
+ max_val = this ->blob_top_ ->data_at (i, j, k, m);
282
+ int count = 0 ;
283
+ for (int l = 0 ; l < shape[3 ]; ++l) {
284
+ if (this ->blob_bottom_ ->data_at (i, j, k, l) > max_val) {
285
+ ++count;
286
+ }
287
+ }
288
+ EXPECT_EQ (m, count);
289
+ }
290
+ }
291
+ }
292
+ }
293
+ }
167
294
168
295
} // namespace caffe
0 commit comments