diff --git a/examples/wasm-emscripten/Cargo.toml b/examples/wasm-emscripten/Cargo.toml index dbb7fd4b..23477f55 100644 --- a/examples/wasm-emscripten/Cargo.toml +++ b/examples/wasm-emscripten/Cargo.toml @@ -9,10 +9,19 @@ path = "src/main.rs" name = "wasm_example" [dependencies] -ort = { path = "../../", default-features = false, features = ["ndarray", "webgpu", "download-binaries"] } +ort-tract = "0.1.0+0.21" +#ort = { path = "../../", default-features = false, features = ["ndarray", "download-binaries"] } ndarray = "0.16" image = "0.25" [build-dependencies] glob = "0.3" reqwest = { version = "0.12", features = ["blocking"] } + +[dependencies.ort] +version = "=2.0.0-rc.10" +default-features = false # Disables the `download-binaries` feature since we don't need it +features = [ + "ndarray", + "alternative-backend" +] \ No newline at end of file diff --git a/examples/wasm-emscripten/serve.py b/examples/wasm-emscripten/serve.py index afd6b649..a949e8dc 100644 --- a/examples/wasm-emscripten/serve.py +++ b/examples/wasm-emscripten/serve.py @@ -23,6 +23,6 @@ def end_headers (self): # Serve index.html. chdir(path.join(path.dirname(__file__), f"target/wasm32-unknown-emscripten/{mode}")) -httpd = HTTPServer(("localhost", 5555), CORSRequestHandler) -print(f"Serving {mode} build at: http://localhost:5555") +httpd = HTTPServer(("0.0.0.0", 5555), CORSRequestHandler) +print(f"Serving {mode} build at: http://0.0.0.0:5555") httpd.serve_forever() diff --git a/examples/wasm-emscripten/src/main.rs b/examples/wasm-emscripten/src/main.rs index 2c16bf40..7526b231 100644 --- a/examples/wasm-emscripten/src/main.rs +++ b/examples/wasm-emscripten/src/main.rs @@ -39,23 +39,18 @@ pub extern "C" fn dealloc(ptr: *mut std::os::raw::c_void, size: usize) { #[no_mangle] pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) { + ort::set_api(ort_tract::api()); ort::init() - .with_global_thread_pool(ort::environment::GlobalThreadPoolOptions::default()) .commit() .expect("Cannot initialize ort."); let mut builder = ort::session::Session::builder() .expect("Cannot create Session builder.") .with_optimization_level(ort::session::builder::GraphOptimizationLevel::Level3) - .expect("Cannot optimize graph.") - .with_parallel_execution(true) - .expect("Cannot activate parallel execution.") - .with_intra_threads(2) - .expect("Cannot set intra thread count.") - .with_inter_threads(1) - .expect("Cannot set inter thread count."); + .expect("Cannot optimize graph."); - let use_webgpu = true; // TODO: Make `use_webgpu` a parameter of `detect_objects`? Or say in README to change it here. + + let use_webgpu = false; // TODO: Make `use_webgpu` a parameter of `detect_objects`? Or say in README to change it here. if use_webgpu { use ort::execution_providers::ExecutionProvider; let ep = ort::execution_providers::WebGPUExecutionProvider::default(); @@ -66,10 +61,14 @@ pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) { } } + let time_start = std::time::Instant::now(); let mut session = builder .commit_from_memory(include_bytes!("../yolov8m.onnx")) .expect("Cannot commit model."); + println!("Model loaded in {} ms", time_start.elapsed().as_millis()); + + let time_start = std::time::Instant::now(); let image_data = unsafe { std::slice::from_raw_parts(ptr, (width * height * 4) as usize).to_vec() }; // Copy via .to_vec might be not necessary as memory lives long enough. let image = image::ImageBuffer::, Vec>::from_raw(width, height, image_data).expect("Cannot parse input image."); let image640 = image::imageops::resize(&image, 640, 640, image::imageops::FilterType::CatmullRom); @@ -79,6 +78,7 @@ pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) { let outputs: ort::session::SessionOutputs = session.run(ort::inputs!["images" => tensor]).unwrap(); let output = outputs["output0"].try_extract_array::().unwrap().t().into_owned(); + println!("time to infer in {} ms", time_start.elapsed().as_millis()); let mut boxes = Vec::new(); let output = output.slice(ndarray::s![.., .., 0]);