-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathlib.rs
More file actions
348 lines (300 loc) · 11 KB
/
Copy pathlib.rs
File metadata and controls
348 lines (300 loc) · 11 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use std::{boxed::Box, future::Future, pin::Pin, sync::Arc, time::Duration};
use color_eyre::{Result, eyre::bail};
use ndarray::{ArrayView2, ArrayView3, Axis};
use ort::{
execution_providers::{CUDAExecutionProvider, TensorRTExecutionProvider},
inputs,
session::{Session, SessionOutputs, builder::GraphOptimizationLevel},
value::TensorRef,
};
use ros_z_streams::CreateAnnouncingPublisher;
use ros2::sensor_msgs::image::Image;
use ros_z::prelude::*;
use tokio::{task::block_in_place, time::Instant};
use types::{
bounding_box::BoundingBox,
object_detection::{NUMBER_OF_VALUES_PER_OBJECT, Object, RobocupObjectLabel, YOLOObjectLabel},
parameters::DetectionParameters,
pose_detection::{NUMBER_OF_VALUES_PER_POSE, Pose},
time_wrapper::TimeWrapper,
};
pub const NUMBER_OF_DETECTIONS: usize = 300;
#[derive(Clone, Copy, Debug)]
enum TaskHead {
ObjectDetection,
PoseDetection,
}
struct DetectionOutput {
inference_duration: Duration,
post_processing_duration: Duration,
non_maximum_suppression_duration: Duration,
detected_objects: Vec<Object<RobocupObjectLabel>>,
detected_poses: Vec<Pose<YOLOObjectLabel>>,
}
impl TaskHead {
fn output_name(self) -> &'static str {
match self {
TaskHead::ObjectDetection => "object_output",
TaskHead::PoseDetection => "pose_output",
}
}
fn expected_shape(self) -> [usize; 3] {
match self {
Self::ObjectDetection => [1, NUMBER_OF_DETECTIONS, NUMBER_OF_VALUES_PER_OBJECT],
Self::PoseDetection => [1, NUMBER_OF_DETECTIONS, NUMBER_OF_VALUES_PER_POSE],
}
}
}
#[derive(Debug)]
struct ModelOutputs<'a> {
objects: ArrayView2<'a, f32>,
poses: ArrayView2<'a, f32>,
}
pub fn run_boxed(ctx: Arc<Context>) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
Box::pin(run(ctx))
}
async fn run(ctx: Arc<Context>) -> Result<()> {
let node = ctx.create_node("detection").build().await?;
let node_parameters = node.bind_parameter_as::<DetectionParameters>("detection")?;
let mut parameter_receiver = node_parameters.subscribe();
let image_sub = node
.subscriber::<Image>("inputs/left_image")
.build()
.await?;
let inference_duration_pub = node
.publisher::<Duration>("inference_duration")
.build()
.await?;
let post_processing_duration_pub = node
.publisher::<Duration>("post_processing_duration")
.build()
.await?;
let non_maximum_suppression_duration_pub = node
.publisher::<Duration>("non_maximum_suppression_duration")
.build()
.await?;
let detected_objects_pub = node
.announcing_publisher::<TimeWrapper<Vec<Object<RobocupObjectLabel>>>>("detected_objects")
.await?;
let detected_poses_pub = node
.announcing_publisher::<TimeWrapper<Vec<Pose<YOLOObjectLabel>>>>("detected_poses")
.await?;
let initial_parameters_snapshot = node_parameters.snapshot();
let parameters = initial_parameters_snapshot.typed();
let model_path = parameters
.neural_networks_folder
.join(¶meters.model_name);
let tensor_rt = TensorRTExecutionProvider::default()
.with_device_id(0)
.with_fp16(true)
.with_engine_cache(true)
.with_engine_cache_path(parameters.neural_networks_folder.display())
.build();
let cuda = CUDAExecutionProvider::default().build();
let mut session = block_in_place(|| {
Session::builder()?
.with_execution_providers([tensor_rt, cuda])?
.with_optimization_level(GraphOptimizationLevel::Level3)?
.with_intra_threads(2)?
.commit_from_file(model_path)
})?;
loop {
parameter_receiver
.wait_for(|parameters| parameters.typed().enable)
.await?;
let image = image_sub.recv().await?;
let parameter_snapshot = node_parameters.snapshot();
let parameters = parameter_snapshot.typed();
if !parameters.enable {
continue;
}
let image_time = image.header.stamp.into();
let detected_objects_pending = detected_objects_pub.announce(image_time).await?;
let detected_poses_pending = detected_poses_pub.announce(image_time).await?;
check_image(&image)?;
let output = block_in_place(|| {
let inference_start = Instant::now();
let nv12_data = ArrayView3::from_shape(
[image.height as usize / 2, image.width as usize / 2, 6],
&image.data,
)?;
let outputs: SessionOutputs = session
.run(inputs!["raw_bytes_input" => TensorRef::from_array_view(nv12_data)?])?;
let inference_duration = inference_start.elapsed();
let post_processing_start = Instant::now();
let outputs = extract_outputs(&outputs)?;
let candidate_detections = extract_candidate_object_detections(
&outputs,
parameters
.object_detection_parameters
.minimum_candidate_confidence,
)?;
let candidate_human_poses = extract_candidate_pose_detections(
&outputs,
parameters
.pose_detection_parameters
.minimum_candidate_confidence,
)?;
let post_processing_duration = post_processing_start.elapsed();
let non_maximum_suppression_start = Instant::now();
let detected_objects = non_maximum_suppression(
candidate_detections,
parameters
.object_detection_parameters
.maximum_intersection_over_union,
);
let detected_poses = non_maximum_suppression(
candidate_human_poses,
parameters
.pose_detection_parameters
.maximum_intersection_over_union,
);
let non_maximum_suppression_duration = non_maximum_suppression_start.elapsed();
Ok::<_, color_eyre::eyre::Error>(DetectionOutput {
inference_duration,
post_processing_duration,
non_maximum_suppression_duration,
detected_objects,
detected_poses,
})
})?;
inference_duration_pub
.publish(&output.inference_duration)
.await?;
post_processing_duration_pub
.publish(&output.post_processing_duration)
.await?;
non_maximum_suppression_duration_pub
.publish(&output.non_maximum_suppression_duration)
.await?;
detected_objects_pending
.publish(&TimeWrapper {
time: image_time,
inner: output.detected_objects,
})
.await?;
detected_poses_pending
.publish(&TimeWrapper {
time: image_time,
inner: output.detected_poses,
})
.await?;
}
}
fn check_image(image: &Image) -> Result<()> {
if image.encoding != "nv12" {
bail!("unsupported image encoding: {}", image.encoding);
}
if !image.width.is_multiple_of(32) || !image.height.is_multiple_of(32) {
bail!(
"image dimensions must be multiples of 32 (got {}x{})",
image.width,
image.height
);
}
Ok(())
}
fn extract_outputs<'a>(outputs: &'a SessionOutputs<'a>) -> Result<ModelOutputs<'a>> {
let objects_output =
outputs[TaskHead::ObjectDetection.output_name()].try_extract_array::<f32>()?;
if objects_output.shape() != TaskHead::ObjectDetection.expected_shape() {
bail!(
"object detection output not of expected shape. Expected: {:?}, got: {:?}",
TaskHead::ObjectDetection.expected_shape(),
objects_output.shape()
);
}
let reshaped_objects_output = objects_output.squeeze().into_dimensionality()?;
let poses_output = outputs[TaskHead::PoseDetection.output_name()].try_extract_array::<f32>()?;
if poses_output.shape() != TaskHead::PoseDetection.expected_shape() {
bail!(
"pose detection output not of expected shape. Expected: {:?}, got: {:?}",
TaskHead::PoseDetection.expected_shape(),
poses_output.shape()
);
}
let reshaped_pose_output = poses_output.squeeze().into_dimensionality()?;
Ok(ModelOutputs {
objects: reshaped_objects_output,
poses: reshaped_pose_output,
})
}
fn extract_candidate_object_detections(
outputs: &ModelOutputs,
confidence_threshold: f32,
) -> Result<Vec<Object<RobocupObjectLabel>>> {
Ok(outputs
.objects
.axis_iter(Axis(0))
.filter_map(|row| {
let confidence = row[4usize];
if confidence < confidence_threshold {
return None;
}
let object_values: [f32; NUMBER_OF_VALUES_PER_OBJECT] = row
.as_slice()
.expect("slice is not contiguous")
.try_into()
.unwrap_or_else(|_| {
panic!("slice is not of length {}", NUMBER_OF_VALUES_PER_OBJECT)
});
Some(Object::from(object_values))
})
.collect())
}
fn extract_candidate_pose_detections(
outputs: &ModelOutputs,
confidence_threshold: f32,
) -> Result<Vec<Pose<YOLOObjectLabel>>> {
Ok(outputs
.poses
.axis_iter(Axis(0))
.filter_map(|row| {
let confidence = row[4usize];
if confidence < confidence_threshold {
return None;
}
let pose_values: [f32; NUMBER_OF_VALUES_PER_POSE] = row
.as_slice()
.expect("slice is not contiguous")
.try_into()
.unwrap_or_else(|_| panic!("slice is not of length {}", NUMBER_OF_VALUES_PER_POSE));
Some(Pose::from(&pose_values))
})
.collect())
}
trait HasBoundingBox {
fn bounding_box(&self) -> &BoundingBox;
}
impl<T> HasBoundingBox for Object<T> {
fn bounding_box(&self) -> &BoundingBox {
&self.bounding_box
}
}
impl<T> HasBoundingBox for Pose<T> {
fn bounding_box(&self) -> &BoundingBox {
&self.object.bounding_box
}
}
fn non_maximum_suppression<T: HasBoundingBox>(
mut sorted_candidate_detections: Vec<T>,
maximum_intersection_over_union: f32,
) -> Vec<T> {
sorted_candidate_detections.sort_by(|detection1, detection2| {
detection1
.bounding_box()
.confidence
.total_cmp(&detection2.bounding_box().confidence)
});
let mut remaining_detections = Vec::new();
while let Some(detection) = sorted_candidate_detections.pop() {
sorted_candidate_detections.retain(|detection_candidate| {
detection
.bounding_box()
.intersection_over_union(detection_candidate.bounding_box())
< maximum_intersection_over_union
});
remaining_detections.push(detection)
}
remaining_detections
}