Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/lrn.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 LRNComputeOutput(const int nthreads, __global T* in, __global T* scale, const T negative_beta, __global T* out) {
28
int index = get_global_id(0);
29
int tmp = get_global_size(0);
30
for(index; index < nthreads; index += tmp)
31
out[index] = in[index] * pow(scale[index], negative_beta);
32
}
33
34
__kernel void LRNFillScale(const int nthreads, __global T* in, const int num, const int channels, const int height, const int width, const int size, const T alpha_over_size, const T k, __global T* scale) {
35
int index = get_global_id(0);
36
int tmp = get_global_size(0);
37
for(index; index < nthreads; index += tmp) {
38
// find out the local offset
39
const int w = index % width;
40
const int h = (index / width) % height;
41
const int n = index / width / height;
42
const int offset = (n * channels * height + h) * width + w;
43
const int step = height * width;
44
in = in + offset;
45
scale = scale + offset;
46
int head = 0;
47
const int pre_pad = (size - 1) / 2;
48
const int post_pad = size - pre_pad - 1;
49
T accum_scale = 0;
50
// fill the scale at [n, :, h, w]
51
// accumulate values
52
while (head < post_pad && head < channels) {
53
accum_scale += in[head * step] * in[head * step];
54
++head;
55
}
56
// both add and subtract
57
while (head < channels) {
58
accum_scale += in[head * step] * in[head * step];
59
if (head - size >= 0) {
60
accum_scale -= in[(head - size) * step]
61
* in[(head - size) * step];
62
}
63
scale[(head - post_pad) * step] = k + accum_scale * alpha_over_size;
64
++head;
65
}
66
// subtract only
67
while (head < channels + post_pad) {
68
if (head - size >= 0) {
69
accum_scale -= in[(head - size) * step]
70
* in[(head - size) * step];
71
}
72
scale[(head - post_pad) * step] = k + accum_scale * alpha_over_size;
73
++head;
74
}
75
}
76
}
77