From 140750bf0e01b74258e9fe23d4365d9a3707ed13 Mon Sep 17 00:00:00 2001 From: Darshana Sanjeewan Adikari Date: Sun, 30 Jun 2024 16:49:38 +0200 Subject: [PATCH] Use new API calls for OpenVino (#1084) * Use new API calls * Fix some mistakes * Remove scratchpad state, directly fill the tensor instead (for now) * Remove manually specifying generics * Remove AsBytes trait --- Cargo.lock | 12 +++---- Cargo.toml | 2 +- crates/object_detection/src/pose_detection.rs | 35 +++---------------- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4614666dad..54d82022f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4294,9 +4294,9 @@ dependencies = [ [[package]] name = "openvino" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03912559b639b3daf7744738bd8cdeb4f3376e2308e299a8b3a427dbcf67e14e" +checksum = "aee013796927eec6012a344f10ecdc06bf26de79c626a2395e3f115464907ef6" dependencies = [ "openvino-finder", "openvino-sys", @@ -4305,9 +4305,9 @@ dependencies = [ [[package]] name = "openvino-finder" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a8bd5f866315374509f6f8db5f81b098cded4f76c3dc33687bf994f25e9ca52" +checksum = "af4c6841df4cd60fef743015f3348f81b6b225bd255ed0c4cab6e8c479e45eaa" dependencies = [ "cfg-if", "log", @@ -4315,9 +4315,9 @@ dependencies = [ [[package]] name = "openvino-sys" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bce71ae0f510b0017dccb049b6c00a7513026b7a2085538da4aa88d93066c629" +checksum = "f62fc2bd6882f2300a6b5017eaad292586d70995d333582aabcf1f1121cd147c" dependencies = [ "env_logger", "libloading 0.8.3", diff --git a/Cargo.toml b/Cargo.toml index 0e344567ca..d9e908ff67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,7 +138,7 @@ nix = { version = "0.28", features = ["ioctl"] } num-derive = "0.4.2" num-traits = "0.2" once_cell = "1.19.0" -openvino = { version = "0.7.1", features = ["runtime-linking"] } +openvino = { version = "0.7.2", features = ["runtime-linking"] } opn = { path = "crates/opn" } object_detection = { path = "crates/object_detection" } opusfile-ng = "0.1.0" diff --git a/crates/object_detection/src/pose_detection.rs b/crates/object_detection/src/pose_detection.rs index 6b84412d80..a997565648 100644 --- a/crates/object_detection/src/pose_detection.rs +++ b/crates/object_detection/src/pose_detection.rs @@ -37,8 +37,6 @@ const STRIDE: usize = DETECTION_IMAGE_HEIGHT * DETECTION_IMAGE_WIDTH; #[derive(Deserialize, Serialize)] pub struct PoseDetection { - #[serde(skip, default = "deserialize_not_implemented")] - scratchpad: Box<[f32]>, #[serde(skip, default = "deserialize_not_implemented")] network: CompiledModel, } @@ -108,16 +106,7 @@ impl PoseDetection { bail!("expected exactly one input and one output"); } - let input_shape = network - .get_input_by_index(0) - .wrap_err("failed to get input node")? - .get_shape() - .wrap_err("failed to get shape of input node")?; - let number_of_elements = input_shape.get_dimensions().iter().product::(); - let scratchpad = vec![0.0; number_of_elements as usize].into_boxed_slice(); - Ok(Self { - scratchpad, network: core.compile_model(&network, DeviceType::CPU)?, }) } @@ -139,10 +128,12 @@ impl PoseDetection { }; let image = context.image; + + let mut tensor = Tensor::new(ElementType::F32, &self.network.get_input()?.get_shape()?)?; { let earlier = SystemTime::now(); - load_into_scratchpad(self.scratchpad.as_mut(), image); + load_into_scratchpad(tensor.get_data_mut()?, image); context.preprocess_duration.fill_if_subscribed(|| { SystemTime::now() @@ -152,11 +143,7 @@ impl PoseDetection { } let mut infer_request = self.network.create_infer_request()?; - let tensor = Tensor::new_from_host_ptr( - ElementType::F32, - &self.network.get_input()?.get_shape()?, - self.scratchpad.as_bytes(), - )?; + infer_request.set_input_tensor(&tensor)?; { @@ -169,7 +156,7 @@ impl PoseDetection { .expect("time ran backwards") }); } - let mut prediction = infer_request.get_output_tensor()?; + let prediction = infer_request.get_output_tensor()?; let prediction = ArrayView::from_shape((56, MAX_DETECTIONS), prediction.get_data::()?)?; @@ -267,15 +254,3 @@ fn non_maximum_suppression( poses } - -trait AsBytes { - fn as_bytes(&self) -> &[u8]; -} - -impl AsBytes for [f32] { - fn as_bytes(&self) -> &[u8] { - unsafe { - std::slice::from_raw_parts(self.as_ptr() as *const u8, std::mem::size_of_val(self)) - } - } -}