Rose
commited on
Commit
Β·
1e7c928
1
Parent(s):
5b1af3c
first commit
Browse files- .gitattributes +1 -0
- app.py +72 -0
- examples/gary_the_snail_54.jpg +0 -0
- examples/pearl_krabs_45.jpg +0 -0
- examples/squidward_tentacles_101.jpg +0 -0
- model.py +44 -0
- model_efficientnet_b0.pth +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
model_efficientnet_b0.pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
### Imports and class names setup ###
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import torch
|
6 |
+
|
7 |
+
from model import create_effnetb0_model
|
8 |
+
from timeit import default_timer as timer
|
9 |
+
from typing import Tuple, Dict
|
10 |
+
|
11 |
+
# Setup class names
|
12 |
+
class_names = ["eugene_h_krabs", "gary_the_snail", "karen_plankton", "mrs_puff", "patrick_star", "pearl_krabs", "sandy_cheeks", "sheldon_j_plankton", "spongebob_squarepants", "squidward_tentacles"]
|
13 |
+
|
14 |
+
### Model and transforms preparation ###
|
15 |
+
# Create EffNetB0 model
|
16 |
+
effnetb0, effnetb0_transforms = create_effnetb0_model(
|
17 |
+
num_classes=10
|
18 |
+
)
|
19 |
+
|
20 |
+
# Load saved weights
|
21 |
+
effnetb0.load_state_dict(
|
22 |
+
torch.load(
|
23 |
+
f="model_efficientnet_b0.pth",
|
24 |
+
map_location=torch.device("cpu")
|
25 |
+
)
|
26 |
+
)
|
27 |
+
|
28 |
+
### Predict function ###
|
29 |
+
|
30 |
+
# Create predict function
|
31 |
+
def predict(img) -> Tuple[Dict, float]:
|
32 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
33 |
+
"""
|
34 |
+
# Start the timer
|
35 |
+
start_time = timer()
|
36 |
+
|
37 |
+
# Transform the target image and add a batch dimension
|
38 |
+
img = effnetb0_transforms(img).unsqueeze(dim=0)
|
39 |
+
|
40 |
+
# Put model into evaluation mode and turn on inference mode
|
41 |
+
effnetb0.eval()
|
42 |
+
with torch.inference_mode():
|
43 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
44 |
+
pred_probs = torch.softmax(effnetb0(img), dim=1)
|
45 |
+
|
46 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (required format for Gradio's output parameter)
|
47 |
+
pred_labels_and_probs = {class_names[i]:float(pred_probs[0][i]) for i in range(len(class_names))}
|
48 |
+
|
49 |
+
# Calculate the prediction time
|
50 |
+
pred_time = round(timer() - start_time)
|
51 |
+
|
52 |
+
# Return the prediction dictionary and prediction time
|
53 |
+
return pred_labels_and_probs, pred_time
|
54 |
+
|
55 |
+
### Gradio app ###
|
56 |
+
title = "Spongebob Character Identifier π§½πππ¦πΏοΈπππ³π₯οΈ"
|
57 |
+
description = "An EfficientNetB0 feature extractor computer vision model to classify between 10 character from Spongebob Squarepants: Spongebob, Patrick, Squidward, Gary, Mr. Krabs, Mrs.Puff, Sandy, Plankton, Karen, and Pearl"
|
58 |
+
article = "Read more at: [Spongebob Character Identifier](https://gulnuravci.github.io/scripts/project_pages/spongebob_identifier.html)"
|
59 |
+
|
60 |
+
# Create examples list from "examples/" directory
|
61 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
62 |
+
|
63 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
64 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
65 |
+
outputs=[gr.Label(num_top_classes=10, label="Predictions"), # what are the outputs?
|
66 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
67 |
+
examples=example_list,
|
68 |
+
title=title,
|
69 |
+
description=description,
|
70 |
+
article=article)
|
71 |
+
|
72 |
+
demo.launch()
|
examples/gary_the_snail_54.jpg
ADDED
![]() |
examples/pearl_krabs_45.jpg
ADDED
![]() |
examples/squidward_tentacles_101.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
|
5 |
+
from torch import nn
|
6 |
+
from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights
|
7 |
+
from torchvision.models._api import WeightsEnum
|
8 |
+
from torch.hub import load_state_dict_from_url
|
9 |
+
|
10 |
+
def create_effnetb0_model(num_classes:int=10,
|
11 |
+
seed:int=42):
|
12 |
+
"""Creates an EficientNetB0 feature extractor model and transforms.
|
13 |
+
|
14 |
+
Args:
|
15 |
+
num_classes (int, optional): number of classes in the classifier head. Defaults to 10.
|
16 |
+
seed (int, optional): random seed value. Defaults to 42.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
model (torch.nn.Module): EffNetB0 feature extractor model.
|
20 |
+
transforms (torchvision.transforms): EfnetB0 image transforms.
|
21 |
+
"""
|
22 |
+
# Fix for wrong hash error from: https://github.com/pytorch/vision/issues/7744
|
23 |
+
def get_state_dict(self, *args, **kwargs):
|
24 |
+
kwargs.pop("check_hash")
|
25 |
+
return load_state_dict_from_url(self.url, *args, **kwargs)
|
26 |
+
WeightsEnum.get_state_dict = get_state_dict
|
27 |
+
|
28 |
+
# Create EffNetB0 pretrained weights, transforms and model
|
29 |
+
weights = EfficientNet_B0_Weights.DEFAULT
|
30 |
+
transforms = weights.transforms()
|
31 |
+
model = efficientnet_b0(weights=weights)
|
32 |
+
|
33 |
+
# Freeze all layers in base model
|
34 |
+
for param in model.features.parameters():
|
35 |
+
param.requires_grad = False
|
36 |
+
|
37 |
+
# Change the classifier head with random seed for reproducibility
|
38 |
+
torch.manual_seed(seed)
|
39 |
+
model.classifier = nn.Sequential(
|
40 |
+
nn.Dropout(p=0.3),
|
41 |
+
nn.Linear(in_features=1408, out_features=num_classes)
|
42 |
+
)
|
43 |
+
|
44 |
+
return model, transforms
|
model_efficientnet_b0.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3196ec7d7bbd0b7d39f047edfbcd0e3e279a7937f312ce0416fa7961cc4669f
|
3 |
+
size 16384522
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch>=1.12.0
|
2 |
+
torchvision>=0.13.0
|
3 |
+
gradio>=3.1.4
|