-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
162 lines (122 loc) · 3.84 KB
/
app.py
File metadata and controls
162 lines (122 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import streamlit as st
import numpy as np
import onnxruntime as ort
from PIL import Image
from huggingface_hub import hf_hub_download
import time
import os
st.set_page_config(
page_title="WaferShield AI",
page_icon="",
layout="wide"
)
MODEL_REPO = "coder0304/wafer-defect-classification"
MODEL_FILENAME = "model_fp16.onnx"
CLASS_NAMES = [
"Center",
"Clean",
"Donut",
"Edge-Loc",
"Edge-Ring",
"Loc",
"Random",
"Scratch"
]
@st.cache_resource
def load_model():
model_path = hf_hub_download(
repo_id=MODEL_REPO,
filename=MODEL_FILENAME
)
session = ort.InferenceSession(
model_path,
providers=["CPUExecutionProvider"]
)
return session
session = load_model()
input_name = session.get_inputs()[0].name
def preprocess_image(image: Image.Image):
image = image.convert("RGB")
image = image.resize((320, 320))
img_array = (np.array(image).astype(np.float32) / 255.0).astype(np.float16)
img_array = np.transpose(img_array, (2, 0, 1)) # HWC → CHW
img_array = np.expand_dims(img_array, axis=0) # Add batch dim
return img_array
st.sidebar.title("System Information")
st.sidebar.markdown("### Model")
st.sidebar.markdown("- **Architecture:** EfficientNet-Lite0")
st.sidebar.markdown("- **Format:** FP16 ONNX")
st.sidebar.markdown("- **Size:** 6.76 MB")
st.sidebar.markdown("### Edge Benchmark")
st.sidebar.markdown("- **Accuracy:** 89.77% (ONNX)")
st.sidebar.markdown("- **Latency:** 8.55 ms")
st.sidebar.markdown("- **Throughput:** 116.9 img/sec")
st.sidebar.markdown("- **Provider:** ONNX Runtime CPU")
st.sidebar.markdown("---")
st.sidebar.markdown("Built for Edge AI Deployment")
st.title("WaferShield AI")
st.markdown("### Edge AI Semiconductor Defect Classification System")
st.markdown(
"""
Upload a wafer inspection image to classify defect type using a lightweight
edge-optimized EfficientNet-Lite0 model deployed in FP16 ONNX format.
"""
)
st.markdown("---")
uploaded_file = st.file_uploader(
"Upload Wafer Image",
type=["png", "jpg", "jpeg"]
)
if uploaded_file:
col1, col2 = st.columns(2)
image = Image.open(uploaded_file)
with col1:
st.subheader("Original Wafer Image")
st.image(image, width="stretch")
# Preprocess
input_tensor = preprocess_image(image)
# Inference
start_time = time.time()
outputs = session.run(None, {input_name: input_tensor})
end_time = time.time()
inference_time_ms = (end_time - start_time) * 1000
logits = outputs[0]
probabilities = np.exp(logits) / np.sum(np.exp(logits), axis=1, keepdims=True)
predicted_index = np.argmax(probabilities)
predicted_class = CLASS_NAMES[predicted_index]
confidence = probabilities[0][predicted_index] * 100
with col2:
st.subheader("Prediction Results")
st.metric(
label="Predicted Defect",
value=predicted_class
)
st.metric(
label="Confidence",
value=f"{confidence:.2f}%"
)
st.metric(
label="Inference Time",
value=f"{inference_time_ms:.2f} ms"
)
st.progress(float(confidence) / 100.0)
st.markdown("---")
st.subheader("Class Probability Distribution")
prob_dict = {
CLASS_NAMES[i]: float(probabilities[0][i])
for i in range(len(CLASS_NAMES))
}
st.bar_chart(prob_dict)
st.markdown("---")
st.subheader("Explainability (Grad-CAM)")
st.info(
"Grad-CAM visualizations are generated during evaluation phase. "
"You can integrate live Grad-CAM inference for production explainability."
)
st.markdown("---")
st.markdown(
"""
**WaferShield AI** - Lightweight Edge-AI system for real-time semiconductor defect inspection.
Designed for deployment in resource-constrained fabrication environments.
"""
)