Spaces:
Runtime error
Runtime error
lfs untrack non-binary files
Browse files- .gitattributes +2 -0
- app.py +88 -3
- class_names.txt +120 -3
- conda/environment.yml +34 -3
- examples/black-dog-breeds-black-labrador-retriever-1566497968.jpg +0 -3
- examples/german_sheoherd-612x612.jpg +0 -3
- examples/husky.jpeg +0 -3
- helper.py +31 -3
- notebook/dogVison.ipynb +0 -0
.gitattributes
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 1 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,3 +1,88 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### 1. Imports and class names setup ###
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
from timeit import default_timer as timer
|
| 7 |
+
from typing import Tuple, Dict
|
| 8 |
+
|
| 9 |
+
from helper import load_model
|
| 10 |
+
|
| 11 |
+
# Setup class names
|
| 12 |
+
with open("class_names.txt", "r") as f:
|
| 13 |
+
unique_breeds = [l.strip() for l in f.readlines()]
|
| 14 |
+
|
| 15 |
+
# 2. Model generation and weight
|
| 16 |
+
model = load_model("models/20230727-13521690480331-all-images.h5")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# 3. Prefict function
|
| 20 |
+
# Define image size
|
| 21 |
+
IMG_SIZE = 224
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def process_image(image_path):
|
| 25 |
+
"""
|
| 26 |
+
Takes an image file path and turns it into a Tensor.
|
| 27 |
+
"""
|
| 28 |
+
# Read in image file
|
| 29 |
+
image = tf.io.read_file(image_path)
|
| 30 |
+
# Turn the jpeg image into numerical Tensor with 3 colour channels (Red, Green, Blue)
|
| 31 |
+
image = tf.image.decode_jpeg(image, channels=3)
|
| 32 |
+
# Convert the colour channel values from 0-225 values to 0-1 values
|
| 33 |
+
image = tf.image.convert_image_dtype(image, tf.float32)
|
| 34 |
+
# Resize the image to our desired size (224, 244)
|
| 35 |
+
image = tf.image.resize(image, size=[IMG_SIZE, IMG_SIZE])
|
| 36 |
+
return image
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 40 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken."""
|
| 41 |
+
# Start the timer
|
| 42 |
+
start_time = timer()
|
| 43 |
+
|
| 44 |
+
# Transform the target image and add a batch dimension
|
| 45 |
+
img = process_image(img)
|
| 46 |
+
|
| 47 |
+
img = tf.expand_dims(img, axis=0)
|
| 48 |
+
|
| 49 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 50 |
+
pred_probs = model(img)
|
| 51 |
+
|
| 52 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 53 |
+
pred_labels_and_probs = {
|
| 54 |
+
unique_breeds[i]: float(pred_probs[0][i]) for i in range(len(unique_breeds))
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Calculate the prediction time
|
| 58 |
+
pred_time = round(timer() - start_time, 5)
|
| 59 |
+
|
| 60 |
+
# Return the prediction dictionary and prediction time
|
| 61 |
+
return pred_labels_and_probs, pred_time
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# 4. Gradio app
|
| 65 |
+
# Create title, description and article strings
|
| 66 |
+
title = "120 Dog Breed Vision classifier 🐶🐩🐕🐕🦺"
|
| 67 |
+
description = (
|
| 68 |
+
"An mobilenet feature extractor computer vision model to classify 120 dog breeds."
|
| 69 |
+
)
|
| 70 |
+
article = "Modele from [mobilenet](https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/5)."
|
| 71 |
+
example_list = example_list = [["examples/" + str(p)] for p in os.listdir("examples/")]
|
| 72 |
+
|
| 73 |
+
# Create the Gradio demo
|
| 74 |
+
demo = gr.Interface(
|
| 75 |
+
fn=predict, # mapping function from input to output
|
| 76 |
+
inputs=gr.Image(type="filepath"), # what are the inputs?
|
| 77 |
+
outputs=[
|
| 78 |
+
gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
| 79 |
+
gr.Number(label="Prediction time (s)"),
|
| 80 |
+
], # our fn has two outputs, therefore we have two outputs
|
| 81 |
+
examples=example_list,
|
| 82 |
+
title=title,
|
| 83 |
+
description=description,
|
| 84 |
+
article=article,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Launch the demo!
|
| 88 |
+
demo.launch(debug=False)
|
class_names.txt
CHANGED
|
@@ -1,3 +1,120 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
conda/environment.yml
CHANGED
|
@@ -1,3 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: tf
|
| 2 |
+
channels:
|
| 3 |
+
- apple
|
| 4 |
+
- defaults
|
| 5 |
+
- conda-forge
|
| 6 |
+
dependencies:
|
| 7 |
+
- pip=23.1.2
|
| 8 |
+
- python=3.10.12
|
| 9 |
+
|
| 10 |
+
- pip:
|
| 11 |
+
- gradio==3.39.0
|
| 12 |
+
- gradio-client==0.3.0
|
| 13 |
+
|
| 14 |
+
- huggingface-hub==0.16.4
|
| 15 |
+
|
| 16 |
+
- keras==2.13.1
|
| 17 |
+
|
| 18 |
+
- matplotlib==3.7.2
|
| 19 |
+
- numpy==1.24.3
|
| 20 |
+
|
| 21 |
+
- pandas==2.0.3
|
| 22 |
+
- requests==2.31.0
|
| 23 |
+
|
| 24 |
+
- tensorboard==2.13.0
|
| 25 |
+
- tensorboard-data-server==0.7.1
|
| 26 |
+
- tensorflow==2.13.0
|
| 27 |
+
- tensorflow-estimator==2.13.0
|
| 28 |
+
- tensorflow-hub==0.13.0
|
| 29 |
+
- tensorflow-macos==2.13.0
|
| 30 |
+
- tensorflow-metal==1.0.1
|
| 31 |
+
|
| 32 |
+
- tqdm==4.65.0
|
| 33 |
+
|
| 34 |
+
prefix: /Users/yusali/miniforge3/envs/tf
|
examples/black-dog-breeds-black-labrador-retriever-1566497968.jpg
DELETED
Git LFS Details
|
examples/german_sheoherd-612x612.jpg
DELETED
Git LFS Details
|
examples/husky.jpeg
DELETED
Git LFS Details
|
helper.py
CHANGED
|
@@ -1,3 +1,31 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import datetime
|
| 3 |
+
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import tensorflow_hub as hub
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def save_model(model, suffix=None):
|
| 9 |
+
"""
|
| 10 |
+
Saves a given model in a models directory and appends a suffix (str)
|
| 11 |
+
for clarity and reuse.
|
| 12 |
+
"""
|
| 13 |
+
# Create model directory with current time
|
| 14 |
+
modeldir = os.path.join(
|
| 15 |
+
"../models", datetime.datetime.now().strftime("%Y%m%d-%H%M%s")
|
| 16 |
+
)
|
| 17 |
+
model_path = modeldir + "-" + suffix + ".h5" # save format of model
|
| 18 |
+
print(f"Saving model to: {model_path}...")
|
| 19 |
+
model.save(model_path)
|
| 20 |
+
return model_path
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def load_model(model_path):
|
| 24 |
+
"""
|
| 25 |
+
Loads a saved model from a specified path.
|
| 26 |
+
"""
|
| 27 |
+
print(f"Loading saved model from: {model_path}")
|
| 28 |
+
model = tf.keras.models.load_model(
|
| 29 |
+
model_path, custom_objects={"KerasLayer": hub.KerasLayer}
|
| 30 |
+
)
|
| 31 |
+
return model
|
notebook/dogVison.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|