Full catalogue sync: example.py
Browse files- example.py +7 -16
example.py
CHANGED
|
@@ -27,14 +27,14 @@ from pathlib import Path
|
|
| 27 |
|
| 28 |
import numpy as np
|
| 29 |
from PIL import Image
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
SCRIPT_DIR = Path(__file__).resolve().parent
|
| 33 |
REPO_ROOT = SCRIPT_DIR.parents[2]
|
| 34 |
|
| 35 |
-
MODEL_PATH = SCRIPT_DIR / "deit-
|
| 36 |
IMAGE_PATH = SCRIPT_DIR / "sample_input.jpg"
|
| 37 |
-
LABELS_PATH = SCRIPT_DIR / "imagenet_classes.json"
|
| 38 |
DEFAULT_WORKDIR = SCRIPT_DIR / "fvp_workdir"
|
| 39 |
DEFAULT_RUNNER_ELF = (
|
| 40 |
REPO_ROOT / "third_party/executorch-arm-runner/cmake-out/arm_executor_runner"
|
|
@@ -49,6 +49,10 @@ TOP_K = 5
|
|
| 49 |
NUM_MACS = 256
|
| 50 |
SEMIHOSTING_CMD_LINE_MAX = 256
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def parse_args() -> argparse.Namespace:
|
| 54 |
parser = argparse.ArgumentParser(
|
|
@@ -56,7 +60,6 @@ def parse_args() -> argparse.Namespace:
|
|
| 56 |
)
|
| 57 |
parser.add_argument("--model", type=Path, default=MODEL_PATH)
|
| 58 |
parser.add_argument("--image", type=Path, default=IMAGE_PATH)
|
| 59 |
-
parser.add_argument("--labels", type=Path, default=LABELS_PATH)
|
| 60 |
parser.add_argument("--workdir", type=Path, default=DEFAULT_WORKDIR)
|
| 61 |
parser.add_argument(
|
| 62 |
"--fvp-bin",
|
|
@@ -94,16 +97,6 @@ def preprocess(image_path: Path) -> np.ndarray:
|
|
| 94 |
return np.expand_dims(array.astype(np.float32), axis=0)
|
| 95 |
|
| 96 |
|
| 97 |
-
def load_labels(labels_path: Path) -> list[str] | None:
|
| 98 |
-
if not labels_path.exists():
|
| 99 |
-
return None
|
| 100 |
-
with labels_path.open() as f:
|
| 101 |
-
labels = json.load(f)
|
| 102 |
-
if isinstance(labels, dict):
|
| 103 |
-
return [labels[str(i)] for i in range(len(labels))]
|
| 104 |
-
return labels
|
| 105 |
-
|
| 106 |
-
|
| 107 |
def build_fvp_cmd(
|
| 108 |
*,
|
| 109 |
fvp_bin: str,
|
|
@@ -230,7 +223,6 @@ def main() -> None:
|
|
| 230 |
args = parse_args()
|
| 231 |
model_path = args.model.resolve()
|
| 232 |
image_path = args.image.resolve()
|
| 233 |
-
labels_path = args.labels.resolve()
|
| 234 |
runner_elf = args.runner_elf.resolve()
|
| 235 |
|
| 236 |
if not model_path.exists():
|
|
@@ -243,7 +235,6 @@ def main() -> None:
|
|
| 243 |
"Build it with make install-corstone, or pass --runner-elf."
|
| 244 |
)
|
| 245 |
|
| 246 |
-
labels = load_labels(labels_path)
|
| 247 |
input_tensor = preprocess(image_path)
|
| 248 |
raw_output = run_fvp(
|
| 249 |
model_path=model_path,
|
|
@@ -253,7 +244,7 @@ def main() -> None:
|
|
| 253 |
runner_elf=runner_elf,
|
| 254 |
timelimit=args.timelimit,
|
| 255 |
)
|
| 256 |
-
results = postprocess(raw_output,
|
| 257 |
|
| 258 |
print("Top-5 predictions:")
|
| 259 |
for rank, item in enumerate(results, start=1):
|
|
|
|
| 27 |
|
| 28 |
import numpy as np
|
| 29 |
from PIL import Image
|
| 30 |
+
from torchvision.models import GoogLeNet_Weights
|
| 31 |
|
| 32 |
|
| 33 |
SCRIPT_DIR = Path(__file__).resolve().parent
|
| 34 |
REPO_ROOT = SCRIPT_DIR.parents[2]
|
| 35 |
|
| 36 |
+
MODEL_PATH = SCRIPT_DIR / "deit-tiny_ethos_ethosu_optimized.pte"
|
| 37 |
IMAGE_PATH = SCRIPT_DIR / "sample_input.jpg"
|
|
|
|
| 38 |
DEFAULT_WORKDIR = SCRIPT_DIR / "fvp_workdir"
|
| 39 |
DEFAULT_RUNNER_ELF = (
|
| 40 |
REPO_ROOT / "third_party/executorch-arm-runner/cmake-out/arm_executor_runner"
|
|
|
|
| 49 |
NUM_MACS = 256
|
| 50 |
SEMIHOSTING_CMD_LINE_MAX = 256
|
| 51 |
|
| 52 |
+
# ImageNet class labels (1000 classes)
|
| 53 |
+
WEIGHTS = GoogLeNet_Weights.IMAGENET1K_V1
|
| 54 |
+
IMAGENET_CLASSES = WEIGHTS.meta["categories"]
|
| 55 |
+
|
| 56 |
|
| 57 |
def parse_args() -> argparse.Namespace:
|
| 58 |
parser = argparse.ArgumentParser(
|
|
|
|
| 60 |
)
|
| 61 |
parser.add_argument("--model", type=Path, default=MODEL_PATH)
|
| 62 |
parser.add_argument("--image", type=Path, default=IMAGE_PATH)
|
|
|
|
| 63 |
parser.add_argument("--workdir", type=Path, default=DEFAULT_WORKDIR)
|
| 64 |
parser.add_argument(
|
| 65 |
"--fvp-bin",
|
|
|
|
| 97 |
return np.expand_dims(array.astype(np.float32), axis=0)
|
| 98 |
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def build_fvp_cmd(
|
| 101 |
*,
|
| 102 |
fvp_bin: str,
|
|
|
|
| 223 |
args = parse_args()
|
| 224 |
model_path = args.model.resolve()
|
| 225 |
image_path = args.image.resolve()
|
|
|
|
| 226 |
runner_elf = args.runner_elf.resolve()
|
| 227 |
|
| 228 |
if not model_path.exists():
|
|
|
|
| 235 |
"Build it with make install-corstone, or pass --runner-elf."
|
| 236 |
)
|
| 237 |
|
|
|
|
| 238 |
input_tensor = preprocess(image_path)
|
| 239 |
raw_output = run_fvp(
|
| 240 |
model_path=model_path,
|
|
|
|
| 244 |
runner_elf=runner_elf,
|
| 245 |
timelimit=args.timelimit,
|
| 246 |
)
|
| 247 |
+
results = postprocess(raw_output, IMAGENET_CLASSES)
|
| 248 |
|
| 249 |
print("Top-5 predictions:")
|
| 250 |
for rank, item in enumerate(results, start=1):
|