Spaces:
Runtime error
Runtime error
first commit
Browse files- Cat_Breed_Classifier_12_class_90_acc.pth +3 -0
- app.py +57 -0
- classes.txt +12 -0
- examples/Abyssinian.jpg +0 -0
- examples/British Shorthair.jpg +0 -0
- examples/bengal.jpg +0 -0
- examples/bombay.jpg +0 -0
- examples/persian.jpg +0 -0
- model.py +20 -0
- requirements.txt +3 -0
Cat_Breed_Classifier_12_class_90_acc.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9b7419cbb0437f1d8a8fe25ea511c2e881e7c08a279f3d03fe39c7fdc0767215
|
3 |
+
size 31323721
|
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from timeit import default_timer as timer
|
4 |
+
import gradio as gr
|
5 |
+
from typing import Tuple ,Dict
|
6 |
+
from model import create_effnetb2_model
|
7 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
with open("classes.txt") as f:
|
12 |
+
classes= [line.rstrip() for line in f]
|
13 |
+
|
14 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
15 |
+
num_classes=len(classes))
|
16 |
+
|
17 |
+
effnetb2.load_state_dict(
|
18 |
+
torch.load(
|
19 |
+
f="Cat_Breed_Classifier_12_class_90_acc.pth",
|
20 |
+
map_location=torch.device("cpu"), # load to CPU
|
21 |
+
)
|
22 |
+
)
|
23 |
+
|
24 |
+
def predict(img):
|
25 |
+
start_time = timer()
|
26 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
27 |
+
effnetb2.eval()
|
28 |
+
with torch.inference_mode():
|
29 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
30 |
+
pred_labels_and_probs = {
|
31 |
+
classes[i]: float(pred_probs[0][i]) for i in range(len(classes))
|
32 |
+
}
|
33 |
+
pred_time = round(timer() - start_time, 5)
|
34 |
+
return pred_labels_and_probs, pred_time
|
35 |
+
|
36 |
+
title = "Cat Breed Classifier Demo 😼"
|
37 |
+
description = "<p style='text-align: center'>Gradio Demo for Classifying Cat Breeds of these <a href='https://huggingface.co/'>5 different types.<a></p>"
|
38 |
+
article = "</br><p style='text-align: center'><a href='https://github.com/Mr-Hexi' target='_blank'>GitHub</a></br>![visitors](https://visitor-badge.glitch.me/badge?page_id=Hexii.Cat-Breed-Classifier)</p> "
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
43 |
+
|
44 |
+
app = gr.Interface(
|
45 |
+
fn=predict,
|
46 |
+
inputs=gr.Image(type="pil"),
|
47 |
+
outputs=[
|
48 |
+
gr.Label(num_top_classes=5, label="Predictions"),
|
49 |
+
gr.Number(label="Prediction time (s)"),
|
50 |
+
],
|
51 |
+
examples=example_list,
|
52 |
+
title=title,
|
53 |
+
description=description,
|
54 |
+
article=article,
|
55 |
+
)
|
56 |
+
|
57 |
+
app.launch()
|
classes.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Abyssinian
|
2 |
+
Bengal
|
3 |
+
Birman
|
4 |
+
Bombay
|
5 |
+
British Shorthair
|
6 |
+
Egyptian Mau
|
7 |
+
Maine Coon
|
8 |
+
Persian
|
9 |
+
Ragdoll
|
10 |
+
Russian Blue
|
11 |
+
Siamese
|
12 |
+
Sphynx
|
examples/Abyssinian.jpg
ADDED
examples/British Shorthair.jpg
ADDED
examples/bengal.jpg
ADDED
examples/bombay.jpg
ADDED
examples/persian.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import torchvision
|
4 |
+
|
5 |
+
def create_effnetb2_model(num_classes:int=3,
|
6 |
+
seed:int=42):
|
7 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
8 |
+
transforms = weights.transforms()
|
9 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
10 |
+
|
11 |
+
for param in model.parameters():
|
12 |
+
param.requires_grad = False
|
13 |
+
|
14 |
+
torch.manual_seed(seed)
|
15 |
+
model.classifier = nn.Sequential(
|
16 |
+
nn.Dropout(p=0.3, inplace=True),
|
17 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
18 |
+
)
|
19 |
+
|
20 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|