im2col.cc 11.4 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* 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 {
namespace operators {
namespace math {

/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
 */
L
liuruilong 已提交
27 28 29 30 31
template <class T> class Im2ColFunctor<ColFormat::kCFO, CPU, T> {
public:
  void operator()(const framework::Tensor &im, const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *col) {
朔-望's avatar
朔-望 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    //    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];

    //    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.");

    int channels_col = im_channels * filter_height * filter_width;

L
liuruilong 已提交
60 61
    const T *im_data = im.data<T>();
    T *col_data = col->data<T>();
朔-望's avatar
朔-望 已提交
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
    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;

          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];
        }
      }
    }
  }
};

/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height, output_width]
 */
L
liuruilong 已提交
88 89 90 91 92 93
template <class T> class Col2ImFunctor<ColFormat::kCFO, CPU, T> {
public:
  void operator()(const framework::Tensor &col,
                  const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *im) {
朔-望's avatar
朔-望 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    //    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];

    //    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.");

    int channels_col = im_channels * filter_height * filter_width;

L
liuruilong 已提交
121 122
    T *im_data = im->data<T>();
    const T *col_data = col.data<T>();
朔-望's avatar
朔-望 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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];
          }
        }
      }
    }
  }
};

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>;

/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
 */
L
liuruilong 已提交
153 154 155 156 157
template <class T> class Im2ColFunctor<ColFormat::kOCF, CPU, T> {
public:
  void operator()(const framework::Tensor &im, const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *col) {
朔-望's avatar
朔-望 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    //    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];

    //    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.");

L
liuruilong 已提交
177 178
    const T *im_data = im.data<T>();
    T *col_data = col->data<T>();
朔-望's avatar
朔-望 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

    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];

              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];
            }
          }
        }
      }
    }
  }
};

/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height, filter_width]
 */
L
liuruilong 已提交
220 221 222 223 224 225
template <class T> class Col2ImFunctor<ColFormat::kOCF, CPU, T> {
public:
  void operator()(const framework::Tensor &col,
                  const std::vector<int> &dilation,
                  const std::vector<int> &stride,
                  const std::vector<int> &padding, framework::Tensor *im) {
朔-望's avatar
朔-望 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    //    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];

    //    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.");

L
liuruilong 已提交
245 246
    T *im_data = im->data<T>();
    const T *col_data = col.data<T>();
朔-望's avatar
朔-望 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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];

              int col_offset =
                  (((col_row_idx * col_width + col_col_idx) * im_channels +
                    channel) *
                       filter_height +
                   filter_row_idx) *
                      filter_width +
                  filter_col_idx;

              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];
              }
            }
          }
        }
      }
    }
  }
};

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>;

L
liuruilong 已提交
288 289 290
} // namespace math
} // namespace operators
} // namespace paddle_mobile