Priyanshuchaudhary2425
commited on
Commit
•
5f54a97
1
Parent(s):
ac9ef27
launching!
Browse files- DOG-BREED-CLASSIFICATION-MODEL.pth +3 -0
- app.py +71 -0
- examples/n02087046_2276.jpg +0 -0
- examples/n02100236_111.jpg +0 -0
- examples/n02116738_5519.jpg +0 -0
- model.py +36 -0
- requirements.txt +3 -0
DOG-BREED-CLASSIFICATION-MODEL.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3f577ab51f67625c7f5f89049f01da86541fa527232ff5999cd271003918d4f1
|
3 |
+
size 31932425
|
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_effnetb2_model
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
from typing import Tuple, Dict
|
9 |
+
|
10 |
+
### 2. Model and transforms preparation ###
|
11 |
+
|
12 |
+
# Create EffNetB2 model
|
13 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
14 |
+
num_classes=120,
|
15 |
+
)
|
16 |
+
|
17 |
+
# Load saved weights
|
18 |
+
effnetb2.load_state_dict(
|
19 |
+
torch.load(
|
20 |
+
f="DOG-BREED-CLASSIFICATION-MODEL.pth",
|
21 |
+
map_location=torch.device("cpu"), # load to CPU
|
22 |
+
)
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
+
### 3. Predict function ###
|
27 |
+
|
28 |
+
def predict(img) -> Tuple[Dict, float]:
|
29 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
30 |
+
"""
|
31 |
+
# Start the timer
|
32 |
+
start_time = timer()
|
33 |
+
|
34 |
+
# Transform the target image and add a batch dimension
|
35 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
36 |
+
|
37 |
+
# Put model into evaluation mode and turn on inference mode
|
38 |
+
best_model.eval()
|
39 |
+
with torch.inference_mode():
|
40 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
41 |
+
pred_probs = torch.softmax(best_model(img), dim=1)
|
42 |
+
|
43 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
44 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
45 |
+
|
46 |
+
# Calculate the prediction time
|
47 |
+
pred_time = round(timer() - start_time, 5)
|
48 |
+
|
49 |
+
# Return the prediction dictionary and prediction time
|
50 |
+
return pred_labels_and_probs, pred_time
|
51 |
+
|
52 |
+
|
53 |
+
### 4. Gradio app ###
|
54 |
+
|
55 |
+
# Create title, description and article strings
|
56 |
+
title = "🐶DOG BREED CLASSIFICATION🐶"
|
57 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify different class of dog breeds "
|
58 |
+
article = "Created at [Google Colab](https://colab.research.google.com/drive/1pxA88oSjeoXS4Kt9fKAWIBaHlXwJ0vQr#scrollTo=e7MgaOxqEVJn)."
|
59 |
+
|
60 |
+
# Create the Gradio demo
|
61 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
62 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
63 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
64 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
65 |
+
examples=example_list,
|
66 |
+
title=title,
|
67 |
+
description=description,
|
68 |
+
article=article)
|
69 |
+
|
70 |
+
# Launch the demo!
|
71 |
+
demo.launch()
|
examples/n02087046_2276.jpg
ADDED
examples/n02100236_111.jpg
ADDED
examples/n02116738_5519.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
|
7 |
+
def create_effnetb2_model(num_classes:int=120,
|
8 |
+
seed:int=42):
|
9 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
num_classes (int, optional): number of classes in the classifier head.
|
13 |
+
Defaults to 3.
|
14 |
+
seed (int, optional): random seed value. Defaults to 42.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
18 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
19 |
+
"""
|
20 |
+
# Create EffNetB2 pretrained weights, transforms and model
|
21 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
22 |
+
transforms = weights.transforms()
|
23 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
24 |
+
|
25 |
+
# Freeze all layers in base model
|
26 |
+
for param in model.parameters():
|
27 |
+
param.requires_grad = False
|
28 |
+
|
29 |
+
# Change classifier head with random seed for reproducibility
|
30 |
+
torch.manual_seed(seed)
|
31 |
+
model.classifier = nn.Sequential(
|
32 |
+
nn.Dropout(p=0.3, inplace=True),
|
33 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
34 |
+
)
|
35 |
+
|
36 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==4.13.0
|