duchaba commited on
Commit
172ab76
1 Parent(s): 07bb7a1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -44
app.py CHANGED
@@ -8,54 +8,92 @@ import numpy
8
  import pandas
9
  from fastai.vision.all import *
10
  #
11
- learn = fastai.learner.load_learner('ada.pkl')
12
- categories = learn.dls.vocab
13
- def _predict_image(img):
14
- pred,idx,probs = learn.predict(img)
15
- return dict(zip(categories, map(float,probs)))
16
- image = gradio.inputs.Image(shape=(192, 192))
17
- label = gradio.outputs.Label()
18
- examples = ['dog1.jpg','dog2.jpg','dog3.jpg','dog4.jpg','dog5.png','dog6.jpg', 'dog7.jpg','duc.jpg']
19
- # from fancy up below (but don't do the canvas.show())
20
- def _draw_pred(df_pred):
21
- canvas, pic = matplotlib.pyplot.subplots(1,1, figsize=(6,6))
22
- ti = df_pred["breeds"].head(5).values
23
- df_pred["pred"].head(5).plot(ax=pic,kind="pie",figsize=(6,6),
24
- cmap="Set2",labels=ti)
25
- t = str(ti[0]) + ": " + str(numpy.round(df_pred.head(1).pred.values[0]*100, 2)) + "% Certainty"
26
- pic.set_title(t,fontsize=14.0, fontweight="bold")
27
- pic.axis('off')
 
 
 
 
 
 
 
 
 
 
 
 
28
  #
29
- # draw circle
30
- centre_circle = matplotlib.pyplot.Circle((0, 0), 0.6, fc='white')
31
- canvas = matplotlib.pyplot.gcf()
32
- # Adding Circle in Pie chart
33
- canvas.gca().add_artist(centre_circle)
 
 
 
34
  #
35
- canvas.legend(ti, loc="lower right",title="120 Dog Breeds: Top 5")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  #
37
- canvas.tight_layout()
38
- return canvas
39
- def predict_donut(img):
40
- d = _predict_image(img)
41
- df = pandas.DataFrame(d, index=[0])
42
- df = df.transpose().reset_index()
43
- df.columns = ["breeds", "pred"]
44
- df.sort_values("pred", inplace=True,ascending=False, ignore_index=True)
45
- canvas = _draw_pred(df)
46
- return canvas
47
- art = '<br><ul><li>'
48
- art += 'Author: Duc Haba, 2022.</li>'
49
- art += '<li>https://linkedin.com/in/duchaba</li>'
50
- art += '<li>The training dataset is from the Data Scientist at Department of Health '
51
- art += 'and Social Care London, England, United Kingdom.</li>'
52
- art += '<li>https://www.kaggle.com/datasets/amandam1/120-dog-breeds-breed-classification</li>'
53
- art += '</ul>'
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=art)
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)