-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Add the named model feature into pytorch demo. (#27)
Signed-off-by: vincent <vincent@secondstate.io>
- Loading branch information
1 parent
841fac1
commit 3db57ce
Showing
5 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use image; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use wasi_nn; | ||
mod imagenet_classes; | ||
|
||
pub fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
let model_name: &str = &args[1]; | ||
let image_name: &str = &args[2]; | ||
|
||
let graph = wasi_nn::GraphBuilder::new( | ||
wasi_nn::GraphEncoding::Autodetec, | ||
wasi_nn::ExecutionTarget::CPU, | ||
) | ||
.build_from_cache(model_name) | ||
.unwrap(); | ||
println!("Loaded graph into wasi-nn with ID: {:?}", graph); | ||
|
||
let mut context = graph.init_execution_context().unwrap(); | ||
println!("Created wasi-nn execution context with ID: {:?}", context); | ||
|
||
// Load a tensor that precisely matches the graph input tensor (see | ||
let tensor_data = image_to_tensor(image_name.to_string(), 224, 224); | ||
println!("Read input tensor, size in bytes: {}", tensor_data.len()); | ||
context | ||
.set_input(0, wasi_nn::TensorType::F32, &[1, 3, 224, 224], &tensor_data) | ||
.unwrap(); | ||
// Execute the inference. | ||
context.compute().unwrap(); | ||
println!("Executed graph inference"); | ||
// Retrieve the output. | ||
let mut output_buffer = vec![0f32; 1000]; | ||
context.get_output(0, &mut output_buffer).unwrap(); | ||
|
||
let results = sort_results(&output_buffer); | ||
for i in 0..5 { | ||
println!( | ||
" {}.) [{}]({:.4}){}", | ||
i + 1, | ||
results[i].0, | ||
results[i].1, | ||
imagenet_classes::IMAGENET_CLASSES[results[i].0] | ||
); | ||
} | ||
} | ||
|
||
// Sort the buffer of probabilities. The graph places the match probability for each class at the | ||
// index for that class (e.g. the probability of class 42 is placed at buffer[42]). Here we convert | ||
// to a wrapping InferenceResult and sort the results. | ||
fn sort_results(buffer: &[f32]) -> Vec<InferenceResult> { | ||
let mut results: Vec<InferenceResult> = buffer | ||
.iter() | ||
.enumerate() | ||
.map(|(c, p)| InferenceResult(c, *p)) | ||
.collect(); | ||
results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); | ||
results | ||
} | ||
|
||
// Take the image located at 'path', open it, resize it to height x width, and then converts | ||
// the pixel precision to FP32. The resulting BGR pixel vector is then returned. | ||
fn image_to_tensor(path: String, height: u32, width: u32) -> Vec<u8> { | ||
let mut file_img = File::open(path).unwrap(); | ||
let mut img_buf = Vec::new(); | ||
file_img.read_to_end(&mut img_buf).unwrap(); | ||
let img = image::load_from_memory(&img_buf).unwrap().to_rgb8(); | ||
let resized = | ||
image::imageops::resize(&img, height, width, ::image::imageops::FilterType::Triangle); | ||
let mut flat_img: Vec<f32> = Vec::new(); | ||
for rgb in resized.pixels() { | ||
flat_img.push((rgb[0] as f32 / 255. - 0.485) / 0.229); | ||
flat_img.push((rgb[1] as f32 / 255. - 0.456) / 0.224); | ||
flat_img.push((rgb[2] as f32 / 255. - 0.406) / 0.225); | ||
} | ||
let bytes_required = flat_img.len() * 4; | ||
let mut u8_f32_arr: Vec<u8> = vec![0; bytes_required]; | ||
|
||
for c in 0..3 { | ||
for i in 0..(flat_img.len() / 3) { | ||
// Read the number as a f32 and break it into u8 bytes | ||
let u8_f32: f32 = flat_img[i * 3 + c] as f32; | ||
let u8_bytes = u8_f32.to_ne_bytes(); | ||
|
||
for j in 0..4 { | ||
u8_f32_arr[((flat_img.len() / 3 * c + i) * 4) + j] = u8_bytes[j]; | ||
} | ||
} | ||
} | ||
return u8_f32_arr; | ||
} | ||
|
||
// A wrapper for class ID and match probabilities. | ||
#[derive(Debug, PartialEq)] | ||
struct InferenceResult(usize, f32); |
Binary file added
BIN
+2.93 MB
pytorch-mobilenet-image/wasmedge-wasinn-example-mobilenet-image-named-model.wasm
Binary file not shown.
Binary file modified
BIN
+5.79 KB
(100%)
pytorch-mobilenet-image/wasmedge-wasinn-example-mobilenet-image.wasm
Binary file not shown.