im2col.cc 10.9 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>
T
tensor-tang 已提交
17
#include "paddle/fluid/operators/math/im2col_cfo_cpu.h"
H
hedaoyuan 已提交
18 19

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

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

T
tensor-tang 已提交
40
    if (stride[0] == 1 && stride[1] == 1 && dilation[0] == 1 &&
41
        dilation[1] == 1) {
L
liym27 已提交
42 43
      if (padding[0] == 0 && padding[1] == 0 && padding[2] == 0 &&
          padding[3] == 0) {
T
tensor-tang 已提交
44
        im2col_sh1sw1dh1dw1ph0pw0<T>(im, col);
45
        return;
L
liym27 已提交
46 47
      } else if (padding[0] == 1 && padding[1] == 1 && padding[2] == 1 &&
                 padding[3] == 1) {
48 49
        im2col_sh1sw1dh1dw1ph1pw1<T>(im, col);
        return;
H
hedaoyuan 已提交
50
      }
51
      // TODO(TJ): complete padding >=2
H
hedaoyuan 已提交
52
    }
T
tensor-tang 已提交
53
    im2col_common<T>(im, dilation, stride, padding, col);
H
hedaoyuan 已提交
54 55 56 57
  }
};

/*
H
hedaoyuan 已提交
58 59 60
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
H
hedaoyuan 已提交
61 62
 */
template <class T>
H
hedaoyuan 已提交
63
class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
64
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
65
 public:
Q
QI JUN 已提交
66
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
67 68 69 70
                  const framework::Tensor& col,
                  const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* im) {
L
liym27 已提交
71 72 73
    PADDLE_ENFORCE_EQ(im->dims().size(), 3, "The dimension of im should be 3.");
    PADDLE_ENFORCE_EQ(col.dims().size(), 5,
                      "The dimension of col should be 5.");
C
chengduoZH 已提交
74 75 76
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
H
hedaoyuan 已提交
77 78
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
C
chengduoZH 已提交
79 80
    int col_height = col.dims()[3];
    int col_width = col.dims()[4];
C
chengduoZH 已提交
81

C
chengduoZH 已提交
82 83 84
    PADDLE_ENFORCE_EQ((im_height + padding[0] + padding[2] -
                       ((dilation[0] * (filter_height - 1) + 1))) /
                              stride[0] +
C
chengduoZH 已提交
85 86 87 88
                          1,
                      col_height,
                      "Output_height and padding(padding_up, padding_down) are "
                      "inconsistent.");
C
chengduoZH 已提交
89 90 91
    PADDLE_ENFORCE_EQ((im_width + padding[1] + padding[3] -
                       ((dilation[1] * (filter_width - 1) + 1))) /
                              stride[1] +
C
chengduoZH 已提交
92 93
                          1,
                      col_width,
C
chengduoZH 已提交
94
                      "Output_height and padding(padding_up, padding_down) are "
C
chengduoZH 已提交
95
                      "inconsistent.");
C
chengduoZH 已提交
96

C
chengduoZH 已提交
97
    int channels_col = im_channels * filter_height * filter_width;
H
hedaoyuan 已提交
98

C
chengduoZH 已提交
99
    T* im_data = im->data<T>();
H
hedaoyuan 已提交
100 101 102
    const T* col_data = col.data<T>();

    for (int c = 0; c < channels_col; ++c) {
C
chengduoZH 已提交
103 104 105
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int c_im = c / (filter_width * filter_height);
C
chengduoZH 已提交
106
      for (int h = 0; h < col_height; ++h) {
C
chengduoZH 已提交
107
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
C
chengduoZH 已提交
108
        for (int w = 0; w < col_width; ++w) {
C
chengduoZH 已提交
109
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
C
chengduoZH 已提交
110 111
          if ((im_row_idx) >= 0 && (im_row_idx) < im_height &&
              (im_col_idx) >= 0 && (im_col_idx) < im_width) {
C
chengduoZH 已提交
112
            im_data[(im_row_idx + c_im * im_height) * im_width + im_col_idx] +=
C
chengduoZH 已提交
113
                col_data[(c * col_height + h) * col_width + w];
H
hedaoyuan 已提交
114 115 116 117 118 119 120
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
121
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
122
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
123
template class Im2ColFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
124
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
125
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
126
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
127
template class Col2ImFunctor<paddle::operators::math::ColFormat::kCFO,
Q
QI JUN 已提交
128
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
129 130

/*
H
hedaoyuan 已提交
131 132 133
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
134 135
 */
template <class T>
H
hedaoyuan 已提交
136
class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
137
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
138
 public:
Q
QI JUN 已提交
139
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
140 141 142
                  const framework::Tensor& im, const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* col) {
L
liym27 已提交
143 144 145
    PADDLE_ENFORCE_EQ(im.dims().size(), 3, "The dimension of im should be 3.");
    PADDLE_ENFORCE_EQ(col->dims().size(), 5,
                      "The dimension of col should be 5.");
C
chengduoZH 已提交
146 147 148
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
C
chengduoZH 已提交
149 150 151 152
    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 已提交
153 154

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

C
chengduoZH 已提交
157 158 159
    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 已提交
160 161
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
C
refine  
chengduoZH 已提交
162 163
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
H
hedaoyuan 已提交
164 165 166
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
C
chengduoZH 已提交
167
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
C
refine  
chengduoZH 已提交
168

C
chengduoZH 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
              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 已提交
184 185 186 187 188 189 190 191 192
            }
          }
        }
      }
    }
  }
};

/*
H
hedaoyuan 已提交
193 194 195
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
H
hedaoyuan 已提交
196 197
 */
template <class T>
H
hedaoyuan 已提交
198
class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
199
                    platform::CPUDeviceContext, T> {
H
hedaoyuan 已提交
200
 public:
Q
QI JUN 已提交
201
  void operator()(const platform::CPUDeviceContext& context,
C
chengduoZH 已提交
202 203 204 205
                  const framework::Tensor& col,
                  const std::vector<int>& dilation,
                  const std::vector<int>& stride,
                  const std::vector<int>& padding, framework::Tensor* im) {
L
liym27 已提交
206 207 208
    PADDLE_ENFORCE_EQ(im->dims().size(), 3, "The dimension of im should be 3.");
    PADDLE_ENFORCE_EQ(col.dims().size(), 5,
                      "The dimension of col should be 5.");
C
chengduoZH 已提交
209 210 211
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
H
hedaoyuan 已提交
212 213
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
C
chengduoZH 已提交
214 215
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
H
hedaoyuan 已提交
216

C
chengduoZH 已提交
217 218 219 220 221 222 223 224 225 226
    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.");
227

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

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

C
chengduoZH 已提交
243 244 245 246 247 248 249
              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 已提交
250

C
chengduoZH 已提交
251 252
              if (im_row_offset >= 0 && im_row_offset < im_height &&
                  im_col_offset >= 0 && im_col_offset < im_width) {
H
hedaoyuan 已提交
253
                int im_offset =
C
chengduoZH 已提交
254
                    (channel * im_height + im_row_offset) * im_width +
H
hedaoyuan 已提交
255 256
                    im_col_offset;
                im_data[im_offset] += col_data[col_offset];
H
hedaoyuan 已提交
257 258 259 260 261 262 263 264 265
              }
            }
          }
        }
      }
    }
  }
};

H
hedaoyuan 已提交
266
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
267
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
268
template class Im2ColFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
269
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
270
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
271
                             platform::CPUDeviceContext, float>;
H
hedaoyuan 已提交
272
template class Col2ImFunctor<paddle::operators::math::ColFormat::kOCF,
Q
QI JUN 已提交
273
                             platform::CPUDeviceContext, double>;
H
hedaoyuan 已提交
274

275
}  // namespace math
276
}  // namespace operators
H
hedaoyuan 已提交
277
}  // namespace paddle