im2col.cc 12.7 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 28
class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
                    platform::CPUPlace, T> {
H
hedaoyuan 已提交
29
 public:
H
hedaoyuan 已提交
30 31
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& im, framework::Tensor& col,
C
chengduoZH 已提交
32 33 34
                  int dilation_h, int dilation_w, int stride_height,
                  int stride_width, int padding_up, int padding_down,
                  int padding_left, int padding_right) {
H
hedaoyuan 已提交
35 36 37
    PADDLE_ENFORCE(im.dims().size() == 3);
    PADDLE_ENFORCE(col.dims().size() == 5);

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

C
chengduoZH 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    PADDLE_ENFORCE_EQ((im_height + padding_up + padding_down -
                       ((dilation_h * (filter_height - 1) + 1))) /
                              stride_height +
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
    PADDLE_ENFORCE_EQ((im_width + padding_left + padding_right -
                       ((dilation_w * (filter_width - 1) + 1))) /
                              stride_width +
                          1,
                      col_width,
                      "col_width and padding(padding_left, padding_right) are "
                      "inconsistent.");
C
chengduoZH 已提交
60

C
chengduoZH 已提交
61
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
62 63 64 65 66 67 68 69

    const T* im_data = im.data<T>();
    T* col_data = col.data<T>();

    for (int c = 0; c < channels_col; ++c) {
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / filter_width / filter_height;
C
chengduoZH 已提交
70 71 72 73 74 75
      for (int h = 0; h < col_height; ++h) {
        for (int w = 0; w < col_width; ++w) {
          int im_row_idx =
              h * stride_height - padding_up + h_offset * dilation_h;
          int im_col_idx =
              w * stride_width - padding_left + w_offset * dilation_w;
C
chengduoZH 已提交
76

C
chengduoZH 已提交
77 78 79 80 81 82
          col_data[(c * col_height + h) * col_width + w] =
              (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_row_idx + c_im * im_height) * im_width +
                            im_col_idx];
H
hedaoyuan 已提交
83 84 85 86 87 88 89
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
90 91 92
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
H
hedaoyuan 已提交
93 94
 */
template <class T>
H
hedaoyuan 已提交
95 96
class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
                    platform::CPUPlace, T> {
H
hedaoyuan 已提交
97
 public:
H
hedaoyuan 已提交
98
  void operator()(const platform::DeviceContext& context, framework::Tensor& im,
C
chengduoZH 已提交
99 100 101
                  const framework::Tensor& col, int dilation_h, int dilation_w,
                  int stride_height, int stride_width, int padding_up,
                  int padding_down, int padding_left, int padding_right) {
H
hedaoyuan 已提交
102 103
    PADDLE_ENFORCE(im.dims().size() == 3);
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
104 105 106
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
H
hedaoyuan 已提交
107 108
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
C
chengduoZH 已提交
109 110
    int col_height = col.dims()[3];
    int col_width = col.dims()[4];
C
chengduoZH 已提交
111

C
chengduoZH 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    PADDLE_ENFORCE_EQ((im_height + padding_up + padding_down -
                       ((dilation_h * (filter_height - 1) + 1))) /
                              stride_height +
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
    PADDLE_ENFORCE_EQ((im_width + padding_left + padding_right -
                       ((dilation_w * (filter_width - 1) + 1))) /
                              stride_width +
                          1,
                      col_width,
                      "col_width and padding(padding_left, padding_right) are "
                      "inconsistent.");
C
chengduoZH 已提交
126

C
chengduoZH 已提交
127
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
128 129 130 131 132 133 134 135

    T* im_data = im.data<T>();
    const T* col_data = col.data<T>();

    for (int c = 0; c < channels_col; ++c) {
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / filter_width / filter_height;
C
chengduoZH 已提交
136 137 138 139 140 141
      for (int h = 0; h < col_height; ++h) {
        for (int w = 0; w < col_width; ++w) {
          int im_row_idx =
              h * stride_height - padding_up + h_offset * dilation_h;
          int im_col_idx =
              w * stride_width - padding_left + w_offset * dilation_w;
C
chengduoZH 已提交
142

C
chengduoZH 已提交
143 144 145 146 147
          if ((im_row_idx) >= 0 && (im_row_idx) < im_height &&
              (im_col_idx) >= 0 && (im_col_idx) < im_width) {
            im_row_idx += c_im * im_height;
            im_data[im_row_idx * im_width + im_col_idx] +=
                col_data[(c * col_height + h) * col_width + w];
H
hedaoyuan 已提交
148 149 150 151 152 153 154
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
155 156 157 158 159 160 161 162
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
                             platform::CPUPlace, float>;
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
                             platform::CPUPlace, double>;
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
                             platform::CPUPlace, float>;
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
                             platform::CPUPlace, double>;
H
hedaoyuan 已提交
163 164

/*
H
hedaoyuan 已提交
165 166 167
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
168 169
 */
template <class T>
H
hedaoyuan 已提交
170 171
class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
                    platform::CPUPlace, T> {
H
hedaoyuan 已提交
172
 public:
H
hedaoyuan 已提交
173 174
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& im, framework::Tensor& col,
C
chengduoZH 已提交
175 176 177
                  int dilation_h, int dilation_w, int stride_height,
                  int stride_width, int padding_up, int padding_down,
                  int padding_left, int padding_right) {
H
hedaoyuan 已提交
178 179
    PADDLE_ENFORCE(im.dims().size() == 3);
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
180 181 182
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
H
hedaoyuan 已提交
183 184
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
C
chengduoZH 已提交
185 186
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
H
hedaoyuan 已提交
187

C
chengduoZH 已提交
188 189 190 191 192 193 194 195 196 197 198 199
    PADDLE_ENFORCE_EQ((im_height + padding_up + padding_down - filter_height) /
                              stride_height +
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
    PADDLE_ENFORCE_EQ((im_width + padding_left + padding_right - filter_width) /
                              stride_width +
                          1,
                      col_width,
                      "col_width and padding(padding_left, padding_right) are "
                      "inconsistent.");
200

H
hedaoyuan 已提交
201 202 203
    const T* im_data = im.data<T>();
    T* col_data = col.data<T>();

C
chengduoZH 已提交
204 205 206
    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 已提交
207 208 209 210 211
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_row_offset =
C
chengduoZH 已提交
212
                  col_row_idx * stride_height + filter_row_idx - padding_up;
H
hedaoyuan 已提交
213
              int im_col_offset =
C
chengduoZH 已提交
214
                  col_col_idx * stride_width + filter_col_idx - padding_left;
C
chengduoZH 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
              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 已提交
230 231 232 233 234 235 236 237 238
            }
          }
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
239 240 241
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
242 243
 */
template <class T>
H
hedaoyuan 已提交
244 245
class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
                    platform::CPUPlace, T> {
H
hedaoyuan 已提交
246
 public:
H
hedaoyuan 已提交
247
  void operator()(const platform::DeviceContext& context, framework::Tensor& im,
C
chengduoZH 已提交
248 249 250
                  const framework::Tensor& col, int dilation_h, int dilation_w,
                  int stride_height, int stride_width, int padding_up,
                  int padding_down, int padding_left, int padding_right) {
H
hedaoyuan 已提交
251 252
    PADDLE_ENFORCE(im.dims().size() == 3);
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
253 254 255
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
H
hedaoyuan 已提交
256 257
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
C
chengduoZH 已提交
258 259
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
H
hedaoyuan 已提交
260

C
chengduoZH 已提交
261 262 263 264 265 266 267 268 269 270 271 272
    PADDLE_ENFORCE_EQ((im_height + padding_up + padding_down - filter_height) /
                              stride_height +
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
    PADDLE_ENFORCE_EQ((im_width + padding_left + padding_right - filter_width) /
                              stride_width +
                          1,
                      col_width,
                      "col_width and padding(padding_left, padding_right) are "
                      "inconsistent.");
273

H
hedaoyuan 已提交
274 275 276
    T* im_data = im.data<T>();
    const T* col_data = col.data<T>();

C
chengduoZH 已提交
277 278 279
    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 已提交
280 281 282 283
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
C
chengduoZH 已提交
284
              int im_row_offset =
C
chengduoZH 已提交
285
                  col_row_idx * stride_height + filter_row_idx - padding_up;
H
hedaoyuan 已提交
286
              int im_col_offset =
C
chengduoZH 已提交
287
                  col_col_idx * stride_width + filter_col_idx - padding_left;
C
chengduoZH 已提交
288 289 290 291 292 293 294 295 296
              int col_offset =
                  (((col_row_idx * col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;
              if (im_row_offset >= 0 && im_row_offset < im_height &&
                  im_col_offset >= 0 && im_col_offset < im_width) {
H
hedaoyuan 已提交
297
                int im_offset =
C
chengduoZH 已提交
298
                    (channel * im_height + im_row_offset) * im_width +
H
hedaoyuan 已提交
299 300
                    im_col_offset;
                im_data[im_offset] += col_data[col_offset];
H
hedaoyuan 已提交
301 302 303 304 305 306 307 308 309
              }
            }
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
310 311 312 313 314 315 316 317
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
                             platform::CPUPlace, float>;
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
                             platform::CPUPlace, double>;
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
                             platform::CPUPlace, float>;
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
                             platform::CPUPlace, double>;
H
hedaoyuan 已提交
318

319
}  // namespace math
320
}  // namespace operators
H
hedaoyuan 已提交
321
}  // namespace paddle