Spaces:
Runtime error
Runtime error
Thomas J. Trebat
commited on
Commit
·
135173a
1
Parent(s):
474c480
Added streamlit classifier widget
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import timm
|
2 |
import torch
|
3 |
-
import
|
4 |
-
from PIL import Image
|
5 |
from timm.data import resolve_data_config
|
6 |
from timm.data.transforms_factory import create_transform
|
7 |
|
@@ -10,25 +9,20 @@ model = timm.create_model(
|
|
10 |
'hf-hub:nateraw/resnet50-oxford-iiit-pet',
|
11 |
pretrained=True
|
12 |
)
|
13 |
-
|
14 |
model.eval()
|
15 |
-
|
16 |
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
|
17 |
-
|
18 |
labels = model.pretrained_cfg['label_names']
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
for
|
32 |
-
|
33 |
-
|
34 |
-
print(predictions)
|
|
|
1 |
import timm
|
2 |
import torch
|
3 |
+
import streamlit as st
|
|
|
4 |
from timm.data import resolve_data_config
|
5 |
from timm.data.transforms_factory import create_transform
|
6 |
|
|
|
9 |
'hf-hub:nateraw/resnet50-oxford-iiit-pet',
|
10 |
pretrained=True
|
11 |
)
|
|
|
12 |
model.eval()
|
|
|
13 |
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
|
|
|
14 |
labels = model.pretrained_cfg['label_names']
|
15 |
+
st.title("Pet Image Classification App")
|
16 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
17 |
+
if uploaded_image is not None:
|
18 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
19 |
+
st.subheader("Classification Results:")
|
20 |
+
output = model(transform(uploaded_image).unsqueeze(0))
|
21 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
22 |
+
values, indices = torch.topk(probabilities, 5)
|
23 |
+
predictions = [
|
24 |
+
{'label': labels[i], 'score': v.item()}
|
25 |
+
for i, v in zip(indices, values)
|
26 |
+
]
|
27 |
+
for prediction in predictions:
|
28 |
+
st.write(f"- {prediction.label}: {prediction.score:.4f}")
|
|
|
|