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