alok94 commited on
Commit
c6e9382
·
1 Parent(s): f086e29

made changes in ui and added barplot

Browse files
app.py CHANGED
@@ -5,6 +5,7 @@ from tensorflow.keras.layers import Dense, Flatten
5
  import matplotlib.pyplot as plt
6
  import gradio as gr
7
  import numpy as np
 
8
  #%matplotlib inline
9
 
10
 
@@ -15,46 +16,195 @@ print(X_train.shape)
15
 
16
  print(y_train)
17
 
18
- for i in range(9):
19
- plt.subplot(330+1+i)
20
- plt.imshow(X_train[i])
21
- plt.show()
22
 
23
  X_train=X_train/255.0
24
  X_test=X_test/255.0
25
 
26
- model=tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
27
 
28
- Dense(650,activation='relu'),
29
 
30
- Dense(450,activation='relu'),
31
 
32
- Dense(250,activation='relu'),
33
 
34
- Dense(150,activation='relu'),
35
 
36
- Dense(10,activation=tf.nn.softmax)])
37
 
38
- model.compile(optimizer='adam',
39
- loss='sparse_categorical_crossentropy',
40
- metrics=['accuracy'])
41
- model.fit(X_train,y_train, epochs=10)
42
- model.save("keras_digit_temp.h5")
43
- test=X_test[0].reshape(-1,28,28)
44
- predicted=model.predict(test)
45
- print(predicted)
46
 
47
- def prdict_digit(img):
48
- loaded_model = keras.models.load_model('keras_digit_temp.h5')
49
- img_3d=img.reshape(-1,28,28)
50
- img_resized=img_3d/255.0
51
- pred_prob=loaded_model.predict(img_resized)
52
- predicted_val=np.argmax(pred_prob)
53
- return int(predicted_val)
54
 
55
- iface=gr.Interface(prdict_digit, inputs='sketchpad', outputs='label').launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- iface.launch(debug='true')
58
 
59
 
60
 
 
5
  import matplotlib.pyplot as plt
6
  import gradio as gr
7
  import numpy as np
8
+ import pandas as pd
9
  #%matplotlib inline
10
 
11
 
 
16
 
17
  print(y_train)
18
 
19
+ # for i in range(9):
20
+ # plt.subplot(330+1+i)
21
+ # plt.imshow(X_train[i])
22
+ # plt.show()
23
 
24
  X_train=X_train/255.0
25
  X_test=X_test/255.0
26
 
27
+ # model=tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
28
 
29
+ # Dense(650,activation='relu'),
30
 
31
+ # Dense(450,activation='relu'),
32
 
33
+ # Dense(250,activation='relu'),
34
 
35
+ # Dense(150,activation='relu'),
36
 
37
+ # Dense(10,activation=tf.nn.softmax)])
38
 
39
+ # model.compile(optimizer='adam',
40
+ # loss='sparse_categorical_crossentropy',
41
+ # metrics=['accuracy'])
42
+ # model.fit(X_train,y_train, epochs=10)
43
+ # model.save("keras_digit_temp.h5")
44
+ # test=X_test[0].reshape(-1,28,28)
45
+ # predicted=model.predict(test)
46
+ # print(predicted)
47
 
48
+ def predict_digit(img):
49
+ if img is not None:
 
 
 
 
 
50
 
51
+ loaded_model = keras.models.load_model('keras_digit_temp.h5')
52
+
53
+
54
+ img_3d=img.reshape(-1,28,28)
55
+ img_resized=img_3d/255.0
56
+ pred_prob=loaded_model.predict(img_resized)
57
+
58
+ pred_prob=pred_prob*100
59
+
60
+ print((pred_prob))
61
+ # prob0= 100*pred_prob[0]
62
+ # prob1= 100*pred_prob[1]
63
+ # prob2= 100*pred_prob[2]
64
+ # prob3= 100*pred_prob[3]
65
+ # prob4= 100*pred_prob[4]
66
+ # prob5= 100*pred_prob[5]
67
+ # prob6= 100*pred_prob[6]
68
+ # prob7= 100*pred_prob[7]
69
+ # prob8= 100*pred_prob[8]
70
+ # prob9= 100*pred_prob[9]
71
+
72
+ # print(prob2)
73
+
74
+ simple = pd.DataFrame(
75
+ {
76
+ "a": ["0", "1", "2", "3", "4", "5", "6", "7", "8","9"],
77
+ "b": pred_prob[0], #[28, 55, 43, 91, 81, 53, 19, 87, 52,80],
78
+ }
79
+ )
80
+
81
+ predicted_val=np.argmax(pred_prob)
82
+ return int(predicted_val), gr.BarPlot.update(
83
+ simple,
84
+ x="a",
85
+ y="b",
86
+ x_title="Digits",
87
+ y_title="Identification Probabilities",
88
+ title="Identification Probability",
89
+ tooltip=["a", "b"],
90
+ vertical=False,
91
+ y_lim=[0, 100],
92
+ )
93
+
94
+ else:
95
+ return " "
96
+
97
+
98
+ # iface=gr.Interface(prdict_digit, inputs='sketchpad', outputs=['label', gr.Slider(0,100, label='Probably 0'), gr.Slider(0,100, label='Probably 1')] ).launch()
99
+
100
+ # iface.launch(debug='true')
101
+
102
+ with gr.Blocks() as demo:
103
+ with gr.Row():
104
+ with gr.Column():
105
+ gr.Markdown("Digit Identify", elem_id='title_head')
106
+ gr.Markdown("By Alok")
107
+ with gr.Row():
108
+ with gr.Column():
109
+ with gr.Row():
110
+ skch=gr.Sketchpad()
111
+ with gr.Row():
112
+ with gr.Column():
113
+ clear=gr.ClearButton(skch)
114
+ with gr.Column():
115
+ btn=gr.Button("Identify")
116
+
117
+ with gr.Column():
118
+ gr.Markdown("Identified digit")
119
+ label=gr.Label("")
120
+ gr.Markdown("Other possible values")
121
+ bar = gr.BarPlot()
122
+ btn.click(predict_digit,inputs=skch,outputs=[label,bar])
123
+
124
+
125
+
126
+
127
+ demo.launch()
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+ # import tensorflow as tf
152
+ # from tensorflow import keras
153
+ # from tensorflow.keras import Sequential
154
+ # from tensorflow.keras.layers import Dense, Flatten
155
+ # import matplotlib.pyplot as plt
156
+ # import gradio as gr
157
+ # import numpy as np
158
+ # %matplotlib inline
159
+
160
+
161
+ # objt=tf.keras.datasets.mnist
162
+ # (X_train, y_train), (X_test,y_test)=objt.load_data()
163
+
164
+ # print(X_train.shape)
165
+
166
+ # print(y_train)
167
+
168
+ # for i in range(9):
169
+ # plt.subplot(330+1+i)
170
+ # plt.imshow(X_train[i])
171
+ # plt.show()
172
+
173
+ # X_train=X_train/255.0
174
+ # X_test=X_test/255.0
175
+
176
+ # model=tf.keras.models.Sequential([Flatten(input_shape=(28,28)),
177
+
178
+ # Dense(650,activation='relu'),
179
+
180
+ # Dense(450,activation='relu'),
181
+
182
+ # Dense(250,activation='relu'),
183
+
184
+ # Dense(150,activation='relu'),
185
+
186
+ # Dense(10,activation=tf.nn.softmax)])
187
+
188
+ # model.compile(optimizer='adam',
189
+ # loss='sparse_categorical_crossentropy',
190
+ # metrics=['accuracy'])
191
+ # model.fit(X_train,y_train, epochs=10)
192
+ # model.save("keras_digit_temp.h5")
193
+ # test=X_test[0].reshape(-1,28,28)
194
+ # predicted=model.predict(test)
195
+ # print(predicted)
196
+
197
+ # def prdict_digit(img):
198
+ # loaded_model = keras.models.load_model('keras_digit_temp.h5')
199
+ # img_3d=img.reshape(-1,28,28)
200
+ # img_resized=img_3d/255.0
201
+ # pred_prob=loaded_model.predict(img_resized)
202
+ # predicted_val=np.argmax(pred_prob)
203
+ # return int(predicted_val)
204
+
205
+ # iface=gr.Interface(prdict_digit, inputs='sketchpad', outputs='label').launch()
206
 
207
+ # iface.launch(debug='true')
208
 
209
 
210
 
flagged/img/2230c2787153e177b50659d8d00542de6771f0ff/tmptde5e2ko.png ADDED
flagged/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ img,output,flag,username,timestamp
2
+ D:\Projects\alok-digit-identify\flagged\img\2230c2787153e177b50659d8d00542de6771f0ff\tmptde5e2ko.png,D:\Projects\alok-digit-identify\flagged\output\tmp11i68aun.json,,,2023-09-11 00:52:06.273490
flagged/output/tmp11i68aun.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"label": "2"}
keras_digit_temp.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:381032914c5af60afcaa0aaf305475fd6d9228ef6496bb56c3153913b1974d5c
3
+ size 11511128