|
| 1 | +// Copyright(c) OpenMMLab.All rights reserved. |
| 2 | +#include "pytorch_cpp_helper.hpp" |
| 3 | +#include "pytorch_device_registry.hpp" |
| 4 | + |
| 5 | +using torch::indexing::None; |
| 6 | +using torch::indexing::Slice; |
| 7 | + |
| 8 | +void bbox_overlaps_cpu_kernel(const Tensor boxes1, const Tensor boxes2, |
| 9 | + Tensor ious, const int mode_flag, |
| 10 | + const bool aligned, const int offset) { |
| 11 | + Tensor temp_ious; |
| 12 | + if (aligned) { |
| 13 | + Tensor lt = torch::max(boxes1.index({Slice(None), Slice({None, 2})}), |
| 14 | + boxes2.index({Slice(None), Slice({None, 2})})); |
| 15 | + Tensor rb = torch::min(boxes1.index({Slice(None), Slice(2)}), |
| 16 | + boxes2.index({Slice(None), Slice(2)})); |
| 17 | + Tensor wh = (rb - lt + offset).clamp(0.f, INT_MAX * 1.f); |
| 18 | + Tensor overlap = wh.index({Slice(None), 0}) * wh.index({Slice(None), 1}); |
| 19 | + Tensor area1 = (boxes1.index({Slice(None), 2}) - |
| 20 | + boxes1.index({Slice(None), 0}) + offset) * |
| 21 | + (boxes1.index({Slice(None), 3}) - |
| 22 | + boxes1.index({Slice(None), 1}) + offset); |
| 23 | + if (mode_flag == 0) { |
| 24 | + Tensor area2 = (boxes2.index({Slice(None), 2}) - |
| 25 | + boxes2.index({Slice(None), 0}) + offset) * |
| 26 | + (boxes2.index({Slice(None), 3}) - |
| 27 | + boxes2.index({Slice(None), 1}) + offset); |
| 28 | + temp_ious = overlap / (area1 + area2 - overlap); |
| 29 | + } else { |
| 30 | + temp_ious = overlap / area1; |
| 31 | + } |
| 32 | + } else { |
| 33 | + Tensor lt = torch::max(boxes1.index({Slice(None), None, Slice({None, 2})}), |
| 34 | + boxes2.index({Slice(None), Slice({None, 2})})); |
| 35 | + Tensor rb = torch::min(boxes1.index({Slice(None), None, Slice(2)}), |
| 36 | + boxes2.index({Slice(None), Slice(2)})); |
| 37 | + Tensor wh = (rb - lt + offset).clamp(0.f, INT_MAX * 1.f); |
| 38 | + Tensor overlap = wh.index({"...", 0}) * wh.index({"...", 1}); |
| 39 | + Tensor area1 = (boxes1.index({Slice(None), 2}) - |
| 40 | + boxes1.index({Slice(None), 0}) + offset) * |
| 41 | + (boxes1.index({Slice(None), 3}) - |
| 42 | + boxes1.index({Slice(None), 1}) + offset); |
| 43 | + if (mode_flag == 0) { |
| 44 | + Tensor area2 = (boxes2.index({Slice(None), 2}) - |
| 45 | + boxes2.index({Slice(None), 0}) + offset) * |
| 46 | + (boxes2.index({Slice(None), 3}) - |
| 47 | + boxes2.index({Slice(None), 1}) + offset); |
| 48 | + temp_ious = |
| 49 | + overlap / (area1.index({Slice(None), None}) + area2 - overlap); |
| 50 | + } else { |
| 51 | + temp_ious = overlap / area1.index({Slice(None), None}); |
| 52 | + } |
| 53 | + } |
| 54 | + ious.copy_(temp_ious); |
| 55 | +} |
| 56 | + |
| 57 | +void bbox_overlaps_cpu(const Tensor boxes1, const Tensor boxes2, Tensor ious, |
| 58 | + const int mode, const bool aligned, const int offset) { |
| 59 | + bbox_overlaps_cpu_kernel(boxes1, boxes2, ious, mode, aligned, offset); |
| 60 | +} |
| 61 | + |
| 62 | +void bbox_overlaps_impl(const Tensor boxes1, const Tensor boxes2, Tensor ious, |
| 63 | + const int mode, const bool aligned, const int offset); |
| 64 | + |
| 65 | +REGISTER_DEVICE_IMPL(bbox_overlaps_impl, CPU, bbox_overlaps_cpu); |
0 commit comments