1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crateUniversal2DBox;
use Itertools;
use HashSet;
/// NMS algorithm implementation
///
/// # Parameters
/// * `detections` - boxes with optional scores to filter out with NMS; if `detection.1` is `None`, that the score is set as `detection.0.height`;
/// * `nms_threshold` - when to exclude the box from set by NMS;
/// * `score_threshold` - when to exclude the from set by initial score. if `score_threshold` is None, then `f32::MAX` is used.
///
// /// NMS algorithm implementation
// ///
// /// # Parameters
// /// * `detections` - boxes with optional scores to filter out with NMS; if `detection.1` is `None`, that the score is set as `detection.0.height`;
// /// * `nms_threshold` - when to exclude the box from set by NMS;
// /// * `score_threshold` - when to exclude the from set by initial score. if `score_threshold` is None, then `f32::MAX` is used.
// ///
// pub fn parallel_nms(
// detections: &[(Universal2DBox, Option<f32>)],
// nms_threshold: f32,
// score_threshold: Option<f32>,
// ) -> Vec<&Universal2DBox> {
// let score_threshold = score_threshold.unwrap_or(f32::MIN);
// let nms_boxes = detections
// .iter()
// .filter(|(e, score)| {
// score.unwrap_or(f32::MAX) > score_threshold && e.height() > 0.0 && e.aspect() > 0.0
// })
// .enumerate()
// .map(|(index, (b, score))| Candidate::new(b, score, index))
// .sorted_by(|a, b| b.rank.partial_cmp(&a.rank).unwrap())
// .collect::<Vec<_>>();
//
// let weight_matrix = nms_boxes
// .par_iter()
// .enumerate()
// .flat_map(|(index, cb)| {
// nms_boxes[index + 1..]
// .iter()
// .enumerate()
// .map(|(inner_index, ob)| {
// (
// (index, inner_index),
// Universal2DBox::intersection(cb.bbox, ob.bbox) as f32 / ob.bbox.area(),
// )
// })
// .collect::<Vec<_>>()
// })
// .collect::<HashMap<_, _>>();
//
// let mut excluded = HashSet::new();
//
// for (index, cb) in nms_boxes.iter().enumerate() {
// if excluded.contains(&cb.index) {
// continue;
// }
//
// for (internal_index, ob) in nms_boxes[index + 1..].iter().enumerate() {
// if excluded.contains(&ob.index) {
// continue;
// }
//
// let metric = weight_matrix.get(&(index, internal_index)).unwrap();
// if *metric > nms_threshold {
// excluded.insert(ob.index);
// }
// }
// }
//
// nms_boxes
// .into_iter()
// .filter(|e| !excluded.contains(&e.index))
// .map(|e| e.bbox)
// .collect()
// }
//
// #[cfg(test)]
// mod tests {
// use crate::utils::bbox::Universal2DBox;
// use crate::utils::nms::{nms, parallel_nms};
//
// #[test]
// fn nms_test() {
// let bboxes = [
// (Universal2DBox::new(0.0, 0.0, None, 1.0, 5.0), None),
// (Universal2DBox::new(0.0, 0.0, None, 1.05, 5.1), None),
// (Universal2DBox::new(0.0, 0.0, None, 1.0, 4.9), None),
// (Universal2DBox::new(3.0, 4.0, None, 1.0, 4.5), None),
// ];
// let res_serial = nms(&bboxes, 0.8, None);
// let res_parallel = parallel_nms(&bboxes, 0.8, None);
// assert_eq!(res_serial, res_parallel);
// }
// }