006f86 commited on
Commit
dff62de
·
1 Parent(s): 4267fcb

Added app, model, class names, and dependencies

Browse files
Files changed (4) hide show
  1. app.py +56 -0
  2. best_model.pth +3 -0
  3. class_names.json +161 -0
  4. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ from torchvision import transforms
5
+ from torchvision.models import efficientnet_v2_s, EfficientNet_V2_S_Weights
6
+ from PIL import Image
7
+ import json
8
+ import os
9
+
10
+ MODEL_PATH = "best_model.pth"
11
+ with open("class_names.json", "r") as f:
12
+ CLASS_NAMES = json.load(f)
13
+
14
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+
16
+ weights = EfficientNet_V2_S_Weights.IMAGENET1K_V1
17
+ model = efficientnet_v2_s(weights=weights)
18
+ model.classifier[1] = nn.Linear(model.classifier[1].in_features, len(CLASS_NAMES))
19
+ model.load_state_dict(torch.load(MODEL_PATH, map_location=device))
20
+ model.eval().to(device)
21
+
22
+ mean = getattr(weights, "meta", {}).get("mean", [0.485, 0.456, 0.406])
23
+ std = getattr(weights, "meta", {}).get("std", [0.229, 0.224, 0.225])
24
+
25
+ transform = transforms.Compose([
26
+ transforms.Resize((384, 384)),
27
+ transforms.ToTensor(),
28
+ transforms.Normalize(mean=mean, std=std),
29
+ ])
30
+
31
+ def predict(image):
32
+ image = transform(image).unsqueeze(0).to(device)
33
+ with torch.no_grad():
34
+ outputs = model(image)
35
+ probs = torch.nn.functional.softmax(outputs, dim=1)[0]
36
+
37
+ results = {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
38
+
39
+ sorted_indices = torch.argsort(probs, descending=True)
40
+ top5_indices = sorted_indices[:5]
41
+
42
+ top5_results = {CLASS_NAMES[i]: float(probs[i]) for i in top5_indices}
43
+
44
+ return top5_results, results
45
+
46
+
47
+ demo = gr.Interface(
48
+ fn=predict,
49
+ inputs=gr.Image(type="pil"),
50
+ outputs=[gr.Label(label="Prediction"), gr.JSON(label="Confidence Scores")],
51
+ title="StrAI - Cat Identifier",
52
+ description="Upload an image to identify which cat it is."
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()
best_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:22e835f26352710b5a90b96e6085666b16b3cc7eacdca68d49bb547499685989
3
+ size 82426399
class_names.json ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ "Achillies",
3
+ "Adolf",
4
+ "Al Waldo",
5
+ "Alfie",
6
+ "Ally",
7
+ "Anselmo",
8
+ "Baby Belgian",
9
+ "Balls",
10
+ "Bambi",
11
+ "Bangus",
12
+ "Barry",
13
+ "Bingo",
14
+ "Bingus",
15
+ "Borgee",
16
+ "Bozo",
17
+ "Bulgogi",
18
+ "Butter",
19
+ "Button",
20
+ "Callie",
21
+ "Cara",
22
+ "Carme",
23
+ "Chichi Shopwise",
24
+ "Chico",
25
+ "Chowder",
26
+ "Cleo",
27
+ "Coconut",
28
+ "DOG 2",
29
+ "DOG 3",
30
+ "Daisy",
31
+ "Daphne",
32
+ "Darlene",
33
+ "Dorian",
34
+ "Doris",
35
+ "Dot",
36
+ "Ebi",
37
+ "Echo",
38
+ "Elise",
39
+ "Enji",
40
+ "Ensaymada Cheese Roll",
41
+ "Faith",
42
+ "Fave",
43
+ "Flora",
44
+ "Floyd",
45
+ "Francis",
46
+ "Frankie",
47
+ "Georgie",
48
+ "Gigi",
49
+ "Ginger",
50
+ "Gruyere",
51
+ "Hansel",
52
+ "Hany",
53
+ "Harith",
54
+ "Hazel",
55
+ "Helga",
56
+ "Huey",
57
+ "Huni",
58
+ "Inu",
59
+ "JR",
60
+ "Jack",
61
+ "Jackie",
62
+ "Jerome",
63
+ "Josie",
64
+ "Juliet",
65
+ "Julio",
66
+ "Juno",
67
+ "Kalbi",
68
+ "Kenta",
69
+ "Kimchi",
70
+ "Kimono",
71
+ "KittyPerry",
72
+ "Kucing",
73
+ "Kucingcing",
74
+ "Lambing",
75
+ "Lara",
76
+ "Leeca",
77
+ "Lennon",
78
+ "Leslie",
79
+ "Lily",
80
+ "Limper_Bulag",
81
+ "Limpkin",
82
+ "Lion",
83
+ "Louis",
84
+ "Lucky",
85
+ "Lyka",
86
+ "Maggie",
87
+ "Mama Waffle",
88
+ "Maple Oscar",
89
+ "Marga",
90
+ "Marikit",
91
+ "Marty",
92
+ "Maxie",
93
+ "Meemon",
94
+ "Meowming",
95
+ "Miki",
96
+ "Milo",
97
+ "Mingkay",
98
+ "Minty",
99
+ "Mitzki",
100
+ "Moji",
101
+ "Munchie",
102
+ "NO_NAME 11",
103
+ "NO_NAME 13 - Jinu",
104
+ "NO_NAME 3",
105
+ "NO_NAME 4",
106
+ "NO_NAME 5",
107
+ "NO_NAME 6",
108
+ "NO_NAME 7 - Batgirl",
109
+ "NO_NAME Security Cat",
110
+ "Nala",
111
+ "Nemo",
112
+ "Nero",
113
+ "Nyssa Nisa",
114
+ "Paris",
115
+ "Patches",
116
+ "Patchot",
117
+ "Peeta",
118
+ "Penny",
119
+ "Pepper",
120
+ "Percy",
121
+ "Prof",
122
+ "Pumpkin",
123
+ "Raj",
124
+ "Remy",
125
+ "Renz",
126
+ "Ringo",
127
+ "Rocky",
128
+ "Rory",
129
+ "Ross",
130
+ "Salvi",
131
+ "Sam",
132
+ "Shaq",
133
+ "Sheeba",
134
+ "Shibuya",
135
+ "Sniper",
136
+ "Socks",
137
+ "Soup",
138
+ "Spaghetti",
139
+ "Sunkiss",
140
+ "Suzy",
141
+ "Tarsier",
142
+ "Tasi",
143
+ "Teddy Chi",
144
+ "Toes",
145
+ "Toffee",
146
+ "Toothless",
147
+ "Trex",
148
+ "Tutti",
149
+ "Twister Fries",
150
+ "Val",
151
+ "Void",
152
+ "Waffle",
153
+ "Weena",
154
+ "Whiskers",
155
+ "Wilma",
156
+ "Yoyo",
157
+ "Yuris",
158
+ "Ziggy",
159
+ "Zorro",
160
+ "not_faith"
161
+ ]
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
4
+ numpy
5
+ pillow