Upload first files
Browse files- app.py +87 -0
- class_names.txt +120 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import tensorflow
|
4 |
+
|
5 |
+
from typing import Tuple, Dict
|
6 |
+
import tensorflow as tf
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image
|
9 |
+
from timeit import default_timer as timer
|
10 |
+
|
11 |
+
|
12 |
+
# Setup class names
|
13 |
+
with open('class_names.txt') as f:
|
14 |
+
class_names = [breed.strip() for breed in f.readlines()]
|
15 |
+
|
16 |
+
# Load the pre-trained EfficientNetV2 model
|
17 |
+
effnet = tensorflow.keras.models.load_model('demo/dog_breed_classifier/dog_breed_effnet_augmentation.h5')
|
18 |
+
|
19 |
+
# Get the preprocessing function for EfficientNetV2
|
20 |
+
effnet_preprocess_input = tensorflow.keras.applications.efficientnet_v2.preprocess_input
|
21 |
+
|
22 |
+
# Create examples list from "examples/" directory
|
23 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
24 |
+
|
25 |
+
## `predict` function
|
26 |
+
def predict(img) -> Tuple[Dict, float]:
|
27 |
+
"""
|
28 |
+
Transforms and performs a prediction on an image and returns prediction and time taken.
|
29 |
+
|
30 |
+
Args:
|
31 |
+
image_path (str): Path to the input image.
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
Tuple[Dict, float]: A tuple containing a dictionary of class labels and prediction probabilities
|
35 |
+
and the prediction time.
|
36 |
+
"""
|
37 |
+
# Start the timer
|
38 |
+
start_time = timer()
|
39 |
+
|
40 |
+
# Open the image using PIL
|
41 |
+
img = img.resize((224, 224)) # Resize the image to the model's expected input size
|
42 |
+
|
43 |
+
# Convert the image to a NumPy array
|
44 |
+
x = np.array(img)
|
45 |
+
x = effnet_preprocess_input(x)
|
46 |
+
|
47 |
+
# Add a batch dimension
|
48 |
+
x = tf.expand_dims(x, axis=0)
|
49 |
+
|
50 |
+
# Pass the image through the model
|
51 |
+
predictions = effnet(x)
|
52 |
+
|
53 |
+
top_classes_indices = np.argsort(predictions[0])[::-1][:3] # Get the indices of top 3 classes
|
54 |
+
top_classes = [class_names[i] for i in top_classes_indices] # Get the class names of top 3 classes
|
55 |
+
top_probabilities = [predictions[0][index] for index in top_classes_indices] * 100 # Get the probabilities of top 3 classes
|
56 |
+
|
57 |
+
# Create a dictionary of class labels and prediction probabilities
|
58 |
+
pred_labels_and_probs = {top_classes[i]: float(top_probabilities[i]) for i in range(len(top_classes_indices))}
|
59 |
+
|
60 |
+
# Calculate the prediction time
|
61 |
+
pred_time = round(timer() - start_time, 5)
|
62 |
+
|
63 |
+
# Return the prediction dictionary and prediction time
|
64 |
+
return pred_labels_and_probs, pred_time
|
65 |
+
|
66 |
+
|
67 |
+
# Create title, description, and article strings
|
68 |
+
title = "🐶 Dog Breeds Classifier 🐾"
|
69 |
+
description = "🚀 An EfficientNetV2S feature extractor computer vision model to classify images of 120 different breeds. 📸"
|
70 |
+
article = "🌟 Created at [GitHub](https://github.com/adinmg/dog_breed_classifier)."
|
71 |
+
|
72 |
+
# Create the Gradio demo
|
73 |
+
demo = gr.Interface(
|
74 |
+
fn=predict, # mapping function from input to output
|
75 |
+
inputs=gr.Image(type="pil"),
|
76 |
+
outputs=[
|
77 |
+
gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
78 |
+
gr.Number(label="Prediction time (s)"),
|
79 |
+
],
|
80 |
+
examples=example_list,
|
81 |
+
title=title,
|
82 |
+
description=description,
|
83 |
+
article=article,
|
84 |
+
)
|
85 |
+
|
86 |
+
# Launch the demo!
|
87 |
+
demo.launch()
|
class_names.txt
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Affenpinscher
|
2 |
+
Afghan_hound
|
3 |
+
African_hunting_dog
|
4 |
+
Airedale
|
5 |
+
American_staffordshire_terrier
|
6 |
+
Appenzeller
|
7 |
+
Australian_terrier
|
8 |
+
Basenji
|
9 |
+
Basset
|
10 |
+
Beagle
|
11 |
+
Bedlington_terrier
|
12 |
+
Bernese_mountain_dog
|
13 |
+
Black-and-tan_coonhound
|
14 |
+
Blenheim_spaniel
|
15 |
+
Bloodhound
|
16 |
+
Bluetick
|
17 |
+
Border_collie
|
18 |
+
Border_terrier
|
19 |
+
Borzoi
|
20 |
+
Boston_bull
|
21 |
+
Bouvier_des_flandres
|
22 |
+
Boxer
|
23 |
+
Brabancon_griffon
|
24 |
+
Briard
|
25 |
+
Brittany_spaniel
|
26 |
+
Bull_mastiff
|
27 |
+
Cairn
|
28 |
+
Cardigan
|
29 |
+
Chesapeake_bay_retriever
|
30 |
+
Chihuahua
|
31 |
+
Chow
|
32 |
+
Clumber
|
33 |
+
Cocker_spaniel
|
34 |
+
Collie
|
35 |
+
Curly-coated_retriever
|
36 |
+
Dandie_dinmont
|
37 |
+
Dhole
|
38 |
+
Dingo
|
39 |
+
Doberman
|
40 |
+
English_foxhound
|
41 |
+
English_setter
|
42 |
+
English_springer
|
43 |
+
Entlebucher
|
44 |
+
Eskimo_dog
|
45 |
+
Flat-coated_retriever
|
46 |
+
French_bulldog
|
47 |
+
German_shepherd
|
48 |
+
German_short-haired_pointer
|
49 |
+
Giant_schnauzer
|
50 |
+
Golden_retriever
|
51 |
+
Gordon_setter
|
52 |
+
Great_dane
|
53 |
+
Great_pyrenees
|
54 |
+
Greater_swiss_mountain_dog
|
55 |
+
Groenendael
|
56 |
+
Ibizan_hound
|
57 |
+
Irish_setter
|
58 |
+
Irish_terrier
|
59 |
+
Irish_water_spaniel
|
60 |
+
Irish_wolfhound
|
61 |
+
Italian_greyhound
|
62 |
+
Japanese_spaniel
|
63 |
+
Keeshond
|
64 |
+
Kelpie
|
65 |
+
Kerry_blue_terrier
|
66 |
+
Komondor
|
67 |
+
Kuvasz
|
68 |
+
Labrador_retriever
|
69 |
+
Lakeland_terrier
|
70 |
+
Leonberg
|
71 |
+
Lhasa
|
72 |
+
Malamute
|
73 |
+
Malinois
|
74 |
+
Maltese_dog
|
75 |
+
Mexican_hairless
|
76 |
+
Miniature_pinscher
|
77 |
+
Miniature_poodle
|
78 |
+
Miniature_schnauzer
|
79 |
+
Newfoundland
|
80 |
+
Norfolk_terrier
|
81 |
+
Norwegian_elkhound
|
82 |
+
Norwich_terrier
|
83 |
+
Old_english_sheepdog
|
84 |
+
Otterhound
|
85 |
+
Papillon
|
86 |
+
Pekinese
|
87 |
+
Pembroke
|
88 |
+
Pomeranian
|
89 |
+
Pug
|
90 |
+
Redbone
|
91 |
+
Rhodesian_ridgeback
|
92 |
+
Rottweiler
|
93 |
+
Saint_bernard
|
94 |
+
Saluki
|
95 |
+
Samoyed
|
96 |
+
Schipperke
|
97 |
+
Scotch_terrier
|
98 |
+
Scottish_deerhound
|
99 |
+
Sealyham_terrier
|
100 |
+
Shetland_sheepdog
|
101 |
+
Shih-tzu
|
102 |
+
Siberian_husky
|
103 |
+
Silky_terrier
|
104 |
+
Soft-coated_wheaten_terrier
|
105 |
+
Staffordshire_bullterrier
|
106 |
+
Standard_poodle
|
107 |
+
Standard_schnauzer
|
108 |
+
Sussex_spaniel
|
109 |
+
Tibetan_mastiff
|
110 |
+
Tibetan_terrier
|
111 |
+
Toy_poodle
|
112 |
+
Toy_terrier
|
113 |
+
Vizsla
|
114 |
+
Walker_hound
|
115 |
+
Weimaraner
|
116 |
+
Welsh_springer_spaniel
|
117 |
+
West_highland_white_terrier
|
118 |
+
Whippet
|
119 |
+
Wire-haired_fox_terrier
|
120 |
+
Yorkshire_terrier
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio==3.43.2
|
2 |
+
tensorflow==2.13.0
|