im2col.cc 11.8 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

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

#include "im2col.h"
#include "common/types.h"

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

朔-望's avatar
朔-望 已提交
22 23 24 25 26 27
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
朔-望's avatar
朔-望 已提交
28 29 30
template <class T>
class Im2ColFunctor<ColFormat::kCFO, CPU, T> {
 public:
31 32 33 34 35
  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
朔-望 已提交
36

37 38 39 40 41 42 43
    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
朔-望 已提交
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    //    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
朔-望 已提交
65

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

68 69 70 71 72 73 74 75 76 77 78 79
    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
朔-望 已提交
80

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

朔-望's avatar
朔-望 已提交
91 92 93 94 95 96
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
朔-望's avatar
朔-望 已提交
97 98 99
template <class T>
class Col2ImFunctor<ColFormat::kCFO, CPU, T> {
 public:
100 101 102 103 104 105 106 107 108 109 110 111 112
  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
朔-望 已提交
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    //    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
朔-望 已提交
134

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

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

140 141 142 143 144 145 146 147 148 149 150 151 152
    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
朔-望 已提交
153
        }
154
      }
朔-望's avatar
朔-望 已提交
155
    }
156
  }
朔-望's avatar
朔-望 已提交
157
};
朔-望's avatar
朔-望 已提交
158

朔-望's avatar
朔-望 已提交
159 160 161 162
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
朔-望 已提交
163

朔-望's avatar
朔-望 已提交
164 165 166 167 168 169
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
170 171 172
template <class T>
class Im2ColFunctor<ColFormat::kOCF, CPU, T> {
 public:
173 174 175 176 177 178 179 180 181 182 183 184
  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
朔-望 已提交
185

186 187 188 189 190 191 192 193 194 195 196 197
    //    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
朔-望 已提交
198

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

202 203 204 205 206 207 208 209 210 211 212
    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
朔-望 已提交
213

214 215 216 217 218 219 220
              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
朔-望 已提交
221

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

朔-望's avatar
朔-望 已提交
237 238 239 240 241 242
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
243 244 245
template <class T>
class Col2ImFunctor<ColFormat::kOCF, CPU, T> {
 public:
246 247 248 249 250 251 252 253 254 255 256 257 258
  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
朔-望 已提交
259

260 261 262 263 264 265 266 267 268 269 270 271
    //    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
朔-望 已提交
272

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

276 277 278 279 280 281 282 283 284 285 286
    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
朔-望 已提交
287

288 289 290 291 292 293 294
              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
朔-望 已提交
295

296 297 298 299 300 301 302
              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
朔-望 已提交
303
            }
304
          }
朔-望's avatar
朔-望 已提交
305
        }
306
      }
朔-望's avatar
朔-望 已提交
307
    }
308
  }
朔-望's avatar
朔-望 已提交
309
};
朔-望's avatar
朔-望 已提交
310

朔-望's avatar
朔-望 已提交
311 312 313 314
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
朔-望 已提交
315

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