Skip to content

Commit e7f989a

Browse files
Merge pull request #37 from google-ai-edge:add-mobilenetv3large-example
PiperOrigin-RevId: 917269356
2 parents ce31b33 + b19fc32 commit e7f989a

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

examples/models/mobilenet.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
# Copyright 2026 The LiteRT CLI Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ==============================================================================
16+
17+
# LiteRT CLI MobileNet Demo & Test Script
18+
set -e
19+
20+
# Source shared utilities relative to script
21+
source "$(dirname "${BASH_SOURCE[0]}")/../utils.sh"
22+
23+
setup_test_env "mobilenet" "MobileNet Demo Script"
24+
25+
# --- 1. Download MobileNet-v3-large model ---
26+
run_case "Download: MobileNet from HuggingFace" \
27+
litert download litert-community/MobileNet-v3-large --file "*.tflite" --output "models/mobilenet"
28+
29+
# Verify the downloaded model exists (Updated to match exact HF filename)
30+
MOBILENET_TFLITE="models/mobilenet/mobilenet_v3_large.tflite"
31+
if [ ! -f "$MOBILENET_TFLITE" ]; then
32+
echo -e "${RED}Error: Downloaded model not found at $MOBILENET_TFLITE${NC}"
33+
exit 1
34+
fi
35+
36+
# --- 2. Quantize the MobileNet model ---
37+
run_case "Quantize: MobileNet Dynamic Range INT8" \
38+
litert quantize "$MOBILENET_TFLITE" --recipe dynamic_wi8_afp32 --output "models/mobilenet/mobilenet_v3_large_int8_dynamic.tflite"
39+
40+
run_case "Quantize: MobileNet Weight-Only INT8" \
41+
litert quantize "$MOBILENET_TFLITE" --recipe weight_only_wi8_afp32 --output "models/mobilenet/mobilenet_v3_large_int8_weight_only.tflite"
42+
43+
# --- 3. Run Inference (Desktop & Android) ---
44+
run_case "Run: MobileNet FP32 on Desktop (CPU)" \
45+
litert run "$MOBILENET_TFLITE" --desktop --cpu --iterations 1
46+
47+
if has_desktop_gpu "$MOBILENET_TFLITE"; then
48+
run_case "Run: MobileNet FP32 on Desktop (GPU)" \
49+
litert run "$MOBILENET_TFLITE" --desktop --gpu --iterations 1
50+
else
51+
echo -e "\n${YELLOW}Desktop GPU delegate is not supported. Skipping Desktop GPU run.${NC}"
52+
fi
53+
54+
run_case "Run: MobileNet Dynamic INT8 on Desktop (CPU)" \
55+
litert run "models/mobilenet/mobilenet_v3_large_int8_dynamic.tflite" --desktop --cpu --iterations 1
56+
57+
if has_android_device; then
58+
echo -e "\n${GREEN}Android device detected. Running Android inference...${NC}"
59+
run_case "Run: MobileNet FP32 on Android (CPU)" \
60+
litert run "$MOBILENET_TFLITE" --android --cpu --iterations 1
61+
62+
run_case "Run: MobileNet FP32 on Android (GPU)" \
63+
litert run "$MOBILENET_TFLITE" --android --gpu --iterations 1
64+
65+
# If you have Android devices with NPU connected, enable those use cases for MacOS
66+
# run_case "Run: MobileNet FP32 on Android (NPU)" \
67+
# litert run "$MOBILENET_TFLITE" --android --npu --iterations 1
68+
69+
run_case "Run: MobileNet Dynamic INT8 on Android (CPU)" \
70+
litert run "models/mobilenet/mobilenet_v3_large_int8_dynamic.tflite" --android --cpu --iterations 1
71+
fi
72+
73+
# --- 4. Benchmark (Desktop & Android) ---
74+
run_case "Benchmark: MobileNet FP32 on Desktop (CPU)" \
75+
litert benchmark "$MOBILENET_TFLITE" --desktop --cpu
76+
77+
run_case "Benchmark: MobileNet Dynamic INT8 on Desktop (CPU)" \
78+
litert benchmark "models/mobilenet/mobilenet_v3_large_int8_dynamic.tflite" --desktop --cpu
79+
80+
if has_android_device; then
81+
echo -e "\n${GREEN}Android device detected. Running Android benchmarks...${NC}"
82+
run_case "Benchmark: MobileNet FP32 on Android (CPU)" \
83+
litert benchmark "$MOBILENET_TFLITE" --android
84+
85+
run_case "Benchmark: MobileNet FP32 on Android (GPU)" \
86+
litert benchmark "$MOBILENET_TFLITE" --android --gpu
87+
88+
# If you have Android devices with NPU connected, enable those use cases for MacOS
89+
# run_case "Benchmark: MobileNet FP32 on Android (NPU)" \
90+
# litert benchmark "$MOBILENET_TFLITE" --android --npu
91+
92+
run_case "Benchmark: MobileNet Dynamic INT8 on Android" \
93+
litert benchmark "models/mobilenet/mobilenet_v3_large_int8_dynamic.tflite" --android
94+
else
95+
echo -e "\n${YELLOW}No Android device detected. Skipping benchmarks on Android.${NC}"
96+
fi
97+
98+
# --- 5. Compile (AOT Compilation) ---
99+
if [[ "$(uname)" == "Linux" ]]; then
100+
run_case "Compile: MobileNet FP32 for Qualcomm sm8750 NPU" \
101+
litert compile "$MOBILENET_TFLITE" --target sm8750 --output-dir "models/mobilenet"
102+
103+
else
104+
echo -e "\n${YELLOW}Skipping offline AOT compilation on non-Linux platform ($(uname)).${NC}"
105+
fi
106+
107+
# --- 6. Visualize (Model Explorer) ---
108+
run_case "Visualize: Launch Model Explorer in the background" \
109+
litert visualize "$MOBILENET_TFLITE"
110+
111+
run_case "Visualize: Stop all Model Explorer servers" \
112+
litert visualize --stop-all
113+
114+
# --- Summary Report ---
115+
print_summary_report "MobileNet"

0 commit comments

Comments
 (0)