Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / cldnn_engine / cldnn_global_custom_kernels / prior_box_clustered.cl
1 // Copyright (C) 2018-2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma OPENCL EXTENSION cl_khr_fp16 : enable
16
17 __kernel void prior_box_clustered(
18     const __global INPUT0_TYPE*  input0,
19     const __global INPUT0_TYPE*  input1,
20     __global OUTPUT0_TYPE* output)
21 {
22     const int num_priors_ = sizeof(width_)/sizeof(width_[0]);
23     const int var_size = sizeof(variance_)/sizeof(variance_[0]);
24
25     const float img_width  = (img_w_ == 0) ? INPUT1_DIMS[3] : img_w_;
26     const float img_height = (img_h_ == 0) ? INPUT1_DIMS[2] : img_h_;
27
28     const float r_img_width  = 1.f/img_width;
29     const float r_img_height = 1.f/img_height;
30
31     float step_w = (step_w_ == 0) ? step_ : step_w_;
32     float step_h = (step_h_ == 0) ? step_ : step_h_;
33
34     if ((step_w == 0) & (step_h == 0))
35     {
36         step_w = img_width / INPUT0_DIMS[3];
37         step_h = img_height / INPUT0_DIMS[2];
38     }
39
40     int h = get_global_id(0);
41     int w = get_global_id(1);
42
43     __global OUTPUT0_TYPE* top_data = output + h*INPUT0_DIMS[3]*num_priors_*4 + w*num_priors_*4;
44     __global OUTPUT0_TYPE* top_data_var = output + OUTPUT0_DIMS[2] + h*INPUT0_DIMS[3]*num_priors_*var_size + w * num_priors_ * var_size;
45
46     const float center_x = (w + offset_) * step_w;
47     const float center_y = (h + offset_) * step_h;
48
49     int idx = 0;
50     for (int s = 0; s < num_priors_; ++s)
51     {
52         const float box_width = width_[s];
53         const float box_height = height_[s];
54
55         OUTPUT0_TYPE xmin = (center_x - box_width*0.5f)  * r_img_width;
56         OUTPUT0_TYPE ymin = (center_y - box_height*0.5f) * r_img_height;
57         OUTPUT0_TYPE xmax = (center_x + box_width*0.5f)  * r_img_width;
58         OUTPUT0_TYPE ymax = (center_y + box_height*0.5f) * r_img_height;
59
60         if (clip_)
61         {
62             xmin = min(max(xmin, (OUTPUT0_TYPE)(0.0f)), (OUTPUT0_TYPE)(1.0f));
63             ymin = min(max(ymin, (OUTPUT0_TYPE)(0.0f)), (OUTPUT0_TYPE)(1.0f));
64             xmax = min(max(xmax, (OUTPUT0_TYPE)(0.0f)), (OUTPUT0_TYPE)(1.0f));
65             ymax = min(max(ymax, (OUTPUT0_TYPE)(0.0f)), (OUTPUT0_TYPE)(1.0f));
66         }
67
68         top_data[idx++] = xmin;
69         top_data[idx++] = ymin;
70         top_data[idx++] = xmax;
71         top_data[idx++] = ymax;
72
73         for (int i = 0; i < var_size; i++)
74         {
75             top_data_var[s * var_size + i] = variance_[i];
76         }
77     }
78 }