Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/pooling.cl
16337 views
1
/*************************************************************************************
2
* Copyright (c) 2015, Advanced Micro Devices, Inc.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without modification,
6
* are permitted provided that the following conditions are met:
7
*
8
* 1. Redistributions of source code must retain the above copyright notice, this
9
* list of conditions and the following disclaimer.
10
*
11
* 2. Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation and/or
13
* other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
* POSSIBILITY OF SUCH DAMAGE.
25
**************************************************************************************/
26
27
__kernel void MaxPoolForward(const int nthreads,
28
__global T* bottom_data, const int num, const int channels, const int height, const int width,
29
const int pooled_height, const int pooled_width, const int kernel_h, const int kernel_w,
30
const int stride_h, const int stride_w, const int pad_t, const int pad_l, const int pad_b, const int pad_r,
31
__global T* top_data
32
#ifdef MASK
33
, __global float* mask
34
#endif
35
)
36
{
37
int index = get_global_id(0);
38
int tmp = get_global_size(0);
39
for(index; index < nthreads; index += tmp) {
40
int pw = index % pooled_width;
41
int ph = (index / pooled_width) % pooled_height;
42
int c = (index / pooled_width / pooled_height) % channels;
43
int n = index / pooled_width / pooled_height / channels;
44
int hstart = ph * stride_h - pad_t;
45
int wstart = pw * stride_w - pad_l;
46
const int hend = min(hstart + kernel_h, height);
47
const int wend = min(wstart + kernel_w, width);
48
hstart = max(hstart, 0);
49
wstart = max(wstart, 0);
50
T maxval = -FLT_MAX;
51
int maxidx = -1;
52
bottom_data =
53
bottom_data + (n * channels + c) * height * width;
54
for (int h = hstart; h < hend; ++h) {
55
for (int w = wstart; w < wend; ++w) {
56
if (bottom_data[h * width + w] > maxval) {
57
maxidx = h * width + w;
58
maxval = bottom_data[maxidx];
59
}
60
}
61
}
62
63
top_data[index] = maxval;
64
65
#ifdef MASK
66
mask[index] = maxidx;
67
#endif
68
}
69
}
70
71
__kernel void AvePoolForward(const int nthreads,
72
__global T* bottom_data, const int num, const int channels, const int height, const int width,
73
const int pooled_height, const int pooled_width, const int kernel_h, const int kernel_w,
74
const int stride_h, const int stride_w, const int pad_t, const int pad_l, const int pad_b, const int pad_r,
75
__global T* top_data
76
#ifdef MASK
77
, __global float* mask // NOT USED
78
#endif
79
)
80
{
81
int index = get_global_id(0);
82
int tmp = get_global_size(0);
83
for(index; index < nthreads; index+=tmp) {
84
int pw = index % pooled_width;
85
int ph = (index / pooled_width) % pooled_height;
86
int c = (index / pooled_width / pooled_height) % channels;
87
int n = index / pooled_width / pooled_height / channels; int hstart = ph * stride_h - pad_t; int wstart = pw * stride_w - pad_l;
88
int hend = min(hstart + kernel_h, height + pad_b);
89
int wend = min(wstart + kernel_w, width + pad_r);
90
const int pool_size = (hend - hstart) * (wend - wstart);
91
hstart = max(hstart, 0);
92
wstart = max(wstart, 0);
93
hend = min(hend, height);
94
wend = min(wend, width);
95
T aveval = 0;
96
bottom_data =
97
bottom_data + (n * channels + c) * height * width;
98
for (int h = hstart; h < hend; ++h) {
99
for (int w = wstart; w < wend; ++w) {
100
aveval += bottom_data[h * width + w];
101
}
102
}
103
top_data[index] = aveval / pool_size;
104
}
105
106
}
107
108