op_im2col.hpp 11.8 KB
Newer Older
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#ifndef __OPENCV_DNN_LAYERS_IM2COL_HPP__
#define __OPENCV_DNN_LAYERS_IM2COL_HPP__
44
#include <opencv2/core.hpp>
45
#include <cstdlib>
46 47 48 49 50 51 52

namespace cv
{
namespace dnn
{

template <typename Dtype>
53
class im2col_CpuPBody : public cv::ParallelLoopBody
54
{
55 56 57 58 59
    const Dtype* data_im;
    int channels, height, width;
    int kernel_h, kernel_w;
    int pad_h, pad_w;
    int stride_h, stride_w;
60
    int dilation_h, dilation_w;
61 62 63
    Dtype* data_col;
    int height_col, width_col, channels_col;

64
    im2col_CpuPBody() {}
65 66 67 68 69 70 71
public:

    static void run(const Dtype* data_im,
                    int channels, int height, int width,
                    int kernel_h, int kernel_w,
                    int pad_h, int pad_w,
                    int stride_h, int stride_w,
72
                    int dilation_h, int dilation_w,
73
                    int height_col, int width_col,
74 75
                    Dtype* data_col)
    {
76
        im2col_CpuPBody<Dtype> t;
77

78 79 80 81 82 83
        t.data_im = data_im;
        t.data_col = data_col;
        t.channels = channels; t.height = height; t.width = width;
        t.kernel_h = kernel_h; t.kernel_w = kernel_w;
        t.pad_h = pad_h; t.pad_w = pad_w;
        t.stride_h = stride_h; t.stride_w = stride_w;
84 85
        t.dilation_h = dilation_h; t.dilation_w = dilation_w;

86 87
        t.height_col = height_col;
        t.width_col = width_col;
88 89 90
        t.channels_col = channels * kernel_h * kernel_w;

        cv::parallel_for_(Range(0, t.channels_col), t);
91 92 93 94
    }

    virtual void operator ()(const Range &r) const
    {
95 96
        for (int c = r.start; c < r.end; ++c)
        {
97 98 99
            int w_offset = c % kernel_w;
            int h_offset = (c / kernel_w) % kernel_h;
            int c_im = c / kernel_h / kernel_w;
100 101 102 103
            for (int h = 0; h < height_col; ++h)
            {
                for (int w = 0; w < width_col; ++w)
                {
104 105
                    int h_pad = h * stride_h - pad_h + h_offset * dilation_h;
                    int w_pad = w * stride_w - pad_w + w_offset * dilation_w;
106 107
                    if (h_pad >= 0 && h_pad < height && w_pad >= 0 && w_pad < width)
                        data_col[(c * height_col + h) * width_col + w] =
108
                            data_im[(c_im * height + h_pad) * width + w_pad];
109 110 111
                    else
                        data_col[(c * height_col + h) * width_col + w] = 0;
                }
112 113 114
            }
        }
    }
115
};
116

117 118 119 120 121 122 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
template <typename Dtype>
class im2row_CpuPBody : public cv::ParallelLoopBody
{
    const Dtype* data_im;
    int channels, height, width;
    int kernel_h, kernel_w;
    int pad_h, pad_w;
    int stride_h, stride_w;
    int dilation_h, dilation_w;
    Dtype* data_col;
    int height_col, width_col, channels_col;

    im2row_CpuPBody() {}
public:

    static void run(const Dtype* data_im,
                    int channels, int height, int width,
                    int kernel_h, int kernel_w,
                    int pad_h, int pad_w,
                    int stride_h, int stride_w,
                    int dilation_h, int dilation_w,
                    int height_col, int width_col,
                    Dtype* data_col)
    {
        im2row_CpuPBody<Dtype> t;

        t.data_im = data_im;
        t.data_col = data_col;
        t.channels = channels; t.height = height; t.width = width;
        t.kernel_h = kernel_h; t.kernel_w = kernel_w;
        t.pad_h = pad_h; t.pad_w = pad_w;
        t.stride_h = stride_h; t.stride_w = stride_w;
        t.dilation_h = dilation_h; t.dilation_w = dilation_w;

        t.height_col = height_col;
        t.width_col = width_col;
        t.channels_col = channels * kernel_h * kernel_w;

        cv::parallel_for_(Range(0, t.height_col*t.width_col), t, 16);
    }

    virtual void operator ()(const Range &r) const
    {
        int dh = dilation_h, dw = dilation_w;
        Dtype* data_col_ = data_col;
        const Dtype* data_im_ = data_im;

        for (int row = r.start; row < r.end; ++row)
        {
            int out_c = row % width_col;
            int out_r = row / width_col;
            int out_row_offset = row*kernel_h*kernel_w*channels;

            int start_in_r = out_r * stride_h - pad_h;
            int start_in_c = out_c * stride_w - pad_w;
            int start_k_r = std::max(0, cvCeil(-start_in_r/(float)dilation_h));
            int end_k_r = std::min(kernel_h, cvCeil((height - start_in_r)/(float)dilation_h));
            int start_k_c = std::max(0, cvCeil(-start_in_c/(float)dilation_w));
            int end_k_c = std::min(kernel_w, cvCeil((width - start_in_c)/(float)dilation_w));

            for(int i_c = 0; i_c < channels; i_c++)
            {
                int channels_offset = i_c * width * height;
                int out_ch_offset = i_c*kernel_h*kernel_w;
                int in_r = start_in_r + start_k_r*dilation_h;

                for(int k_r = start_k_r; k_r < end_k_r; k_r++, in_r += dh)
                {
                    int row_offset = in_r*width;
                    int out_col_offset = k_r*kernel_w;
                    int in_c = start_in_c + start_k_c*dilation_w;

                    for(int k_c = start_k_c; k_c < end_k_c; k_c++, in_c += dw)
                    {
                        int in_index = channels_offset + row_offset + in_c;

                        int out_index = out_row_offset + out_ch_offset + out_col_offset + k_c;

                        data_col_[out_index] = data_im_[in_index];
                    }
                }
            }
        }
    }
};

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
template <typename Dtype>
class col2im_CpuPBody : public cv::ParallelLoopBody
{
    const Dtype* data_col;
    int channels, height, width;
    int kernel_h, kernel_w;
    int pad_h, pad_w;
    int stride_h, stride_w;
    Dtype* data_im;
    int height_col, width_col;

    col2im_CpuPBody() {}

public:

    static void run(const Dtype* data_col,
                    int channels, int height, int width,
                    int kernel_h, int kernel_w,
                    int pad_h, int pad_w,
                    int stride_h, int stride_w,
                    Dtype* data_im)
    {
        //TODO: single-threaded version switch

        col2im_CpuPBody t;
        t.data_col = data_col;
        t.data_im = data_im;
        t.channels = channels; t.height = height; t.width = width;
        t.kernel_h = kernel_h; t.kernel_w = kernel_w;
        t.pad_h = pad_h; t.pad_w = pad_w;
        t.stride_h = stride_h; t.stride_w = stride_w;
        t.height_col = (height + 2 * pad_h - kernel_h) / stride_h + 1;
        t.width_col = (width + 2 * pad_w - kernel_w) / stride_w + 1;
        int img_total = channels * height * width;

        cv::parallel_for_(Range(0, img_total), t);
    }

    virtual void operator ()(const Range &r) const
    {
243 244 245 246
        const Dtype* data_col_ = data_col;
        Dtype* data_im_ = data_im;
        int coeff_h_col = (1 - stride_h * kernel_w * height_col) * width_col;
        int coeff_w_col = (1 - stride_w * height_col * width_col);
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        for (int index = r.start; index < r.end; index++)
        {
            Dtype val = 0;
            int w = index % width + pad_w;
            int h = (index / width) % height + pad_h;
            int c = index / (width * height);

            // compute the start and end of the output
            int w_col_start = (w < kernel_w) ? 0 : (w - kernel_w) / stride_w + 1;
            int w_col_end = std::min(w / stride_w + 1, width_col);
            int h_col_start = (h < kernel_h) ? 0 : (h - kernel_h) / stride_h + 1;
            int h_col_end = std::min(h / stride_h + 1, height_col);

            // equivalent implementation
            int offset =
            (c * kernel_h * kernel_w + h * kernel_w + w) * height_col * width_col;
263

264 265
            for (int h_col = h_col_start; h_col < h_col_end; ++h_col) {
              for (int w_col = w_col_start; w_col < w_col_end; ++w_col) {
266
                val += data_col_[offset + h_col * coeff_h_col + w_col * coeff_w_col];
267 268
              }
            }
269
            data_im_[index] = val;
270 271 272 273 274
        }
    }
};

//single-threaded version
275 276 277
template <typename Dtype>
void col2im_cpu(const Dtype* data_col,
                int channels, int height, int width,
278
                int kernel_h, int kernel_w,
279 280
                int pad_h, int pad_w,
                int stride_h, int stride_w,
281
                int dilation_h, int dilation_w,
282 283
                Dtype* data_im)
{
284 285
    int height_col = (height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
    int width_col = (width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
286
    int channels_col = channels * kernel_h * kernel_w;
287

288
    std::memset(data_im, 0, height * width * channels * sizeof(Dtype));
289 290 291

    for (int c = 0; c < channels_col; ++c)
    {
292 293 294
        int w_offset = c % kernel_w;
        int h_offset = (c / kernel_w) % kernel_h;
        int c_im = c / kernel_h / kernel_w;
295 296 297 298 299

        for (int h = 0; h < height_col; ++h)
        {
            for (int w = 0; w < width_col; ++w)
            {
300 301
                int h_pad = h * stride_h - pad_h + h_offset * dilation_h;
                int w_pad = w * stride_w - pad_w + w_offset * dilation_w;
302 303 304

                if (h_pad >= 0 && h_pad < height && w_pad >= 0 && w_pad < width)
                    data_im[(c_im * height + h_pad) * width + w_pad] +=
305
                        data_col[(c * height_col + h) * width_col + w];
306 307 308 309 310
            }
        }
    }
}

311
#ifdef HAVE_OPENCL
312
bool im2col_ocl(const UMat &img,
313 314 315 316
                int channels, int height, int width,
                int kernel_h, int kernel_w,
                int pad_h, int pad_w,
                int stride_h, int stride_w,
317
                int dilation_h, int dilation_w,
318
                UMat &col);
319 320 321 322 323 324 325

bool col2im_ocl(const UMat &col,
                int channels, int height, int width,
                int kernel_h, int kernel_w,
                int pad_h, int pad_w,
                int stride_h, int stride_w,
                UMat &img);
326
#endif
327 328 329 330 331

}
}

#endif