im2col.cc 12.5 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

H
hedaoyuan 已提交
15
#include "paddle/operators/math/im2col.h"
H
hedaoyuan 已提交
16 17

namespace paddle {
18
namespace operators {
19
namespace math {
H
hedaoyuan 已提交
20 21

/*
H
hedaoyuan 已提交
22 23 24
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
H
hedaoyuan 已提交
25 26
 */
template <class T>
H
hedaoyuan 已提交
27
class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
28
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
29
 public:
Q
QI JUN 已提交
30
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
31 32 33
                  const framework::Tensor& im, const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* col) {
H
hedaoyuan 已提交
34
    PADDLE_ENFORCE(im.dims().size() == 3);
C
chengduoZH 已提交
35
    PADDLE_ENFORCE(col->dims().size() == 5);
H
hedaoyuan 已提交
36

C
chengduoZH 已提交
37 38 39
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
C
chengduoZH 已提交
40 41 42 43
    int filter_height = col->dims()[1];
    int filter_width = col->dims()[2];
    int col_height = col->dims()[3];
    int col_width = col->dims()[4];
C
chengduoZH 已提交
44

C
chengduoZH 已提交
45 46 47
    PADDLE_ENFORCE_EQ((im_height + padding[0] + padding[2] -
                       ((dilation[0] * (filter_height - 1) + 1))) /
                              stride[0] +
C
chengduoZH 已提交
48 49 50 51
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
C
chengduoZH 已提交
52 53 54
    PADDLE_ENFORCE_EQ((im_width + padding[1] + padding[3] -
                       ((dilation[1] * (filter_width - 1) + 1))) /
                              stride[1] +
C
chengduoZH 已提交
55 56
                          1,
                      col_width,
C
chengduoZH 已提交
57
                      "Output_height and padding(padding_up, padding_down) are "
C
chengduoZH 已提交
58
                      "inconsistent.");
C
chengduoZH 已提交
59

C
chengduoZH 已提交
60
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
61 62

    const T* im_data = im.data<T>();
C
chengduoZH 已提交
63
    T* col_data = col->data<T>();
C
chengduoZH 已提交
64 65 66
    int w_offset = -1;
    int h_offset = 0;
    int c_im = 0;
H
hedaoyuan 已提交
67
    for (int c = 0; c < channels_col; ++c) {
C
chengduoZH 已提交
68
      ++w_offset;
C
refine  
chengduoZH 已提交
69
      if (w_offset == filter_width) {
C
chengduoZH 已提交
70 71
        w_offset = 0;
        ++h_offset;
C
refine  
chengduoZH 已提交
72
        if (h_offset == filter_height) {
C
chengduoZH 已提交
73 74 75 76
          h_offset = 0;
          ++c_im;
        }
      }
C
chengduoZH 已提交
77
      for (int h = 0; h < col_height; ++h) {
C
chengduoZH 已提交
78
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
C
chengduoZH 已提交
79
        for (int w = 0; w < col_width; ++w) {
C
chengduoZH 已提交
80
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
C
chengduoZH 已提交
81 82
          int col_idx = (c * col_height + h) * col_width + w;
          int im_idx = (im_row_idx + c_im * im_height) * im_width + im_col_idx;
C
chengduoZH 已提交
83

C
chengduoZH 已提交
84 85 86 87
          col_data[col_idx] = (im_row_idx < 0 || im_row_idx >= im_height ||
                               im_col_idx < 0 || im_col_idx >= im_width)
                                  ? static_cast<T>(0)
                                  : im_data[im_idx];
H
hedaoyuan 已提交
88 89 90 91 92 93 94
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
95 96 97
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
H
hedaoyuan 已提交
98 99
 */
template <class T>
H
hedaoyuan 已提交
100
class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
101
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
102
 public:
Q
QI JUN 已提交
103
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
104 105 106 107 108
                  const framework::Tensor& col,
                  const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* im) {
    PADDLE_ENFORCE(im->dims().size() == 3);
H
hedaoyuan 已提交
109
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
110 111 112
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
H
hedaoyuan 已提交
113 114
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
C
chengduoZH 已提交
115 116
    int col_height = col.dims()[3];
    int col_width = col.dims()[4];
C
chengduoZH 已提交
117

C
chengduoZH 已提交
118 119 120
    PADDLE_ENFORCE_EQ((im_height + padding[0] + padding[2] -
                       ((dilation[0] * (filter_height - 1) + 1))) /
                              stride[0] +
C
chengduoZH 已提交
121 122 123 124
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
C
chengduoZH 已提交
125 126 127
    PADDLE_ENFORCE_EQ((im_width + padding[1] + padding[3] -
                       ((dilation[1] * (filter_width - 1) + 1))) /
                              stride[1] +
C
chengduoZH 已提交
128 129
                          1,
                      col_width,
C
chengduoZH 已提交
130
                      "Output_height and padding(padding_up, padding_down) are "
C
chengduoZH 已提交
131
                      "inconsistent.");
C
chengduoZH 已提交
132

C
chengduoZH 已提交
133
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
134

C
chengduoZH 已提交
135
    T* im_data = im->data<T>();
H
hedaoyuan 已提交
136 137
    const T* col_data = col.data<T>();

C
chengduoZH 已提交
138 139 140
    int w_offset = -1;
    int h_offset = 0;
    int c_im = 0;
H
hedaoyuan 已提交
141
    for (int c = 0; c < channels_col; ++c) {
C
chengduoZH 已提交
142
      ++w_offset;
C
refine  
chengduoZH 已提交
143
      if (w_offset == filter_width) {
C
chengduoZH 已提交
144 145
        w_offset = 0;
        ++h_offset;
C
refine  
chengduoZH 已提交
146
        if (h_offset == filter_height) {
C
chengduoZH 已提交
147 148 149 150
          h_offset = 0;
          ++c_im;
        }
      }
C
chengduoZH 已提交
151
      for (int h = 0; h < col_height; ++h) {
C
chengduoZH 已提交
152
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
C
chengduoZH 已提交
153
        for (int w = 0; w < col_width; ++w) {
C
chengduoZH 已提交
154
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
C
chengduoZH 已提交
155 156
          if ((im_row_idx) >= 0 && (im_row_idx) < im_height &&
              (im_col_idx) >= 0 && (im_col_idx) < im_width) {
C
chengduoZH 已提交
157
            im_data[(im_row_idx + c_im * im_height) * im_width + im_col_idx] +=
C
chengduoZH 已提交
158
                col_data[(c * col_height + h) * col_width + w];
H
hedaoyuan 已提交
159 160 161 162 163 164 165
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
166
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
167
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
168
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
169
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
170
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
171
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
172
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
173
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
174 175

/*
H
hedaoyuan 已提交
176 177 178
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
179 180
 */
template <class T>
H
hedaoyuan 已提交
181
class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
182
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
183
 public:
Q
QI JUN 已提交
184
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
185 186 187
                  const framework::Tensor& im, const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* col) {
H
hedaoyuan 已提交
188
    PADDLE_ENFORCE(im.dims().size() == 3);
C
chengduoZH 已提交
189
    PADDLE_ENFORCE(col->dims().size() == 5);
C
chengduoZH 已提交
190 191 192
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
C
chengduoZH 已提交
193 194 195 196
    int filter_height = col->dims()[3];
    int filter_width = col->dims()[4];
    int col_height = col->dims()[0];
    int col_width = col->dims()[1];
H
hedaoyuan 已提交
197

C
chengduoZH 已提交
198 199 200 201 202 203 204 205 206 207
    PADDLE_ENFORCE_EQ(
        (im_height + padding[0] + padding[2] - filter_height) / stride[0] + 1,
        col_height,
        "Output_height and padding(padding_up, padding_down) are "
        "inconsistent.");
    PADDLE_ENFORCE_EQ(
        (im_width + padding[1] + padding[3] - filter_width) / stride[1] + 1,
        col_width,
        "col_width and padding(padding_left, padding_right) are "
        "inconsistent.");
208

H
hedaoyuan 已提交
209
    const T* im_data = im.data<T>();
C
chengduoZH 已提交
210
    T* col_data = col->data<T>();
H
hedaoyuan 已提交
211

C
chengduoZH 已提交
212 213 214
    for (int col_row_idx = 0; col_row_idx < col_height; ++col_row_idx) {
      for (int col_col_idx = 0; col_col_idx < col_width; ++col_col_idx) {
        for (int channel = 0; channel < im_channels; ++channel) {
H
hedaoyuan 已提交
215 216
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
C
refine  
chengduoZH 已提交
217 218
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
H
hedaoyuan 已提交
219 220 221
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
C
chengduoZH 已提交
222
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
C
refine  
chengduoZH 已提交
223

C
chengduoZH 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
              int col_offset =
                  ((((col_row_idx)*col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;

              int im_offset = (channel * im_height + im_row_offset) * im_width +
                              im_col_offset;
              col_data[col_offset] =
                  (im_row_offset < 0 || im_row_offset >= im_height ||
                   im_col_offset < 0 || im_col_offset >= im_width)
                      ? static_cast<T>(0)
                      : im_data[im_offset];
H
hedaoyuan 已提交
239 240 241 242 243 244 245 246 247
            }
          }
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
248 249 250
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
251 252
 */
template <class T>
H
hedaoyuan 已提交
253
class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
254
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
255
 public:
Q
QI JUN 已提交
256
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
257 258 259 260 261
                  const framework::Tensor& col,
                  const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* im) {
    PADDLE_ENFORCE(im->dims().size() == 3);
H
hedaoyuan 已提交
262
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
263 264 265
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
H
hedaoyuan 已提交
266 267
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
C
chengduoZH 已提交
268 269
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
H
hedaoyuan 已提交
270

C
chengduoZH 已提交
271 272 273 274 275 276 277 278 279 280
    PADDLE_ENFORCE_EQ(
        (im_height + padding[0] + padding[2] - filter_height) / stride[0] + 1,
        col_height,
        "Output_height and padding(padding_up, padding_down) are "
        "inconsistent.");
    PADDLE_ENFORCE_EQ(
        (im_width + padding[1] + padding[3] - filter_width) / stride[1] + 1,
        col_width,
        "col_width and padding(padding_left, padding_right) are "
        "inconsistent.");
281

C
chengduoZH 已提交
282
    T* im_data = im->data<T>();
H
hedaoyuan 已提交
283 284
    const T* col_data = col.data<T>();

C
chengduoZH 已提交
285 286 287
    for (int col_row_idx = 0; col_row_idx < col_height; ++col_row_idx) {
      for (int col_col_idx = 0; col_col_idx < col_width; ++col_col_idx) {
        for (int channel = 0; channel < im_channels; ++channel) {
H
hedaoyuan 已提交
288 289
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
C
refine  
chengduoZH 已提交
290 291
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
H
hedaoyuan 已提交
292 293 294
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
C
chengduoZH 已提交
295
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
C
refine  
chengduoZH 已提交
296

C
chengduoZH 已提交
297 298 299 300 301 302 303
              int col_offset =
                  (((col_row_idx * col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;
C
refine  
chengduoZH 已提交
304

C
chengduoZH 已提交
305 306
              if (im_row_offset >= 0 && im_row_offset < im_height &&
                  im_col_offset >= 0 && im_col_offset < im_width) {
H
hedaoyuan 已提交
307
                int im_offset =
C
chengduoZH 已提交
308
                    (channel * im_height + im_row_offset) * im_width +
H
hedaoyuan 已提交
309 310
                    im_col_offset;
                im_data[im_offset] += col_data[col_offset];
H
hedaoyuan 已提交
311 312 313 314 315 316 317 318 319
              }
            }
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
320
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
321
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
322
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
323
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
324
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
325
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
326
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
327
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
328

329
}  // namespace math
330
}  // namespace operators
H
hedaoyuan 已提交
331
}  // namespace paddle