1use super::deep::{DeepFeatureExtractor, DeepFeatureSet, DeepFeatureSetError};
47use super::GrayscaleImage;
48#[cfg(feature = "onnx-inference")]
49use nalgebra::Point2;
50use std::fmt;
51use std::path::Path;
52use std::sync::atomic::{AtomicUsize, Ordering};
53use std::sync::Arc;
54#[cfg(feature = "onnx-inference")]
55use std::sync::Mutex;
56
57#[derive(Debug, Clone, PartialEq)]
64pub struct SuperPointOnnxConfig {
65 pub nms_radius_pixels: u32,
67 pub min_score: f32,
69 pub max_keypoints: usize,
71}
72
73impl Default for SuperPointOnnxConfig {
74 fn default() -> Self {
75 Self {
76 nms_radius_pixels: 4,
77 min_score: 0.005,
78 max_keypoints: 1500,
79 }
80 }
81}
82
83#[derive(Clone)]
100pub struct SuperPointOnnxExtractor {
101 config: SuperPointOnnxConfig,
102 runtime_max_keypoints: Arc<AtomicUsize>,
106 #[cfg(feature = "onnx-inference")]
107 session: Arc<Mutex<ort::session::Session>>,
108}
109
110impl fmt::Debug for SuperPointOnnxExtractor {
111 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
112 formatter
113 .debug_struct("SuperPointOnnxExtractor")
114 .field("config", &self.config)
115 .field("onnx_inference_feature", &cfg!(feature = "onnx-inference"))
116 .finish()
117 }
118}
119
120impl SuperPointOnnxExtractor {
121 pub const fn config(&self) -> &SuperPointOnnxConfig {
123 &self.config
124 }
125
126 pub fn set_runtime_max_keypoints(&self, max_keypoints: usize) {
131 self.runtime_max_keypoints
132 .store(max_keypoints, Ordering::Relaxed);
133 }
134
135 pub fn effective_max_keypoints(&self) -> usize {
138 match self.runtime_max_keypoints.load(Ordering::Relaxed) {
139 0 => self.config.max_keypoints,
140 override_value => override_value,
141 }
142 }
143}
144
145#[cfg(not(feature = "onnx-inference"))]
150impl SuperPointOnnxExtractor {
151 pub fn load_from_path<P: AsRef<Path>>(
156 _path: P,
157 config: SuperPointOnnxConfig,
158 ) -> Result<Self, SuperPointOnnxError> {
159 Err(SuperPointOnnxError::FeatureDisabled {
160 requested: "load_from_path",
161 config,
162 })
163 }
164}
165
166#[cfg(not(feature = "onnx-inference"))]
167impl DeepFeatureExtractor for SuperPointOnnxExtractor {
168 type Image = GrayscaleImage;
169 type Error = SuperPointOnnxError;
170
171 fn extract_deep(&self, _image: &Self::Image) -> Result<DeepFeatureSet, Self::Error> {
172 Err(SuperPointOnnxError::FeatureDisabled {
173 requested: "extract_deep",
174 config: self.config.clone(),
175 })
176 }
177}
178
179#[cfg(feature = "onnx-inference")]
193#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
194pub enum OnnxBackend {
195 #[default]
199 CudaThenCpu,
200 Cuda,
205 Cpu,
207}
208
209#[cfg(feature = "onnx-inference")]
215pub(crate) fn execution_providers(
216 backend: OnnxBackend,
217) -> Result<Vec<ort::ep::ExecutionProviderDispatch>, String> {
218 use ort::ep::CPU;
219 match backend {
220 #[cfg(feature = "onnx-cuda")]
221 OnnxBackend::CudaThenCpu => Ok(vec![
222 ort::ep::CUDA::default().build(),
223 CPU::default().build(),
224 ]),
225 #[cfg(not(feature = "onnx-cuda"))]
226 OnnxBackend::CudaThenCpu => Ok(vec![CPU::default().build()]),
227 #[cfg(feature = "onnx-cuda")]
228 OnnxBackend::Cuda => Ok(vec![ort::ep::CUDA::default().build().error_on_failure()]),
229 #[cfg(not(feature = "onnx-cuda"))]
230 OnnxBackend::Cuda => {
231 Err("the CUDA execution provider needs the `onnx-cuda` feature".to_string())
232 }
233 OnnxBackend::Cpu => Ok(vec![CPU::default().build()]),
234 }
235}
236
237#[cfg(feature = "onnx-inference")]
238impl SuperPointOnnxExtractor {
239 pub fn load_from_path<P: AsRef<Path>>(
244 path: P,
245 config: SuperPointOnnxConfig,
246 ) -> Result<Self, SuperPointOnnxError> {
247 Self::load_from_path_with_backend(path, config, OnnxBackend::default())
248 }
249
250 pub fn load_from_path_with_backend<P: AsRef<Path>>(
253 path: P,
254 config: SuperPointOnnxConfig,
255 backend: OnnxBackend,
256 ) -> Result<Self, SuperPointOnnxError> {
257 let providers = execution_providers(backend).map_err(SuperPointOnnxError::from_ort)?;
262
263 let session = ort::session::Session::builder()
264 .map_err(SuperPointOnnxError::from_ort)?
265 .with_optimization_level(ort::session::builder::GraphOptimizationLevel::All)
266 .map_err(SuperPointOnnxError::from_ort)?
267 .with_execution_providers(providers)
268 .map_err(SuperPointOnnxError::from_ort)?
269 .commit_from_file(path.as_ref())
270 .map_err(SuperPointOnnxError::from_ort)?;
271 Ok(Self {
272 config,
273 runtime_max_keypoints: Arc::new(AtomicUsize::new(0)),
274 session: Arc::new(Mutex::new(session)),
275 })
276 }
277}
278
279#[cfg(feature = "onnx-inference")]
280impl DeepFeatureExtractor for SuperPointOnnxExtractor {
281 type Image = GrayscaleImage;
282 type Error = SuperPointOnnxError;
283
284 fn extract_deep(&self, image: &Self::Image) -> Result<DeepFeatureSet, Self::Error> {
285 let width = image.width();
286 let height = image.height();
287 if width == 0 || height == 0 {
288 return Err(SuperPointOnnxError::EmptyImage);
289 }
290
291 let input =
292 ndarray::Array4::<f32>::from_shape_vec((1, 1, height, width), image.pixels().to_vec())
293 .map_err(|error| SuperPointOnnxError::PreprocessShape {
294 width,
295 height,
296 message: error.to_string(),
297 })?;
298
299 let input_value =
300 ort::value::Tensor::from_array(input).map_err(SuperPointOnnxError::from_ort)?;
301 let mut session = self
302 .session
303 .lock()
304 .map_err(|error| SuperPointOnnxError::OnnxRuntime(error.to_string()))?;
305 let mut outputs = session
306 .run(ort::inputs![input_value])
307 .map_err(SuperPointOnnxError::from_ort)?;
308
309 let (keypoints_array, scores_array, descriptors_array) =
310 extract_named_outputs(&mut outputs)?;
311
312 let triples = postprocess_with_cap(
313 keypoints_array.view(),
314 scores_array.view(),
315 descriptors_array.view(),
316 &self.config,
317 self.effective_max_keypoints(),
318 )?;
319
320 let (keypoints, scores, descriptors) = unzip_triples(triples);
321 DeepFeatureSet::new(keypoints, scores, descriptors).map_err(SuperPointOnnxError::from)
322 }
323}
324
325#[cfg(feature = "onnx-inference")]
328type NamedOutputs = (
329 ndarray::Array2<i64>,
330 ndarray::Array1<f32>,
331 ndarray::Array2<f32>,
332);
333
334#[cfg(feature = "onnx-inference")]
337type DecodedKeypoints = Vec<(Point2<f64>, f32, Vec<f32>)>;
338
339#[cfg(feature = "onnx-inference")]
340fn extract_named_outputs(
341 outputs: &mut ort::session::SessionOutputs<'_>,
342) -> Result<NamedOutputs, SuperPointOnnxError> {
343 let kp_value =
349 outputs
350 .remove("keypoints")
351 .ok_or_else(|| SuperPointOnnxError::OutputShapeMismatch {
352 expected: "output named `keypoints`",
353 actual: "missing".to_string(),
354 })?;
355 let score_value =
356 outputs
357 .remove("scores")
358 .ok_or_else(|| SuperPointOnnxError::OutputShapeMismatch {
359 expected: "output named `scores`",
360 actual: "missing".to_string(),
361 })?;
362 let desc_value =
363 outputs
364 .remove("descriptors")
365 .ok_or_else(|| SuperPointOnnxError::OutputShapeMismatch {
366 expected: "output named `descriptors`",
367 actual: "missing".to_string(),
368 })?;
369
370 let kp_array: ndarray::ArrayD<i64> = kp_value
371 .try_extract_array::<i64>()
372 .map_err(SuperPointOnnxError::from_ort)?
373 .into_owned();
374 let score_array: ndarray::ArrayD<f32> = score_value
375 .try_extract_array::<f32>()
376 .map_err(SuperPointOnnxError::from_ort)?
377 .into_owned();
378 let desc_array: ndarray::ArrayD<f32> = desc_value
379 .try_extract_array::<f32>()
380 .map_err(SuperPointOnnxError::from_ort)?
381 .into_owned();
382
383 Ok((
384 normalise_keypoints(kp_array)?,
385 normalise_scores(score_array)?,
386 normalise_descriptors(desc_array)?,
387 ))
388}
389
390#[cfg(feature = "onnx-inference")]
391fn normalise_keypoints(
392 array: ndarray::ArrayD<i64>,
393) -> Result<ndarray::Array2<i64>, SuperPointOnnxError> {
394 let shape = array.shape().to_vec();
396 let view = if shape.len() == 3 && shape[0] == 1 && shape[2] == 2 {
397 array.into_shape_with_order((shape[1], 2)).map_err(|e| {
398 SuperPointOnnxError::OutputShapeMismatch {
399 expected: "(N, 2) or (1, N, 2) keypoints",
400 actual: format!("{shape:?}: {e}"),
401 }
402 })?
403 } else if shape.len() == 2 && shape[1] == 2 {
404 array.into_dimensionality::<ndarray::Ix2>().map_err(|e| {
405 SuperPointOnnxError::OutputShapeMismatch {
406 expected: "(N, 2) keypoints",
407 actual: format!("{shape:?}: {e}"),
408 }
409 })?
410 } else {
411 return Err(SuperPointOnnxError::OutputShapeMismatch {
412 expected: "(N, 2) or (1, N, 2) keypoints",
413 actual: format!("{shape:?}"),
414 });
415 };
416 Ok(view)
417}
418
419#[cfg(feature = "onnx-inference")]
420fn normalise_scores(
421 array: ndarray::ArrayD<f32>,
422) -> Result<ndarray::Array1<f32>, SuperPointOnnxError> {
423 let shape = array.shape().to_vec();
425 let view = if shape.len() == 2 && shape[0] == 1 {
426 array.into_shape_with_order((shape[1],)).map_err(|e| {
427 SuperPointOnnxError::OutputShapeMismatch {
428 expected: "(N,) or (1, N) scores",
429 actual: format!("{shape:?}: {e}"),
430 }
431 })?
432 } else if shape.len() == 1 {
433 array.into_dimensionality::<ndarray::Ix1>().map_err(|e| {
434 SuperPointOnnxError::OutputShapeMismatch {
435 expected: "(N,) scores",
436 actual: format!("{shape:?}: {e}"),
437 }
438 })?
439 } else {
440 return Err(SuperPointOnnxError::OutputShapeMismatch {
441 expected: "(N,) or (1, N) scores",
442 actual: format!("{shape:?}"),
443 });
444 };
445 Ok(view)
446}
447
448#[cfg(feature = "onnx-inference")]
449fn normalise_descriptors(
450 array: ndarray::ArrayD<f32>,
451) -> Result<ndarray::Array2<f32>, SuperPointOnnxError> {
452 const DIM: usize = 256;
454 let shape = array.shape().to_vec();
455 let two_d = match shape.as_slice() {
456 [n, d] if *d == DIM => array.into_shape_with_order((*n, DIM)).map_err(|e| {
457 SuperPointOnnxError::OutputShapeMismatch {
458 expected: "(N, 256) descriptors",
459 actual: format!("{shape:?}: {e}"),
460 }
461 })?,
462 [d, n] if *d == DIM => {
463 let arr = array.into_shape_with_order((DIM, *n)).map_err(|e| {
465 SuperPointOnnxError::OutputShapeMismatch {
466 expected: "(256, N) descriptors",
467 actual: format!("{shape:?}: {e}"),
468 }
469 })?;
470 arr.t().to_owned()
471 }
472 [1, n, d] if *d == DIM => array.into_shape_with_order((*n, DIM)).map_err(|e| {
473 SuperPointOnnxError::OutputShapeMismatch {
474 expected: "(1, N, 256) descriptors",
475 actual: format!("{shape:?}: {e}"),
476 }
477 })?,
478 [1, d, n] if *d == DIM => {
479 let arr = array.into_shape_with_order((DIM, *n)).map_err(|e| {
480 SuperPointOnnxError::OutputShapeMismatch {
481 expected: "(1, 256, N) descriptors",
482 actual: format!("{shape:?}: {e}"),
483 }
484 })?;
485 arr.t().to_owned()
486 }
487 _ => {
488 return Err(SuperPointOnnxError::OutputShapeMismatch {
489 expected: "(N, 256), (256, N), (1, N, 256), or (1, 256, N) descriptors",
490 actual: format!("{shape:?}"),
491 });
492 }
493 };
494 Ok(two_d)
495}
496
497#[cfg(feature = "onnx-inference")]
502#[cfg(test)]
504#[cfg(feature = "onnx-inference")]
505fn postprocess(
506 keypoints: ndarray::ArrayView2<'_, i64>,
507 scores: ndarray::ArrayView1<'_, f32>,
508 descriptors: ndarray::ArrayView2<'_, f32>,
509 config: &SuperPointOnnxConfig,
510) -> Result<DecodedKeypoints, SuperPointOnnxError> {
511 postprocess_with_cap(keypoints, scores, descriptors, config, config.max_keypoints)
512}
513
514#[cfg(feature = "onnx-inference")]
517fn postprocess_with_cap(
518 keypoints: ndarray::ArrayView2<'_, i64>,
519 scores: ndarray::ArrayView1<'_, f32>,
520 descriptors: ndarray::ArrayView2<'_, f32>,
521 config: &SuperPointOnnxConfig,
522 max_keypoints: usize,
523) -> Result<DecodedKeypoints, SuperPointOnnxError> {
524 let n_kp = keypoints.shape()[0];
525 let n_score = scores.shape()[0];
526 let n_desc = descriptors.shape()[0];
527 if n_kp != n_score || n_kp != n_desc {
528 return Err(SuperPointOnnxError::OutputShapeMismatch {
529 expected: "keypoints / scores / descriptors agree on N",
530 actual: format!("N_keypoints={n_kp} N_scores={n_score} N_descriptors={n_desc}"),
531 });
532 }
533
534 let descriptor_dim = descriptors.shape()[1];
535
536 let mut triples: Vec<(Point2<f64>, f32, Vec<f32>)> = Vec::with_capacity(n_kp);
537 for i in 0..n_kp {
538 let score = scores[i];
539 if !score.is_finite() || score < config.min_score {
540 continue;
541 }
542 let x = keypoints[(i, 0)] as f64;
543 let y = keypoints[(i, 1)] as f64;
544 let mut descriptor = vec![0.0_f32; descriptor_dim];
545 for d in 0..descriptor_dim {
546 descriptor[d] = descriptors[(i, d)];
547 }
548 let norm = descriptor.iter().map(|v| v * v).sum::<f32>().sqrt();
553 if norm > 0.0 {
554 for v in descriptor.iter_mut() {
555 *v /= norm;
556 }
557 }
558 triples.push((Point2::new(x, y), score, descriptor));
559 }
560
561 triples.sort_by(|left, right| right.1.total_cmp(&left.1));
567 if triples.len() > max_keypoints {
568 triples.truncate(max_keypoints);
569 }
570 Ok(triples)
571}
572
573#[cfg(feature = "onnx-inference")]
574fn unzip_triples(
575 triples: Vec<(Point2<f64>, f32, Vec<f32>)>,
576) -> (Vec<Point2<f64>>, Vec<f32>, Vec<Vec<f32>>) {
577 let mut keypoints = Vec::with_capacity(triples.len());
578 let mut scores = Vec::with_capacity(triples.len());
579 let mut descriptors = Vec::with_capacity(triples.len());
580 for (kp, score, desc) in triples {
581 keypoints.push(kp);
582 scores.push(score);
583 descriptors.push(desc);
584 }
585 (keypoints, scores, descriptors)
586}
587
588#[derive(Debug, Clone, PartialEq)]
589pub enum SuperPointOnnxError {
590 FeatureDisabled {
594 requested: &'static str,
595 config: SuperPointOnnxConfig,
596 },
597 EmptyImage,
599 PreprocessShape {
602 width: usize,
603 height: usize,
604 message: String,
605 },
606 OutputShapeMismatch {
608 expected: &'static str,
609 actual: String,
610 },
611 OnnxRuntime(String),
614 DeepFeatureSet(DeepFeatureSetError),
617}
618
619impl SuperPointOnnxError {
620 #[cfg(feature = "onnx-inference")]
621 fn from_ort<E: std::fmt::Display>(error: E) -> Self {
622 Self::OnnxRuntime(error.to_string())
623 }
624}
625
626impl fmt::Display for SuperPointOnnxError {
627 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
628 match self {
629 Self::FeatureDisabled { requested, .. } => write!(
630 formatter,
631 "SuperPointOnnxExtractor::{requested}: in-Rust ONNX inference is gated behind \
632 the `onnx-inference` feature; rebuild with `--features onnx-inference` and \
633 supply a SuperPoint ONNX model path (see docs/superpoint_onnx_runtime_plan.md)"
634 ),
635 Self::EmptyImage => write!(formatter, "SuperPoint ONNX input image is empty"),
636 Self::PreprocessShape {
637 width,
638 height,
639 message,
640 } => write!(
641 formatter,
642 "SuperPoint ONNX preprocess failed: image {width}x{height} cannot be reshaped \
643 into (1, 1, H, W): {message}"
644 ),
645 Self::OutputShapeMismatch { expected, actual } => write!(
646 formatter,
647 "SuperPoint ONNX output shape mismatch: expected {expected}, got {actual}"
648 ),
649 Self::OnnxRuntime(message) => write!(formatter, "ONNX Runtime error: {message}"),
650 Self::DeepFeatureSet(error) => write!(formatter, "{error}"),
651 }
652 }
653}
654
655impl std::error::Error for SuperPointOnnxError {}
656
657impl From<DeepFeatureSetError> for SuperPointOnnxError {
658 fn from(error: DeepFeatureSetError) -> Self {
659 Self::DeepFeatureSet(error)
660 }
661}
662
663#[cfg(test)]
664mod tests {
665 use super::*;
666
667 #[cfg(not(feature = "onnx-inference"))]
668 #[test]
669 fn skeleton_extractor_load_returns_feature_disabled() {
670 let result = SuperPointOnnxExtractor::load_from_path(
674 "/nonexistent",
675 SuperPointOnnxConfig::default(),
676 );
677 match result {
678 Err(SuperPointOnnxError::FeatureDisabled { requested, .. }) => {
679 assert_eq!(requested, "load_from_path");
680 }
681 other => panic!("expected FeatureDisabled, got {other:?}"),
682 }
683 }
684
685 #[test]
686 fn default_config_matches_phase26_pre_export_settings() {
687 let config = SuperPointOnnxConfig::default();
692 assert_eq!(config.max_keypoints, 1500);
693 assert_eq!(config.nms_radius_pixels, 4);
694 assert!((config.min_score - 0.005).abs() < 1.0e-9);
695 }
696
697 #[cfg(feature = "onnx-inference")]
698 #[test]
699 fn postprocess_filters_by_min_score_and_truncates_to_max_keypoints() {
700 let keypoints = ndarray::array![[1_i64, 2], [3, 4], [5, 6], [7, 8]];
703 let scores = ndarray::array![0.9_f32, 0.7, 0.5, 0.001];
704 let descriptors = ndarray::array![
707 [1.0_f32, 0.0, 0.0, 0.0],
708 [0.0, 1.0, 0.0, 0.0],
709 [0.0, 0.0, 1.0, 0.0],
710 [0.0, 0.0, 0.0, 1.0],
711 ];
712 let config = SuperPointOnnxConfig {
713 min_score: 0.01,
714 max_keypoints: 2,
715 nms_radius_pixels: 4,
716 };
717 let result =
718 postprocess(keypoints.view(), scores.view(), descriptors.view(), &config).unwrap();
719 assert_eq!(result.len(), 2);
720 assert_eq!(result[0].0, Point2::new(1.0, 2.0));
722 assert!((result[0].1 - 0.9).abs() < 1.0e-6);
723 assert_eq!(result[1].0, Point2::new(3.0, 4.0));
724 assert!((result[1].1 - 0.7).abs() < 1.0e-6);
725 }
726
727 #[cfg(feature = "onnx-inference")]
728 #[test]
729 fn runtime_keypoint_cap_overrides_the_configured_default() {
730 use std::sync::atomic::{AtomicUsize, Ordering};
731 let config = SuperPointOnnxConfig {
734 max_keypoints: 1500,
735 ..SuperPointOnnxConfig::default()
736 };
737 let shared = AtomicUsize::new(0);
738 assert_eq!(shared.load(Ordering::Relaxed), 0, "0 means 'use config'");
739 assert_eq!(config.max_keypoints, 1500);
740 let effective = |config: &SuperPointOnnxConfig, shared: &AtomicUsize| match shared
742 .load(Ordering::Relaxed)
743 {
744 0 => config.max_keypoints,
745 override_value => override_value,
746 };
747 shared.store(512, Ordering::Relaxed);
748 assert_eq!(effective(&config, &shared), 512);
749 shared.store(2048, Ordering::Relaxed);
750 assert_eq!(effective(&config, &shared), 2048);
751 shared.store(0, Ordering::Relaxed);
752 assert_eq!(effective(&config, &shared), 1500);
753 }
754
755 #[cfg(feature = "onnx-inference")]
756 #[test]
757 fn postprocess_normalises_descriptors_to_unit_norm() {
758 let keypoints = ndarray::array![[10_i64, 20]];
759 let scores = ndarray::array![0.5_f32];
760 let descriptors = ndarray::array![[3.0_f32, 4.0, 0.0]];
762 let config = SuperPointOnnxConfig::default();
763 let result =
764 postprocess(keypoints.view(), scores.view(), descriptors.view(), &config).unwrap();
765 assert_eq!(result.len(), 1);
766 let descriptor = &result[0].2;
767 let norm = descriptor.iter().map(|v| v * v).sum::<f32>().sqrt();
768 assert!((norm - 1.0).abs() < 1.0e-6, "got norm {norm}");
769 assert!((descriptor[0] - 0.6).abs() < 1.0e-6);
770 assert!((descriptor[1] - 0.8).abs() < 1.0e-6);
771 assert!(descriptor[2].abs() < 1.0e-6);
772 }
773
774 #[cfg(feature = "onnx-inference")]
775 #[test]
776 fn postprocess_rejects_inconsistent_lengths() {
777 let keypoints = ndarray::array![[0_i64, 0], [1, 1]];
778 let scores = ndarray::array![0.5_f32]; let descriptors = ndarray::array![[1.0_f32, 0.0]];
780 let config = SuperPointOnnxConfig::default();
781 let result = postprocess(keypoints.view(), scores.view(), descriptors.view(), &config);
782 match result {
783 Err(SuperPointOnnxError::OutputShapeMismatch { .. }) => {}
784 other => panic!("expected OutputShapeMismatch, got {other:?}"),
785 }
786 }
787
788 #[cfg(feature = "onnx-inference")]
789 #[test]
790 fn postprocess_skips_nonfinite_and_below_threshold_scores() {
791 let keypoints = ndarray::array![[0_i64, 0], [1, 0], [2, 0]];
792 let scores = ndarray::array![f32::NAN, 0.001, 0.5];
793 let descriptors = ndarray::array![[1.0_f32, 0.0], [0.0, 1.0], [1.0, 1.0],];
794 let config = SuperPointOnnxConfig {
795 min_score: 0.01,
796 max_keypoints: 10,
797 nms_radius_pixels: 4,
798 };
799 let result =
800 postprocess(keypoints.view(), scores.view(), descriptors.view(), &config).unwrap();
801 assert_eq!(result.len(), 1);
802 assert_eq!(result[0].0, Point2::new(2.0, 0.0));
803 }
804
805 #[cfg(feature = "onnx-inference")]
806 #[test]
807 fn normalise_descriptors_handles_all_supported_layouts() {
808 use ndarray::ArrayD;
809 let nominal: Vec<f32> = (0..(3 * 256)).map(|i| i as f32 / 1000.0).collect();
810 let a = ArrayD::from_shape_vec(ndarray::IxDyn(&[3, 256]), nominal.clone()).unwrap();
812 let r = normalise_descriptors(a).unwrap();
813 assert_eq!(r.shape(), [3, 256]);
814 let a = ArrayD::from_shape_vec(ndarray::IxDyn(&[1, 3, 256]), nominal.clone()).unwrap();
816 let r = normalise_descriptors(a).unwrap();
817 assert_eq!(r.shape(), [3, 256]);
818 let a = ArrayD::from_shape_vec(ndarray::IxDyn(&[256, 3]), nominal.clone()).unwrap();
820 let r = normalise_descriptors(a).unwrap();
821 assert_eq!(r.shape(), [3, 256]);
822 }
823}