Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -8,54 +8,92 @@ import numpy
|
|
8 |
import pandas
|
9 |
from fastai.vision.all import *
|
10 |
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
#
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
#
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
intf = gradio.Interface(fn=predict_donut,
|
55 |
-
inputs=image,
|
56 |
outputs=["plot"],
|
57 |
-
examples=examples,
|
58 |
title="120 Dog Breeds Prediction",
|
59 |
live=True,
|
60 |
-
article=
|
61 |
intf.launch(inline=False,share=True)
|
|
|
8 |
import pandas
|
9 |
from fastai.vision.all import *
|
10 |
#
|
11 |
+
# create class
|
12 |
+
class ADA_DOGS(object):
|
13 |
+
#
|
14 |
+
# initialize the object
|
15 |
+
def __init__(self, name="Wallaby",verbose=True,*args, **kwargs):
|
16 |
+
super(ADA_DOGS, self).__init__(*args, **kwargs)
|
17 |
+
if (verbose):
|
18 |
+
self.author = "Duc Haba"
|
19 |
+
self.name = name
|
20 |
+
self._ph()
|
21 |
+
self._pp("Hello from class", str(self.__class__) + " Class: " + str(self.__class__.__name__))
|
22 |
+
self._pp("Code name", self.name)
|
23 |
+
self._pp("Author is", self.author)
|
24 |
+
self._ph()
|
25 |
+
#
|
26 |
+
self.article = '<div><h3>Information:</h3><ul><li>'
|
27 |
+
self.article += 'Author: Duc Haba, 2022.</li>'
|
28 |
+
self.article += '<li>https://linkedin.com/in/duchaba</li>'
|
29 |
+
self.article += '<li>The training dataset is from the Data Scientist at Department of Health '
|
30 |
+
self.article += 'and Social Care London, England, United Kingdom.</li>'
|
31 |
+
self.article += '<li>https://www.kaggle.com/datasets/amandam1/120-dog-breeds-breed-classification</li>'
|
32 |
+
self.article += '</ul></div>'
|
33 |
+
self.examples = ['dog1.jpg','dog2.jpg','dog3.jpg','dog4.jpg','dog5.png','dog6.jpg', 'dog7.jpg','duc.jpg']
|
34 |
+
return
|
35 |
+
#
|
36 |
+
# pretty print output name-value line
|
37 |
+
def _pp(self, a, b):
|
38 |
+
print("%34s : %s" % (str(a), str(b)))
|
39 |
+
return
|
40 |
#
|
41 |
+
# pretty print the header or footer lines
|
42 |
+
def _ph(self):
|
43 |
+
print("-" * 34, ":", "-" * 34)
|
44 |
+
return
|
45 |
+
#
|
46 |
+
def _predict_image(self,img,cat):
|
47 |
+
pred,idx,probs = learn.predict(img)
|
48 |
+
return dict(zip(cat, map(float,probs)))
|
49 |
#
|
50 |
+
def _draw_pred(self,df_pred):
|
51 |
+
canvas, pic = matplotlib.pyplot.subplots(1,1, figsize=(6,6))
|
52 |
+
ti = df_pred["breeds"].head(5).values
|
53 |
+
# special case
|
54 |
+
#if (matplotlib.__version__) >= "3.5.2":
|
55 |
+
try:
|
56 |
+
df_pred["pred"].head(5).plot(ax=pic,kind="pie",figsize=(6,6),
|
57 |
+
cmap="Set2",labels=ti, explode=(0.02,0,0,0,0.),
|
58 |
+
normalize=False)
|
59 |
+
except:
|
60 |
+
df_pred["pred"].head(5).plot(ax=pic,kind="pie",figsize=(6,6),
|
61 |
+
cmap="Set2",labels=ti, explode=(0.02,0,0,0,0.))
|
62 |
+
t = str(ti[0]) + ": " + str(numpy.round(df_pred.head(1).pred.values[0]*100, 2)) + "% Certainty"
|
63 |
+
pic.set_title(t,fontsize=14.0, fontweight="bold")
|
64 |
+
pic.axis('off')
|
65 |
+
#
|
66 |
+
# draw circle
|
67 |
+
centre_circle = matplotlib.pyplot.Circle((0, 0), 0.6, fc='white')
|
68 |
+
canvas = matplotlib.pyplot.gcf()
|
69 |
+
# Adding Circle in Pie chart
|
70 |
+
canvas.gca().add_artist(centre_circle)
|
71 |
+
#
|
72 |
+
canvas.legend(ti, loc="lower right",title="120 Dog Breeds: Top 5")
|
73 |
+
#
|
74 |
+
canvas.tight_layout()
|
75 |
+
return canvas
|
76 |
#
|
77 |
+
def predict_donut(self,img):
|
78 |
+
d = self._predict_image(img,self.categories)
|
79 |
+
df = pandas.DataFrame(d, index=[0])
|
80 |
+
df = df.transpose().reset_index()
|
81 |
+
df.columns = ["breeds", "pred"]
|
82 |
+
df.sort_values("pred", inplace=True,ascending=False, ignore_index=True)
|
83 |
+
canvas = self._draw_pred(df)
|
84 |
+
return canvas
|
85 |
+
#
|
86 |
+
maxi = ADA_DOGS(verbose=False)
|
87 |
+
#
|
88 |
+
learn = fastai.learner.load_learner('ada.pkl')
|
89 |
+
maxi.categories = learn.dls.vocab
|
90 |
+
hf_image = gradio.inputs.Image(shape=(192, 192))
|
91 |
+
hf_label = gradio.outputs.Label()
|
92 |
+
intf = gradio.Interface(fn=maxi.predict_donut,
|
93 |
+
inputs=hf_image,
|
|
|
|
|
94 |
outputs=["plot"],
|
95 |
+
examples=maxi.examples,
|
96 |
title="120 Dog Breeds Prediction",
|
97 |
live=True,
|
98 |
+
article=maxi.article)
|
99 |
intf.launch(inline=False,share=True)
|