Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion examples/wasm-emscripten/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
4 changes: 2 additions & 2 deletions examples/wasm-emscripten/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
18 changes: 9 additions & 9 deletions examples/wasm-emscripten/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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::<image::Rgba<u8>, Vec<u8>>::from_raw(width, height, image_data).expect("Cannot parse input image.");
let image640 = image::imageops::resize(&image, 640, 640, image::imageops::FilterType::CatmullRom);
Expand All @@ -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::<f32>().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]);
Expand Down