im2col.cc 12.3 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
                  int stride_height, int stride_width, int padding_up,
                  int padding_down, int padding_left, int padding_right) {
H
hedaoyuan 已提交
34 35 36 37 38 39 40 41 42 43
    PADDLE_ENFORCE(im.dims().size() == 3);
    PADDLE_ENFORCE(col.dims().size() == 5);

    int input_channels = im.dims()[0];
    int input_height = im.dims()[1];
    int input_width = im.dims()[2];
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
    int output_height = col.dims()[3];
    int output_width = col.dims()[4];
C
chengduoZH 已提交
44

C
chengduoZH 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
    PADDLE_ENFORCE_EQ(
        (input_height + padding_up + padding_down - filter_height) /
                stride_height +
            1,
        output_height,
        "Output_height and padding(padding_up, padding_down) are "
        "inconsistent.");
    PADDLE_ENFORCE_EQ(
        (input_width + padding_left + padding_right - filter_width) /
                stride_width +
            1,
        output_width,
        "output_width and padding(padding_left, padding_right) are "
        "inconsistent.");
C
chengduoZH 已提交
59

H
hedaoyuan 已提交
60 61 62 63 64 65 66 67 68 69 70
    int channels_col = input_channels * filter_height * filter_width;

    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;
      for (int h = 0; h < output_height; ++h) {
        for (int w = 0; w < output_width; ++w) {
C
chengduoZH 已提交
71 72 73 74 75
          int im_row_idx = h * stride_height + h_offset - padding_up;
          int im_col_idx = w * stride_width + w_offset - padding_left;

          if (im_row_idx < 0 || im_row_idx >= input_height || im_col_idx < 0 ||
              im_col_idx >= input_width) {
H
hedaoyuan 已提交
76
            col_data[(c * output_height + h) * output_width + w] = T(0);
H
hedaoyuan 已提交
77
          } else {
C
chengduoZH 已提交
78
            im_row_idx += c_im * input_height;
H
hedaoyuan 已提交
79 80
            col_data[(c * output_height + h) * output_width + w] =
                im_data[im_row_idx * input_width + im_col_idx];
H
hedaoyuan 已提交
81 82 83 84 85 86 87 88
          }
        }
      }
    }
  }
};

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

C
chengduoZH 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124
    PADDLE_ENFORCE_EQ(
        (input_height + padding_up + padding_down - filter_height) /
                stride_height +
            1,
        output_height,
        "Output_height and padding(padding_up, padding_down) are "
        "inconsistent.");
    PADDLE_ENFORCE_EQ(
        (input_width + padding_left + padding_right - filter_width) /
                stride_width +
            1,
        output_width,
        "output_width and padding(padding_left, padding_right) are "
        "inconsistent.");
C
chengduoZH 已提交
125

H
hedaoyuan 已提交
126 127 128 129 130 131 132 133 134 135 136
    int channels_col = input_channels * filter_height * filter_width;

    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;
      for (int h = 0; h < output_height; ++h) {
        for (int w = 0; w < output_width; ++w) {
C
chengduoZH 已提交
137 138 139 140 141 142
          int im_row_idx = h * stride_height + h_offset - padding_up;
          int im_col_idx = w * stride_width + w_offset - padding_left;

          if ((im_row_idx) >= 0 && (im_row_idx) < input_height &&
              (im_col_idx) >= 0 && (im_col_idx) < input_width) {
            im_row_idx += c_im * input_height;
H
hedaoyuan 已提交
143 144
            im_data[im_row_idx * input_width + im_col_idx] +=
                col_data[(c * output_height + h) * output_width + w];
H
hedaoyuan 已提交
145 146 147 148 149 150 151
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
152 153 154 155 156 157 158 159
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 已提交
160 161

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

C
chengduoZH 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
    PADDLE_ENFORCE_EQ(
        (input_height + padding_up + padding_down - filter_height) /
                stride_height +
            1,
        output_height,
        "Output_height and padding(padding_up, padding_down) are "
        "inconsistent.");
    PADDLE_ENFORCE_EQ(
        (input_width + padding_left + padding_right - filter_width) /
                stride_width +
            1,
        output_width,
        "output_width and padding(padding_left, padding_right) are "
        "inconsistent.");
198

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

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

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

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

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

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

H
hedaoyuan 已提交
311 312 313 314 315 316 317 318
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 已提交
319

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