Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
Hugging Face
|
| 3 |
-
Models
|
| 4 |
-
Datasets
|
| 5 |
-
Spaces
|
| 6 |
-
Docs
|
| 7 |
-
Pricing
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
Spaces:
|
| 11 |
-
NiranjanIITPKD
|
| 12 |
-
/
|
| 13 |
-
tinyimagenet-resnet18-demo
|
| 14 |
-
|
| 15 |
-
like
|
| 16 |
-
0
|
| 17 |
-
|
| 18 |
-
App
|
| 19 |
-
Files
|
| 20 |
-
Community
|
| 21 |
-
Settings
|
| 22 |
-
tinyimagenet-resnet18-demo
|
| 23 |
-
/
|
| 24 |
-
app.py
|
| 25 |
-
|
| 26 |
-
NiranjanIITPKD's picture
|
| 27 |
-
NiranjanIITPKD
|
| 28 |
-
Create app.py
|
| 29 |
-
5818cdb
|
| 30 |
-
verified
|
| 31 |
-
1 day ago
|
| 32 |
-
raw
|
| 33 |
-
|
| 34 |
-
Copy download link
|
| 35 |
-
history
|
| 36 |
-
blame
|
| 37 |
-
edit
|
| 38 |
-
delete
|
| 39 |
-
|
| 40 |
-
1.3 kB
|
| 41 |
-
import torch
|
| 42 |
import torch.nn as nn
|
| 43 |
from torchvision import models, transforms as T
|
| 44 |
from PIL import Image
|
|
@@ -46,18 +6,21 @@ import gradio as gr
|
|
| 46 |
|
| 47 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 48 |
|
| 49 |
-
# Load label
|
| 50 |
wnids = [line.strip() for line in open("wnids.txt")]
|
| 51 |
|
| 52 |
-
# Map wnid →
|
| 53 |
words = {}
|
| 54 |
with open("words.txt", "r") as f:
|
| 55 |
for line in f:
|
| 56 |
wnid, name = line.split("\t")
|
| 57 |
-
words[wnid] = name.split(",")[0]
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
id_to_label = [words[wnid] for wnid in
|
| 61 |
|
| 62 |
# Load Model
|
| 63 |
model = models.resnet18(weights=None)
|
|
@@ -66,7 +29,7 @@ model.load_state_dict(torch.load("best_resnet18_tinyimagenet.pth", map_location=
|
|
| 66 |
model.to(device)
|
| 67 |
model.eval()
|
| 68 |
|
| 69 |
-
# Same
|
| 70 |
transform = T.Compose([
|
| 71 |
T.Resize((224, 224)),
|
| 72 |
T.ToTensor(),
|
|
@@ -85,5 +48,5 @@ gr.Interface(
|
|
| 85 |
fn=predict,
|
| 86 |
inputs=gr.Image(type="pil"),
|
| 87 |
outputs="text",
|
| 88 |
-
title="Tiny ImageNet Classifier (
|
| 89 |
).launch()
|
|
|
|
| 1 |
+
mport torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch.nn as nn
|
| 3 |
from torchvision import models, transforms as T
|
| 4 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
|
| 9 |
+
# Load label files
|
| 10 |
wnids = [line.strip() for line in open("wnids.txt")]
|
| 11 |
|
| 12 |
+
# Map wnid → readable label
|
| 13 |
words = {}
|
| 14 |
with open("words.txt", "r") as f:
|
| 15 |
for line in f:
|
| 16 |
wnid, name = line.split("\t")
|
| 17 |
+
words[wnid] = name.split(",")[0]
|
| 18 |
+
|
| 19 |
+
# ✅ CORRECT CLASS ORDER (alphabetical)
|
| 20 |
+
sorted_wnids = sorted(wnids)
|
| 21 |
|
| 22 |
+
# ✅ Final label list matching model training order
|
| 23 |
+
id_to_label = [words[wnid] for wnid in sorted_wnids]
|
| 24 |
|
| 25 |
# Load Model
|
| 26 |
model = models.resnet18(weights=None)
|
|
|
|
| 29 |
model.to(device)
|
| 30 |
model.eval()
|
| 31 |
|
| 32 |
+
# Same preprocessing as training
|
| 33 |
transform = T.Compose([
|
| 34 |
T.Resize((224, 224)),
|
| 35 |
T.ToTensor(),
|
|
|
|
| 48 |
fn=predict,
|
| 49 |
inputs=gr.Image(type="pil"),
|
| 50 |
outputs="text",
|
| 51 |
+
title="Tiny ImageNet Classifier (Corrected Labels)"
|
| 52 |
).launch()
|