Spaces:
Sleeping
Sleeping
dim-tsoukalas
commited on
Commit
·
be5b201
1
Parent(s):
bc8f810
Added file.
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import *
|
2 |
+
path = untar_data(URLs.PETS)/'images'
|
3 |
+
def is_cat(x): return x[0].isupper()
|
4 |
+
dls = ImageDataLoaders.from_name_func('.',
|
5 |
+
get_image_files(path), valid_pct=0.2, seed=42,
|
6 |
+
label_func=is_cat,
|
7 |
+
item_tfms=Resize(192))
|
8 |
+
|
9 |
+
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
10 |
+
learn.fine_tune(3)
|
11 |
+
|
12 |
+
learn.export('model.pkl')
|
13 |
+
|
14 |
+
im = PILImage.create('dog.jpg')
|
15 |
+
im.thumbnail((192,192))
|
16 |
+
im
|
17 |
+
|
18 |
+
learn = load_learner('model.pkl')
|
19 |
+
|
20 |
+
learn.predict(im)
|
21 |
+
|
22 |
+
categories = ('Dog', 'Cat')
|
23 |
+
|
24 |
+
def classify_image(img):
|
25 |
+
pred, idx, probs=learn.predict(img)
|
26 |
+
return dict(zip(categories, map(float, probs)))
|
27 |
+
|
28 |
+
classify_image(im)
|
29 |
+
|
30 |
+
import gradio as gr
|
31 |
+
|
32 |
+
image = gr.inputs.Image(shape=(192,192))
|
33 |
+
label = gr.outputs.Label()
|
34 |
+
examples = ['dog.jpg', 'cat.jpeg', 'raccoon.jpg']
|
35 |
+
|
36 |
+
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label,examples=examples)
|
37 |
+
intf.launch(inline=False)
|