clDNN
prior_box.hpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
18 #pragma once
19 
20 #include <cmath>
21 
22 #include "../C/prior_box.h"
23 #include "primitive.hpp"
24 
25 namespace cldnn
26 {
33 
38 struct prior_box : public primitive_base<prior_box, CLDNN_PRIMITIVE_DESC(prior_box)>
39 {
40  CLDNN_DECLATE_PRIMITIVE(prior_box)
41 
42 
43  prior_box(
56  const primitive_id& id,
57  const primitive_id& input,
58  const tensor& img_size,
59  const std::vector<float>& min_sizes,
60  const std::vector<float>& max_sizes = {},
61  const std::vector<float>& aspect_ratios = {},
62  const bool flip = true,
63  const bool clip = false,
64  const std::vector<float>& variance = {},
65  const float step_width = 0.f,
66  const float step_height = 0.f,
67  const float offset = 0.5f,
68  const padding& output_padding = padding()
69  )
74  , flip(flip)
75  , clip(clip)
78  , offset(offset)
79  {
80  this->aspect_ratios.push_back(1.f);
81  for (auto new_aspect_ratio : aspect_ratios) {
82  bool already_exist = false;
83  for (auto aspect_ratio : this->aspect_ratios) {
84  if (std::fabs(new_aspect_ratio - aspect_ratio) < 1e-6) {
85  already_exist = true;
86  break;
87  }
88  }
89  if (!already_exist) {
90  this->aspect_ratios.push_back(new_aspect_ratio);
91  if (flip) {
92  this->aspect_ratios.push_back(1.f / new_aspect_ratio);
93  }
94  }
95  }
96  if (variance.size() > 1) {
97  for (size_t i = 0; i < variance.size(); ++i) {
98  this->variance.push_back(variance[i]);
99  }
100  }
101  else if (variance.size() == 1) {
102  this->variance.push_back(variance[0]);
103  }
104  else {
105  // Set default to 0.1.
106  this->variance.push_back(0.1f);
107  }
108  }
109 
111  prior_box(const dto* dto)
113  , img_size(dto->img_size)
114  , min_sizes(float_arr_to_vector(dto->min_sizes))
115  , max_sizes(float_arr_to_vector(dto->max_sizes))
116  , aspect_ratios(float_arr_to_vector(dto->aspect_ratios))
117  , flip(dto->flip != 0)
118  , clip(dto->clip != 0)
119  , variance(float_arr_to_vector(dto->variance))
122  , offset(dto->offset)
123  {}
124 
128  std::vector<float> min_sizes;
130  std::vector<float> max_sizes;
132  std::vector<float> aspect_ratios;
134  bool flip;
136  bool clip;
138  std::vector<float> variance;
140  float step_width;
142  float step_height;
144  float offset;
145 
146 private:
147  void update_dto(dto& dto) const override
148  {
150  dto.min_sizes = float_vector_to_arr(min_sizes);
151  dto.max_sizes = float_vector_to_arr(max_sizes);
152  dto.aspect_ratios = float_vector_to_arr(aspect_ratios);
153  dto.flip = flip;
154  dto.clip = clip;
155  dto.variance = float_vector_to_arr(variance);
158  dto.offset = offset;
159  }
160 
161  static cldnn_float_arr float_vector_to_arr(const std::vector<float>& stor)
162  {
163  return{ stor.data(), stor.size() };
164  }
165 
166  static std::vector<float> float_arr_to_vector(const cldnn_float_arr& arr)
167  {
168  std::vector<float> result(arr.size);
169  for (size_t i = 0; i < arr.size; i++)
170  {
171  result[i] = arr.data[i];
172  }
173  return result;
174  }
175 };
179 }
prior_box(const primitive_id &id, const primitive_id &input, const tensor &img_size, const std::vector< float > &min_sizes, const std::vector< float > &max_sizes={}, const std::vector< float > &aspect_ratios={}, const bool flip=true, const bool clip=false, const std::vector< float > &variance={}, const float step_width=0.f, const float step_height=0.f, const float offset=0.5f, const padding &output_padding=padding())
Constructs prior-box primitive.
Definition: prior_box.hpp:55
cldnn_float_arr variance
Variance for adjusting the prior boxes.
Definition: prior_box.h:51
Represents data padding information.
Definition: layout.hpp:125
Generates a set of default bounding boxes with different sizes and aspect ratios. ...
Definition: prior_box.h:37
const float * data
Pointer to float array.
Definition: cldnn.h:310
cldnn_float_arr min_sizes
Minimum box sizes in pixels.
Definition: prior_box.h:41
prior_box(const dto *dto)
Constructs a copy from C API cldnn_prior-box_desc.
Definition: prior_box.hpp:111
std::vector< float > min_sizes
Minimum box sizes in pixels.
Definition: prior_box.hpp:128
Represents reference to an array of floats.
Definition: cldnn.h:308
std::vector< float > variance
Variance for adjusting the prior boxes.
Definition: prior_box.hpp:138
N-dimensional vector. Mostly used to represent memory size.
Definition: tensor.hpp:256
uint32_t flip
If not 0, will flip each aspect ratio. For example, if there is aspect ratio "r", aspect ratio "1...
Definition: prior_box.h:47
float step_width
Step width.
Definition: prior_box.hpp:140
size_t size
Size (in floats) of the array.
Definition: cldnn.h:311
std::vector< float > max_sizes
Maximum box sizes in pixels.
Definition: prior_box.hpp:130
std::vector< float > aspect_ratios
Various of aspect ratios. Duplicate ratios will be ignored.
Definition: prior_box.hpp:132
float step_height
Step height.
Definition: prior_box.h:55
Generates a set of default bounding boxes with different sizes and aspect ratios. ...
Definition: prior_box.hpp:38
bool clip
If true, will clip the prior so that it is within [0, 1].
Definition: prior_box.hpp:136
cldnn_float_arr aspect_ratios
Various of aspect ratios. Duplicate ratios will be ignored.
Definition: prior_box.h:45
float step_width
Step width.
Definition: prior_box.h:53
cldnn_float_arr max_sizes
Maximum box sizes in pixels.
Definition: prior_box.h:43
float offset
Offset to the top left corner of each cell.
Definition: prior_box.hpp:144
std::string primitive_id
Unique id of a primitive within a topology.
Definition: primitive.hpp:42
float step_height
Step height.
Definition: prior_box.hpp:142
fixed_size_vector_ref input
List of ids of input primitives.
Definition: primitive.hpp:146
uint32_t clip
If not 0, will clip the prior so that it is within [0, 1].
Definition: prior_box.h:49
base class for all primitives implementations.
Definition: primitive.hpp:190
padding output_padding
Requested output padding.
Definition: primitive.hpp:149
bool flip
If true, will flip each aspect ratio. For example, if there is aspect ratio "r", aspect ratio "1...
Definition: prior_box.hpp:134
cldnn_tensor img_size
Image width and height.
Definition: prior_box.h:39
tensor img_size
Image width and height.
Definition: prior_box.hpp:126
float offset
Offset to the top left corner of each cell.
Definition: prior_box.h:57