im2col.cc 13.7 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 31 32 33 34 35
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) {
        //    PADDLE_ENFORCE(im.dims().size() == 3);
        //    PADDLE_ENFORCE(col->dims().size() == 5);
朔-望's avatar
朔-望 已提交
36

朔-望's avatar
朔-望 已提交
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

朔-望's avatar
朔-望 已提交
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

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

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

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

朔-望's avatar
朔-望 已提交
95 96 97 98 99 100
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [input_channels, filter_height, filter_width, output_height,
 * output_width]
 */
朔-望's avatar
朔-望 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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) {
        //    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
朔-望 已提交
116

朔-望's avatar
朔-望 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        //    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
朔-望 已提交
137

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

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

朔-望's avatar
朔-望 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        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
朔-望 已提交
161 162 163
        }
    }
};
朔-望's avatar
朔-望 已提交
164

朔-望's avatar
朔-望 已提交
165 166 167 168
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
朔-望 已提交
169

朔-望's avatar
朔-望 已提交
170 171 172 173 174 175
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
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) {
        //    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
朔-望 已提交
191

朔-望's avatar
朔-望 已提交
192 193 194 195 196 197 198 199 200 201 202 203
        //    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
朔-望 已提交
204

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

朔-望's avatar
朔-望 已提交
208 209 210 211 212 213 214 215 216 217 218
        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
朔-望 已提交
219

朔-望's avatar
朔-望 已提交
220 221 222 223 224 225 226 227
                            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
朔-望 已提交
228

朔-望's avatar
朔-望 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241
                            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
朔-望 已提交
242 243 244 245
            }
        }
    }
};
朔-望's avatar
朔-望 已提交
246

朔-望's avatar
朔-望 已提交
247 248 249 250 251 252
/*
 * im = [input_channels, input_height, input_width]
 * col =
 *   [output_height, output_width, input_channels, filter_height,
 * filter_width]
 */
朔-望's avatar
朔-望 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
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) {
        //    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
朔-望 已提交
268

朔-望's avatar
朔-望 已提交
269 270 271 272 273 274 275 276 277 278 279 280
        //    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
朔-望 已提交
281

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

朔-望's avatar
朔-望 已提交
285 286 287 288 289 290 291 292 293 294 295
        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
朔-望 已提交
296

朔-望's avatar
朔-望 已提交
297 298 299 300 301 302 303 304
                            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
朔-望 已提交
305

朔-望's avatar
朔-望 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318
                            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
朔-望 已提交
319 320 321 322
            }
        }
    }
};
朔-望's avatar
朔-望 已提交
323

朔-望's avatar
朔-望 已提交
324 325 326 327
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
朔-望 已提交
328

朔-望's avatar
朔-望 已提交
329
} // namespace math
朔-望's avatar
朔-望 已提交
330
} // namespace operators
L
liuruilong 已提交
331
} // namespace paddle_mobile