@@ -160,30 +160,26 @@ void testIm2col() {
160
160
delete context;
161
161
}
162
162
163
- void testIm2colCPU () {
163
+ void testIm2colCPU (int ic, int ih, int iw, int fh, int fw, int ph, int pw ) {
164
164
paddle::framework::Tensor input;
165
165
paddle::framework::Tensor output;
166
- int input_height = 3 ;
167
- int input_width = 4 ;
168
- int filter_size = 2 ;
169
- int ic = 2 ;
170
- std::vector<int > stride ({1 , 1 }); // stride_y, stride_x
171
- std::vector<int > padding ({0 , 0 });
166
+ paddle::framework::Tensor ref_output;
167
+ std::vector<int > padding ({ph, pw});
168
+ std::vector<int > stride ({1 , 1 }); // stride_y, stride_x
172
169
std::vector<int > dilation ({1 , 1 }); // dilation_y, dilation_x
173
- int output_height =
174
- (input_height - filter_size + padding[0 ] * 2 ) / stride[0 ] + 1 ;
175
- int output_width =
176
- (input_width - filter_size + padding[1 ] * 2 ) / stride[1 ] + 1 ;
177
- float * input_ptr = input.mutable_data <float >({ic, input_height, input_width},
178
- paddle::platform::CPUPlace ());
170
+ int output_height = (ih - fh + padding[0 ] * 2 ) / stride[0 ] + 1 ;
171
+ int output_width = (iw - fw + padding[1 ] * 2 ) / stride[1 ] + 1 ;
172
+ float * input_ptr =
173
+ input.mutable_data <float >({ic, ih, iw}, paddle::platform::CPUPlace ());
179
174
for (int i = 0 ; i < input.numel (); ++i) {
180
- input_ptr[i] = static_cast <float >(i);
175
+ input_ptr[i] = static_cast <float >(i + 1 );
181
176
}
182
177
183
178
paddle::platform::CPUPlace place;
184
179
paddle::platform::CPUDeviceContext context (place);
185
- output.mutable_data <float >(
186
- {ic, filter_size, filter_size, output_height, output_width}, place);
180
+ output.mutable_data <float >({ic, fh, fw, output_height, output_width}, place);
181
+ ref_output.mutable_data <float >({ic, fh, fw, output_height, output_width},
182
+ place);
187
183
paddle::operators::math::Im2ColFunctor<
188
184
paddle::operators::math::ColFormat::kCFO ,
189
185
paddle::platform::CPUDeviceContext, float >
@@ -200,7 +196,6 @@ void testIm2colCPU() {
200
196
int filter_width = col->dims ()[2 ];
201
197
int output_height = col->dims ()[3 ];
202
198
int output_width = col->dims ()[4 ];
203
-
204
199
int channels_col = im_channels * filter_height * filter_width;
205
200
206
201
const float * im_data = im.data <float >();
@@ -215,7 +210,6 @@ void testIm2colCPU() {
215
210
int im_col_idx = w * stride[1 ] - padding[1 ] + w_offset * dilation[1 ];
216
211
int col_idx = (c * output_height + h) * output_width + w;
217
212
int im_idx = (im_row_idx + c_im * im_height) * im_width + im_col_idx;
218
-
219
213
col_data[col_idx] = (im_row_idx < 0 || im_row_idx >= im_height ||
220
214
im_col_idx < 0 || im_col_idx >= im_width)
221
215
? 0 .f
@@ -225,19 +219,9 @@ void testIm2colCPU() {
225
219
}
226
220
};
227
221
228
- paddle::framework::Tensor ref_output;
229
- ref_output.mutable_data <float >(
230
- {ic, filter_size, filter_size, output_height, output_width}, place);
231
222
ref_im2col (input, dilation, stride, padding, &ref_output);
232
223
233
224
float * out_cfo_ptr = output.data <float >();
234
- for (int i = 0 ; i < ic * filter_size * filter_size; ++i) {
235
- for (int j = 0 ; j < output_height * output_width; ++j) {
236
- std::cout << out_cfo_ptr[i * output_height * output_width + j] << " ," ;
237
- }
238
- std::cout << std::endl;
239
- }
240
-
241
225
float * out_ref_ptr = ref_output.data <float >();
242
226
for (int i = 0 ; i < output.numel (); ++i) {
243
227
EXPECT_EQ (out_cfo_ptr[i], out_ref_ptr[i]);
@@ -246,7 +230,10 @@ void testIm2colCPU() {
246
230
247
231
TEST (math, im2col) {
248
232
testIm2col<paddle::platform::CPUDeviceContext, paddle::platform::CPUPlace>();
249
- testIm2colCPU ();
233
+ testIm2colCPU (/* ic*/ 3 , /* ih*/ 5 , /* iw*/ 5 , /* fh*/ 3 , /* fw*/ 2 , /* ph*/ 0 ,
234
+ /* pw*/ 0 );
235
+ testIm2colCPU (/* ic*/ 2 , /* ih*/ 5 , /* iw*/ 4 , /* fh*/ 3 , /* fw*/ 3 , /* ph*/ 1 ,
236
+ /* pw*/ 1 );
250
237
#ifdef PADDLE_WITH_CUDA
251
238
testIm2col<paddle::platform::CUDADeviceContext,
252
239
paddle::platform::CUDAPlace>();
0 commit comments