Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +33 -33
src/streamlit_app.py
CHANGED
|
@@ -1,34 +1,34 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
import torchvision.transforms as transforms
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
# โหลดโมเดล
|
| 7 |
-
model =
|
| 8 |
-
model.
|
| 9 |
-
|
| 10 |
-
# โหลด label
|
| 11 |
-
with open('classes.txt', 'r') as f:
|
| 12 |
-
class_names = [line.strip() for line in f]
|
| 13 |
-
|
| 14 |
-
# UI
|
| 15 |
-
st.title("🔥 MobileNetV2 Classifier")
|
| 16 |
-
uploaded_file = st.file_uploader("อัปโหลดภาพ", type=["jpg", "png", "jpeg"])
|
| 17 |
-
|
| 18 |
-
if uploaded_file:
|
| 19 |
-
image = Image.open(uploaded_file).convert("RGB")
|
| 20 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 21 |
-
|
| 22 |
-
transform = transforms.Compose([
|
| 23 |
-
transforms.Resize((224, 224)),
|
| 24 |
-
transforms.ToTensor(),
|
| 25 |
-
])
|
| 26 |
-
img_tensor = transform(image).unsqueeze(0)
|
| 27 |
-
|
| 28 |
-
with torch.no_grad():
|
| 29 |
-
outputs = model(img_tensor)
|
| 30 |
-
probs = torch.softmax(outputs, dim=1)
|
| 31 |
-
pred_class = class_names[probs.argmax().item()]
|
| 32 |
-
confidence = probs.max().item() * 100
|
| 33 |
-
|
| 34 |
st.markdown(f"### 🔍 คำทำนาย: `{pred_class}` ({confidence:.2f}%)")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# โหลดโมเดล
|
| 7 |
+
model = models.mobilenet_v2(pretrained=True)
|
| 8 |
+
model.classifier[1] = torch.nn.Linear(model.last_channel, 3) # 3 class
|
| 9 |
+
model.load_state_dict(torch.load('mobilenetv2.pth', map_location=device))
|
| 10 |
+
# โหลด label
|
| 11 |
+
with open('classes.txt', 'r') as f:
|
| 12 |
+
class_names = [line.strip() for line in f]
|
| 13 |
+
|
| 14 |
+
# UI
|
| 15 |
+
st.title("🔥 MobileNetV2 Classifier")
|
| 16 |
+
uploaded_file = st.file_uploader("อัปโหลดภาพ", type=["jpg", "png", "jpeg"])
|
| 17 |
+
|
| 18 |
+
if uploaded_file:
|
| 19 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 20 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 21 |
+
|
| 22 |
+
transform = transforms.Compose([
|
| 23 |
+
transforms.Resize((224, 224)),
|
| 24 |
+
transforms.ToTensor(),
|
| 25 |
+
])
|
| 26 |
+
img_tensor = transform(image).unsqueeze(0)
|
| 27 |
+
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(img_tensor)
|
| 30 |
+
probs = torch.softmax(outputs, dim=1)
|
| 31 |
+
pred_class = class_names[probs.argmax().item()]
|
| 32 |
+
confidence = probs.max().item() * 100
|
| 33 |
+
|
| 34 |
st.markdown(f"### 🔍 คำทำนาย: `{pred_class}` ({confidence:.2f}%)")
|