From d376b2f93ba5f56cee545b67cd00575ae5bf050f Mon Sep 17 00:00:00 2001 From: Darshana Sanjeewan Adikari Date: Sat, 29 Jun 2024 16:00:22 +0200 Subject: [PATCH 1/5] Use new API calls --- Cargo.lock | 12 ++++++------ Cargo.toml | 2 +- crates/object_detection/src/pose_detection.rs | 10 +++++----- 3 files changed, 12 insertions(+), 12 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..d3e59d1664 100644 --- a/crates/object_detection/src/pose_detection.rs +++ b/crates/object_detection/src/pose_detection.rs @@ -152,11 +152,11 @@ 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(), - )?; + let mut tensor = Tensor::new(ElementType::F32, &self.network.get_input()?.get_shape()?)?; + tensor + .buffer_mut()? + .copy_from_slice(self.scratchpad.as_bytes()); + infer_request.set_input_tensor(&tensor)?; { From e142c9811282fc773cba33215e9257ef29dcdb48 Mon Sep 17 00:00:00 2001 From: Darshana Sanjeewan Adikari Date: Sat, 29 Jun 2024 16:08:24 +0200 Subject: [PATCH 2/5] Fix some mistakes --- crates/object_detection/src/pose_detection.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/object_detection/src/pose_detection.rs b/crates/object_detection/src/pose_detection.rs index d3e59d1664..b80b6f30bb 100644 --- a/crates/object_detection/src/pose_detection.rs +++ b/crates/object_detection/src/pose_detection.rs @@ -153,8 +153,9 @@ impl PoseDetection { let mut infer_request = self.network.create_infer_request()?; let mut tensor = Tensor::new(ElementType::F32, &self.network.get_input()?.get_shape()?)?; + tensor - .buffer_mut()? + .get_raw_data_mut()? .copy_from_slice(self.scratchpad.as_bytes()); infer_request.set_input_tensor(&tensor)?; @@ -169,7 +170,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::()?)?; From 5ce35546138515d71ae7be21ea5f9f45365edfd6 Mon Sep 17 00:00:00 2001 From: Darshana Sanjeewan Adikari Date: Sat, 29 Jun 2024 23:06:28 +0200 Subject: [PATCH 3/5] Remove scratchpad state, directly fill the tensor instead (for now) --- crates/object_detection/src/pose_detection.rs | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/crates/object_detection/src/pose_detection.rs b/crates/object_detection/src/pose_detection.rs index b80b6f30bb..e00c76440e 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,6 @@ impl PoseDetection { } let mut infer_request = self.network.create_infer_request()?; - let mut tensor = Tensor::new(ElementType::F32, &self.network.get_input()?.get_shape()?)?; - - tensor - .get_raw_data_mut()? - .copy_from_slice(self.scratchpad.as_bytes()); infer_request.set_input_tensor(&tensor)?; @@ -269,6 +255,8 @@ fn non_maximum_suppression( poses } +// Allowing dead code for future use of `scratchpad` once it is restored. +#[allow(dead_code)] trait AsBytes { fn as_bytes(&self) -> &[u8]; } From 543b5078af433a0c8b0715f9eef34dcf1454ca87 Mon Sep 17 00:00:00 2001 From: Darshana Sanjeewan Adikari Date: Sat, 29 Jun 2024 23:12:01 +0200 Subject: [PATCH 4/5] Remove manually specifying generics --- crates/object_detection/src/pose_detection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/object_detection/src/pose_detection.rs b/crates/object_detection/src/pose_detection.rs index e00c76440e..d22561f832 100644 --- a/crates/object_detection/src/pose_detection.rs +++ b/crates/object_detection/src/pose_detection.rs @@ -133,7 +133,7 @@ impl PoseDetection { { let earlier = SystemTime::now(); - load_into_scratchpad(tensor.get_data_mut::()?, image); + load_into_scratchpad(tensor.get_data_mut()?, image); context.preprocess_duration.fill_if_subscribed(|| { SystemTime::now() From 877fc26b127010ccf0e632ed09c575dab10d28c3 Mon Sep 17 00:00:00 2001 From: Darshana Sanjeewan Adikari Date: Sun, 30 Jun 2024 14:29:54 +0200 Subject: [PATCH 5/5] Remove AsBytes trait --- crates/object_detection/src/pose_detection.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/crates/object_detection/src/pose_detection.rs b/crates/object_detection/src/pose_detection.rs index d22561f832..a997565648 100644 --- a/crates/object_detection/src/pose_detection.rs +++ b/crates/object_detection/src/pose_detection.rs @@ -254,17 +254,3 @@ fn non_maximum_suppression( poses } - -// Allowing dead code for future use of `scratchpad` once it is restored. -#[allow(dead_code)] -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)) - } - } -}