im2col.cc 11.8 KB
Newer Older
Z
zhaojiaying01 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
朔-望's avatar
朔-望 已提交
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. */

Z
zhaojiaying01 已提交
15 16
#include <vector>
#include "operators/math/im2col.h"
朔-望's avatar
朔-望 已提交
17 18 19
#include "common/types.h"

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
20 21
namespace operators {
namespace math {
朔-望's avatar
朔-望 已提交
22

朔-望's avatar
朔-望 已提交
23 24 25 26 27 28
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
朔-望's avatar
朔-望 已提交
29 30 31
template <class T>
class Im2ColFunctor<ColFormat::kCFO, CPU, T> {
 public:
32 33 34 35 36
  void operator()(const framework::Tensor &im, const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *col) {
    //    PADDLE_ENFORCE(im.dims().size() == 3);
    //    PADDLE_ENFORCE(col->dims().size() == 5);
朔-望's avatar
朔-望 已提交
37

38 39 40 41 42 43 44
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
    int filter_height = col->dims()[1];
    int filter_width = col->dims()[2];
    int col_height = col->dims()[3];
    int col_width = col->dims()[4];
朔-望's avatar
朔-望 已提交
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    //    PADDLE_ENFORCE_EQ((im_height + padding[0] + padding[2]
    //    -
    //                       ((dilation[0] * (filter_height - 1)
    //                       + 1))) /
    //                              stride[0] +
    //                          1,
    //                      col_height,
    //                      "Output_height and
    //                      padding(padding_up, padding_down)
    //                      are " "inconsistent.");
    //    PADDLE_ENFORCE_EQ((im_width + padding[1] + padding[3]
    //    -
    //                       ((dilation[1] * (filter_width - 1)
    //                       + 1))) /
    //                              stride[1] +
    //                          1,
    //                      col_width,
    //                      "Output_height and
    //                      padding(padding_up, padding_down)
    //                      are " "inconsistent.");
朔-望's avatar
朔-望 已提交
66

67
    int channels_col = im_channels * filter_height * filter_width;
朔-望's avatar
朔-望 已提交
68

69 70 71 72 73 74 75 76 77 78 79 80
    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 < col_height; ++h) {
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
        for (int w = 0; w < col_width; ++w) {
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
          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;
朔-望's avatar
朔-望 已提交
81

82 83 84 85
          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];
朔-望's avatar
朔-望 已提交
86
        }
87
      }
朔-望's avatar
朔-望 已提交
88
    }
89
  }
朔-望's avatar
朔-望 已提交
90
};
朔-望's avatar
朔-望 已提交
91

朔-望's avatar
朔-望 已提交
92 93 94 95 96 97
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
朔-望's avatar
朔-望 已提交
98 99 100
template <class T>
class Col2ImFunctor<ColFormat::kCFO, CPU, T> {
 public:
101 102 103 104 105 106 107 108 109 110 111 112 113
  void operator()(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);
    //    PADDLE_ENFORCE(col.dims().size() == 5);
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
    int filter_height = col.dims()[1];
    int filter_width = col.dims()[2];
    int col_height = col.dims()[3];
    int col_width = col.dims()[4];
朔-望's avatar
朔-望 已提交
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    //    PADDLE_ENFORCE_EQ((im_height + padding[0] + padding[2]
    //    -
    //                       ((dilation[0] * (filter_height - 1)
    //                       + 1))) /
    //                              stride[0] +
    //                          1,
    //                      col_height,
    //                      "Output_height and
    //                      padding(padding_up, padding_down)
    //                      are " "inconsistent.");
    //    PADDLE_ENFORCE_EQ((im_width + padding[1] + padding[3]
    //    -
    //                       ((dilation[1] * (filter_width - 1)
    //                       + 1))) /
    //                              stride[1] +
    //                          1,
    //                      col_width,
    //                      "Output_height and
    //                      padding(padding_up, padding_down)
    //                      are " "inconsistent.");
朔-望's avatar
朔-望 已提交
135

136
    int channels_col = im_channels * filter_height * filter_width;
朔-望's avatar
朔-望 已提交
137

138 139
    T *im_data = im->data<T>();
    const T *col_data = col.data<T>();
朔-望's avatar
朔-望 已提交
140

141 142 143 144 145 146 147 148 149 150 151 152 153
    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 < col_height; ++h) {
        int im_row_idx = h * stride[0] - padding[0] + h_offset * dilation[0];
        for (int w = 0; w < col_width; ++w) {
          int im_col_idx = w * stride[1] - padding[1] + w_offset * dilation[1];
          if ((im_row_idx) >= 0 && (im_row_idx) < im_height &&
              (im_col_idx) >= 0 && (im_col_idx) < im_width) {
            im_data[(im_row_idx + c_im * im_height) * im_width + im_col_idx] +=
                col_data[(c * col_height + h) * col_width + w];
          }
朔-望's avatar
朔-望 已提交
154
        }
155
      }
朔-望's avatar
朔-望 已提交
156
    }
157
  }
朔-望's avatar
朔-望 已提交
158
};
朔-望's avatar
朔-望 已提交
159

朔-望's avatar
朔-望 已提交
160 161 162 163
template class Im2ColFunctor<ColFormat::kCFO, CPU, float>;
template class Im2ColFunctor<ColFormat::kCFO, CPU, double>;
template class Col2ImFunctor<ColFormat::kCFO, CPU, float>;
template class Col2ImFunctor<ColFormat::kCFO, CPU, double>;
朔-望's avatar
朔-望 已提交
164

朔-望's avatar
朔-望 已提交
165 166 167 168 169 170
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
171 172 173
template <class T>
class Im2ColFunctor<ColFormat::kOCF, CPU, T> {
 public:
174 175 176 177 178 179 180 181 182 183 184 185
  void operator()(const framework::Tensor &im, const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *col) {
    //    PADDLE_ENFORCE(im.dims().size() == 3);
    //    PADDLE_ENFORCE(col->dims().size() == 5);
    int im_channels = im.dims()[0];
    int im_height = im.dims()[1];
    int im_width = im.dims()[2];
    int filter_height = col->dims()[3];
    int filter_width = col->dims()[4];
    int col_height = col->dims()[0];
    int col_width = col->dims()[1];
朔-望's avatar
朔-望 已提交
186

187 188 189 190 191 192 193 194 195 196 197 198
    //    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.");
朔-望's avatar
朔-望 已提交
199

200 201
    const T *im_data = im.data<T>();
    T *col_data = col->data<T>();
朔-望's avatar
朔-望 已提交
202

203 204 205 206 207 208 209 210 211 212 213
    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) {
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
朔-望's avatar
朔-望 已提交
214

215 216 217 218 219 220 221
              int col_offset =
                  ((((col_row_idx)*col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;
朔-望's avatar
朔-望 已提交
222

223 224 225 226 227 228 229
              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];
朔-望's avatar
朔-望 已提交
230
            }
231
          }
朔-望's avatar
朔-望 已提交
232
        }
233
      }
朔-望's avatar
朔-望 已提交
234
    }
235
  }
朔-望's avatar
朔-望 已提交
236
};
朔-望's avatar
朔-望 已提交
237

朔-望's avatar
朔-望 已提交
238 239 240 241 242 243
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
244 245 246
template <class T>
class Col2ImFunctor<ColFormat::kOCF, CPU, T> {
 public:
247 248 249 250 251 252 253 254 255 256 257 258 259
  void operator()(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);
    //    PADDLE_ENFORCE(col.dims().size() == 5);
    int im_channels = im->dims()[0];
    int im_height = im->dims()[1];
    int im_width = im->dims()[2];
    int filter_height = col.dims()[3];
    int filter_width = col.dims()[4];
    int col_height = col.dims()[0];
    int col_width = col.dims()[1];
朔-望's avatar
朔-望 已提交
260

261 262 263 264 265 266 267 268 269 270 271 272
    //    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.");
朔-望's avatar
朔-望 已提交
273

274 275
    T *im_data = im->data<T>();
    const T *col_data = col.data<T>();
朔-望's avatar
朔-望 已提交
276

277 278 279 280 281 282 283 284 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) {
          for (int filter_row_idx = 0; filter_row_idx < filter_height;
               ++filter_row_idx) {
            int im_row_offset =
                col_row_idx * stride[0] + filter_row_idx - padding[0];
            for (int filter_col_idx = 0; filter_col_idx < filter_width;
                 ++filter_col_idx) {
              int im_col_offset =
                  col_col_idx * stride[1] + filter_col_idx - padding[1];
朔-望's avatar
朔-望 已提交
288

289 290 291 292 293 294 295
              int col_offset =
                  (((col_row_idx * col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;
朔-望's avatar
朔-望 已提交
296

297 298 299 300 301 302 303
              if (im_row_offset >= 0 && im_row_offset < im_height &&
                  im_col_offset >= 0 && im_col_offset < im_width) {
                int im_offset =
                    (channel * im_height + im_row_offset) * im_width +
                    im_col_offset;
                im_data[im_offset] += col_data[col_offset];
              }
朔-望's avatar
朔-望 已提交
304
            }
305
          }
朔-望's avatar
朔-望 已提交
306
        }
307
      }
朔-望's avatar
朔-望 已提交
308
    }
309
  }
朔-望's avatar
朔-望 已提交
310
};
朔-望's avatar
朔-望 已提交
311

朔-望's avatar
朔-望 已提交
312 313 314 315
template class Im2ColFunctor<ColFormat::kOCF, CPU, float>;
template class Im2ColFunctor<ColFormat::kOCF, CPU, double>;
template class Col2ImFunctor<ColFormat::kOCF, CPU, float>;
template class Col2ImFunctor<ColFormat::kOCF, CPU, double>;
朔-望's avatar
朔-望 已提交
316

朔-望's avatar
朔-望 已提交
317 318 319
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile