Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/detection_output.cl
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (c) 2016-2017 Fabian David Tschopp, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#define Dtype float
43
#define Dtype4 float4
44
45
__kernel void DecodeBBoxesCORNER(const int nthreads,
46
__global const Dtype* loc_data,
47
__global const Dtype* prior_data,
48
const int variance_encoded_in_target,
49
const int num_priors,
50
const int share_location,
51
const int num_loc_classes,
52
const int background_label_id,
53
const int clip_bbox,
54
const int locPredTransposed,
55
__global Dtype* bbox_data)
56
{
57
for (int index = get_global_id(0); index < nthreads; index += get_global_size(0))
58
{
59
Dtype bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax;
60
const int i = index % 4;
61
const int p = ((index / 4 / num_loc_classes) % num_priors) * 4;
62
63
const int c = (index / 4) % num_loc_classes;
64
int label = share_location ? -1 : c;
65
if (label == background_label_id)
66
return; // Ignore background class.
67
68
Dtype4 loc_vec = vload4(0, loc_data + index - i);
69
Dtype4 bbox_vec, prior_variance;
70
if (variance_encoded_in_target)
71
{
72
bbox_vec = loc_vec;
73
} else {
74
const int start_index = num_priors * 4 + p;
75
prior_variance = vload4(0, prior_data + start_index);
76
bbox_vec = loc_vec * prior_variance;
77
}
78
79
if (locPredTransposed)
80
{
81
bbox_ymin = bbox_vec.x;
82
bbox_xmin = bbox_vec.y;
83
bbox_ymax = bbox_vec.z;
84
bbox_xmax = bbox_vec.w;
85
} else {
86
bbox_xmin = bbox_vec.x;
87
bbox_ymin = bbox_vec.y;
88
bbox_xmax = bbox_vec.z;
89
bbox_ymax = bbox_vec.w;
90
}
91
92
Dtype4 prior_vec = vload4(0, prior_data + p);
93
Dtype val;
94
switch (i)
95
{
96
case 0:
97
val = prior_vec.x + bbox_xmin;
98
break;
99
case 1:
100
val = prior_vec.y + bbox_ymin;
101
break;
102
case 2:
103
val = prior_vec.z + bbox_xmax;
104
break;
105
case 3:
106
val = prior_vec.w + bbox_ymax;
107
break;
108
}
109
110
if (clip_bbox)
111
val = max(min(val, (Dtype)1.), (Dtype)0.);
112
113
bbox_data[index] = val;
114
}
115
}
116
117
__kernel void DecodeBBoxesCENTER_SIZE(const int nthreads,
118
__global const Dtype* loc_data,
119
__global const Dtype* prior_data,
120
const int variance_encoded_in_target,
121
const int num_priors,
122
const int share_location,
123
const int num_loc_classes,
124
const int background_label_id,
125
const int clip_bbox,
126
const int locPredTransposed,
127
__global Dtype* bbox_data)
128
{
129
for (int index = get_global_id(0); index < nthreads; index += get_global_size(0))
130
{
131
Dtype bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax;
132
const int i = index % 4;
133
const int p = ((index / 4 / num_loc_classes) % num_priors) * 4;
134
135
const int c = (index / 4) % num_loc_classes;
136
int label = share_location ? -1 : c;
137
if (label == background_label_id)
138
return; // Ignore background class.
139
140
Dtype4 loc_vec = vload4(0, loc_data + index - i);
141
Dtype4 bbox_vec, prior_variance;
142
if (variance_encoded_in_target)
143
{
144
bbox_vec = loc_vec;
145
} else {
146
const int start_index = num_priors * 4 + p;
147
prior_variance = vload4(0, prior_data + start_index);
148
bbox_vec = loc_vec * prior_variance;
149
}
150
151
if (locPredTransposed)
152
{
153
bbox_ymin = bbox_vec.x;
154
bbox_xmin = bbox_vec.y;
155
bbox_ymax = bbox_vec.z;
156
bbox_xmax = bbox_vec.w;
157
} else {
158
bbox_xmin = bbox_vec.x;
159
bbox_ymin = bbox_vec.y;
160
bbox_xmax = bbox_vec.z;
161
bbox_ymax = bbox_vec.w;
162
}
163
164
Dtype4 prior_vec = vload4(0, prior_data + p);
165
Dtype prior_width = prior_vec.z - prior_vec.x;
166
Dtype prior_height = prior_vec.w - prior_vec.y;
167
Dtype prior_center_x = (prior_vec.x + prior_vec.z) * .5;
168
Dtype prior_center_y = (prior_vec.y + prior_vec.w) * .5;
169
170
Dtype decode_bbox_center_x, decode_bbox_center_y;
171
Dtype decode_bbox_width, decode_bbox_height;
172
decode_bbox_center_x = bbox_xmin * prior_width + prior_center_x;
173
decode_bbox_center_y = bbox_ymin * prior_height + prior_center_y;
174
decode_bbox_width = exp(bbox_xmax) * prior_width;
175
decode_bbox_height = exp(bbox_ymax) * prior_height;
176
177
Dtype val;
178
switch (i)
179
{
180
case 0:
181
val = decode_bbox_center_x - decode_bbox_width * .5;
182
break;
183
case 1:
184
val = decode_bbox_center_y - decode_bbox_height * .5;
185
break;
186
case 2:
187
val = decode_bbox_center_x + decode_bbox_width * .5;
188
break;
189
case 3:
190
val = decode_bbox_center_y + decode_bbox_height * .5;
191
break;
192
}
193
194
if (clip_bbox)
195
val = max(min(val, (Dtype)1.), (Dtype)0.);
196
197
bbox_data[index] = val;
198
}
199
}
200
201