|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (C) 2019 Intel Corporation. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +# |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | +from pprint import pprint |
| 9 | +import re |
| 10 | +import shlex |
| 11 | +import shutil |
| 12 | +import subprocess |
| 13 | +from typing import List |
| 14 | + |
| 15 | + |
| 16 | +def execute_tflite_birds_v1_image_once( |
| 17 | + runtime_bin: str, runtime_args: List[str], cwd: Path |
| 18 | +) -> str: |
| 19 | + """ |
| 20 | + execute tflite_birds_v1_image example with |
| 21 | +
|
| 22 | + ``` |
| 23 | + iwasm --native-lib=somewhere/libwasi-nn-tflite.so --map-dir=.:. \ |
| 24 | + ./wasmedge-wasinn-example-tflite-bird-image.wasm \ |
| 25 | + lite-model_aiy_vision_classifier_birds_V1_3.tflite \ |
| 26 | + bird.jpg |
| 27 | + ``` |
| 28 | +
|
| 29 | + or |
| 30 | +
|
| 31 | + ``` |
| 32 | + wasmedge --dir=.:. \ |
| 33 | + ./wasmedge-wasinn-example-tflite-bird-image.wasm \ |
| 34 | + lite-model_aiy_vision_classifier_birds_V1_3.tflite \ |
| 35 | + bird.jpg |
| 36 | + ``` |
| 37 | +
|
| 38 | + assumption: |
| 39 | + - under the right directory, tflite-birds_v1-image |
| 40 | + - every materials are ready |
| 41 | + """ |
| 42 | + |
| 43 | + wasm_file = "./wasmedge-wasinn-example-tflite-bird-image.wasm" |
| 44 | + wasm_args = ["lite-model_aiy_vision_classifier_birds_V1_3.tflite", "bird.jpg"] |
| 45 | + |
| 46 | + cmd = [runtime_bin] |
| 47 | + cmd.extend(runtime_args) |
| 48 | + cmd.append(wasm_file) |
| 49 | + cmd.extend(wasm_args) |
| 50 | + |
| 51 | + try: |
| 52 | + p = subprocess.run( |
| 53 | + cmd, |
| 54 | + cwd=cwd, |
| 55 | + capture_output=True, |
| 56 | + check=True, |
| 57 | + text=True, |
| 58 | + universal_newlines=True, |
| 59 | + ) |
| 60 | + return p.stdout |
| 61 | + except subprocess.CalledProcessError as e: |
| 62 | + print(e.stderr) |
| 63 | + print() |
| 64 | + print(e.stdout) |
| 65 | + |
| 66 | + |
| 67 | +def filter_output_tflite_birds_v1_image(output: str) -> List[str]: |
| 68 | + """ |
| 69 | + not all output is needed for comparision |
| 70 | +
|
| 71 | + pick lines like: " 1.) [526](136)Cathartes burrovianus" |
| 72 | + """ |
| 73 | + filtered = [] |
| 74 | + PATTERN = re.compile(r"^\s+\d\.\)\s+\[\d+\]\(\d+\)\w+") |
| 75 | + for line in output.split("\n"): |
| 76 | + if PATTERN.search(line): |
| 77 | + filtered.append(line.strip()) |
| 78 | + |
| 79 | + return filtered |
| 80 | + |
| 81 | + |
| 82 | +def execute_tflite_birds_v1_image(iwasm_bin: str, wasmedge_bin: str, cwd: Path): |
| 83 | + iwasm_output = execute_tflite_birds_v1_image_once( |
| 84 | + iwasm_bin, |
| 85 | + [ |
| 86 | + "--native-lib=/workspaces/wamr/product-mini/platforms/linux/build/libwasi-nn-tflite.so", |
| 87 | + "--map-dir=.:.", |
| 88 | + ], |
| 89 | + cwd, |
| 90 | + ) |
| 91 | + iwasm_output = filter_output_tflite_birds_v1_image(iwasm_output) |
| 92 | + |
| 93 | + wasmedge_output = execute_tflite_birds_v1_image_once( |
| 94 | + wasmedge_bin, ["--dir=.:."], cwd |
| 95 | + ) |
| 96 | + wasmedge_output = filter_output_tflite_birds_v1_image(wasmedge_output) |
| 97 | + |
| 98 | + if iwasm_output == wasmedge_output: |
| 99 | + print("- tflite_birds_v1_image. PASS") |
| 100 | + return |
| 101 | + |
| 102 | + print("- tflite_birds_v1_image. FAILED") |
| 103 | + print("------------------------------------------------------------") |
| 104 | + pprint(iwasm_output) |
| 105 | + print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") |
| 106 | + pprint(wasmedge_output) |
| 107 | + print("------------------------------------------------------------") |
| 108 | + |
| 109 | + |
| 110 | +def execute_wasmedge_wasinn_exmaples(iwasm_bin: str, wasmedge_bin: str): |
| 111 | + assert Path.cwd().name == "wasmedge-wasinn-examples" |
| 112 | + assert shutil.which(iwasm_bin) |
| 113 | + assert shutil.which(wasmedge_bin) |
| 114 | + |
| 115 | + tflite_birds_v1_image_dir = Path.cwd().joinpath("./tflite-birds_v1-image") |
| 116 | + execute_tflite_birds_v1_image(iwasm_bin, wasmedge_bin, tflite_birds_v1_image_dir) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + execute_wasmedge_wasinn_exmaples("iwasm", "wasmedge") |
0 commit comments