Sara Tolosa
commited on
Commit
·
109fe0a
1
Parent(s):
cbf2602
Dog vs Cat classifier
Browse files
app2.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This script is used to create a Gradio interface in which we have a
|
| 2 |
+
# dog vs cat classifier using the fastai library. For more explanation,
|
| 3 |
+
# visit the Google Colab notebook associated.
|
| 4 |
+
|
| 5 |
+
from fastai.vision.all import *
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Define label function
|
| 9 |
+
def is_cat(x): return x[0].isupper()
|
| 10 |
+
|
| 11 |
+
# Load our model
|
| 12 |
+
learner = load_learner('model.pkl')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Transform our model to obtain results that Gradio can handle with
|
| 16 |
+
categories = ('Dog', 'Cat')
|
| 17 |
+
|
| 18 |
+
def classify_image(img):
|
| 19 |
+
# We are saying that this predictions returns: the prediction, its index and the prediction probability
|
| 20 |
+
pred,idx,probs = learn.predict(img)
|
| 21 |
+
|
| 22 |
+
# Here we return a dictionary with categories as keys and its probabilities as values
|
| 23 |
+
return dict(zip(categories, map(float, probs)))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Create the Gradio interface
|
| 27 |
+
image = gr.inputs.Image(shape=(192,192))
|
| 28 |
+
label = gr.outputs.Label()
|
| 29 |
+
examples = ['dogg.jpg', 'cat.jpg', 'dunno.jpg']
|
| 30 |
+
|
| 31 |
+
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
|
| 32 |
+
intf.launch(inline=False, share=True)
|
cat.jpg
ADDED
|
dogg.jpg
ADDED
|
dunno.jpg
ADDED
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b9e615cfcff573a78d8b2bf885d6965ae0c268e7945dfa72f2e010c5449dfc88
|
| 3 |
+
size 47062993
|