first commit
Browse files- .gitattributes +1 -0
- app.py +81 -0
- class_names.txt +37 -0
- examples/beagle.jpeg +0 -0
- examples/boxer.jpeg +0 -0
- examples/egyptian-mau.jpeg +0 -0
- examples/pomeranian.jpeg +0 -0
- examples/pug.jpeg +0 -0
- examples/shiba-inu.jpeg +0 -0
- examples/siamese.jpeg +0 -0
- examples/sphynx.jpeg +0 -0
- flagged/Predictions/tmppt66_mjs.json +1 -0
- flagged/log.csv +2 -0
- model.py +36 -0
- pretrained_effnetb2_feature_extractor_oxfordPets.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 |
+
pretrained_effnetb2_feature_extractor_oxfordPets.pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Setup class names
|
| 11 |
+
with open("class_names.txt", "r") as f: # reading them in from class_names.txt
|
| 12 |
+
class_names = [pet_name.strip() for pet_name in f.readlines()]
|
| 13 |
+
|
| 14 |
+
### 2. Model and transforms preparation ###
|
| 15 |
+
|
| 16 |
+
# Create model
|
| 17 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
| 18 |
+
num_classes=len(class_names)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Load saved weights
|
| 22 |
+
effnetb2.load_state_dict(
|
| 23 |
+
torch.load(
|
| 24 |
+
f="pretrained_effnetb2_feature_extractor_oxfordPets.pth",
|
| 25 |
+
map_location=torch.device("cpu"), # load to CPU
|
| 26 |
+
)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
### 3. Predict function ###
|
| 30 |
+
|
| 31 |
+
# Create predict function
|
| 32 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 33 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
| 34 |
+
"""
|
| 35 |
+
# Start the timer
|
| 36 |
+
start_time = timer()
|
| 37 |
+
|
| 38 |
+
# Transform the target image and add a batch dimension
|
| 39 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
| 40 |
+
|
| 41 |
+
# Put model into evaluation mode and turn on inference mode
|
| 42 |
+
effnetb2.eval()
|
| 43 |
+
with torch.inference_mode():
|
| 44 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 45 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
| 46 |
+
|
| 47 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 48 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 49 |
+
|
| 50 |
+
# Calculate the prediction time
|
| 51 |
+
pred_time = round(timer() - start_time, 5)
|
| 52 |
+
|
| 53 |
+
# Return the prediction dictionary and prediction time
|
| 54 |
+
return pred_labels_and_probs, pred_time
|
| 55 |
+
|
| 56 |
+
### 4. Gradio app ###
|
| 57 |
+
|
| 58 |
+
# Create title, description and article strings
|
| 59 |
+
title = "PetVision 🐱👁"
|
| 60 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of pets into [37 different classes](https://raw.githubusercontent.com/Alejandro-Casanova/pytorch-deep-learning/main/extras/oxfordPets_class_names.txt)."
|
| 61 |
+
article = "Created by Alejandro Casanova, following the course by [Daniel Bourke](https://www.learnpytorch.io/)"
|
| 62 |
+
|
| 63 |
+
# Create examples list from "examples/" directory
|
| 64 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 65 |
+
|
| 66 |
+
# Create Gradio interface
|
| 67 |
+
demo = gr.Interface(
|
| 68 |
+
fn=predict,
|
| 69 |
+
inputs=gr.Image(type="pil"),
|
| 70 |
+
outputs=[
|
| 71 |
+
gr.Label(num_top_classes=5, label="Predictions"),
|
| 72 |
+
gr.Number(label="Prediction time (s)"),
|
| 73 |
+
],
|
| 74 |
+
examples=example_list,
|
| 75 |
+
title=title,
|
| 76 |
+
description=description,
|
| 77 |
+
article=article,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Launch the app!
|
| 81 |
+
demo.launch()
|
class_names.txt
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Abyssinian
|
| 2 |
+
American Bulldog
|
| 3 |
+
American Pit Bull Terrier
|
| 4 |
+
Basset Hound
|
| 5 |
+
Beagle
|
| 6 |
+
Bengal
|
| 7 |
+
Birman
|
| 8 |
+
Bombay
|
| 9 |
+
Boxer
|
| 10 |
+
British Shorthair
|
| 11 |
+
Chihuahua
|
| 12 |
+
Egyptian Mau
|
| 13 |
+
English Cocker Spaniel
|
| 14 |
+
English Setter
|
| 15 |
+
German Shorthaired
|
| 16 |
+
Great Pyrenees
|
| 17 |
+
Havanese
|
| 18 |
+
Japanese Chin
|
| 19 |
+
Keeshond
|
| 20 |
+
Leonberger
|
| 21 |
+
Maine Coon
|
| 22 |
+
Miniature Pinscher
|
| 23 |
+
Newfoundland
|
| 24 |
+
Persian
|
| 25 |
+
Pomeranian
|
| 26 |
+
Pug
|
| 27 |
+
Ragdoll
|
| 28 |
+
Russian Blue
|
| 29 |
+
Saint Bernard
|
| 30 |
+
Samoyed
|
| 31 |
+
Scottish Terrier
|
| 32 |
+
Shiba Inu
|
| 33 |
+
Siamese
|
| 34 |
+
Sphynx
|
| 35 |
+
Staffordshire Bull Terrier
|
| 36 |
+
Wheaten Terrier
|
| 37 |
+
Yorkshire Terrier
|
examples/beagle.jpeg
ADDED
|
examples/boxer.jpeg
ADDED
|
examples/egyptian-mau.jpeg
ADDED
|
examples/pomeranian.jpeg
ADDED
|
examples/pug.jpeg
ADDED
|
examples/shiba-inu.jpeg
ADDED
|
examples/siamese.jpeg
ADDED
|
examples/sphynx.jpeg
ADDED
|
flagged/Predictions/tmppt66_mjs.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{}
|
flagged/log.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
img,Predictions,Prediction time (s),flag,username,timestamp
|
| 2 |
+
,C:\Users\alex\Desktop\PetVision\flagged\Predictions\tmppt66_mjs.json,,,,2023-10-04 22:44:46.311654
|
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=3,
|
| 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
|
pretrained_effnetb2_feature_extractor_oxfordPets.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ac943853bf13dae674ea0b9844fb19c3d0d77e3e6e8da6ee5042183e680abdcf
|
| 3 |
+
size 31489783
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.0.1
|
| 2 |
+
torchvision==0.15.2
|
| 3 |
+
gradio==3.46.1
|