| 37 | { |
| 38 | template <typename T> |
| 39 | SimpleTensor<T> prior_box_layer(const SimpleTensor<T> &src1, |
| 40 | const SimpleTensor<T> &src2, |
| 41 | const PriorBoxLayerInfo &info, |
| 42 | const TensorShape &output_shape) |
| 43 | { |
| 44 | const auto layer_width = static_cast<int>(src1.shape()[0]); |
| 45 | const auto layer_height = static_cast<int>(src1.shape()[1]); |
| 46 | |
| 47 | int img_width = info.img_size().x; |
| 48 | int img_height = info.img_size().y; |
| 49 | if (img_width == 0 || img_height == 0) |
| 50 | { |
| 51 | img_width = static_cast<int>(src2.shape()[0]); |
| 52 | img_height = static_cast<int>(src2.shape()[1]); |
| 53 | } |
| 54 | |
| 55 | float step_x = info.steps()[0]; |
| 56 | float step_y = info.steps()[1]; |
| 57 | if (step_x == 0.f || step_y == 0.f) |
| 58 | { |
| 59 | step_x = static_cast<float>(img_width) / layer_width; |
| 60 | step_x = static_cast<float>(img_height) / layer_height; |
| 61 | } |
| 62 | |
| 63 | // Calculate number of aspect ratios |
| 64 | const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
| 65 | const int total_elements = layer_width * layer_height * num_priors * 4; |
| 66 | |
| 67 | SimpleTensor<T> result(output_shape, src1.data_type()); |
| 68 | |
| 69 | int idx = 0; |
| 70 | for (int y = 0; y < layer_height; ++y) |
| 71 | { |
| 72 | for (int x = 0; x < layer_width; ++x) |
| 73 | { |
| 74 | const float center_x = (x + info.offset()) * step_x; |
| 75 | const float center_y = (y + info.offset()) * step_y; |
| 76 | float box_width; |
| 77 | float box_height; |
| 78 | for (unsigned int i = 0; i < info.min_sizes().size(); ++i) |
| 79 | { |
| 80 | const float min_size = info.min_sizes().at(i); |
| 81 | box_width = min_size; |
| 82 | box_height = min_size; |
| 83 | // (xmin, ymin, xmax, ymax) |
| 84 | result[idx++] = (center_x - box_width / 2.f) / img_width; |
| 85 | result[idx++] = (center_y - box_height / 2.f) / img_height; |
| 86 | result[idx++] = (center_x + box_width / 2.f) / img_width; |
| 87 | result[idx++] = (center_y + box_height / 2.f) / img_height; |
| 88 | |
| 89 | if (!info.max_sizes().empty()) |
| 90 | { |
| 91 | const float max_size = info.max_sizes().at(i); |
| 92 | box_width = sqrt(min_size * max_size); |
| 93 | box_height = box_width; |
| 94 | |
| 95 | // (xmin, ymin, xmax, ymax) |
| 96 | result[idx++] = (center_x - box_width / 2.f) / img_width; |
no test coverage detected