diff --git a/Cargo.lock b/Cargo.lock index 62ab519..ac39c0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1299,6 +1299,7 @@ dependencies = [ [[package]] name = "mnn" version = "0.2.0" +source = "git+https://github.com/uttarayan21/mnn-rs?branch=restructure-tensor-type#4128b5b40e03c8744fc0e68f6684ef8a2dd971e5" dependencies = [ "dunce", "error-stack", @@ -1312,7 +1313,7 @@ dependencies = [ [[package]] name = "mnn-bridge" version = "0.1.0" -source = "git+https://github.com/uttarayan21/mnn-rs?branch=restructure-tensor-type#456c53307ff551d8cb8e4e380c7febf7c16ba0ab" +source = "git+https://github.com/uttarayan21/mnn-rs?branch=restructure-tensor-type#4128b5b40e03c8744fc0e68f6684ef8a2dd971e5" dependencies = [ "error-stack", "mnn", @@ -1322,7 +1323,7 @@ dependencies = [ [[package]] name = "mnn-sync" version = "0.1.0" -source = "git+https://github.com/uttarayan21/mnn-rs?branch=restructure-tensor-type#456c53307ff551d8cb8e4e380c7febf7c16ba0ab" +source = "git+https://github.com/uttarayan21/mnn-rs?branch=restructure-tensor-type#4128b5b40e03c8744fc0e68f6684ef8a2dd971e5" dependencies = [ "error-stack", "flume", @@ -1334,6 +1335,7 @@ dependencies = [ [[package]] name = "mnn-sys" version = "0.1.0" +source = "git+https://github.com/uttarayan21/mnn-rs?branch=restructure-tensor-type#4128b5b40e03c8744fc0e68f6684ef8a2dd971e5" dependencies = [ "anyhow", "bindgen", diff --git a/Cargo.toml b/Cargo.toml index a727093..83cbee7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,15 +5,12 @@ members = ["ndarray-image", "ndarray-resize", ".", "bounding-box"] version = "0.1.0" edition = "2024" -[patch."https://github.com/uttarayan21/mnn-rs"] -mnn = { path = "/Users/fs0c131y/Projects/aftershoot/mnn-rs" } - [workspace.dependencies] ndarray-image = { path = "ndarray-image" } ndarray-resize = { path = "ndarray-resize" } mnn = { git = "https://github.com/uttarayan21/mnn-rs", version = "0.2.0", features = [ - "metal", - "coreml", + # "metal", + # "coreml", "tracing", ], branch = "restructure-tensor-type" } mnn-bridge = { git = "https://github.com/uttarayan21/mnn-rs", version = "0.1.0", features = [ @@ -53,7 +50,11 @@ bounding-box = { version = "0.1.0", path = "bounding-box" } color = "0.3.1" itertools = "0.14.0" ordered-float = "5.0.0" -ort = { version = "2.0.0-rc.10", default-features = false, features = [ "std", "tracing", "ndarray"]} +ort = { version = "2.0.0-rc.10", default-features = false, features = [ + "std", + "tracing", + "ndarray", +] } [profile.release] debug = true @@ -65,5 +66,7 @@ ort-tensorrt = ["ort/tensorrt"] ort-tvm = ["ort/tvm"] ort-openvino = ["ort/openvino"] ort-directml = ["ort/directml"] +mnn-metal = ["mnn/metal"] +mnn-coreml = ["mnn/coreml"] -default = ["ort-coreml"] +default = [] diff --git a/flake.nix b/flake.nix index a91c177..3d82597 100644 --- a/flake.nix +++ b/flake.nix @@ -52,7 +52,7 @@ mnn = mnn-overlay.packages.${system}.mnn.override { src = mnn-src; buildConverter = true; - enableMetal = true; + enableMetal = pkgs.stdenv.isDarwin; enableOpencl = true; }; }) diff --git a/src/facedet/retinaface/mnn.rs b/src/facedet/retinaface/mnn.rs index 94c2346..7422028 100644 --- a/src/facedet/retinaface/mnn.rs +++ b/src/facedet/retinaface/mnn.rs @@ -61,35 +61,6 @@ impl FaceDetection { ) -> std::result::Result> { FaceDetectionBuilder::new(model) } - - pub fn new(path: impl AsRef) -> Result { - let model = std::fs::read(path) - .change_context(Error) - .attach_printable("Failed to read model file")?; - Self::new_from_bytes(&model) - } - - pub fn new_from_bytes(model: &[u8]) -> Result { - tracing::info!("Loading face detection model from bytes"); - let mut model = mnn::Interpreter::from_bytes(model) - .map_err(|e| e.into_inner()) - .change_context(Error) - .attach_printable("Failed to load model from bytes")?; - model.set_session_mode(mnn::SessionMode::Release); - model - .set_cache_file("retinaface.cache", 128) - .change_context(Error) - .attach_printable("Failed to set cache file")?; - let bc = mnn::BackendConfig::default().with_memory_mode(mnn::MemoryMode::High); - let sc = mnn::ScheduleConfig::new() - .with_type(mnn::ForwardType::Metal) - .with_backend_config(bc); - tracing::info!("Creating session handle for face detection model"); - let handle = mnn_sync::SessionHandle::new(model, sc) - .change_context(Error) - .attach_printable("Failed to create session handle")?; - Ok(FaceDetection { handle }) - } } impl FaceDetector for FaceDetection { diff --git a/src/faceembed/facenet/mnn.rs b/src/faceembed/facenet/mnn.rs index 4f305a5..c6819e5 100644 --- a/src/faceembed/facenet/mnn.rs +++ b/src/faceembed/facenet/mnn.rs @@ -56,12 +56,6 @@ impl EmbeddingGeneratorBuilder { impl EmbeddingGenerator { const INPUT_NAME: &'static str = "serving_default_input_6:0"; const OUTPUT_NAME: &'static str = "StatefulPartitionedCall:0"; - pub fn new(path: impl AsRef) -> Result { - let model = std::fs::read(path) - .change_context(Error) - .attach_printable("Failed to read model file")?; - Self::new_from_bytes(&model) - } pub fn builder>( model: T, @@ -69,28 +63,6 @@ impl EmbeddingGenerator { EmbeddingGeneratorBuilder::new(model) } - pub fn new_from_bytes(model: &[u8]) -> Result { - tracing::info!("Loading face embedding model from bytes"); - let mut model = mnn::Interpreter::from_bytes(model) - .map_err(|e| e.into_inner()) - .change_context(Error) - .attach_printable("Failed to load model from bytes")?; - model.set_session_mode(mnn::SessionMode::Release); - model - .set_cache_file("facenet.cache", 128) - .change_context(Error) - .attach_printable("Failed to set cache file")?; - let bc = mnn::BackendConfig::default().with_memory_mode(mnn::MemoryMode::High); - let sc = mnn::ScheduleConfig::new() - .with_type(mnn::ForwardType::Metal) - .with_backend_config(bc); - tracing::info!("Creating session handle for face embedding model"); - let handle = mnn_sync::SessionHandle::new(model, sc) - .change_context(Error) - .attach_printable("Failed to create session handle")?; - Ok(Self { handle }) - } - pub fn run_models(&self, face: ArrayView4) -> Result> { let tensor = face // .permuted_axes((0, 3, 1, 2))