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

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/im2col.h"
16
#include <vector>
H
hedaoyuan 已提交
17 18

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

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

C
chengduoZH 已提交
38 39 40
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
C
chengduoZH 已提交
41 42
    int filter_height = col->dims()[1];
    int filter_width = col->dims()[2];
T
tensor-tang 已提交
43 44
    int output_height = col->dims()[3];
    int output_width = col->dims()[4];
C
chengduoZH 已提交
45

C
chengduoZH 已提交
46
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
47 48

    const T* im_data = im.data<T>();
C
chengduoZH 已提交
49
    T* col_data = col->data<T>();
T
tensor-tang 已提交
50
    // TODO(TJ): change me to template
51
    // further optimize: padding == 1 need special
T
tensor-tang 已提交
52
    if (stride[0] == 1 && stride[1] == 1 && dilation[0] == 1 &&
53
        dilation[1] == 1) {
T
tensor-tang 已提交
54
      int col_matrix_width = output_width * output_height;
T
tensor-tang 已提交
55
      int im_size = im_height * im_width;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      if (padding[0] == 0 && padding[1] == 0) {
        size_t copy_size = sizeof(T) * output_width;
        for (int oh = 0; oh < output_height; ++oh) {
          const T* im_data_start = im_data + oh * im_width;
          T* dst_data = col_data + oh * output_width;
          for (int ic = 0; ic < im_channels; ++ic) {
            const T* src_data = im_data_start + ic * im_size;
            for (int kh = 0; kh < filter_height; ++kh) {
              for (int kw = 0; kw < filter_width; ++kw) {
                std::memcpy(dst_data, src_data + kw, copy_size);
                dst_data = dst_data + col_matrix_width;
              }
              src_data = src_data + im_width;
            }
          }
        }
        return;
      } else {
        int plh = padding[0];
        // int plw = padding[1];
        int prh =
            (output_height - 1) * stride[0] + filter_height - im_height - plh;
        // int prw =  (output_width - 1) * stride[1] + filter_width - im_width -
        // plw;

        // fill height padding : 0 ~ plh-1, (oh-prh) ~ (oh-1)
        // TODO(TJ): reuse sizes
        assert(plh == prh);  // because stride_h == 1
        for (int ph = 0; ph < plh; ++ph) {
          size_t sz = sizeof(T) * output_width * (plh - ph);
          T* col_start_l = col_data + ph * filter_width * col_matrix_width;
          T* col_start_r =
              col_data +
              (filter_width - ph - 1) * filter_width * col_matrix_width +
              col_matrix_width - output_width * (plh - ph);
          for (int ic = 0; ic < im_channels; ++ic) {
            T* dst_data_l =
                col_start_l +
                ic * filter_width * filter_height * col_matrix_width;
            T* dst_data_r =
                col_start_r +
                ic * filter_width * filter_height * col_matrix_width;
T
tensor-tang 已提交
98
            for (int kw = 0; kw < filter_width; ++kw) {
99 100 101 102
              std::memset(dst_data_l, 0, sz);
              std::memset(dst_data_r, 0, sz);
              dst_data_l = dst_data_l + col_matrix_width;
              dst_data_r = dst_data_r + col_matrix_width;
T
tensor-tang 已提交
103 104 105
            }
          }
        }
106
        return;
T
tensor-tang 已提交
107 108 109
      }
    }

H
hedaoyuan 已提交
110
    for (int c = 0; c < channels_col; ++c) {
C
chengduoZH 已提交
111 112 113
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / (filter_width * filter_height);
T
tensor-tang 已提交
114
      for (int h = 0; h < output_height; ++h) {
C
chengduoZH 已提交
115
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
T
tensor-tang 已提交
116
        for (int w = 0; w < output_width; ++w) {
C
chengduoZH 已提交
117
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
T
tensor-tang 已提交
118
          int col_idx = (c * output_height + h) * output_width + w;
C
chengduoZH 已提交
119
          int im_idx = (im_row_idx + c_im * im_height) * im_width + im_col_idx;
C
chengduoZH 已提交
120

C
chengduoZH 已提交
121 122 123 124
          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 已提交
125 126 127 128 129 130 131
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
132 133 134
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
H
hedaoyuan 已提交
135 136
 */
template <class T>
H
hedaoyuan 已提交
137
class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
138
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
139
 public:
Q
QI JUN 已提交
140
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
141 142 143 144 145
                  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 已提交
146
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
147 148 149
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
H
hedaoyuan 已提交
150 151
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
C
chengduoZH 已提交
152 153
    int col_height = col.dims()[3];
    int col_width = col.dims()[4];
C
chengduoZH 已提交
154

C
chengduoZH 已提交
155 156 157
    PADDLE_ENFORCE_EQ((im_height + padding[0] + padding[2] -
                       ((dilation[0] * (filter_height - 1) + 1))) /
                              stride[0] +
C
chengduoZH 已提交
158 159 160 161
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
C
chengduoZH 已提交
162 163 164
    PADDLE_ENFORCE_EQ((im_width + padding[1] + padding[3] -
                       ((dilation[1] * (filter_width - 1) + 1))) /
                              stride[1] +
C
chengduoZH 已提交
165 166
                          1,
                      col_width,
C
chengduoZH 已提交
167
                      "Output_height and padding(padding_up, padding_down) are "
C
chengduoZH 已提交
168
                      "inconsistent.");
C
chengduoZH 已提交
169

C
chengduoZH 已提交
170
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
171

C
chengduoZH 已提交
172
    T* im_data = im->data<T>();
H
hedaoyuan 已提交
173 174 175
    const T* col_data = col.data<T>();

    for (int c = 0; c < channels_col; ++c) {
C
chengduoZH 已提交
176 177 178
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / (filter_width * filter_height);
C
chengduoZH 已提交
179
      for (int h = 0; h < col_height; ++h) {
C
chengduoZH 已提交
180
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
C
chengduoZH 已提交
181
        for (int w = 0; w < col_width; ++w) {
C
chengduoZH 已提交
182
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
C
chengduoZH 已提交
183 184
          if ((im_row_idx) >= 0 && (im_row_idx) < im_height &&
              (im_col_idx) >= 0 && (im_col_idx) < im_width) {
C
chengduoZH 已提交
185
            im_data[(im_row_idx + c_im * im_height) * im_width + im_col_idx] +=
C
chengduoZH 已提交
186
                col_data[(c * col_height + h) * col_width + w];
H
hedaoyuan 已提交
187 188 189 190 191 192 193
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
194
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
195
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
196
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
197
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
198
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
199
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
200
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
201
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
202 203

/*
H
hedaoyuan 已提交
204 205 206
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
207 208
 */
template <class T>
H
hedaoyuan 已提交
209
class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
210
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
211
 public:
Q
QI JUN 已提交
212
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
213 214 215
                  const framework::Tensor& im, const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* col) {
H
hedaoyuan 已提交
216
    PADDLE_ENFORCE(im.dims().size() == 3);
C
chengduoZH 已提交
217
    PADDLE_ENFORCE(col->dims().size() == 5);
C
chengduoZH 已提交
218 219 220
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
C
chengduoZH 已提交
221 222 223 224
    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 已提交
225 226

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

C
chengduoZH 已提交
229 230 231
    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 已提交
232 233
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
C
refine  
chengduoZH 已提交
234 235
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
H
hedaoyuan 已提交
236 237 238
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
C
chengduoZH 已提交
239
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
C
refine  
chengduoZH 已提交
240

C
chengduoZH 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
              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 已提交
256 257 258 259 260 261 262 263 264
            }
          }
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
265 266 267
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
268 269
 */
template <class T>
H
hedaoyuan 已提交
270
class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
271
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
272
 public:
Q
QI JUN 已提交
273
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
274 275 276 277 278
                  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 已提交
279
    PADDLE_ENFORCE(col.dims().size() == 5);
C
chengduoZH 已提交
280 281 282
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
H
hedaoyuan 已提交
283 284
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
C
chengduoZH 已提交
285 286
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
H
hedaoyuan 已提交
287

C
chengduoZH 已提交
288 289 290 291 292 293 294 295 296 297
    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.");
298

C
chengduoZH 已提交
299
    T* im_data = im->data<T>();
H
hedaoyuan 已提交
300 301
    const T* col_data = col.data<T>();

C
chengduoZH 已提交
302 303 304
    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 已提交
305 306
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
C
refine  
chengduoZH 已提交
307 308
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
H
hedaoyuan 已提交
309 310 311
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
C
chengduoZH 已提交
312
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
C
refine  
chengduoZH 已提交
313

C
chengduoZH 已提交
314 315 316 317 318 319 320
              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 已提交
321

C
chengduoZH 已提交
322 323
              if (im_row_offset >= 0 && im_row_offset < im_height &&
                  im_col_offset >= 0 && im_col_offset < im_width) {
H
hedaoyuan 已提交
324
                int im_offset =
C
chengduoZH 已提交
325
                    (channel * im_height + im_row_offset) * im_width +
H
hedaoyuan 已提交
326 327
                    im_col_offset;
                im_data[im_offset] += col_data[col_offset];
H
hedaoyuan 已提交
328 329 330 331 332 333 334 335 336
              }
            }
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
337
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
338
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
339
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
340
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
341
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
342
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
343
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
344
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
345

346
}  // namespace math
347
}  // namespace operators
H
hedaoyuan 已提交
348
}  // namespace paddle