Spaces:
Runtime error
Runtime error
Kyle Dampier
commited on
Commit
•
89e16b4
1
Parent(s):
60d926f
removed example UI from Notebook
Browse files- Week1.ipynb +4 -111
Week1.ipynb
CHANGED
@@ -232,128 +232,21 @@
|
|
232 |
"print(\"Test accuracy:\", score[1])"
|
233 |
]
|
234 |
},
|
235 |
-
{
|
236 |
-
"cell_type": "code",
|
237 |
-
"execution_count": 7,
|
238 |
-
"metadata": {},
|
239 |
-
"outputs": [],
|
240 |
-
"source": [
|
241 |
-
"model.save(\"mnist.h5\")"
|
242 |
-
]
|
243 |
-
},
|
244 |
{
|
245 |
"cell_type": "markdown",
|
246 |
"metadata": {},
|
247 |
"source": [
|
248 |
-
"##
|
249 |
]
|
250 |
},
|
251 |
{
|
252 |
"cell_type": "code",
|
253 |
-
"execution_count":
|
254 |
"metadata": {},
|
255 |
-
"outputs": [
|
256 |
-
{
|
257 |
-
"name": "stdout",
|
258 |
-
"output_type": "stream",
|
259 |
-
"text": [
|
260 |
-
"4\n"
|
261 |
-
]
|
262 |
-
}
|
263 |
-
],
|
264 |
"source": [
|
265 |
-
"
|
266 |
-
"from PIL import ImageGrab\n",
|
267 |
-
"import imageio\n",
|
268 |
-
"import tkinter.font as font\n",
|
269 |
-
"\n",
|
270 |
-
"class Paint(object):\n",
|
271 |
-
" def __init__(self):\n",
|
272 |
-
" self.root=Tk()\n",
|
273 |
-
" self.root.title('Playing with numbers')\n",
|
274 |
-
" # self.root.wm_iconbitmap('44143.ico')\n",
|
275 |
-
" self.root.configure(background='light salmon')\n",
|
276 |
-
" self.c = Canvas(self.root,bg='light cyan', height=330, width=400)\n",
|
277 |
-
" self.label = Label(self.root, text='Draw any numer', font=20, bg='light salmon')\n",
|
278 |
-
" self.label.grid(row=0, column=3)\n",
|
279 |
-
" self.c.grid(row=1, columnspan=9)\n",
|
280 |
-
" self.c.create_line(0,0,400,0,width=20,fill='midnight blue')\n",
|
281 |
-
" self.c.create_line(0,0,0,330,width=20,fill='midnight blue')\n",
|
282 |
-
" self.c.create_line(400,0,400,330,width=20,fill='midnight blue')\n",
|
283 |
-
" self.c.create_line(0,330,400,330,width=20,fill='midnight blue')\n",
|
284 |
-
" self.myfont = font.Font(size=20,weight='bold')\n",
|
285 |
-
" self.predicting_button=Button(self.root,text='Predict', fg='white', bg='blue', height=2, width=6, font=self.myfont, command=lambda:self.classify(self.c))\n",
|
286 |
-
" self.predicting_button.grid(row=2,column=1)\n",
|
287 |
-
" self.clear=Button(self.root,text='Clear', fg='white', bg='orange', height=2, width=6, font=self.myfont, command=self.clear)\n",
|
288 |
-
" self.clear.grid(row=2,column=5)\n",
|
289 |
-
" self.prediction_text = Text(self.root, height=5, width=5)\n",
|
290 |
-
" self.prediction_text.grid(row=4, column=3)\n",
|
291 |
-
" self.label=Label(self.root, text=\"Predicted Number is\", fg=\"black\", font=30, bg='light salmon')\n",
|
292 |
-
"\n",
|
293 |
-
" self.label.grid(row=3,column=3)\n",
|
294 |
-
" self.model=model\n",
|
295 |
-
" self.setup()\n",
|
296 |
-
" self.root.mainloop()\n",
|
297 |
-
"\n",
|
298 |
-
"\n",
|
299 |
-
" def setup(self):\n",
|
300 |
-
" self.old_x=None\n",
|
301 |
-
" self.old_y=None\n",
|
302 |
-
" self.color='black'\n",
|
303 |
-
" self.linewidth=15\n",
|
304 |
-
" self.c.bind('<B1-Motion>', self.paint)\n",
|
305 |
-
" self.c.bind('<ButtonRelease-1>', self.reset)\n",
|
306 |
-
"\n",
|
307 |
-
"\n",
|
308 |
-
" def paint(self,event):\n",
|
309 |
-
" paint_color=self.color\n",
|
310 |
-
" if self.old_x and self.old_y:\n",
|
311 |
-
" self.c.create_line(self.old_x,self.old_y,event.x,event.y,fill=paint_color,width=self.linewidth,capstyle=ROUND,\n",
|
312 |
-
" smooth=TRUE,splinesteps=48)\n",
|
313 |
-
" self.old_x=event.x\n",
|
314 |
-
" self.old_y=event.y\n",
|
315 |
-
"\n",
|
316 |
-
"\n",
|
317 |
-
" def clear(self):\n",
|
318 |
-
" \"\"\"Clear drawing area\"\"\"\n",
|
319 |
-
" self.c.delete(\"all\")\n",
|
320 |
-
"\n",
|
321 |
-
" def reset(self, event):\n",
|
322 |
-
" \"\"\"reset old_x and old_y if the left mouse button is released\"\"\"\n",
|
323 |
-
" self.old_x, self.old_y = None, None\n",
|
324 |
-
"\n",
|
325 |
-
"\n",
|
326 |
-
" def classify(self,widget):\n",
|
327 |
-
" x=self.root.winfo_rootx()+widget.winfo_x()\n",
|
328 |
-
" y=self.root.winfo_rooty()+widget.winfo_y()\n",
|
329 |
-
" x1=widget.winfo_width()\n",
|
330 |
-
" y1=widget.winfo_height()\n",
|
331 |
-
" ImageGrab.grab().crop((x,y,x1,y1)).resize((28,28)).save('classify.png')\n",
|
332 |
-
" img=imageio.imread('classify.png', as_gray=True, pilmode='P')\n",
|
333 |
-
" img=np.array(img)\n",
|
334 |
-
" img=np.reshape(img,(1,28,28,1))\n",
|
335 |
-
" img[img==0] = 255\n",
|
336 |
-
" img[img==225] = 0\n",
|
337 |
-
" # Predict digit\n",
|
338 |
-
" pred = self.model.predict([img])\n",
|
339 |
-
" # Get index with highest probability\n",
|
340 |
-
" pred = np.argmax(pred)\n",
|
341 |
-
" print(pred)\n",
|
342 |
-
" self.prediction_text.delete(\"1.0\", END)\n",
|
343 |
-
" self.prediction_text.insert(END, pred)\n",
|
344 |
-
" labelfont = ('times', 30, 'bold')\n",
|
345 |
-
" self.prediction_text.config(font=labelfont)\n",
|
346 |
-
"\n",
|
347 |
-
"if __name__ == '__main__':\n",
|
348 |
-
" Paint()"
|
349 |
]
|
350 |
-
},
|
351 |
-
{
|
352 |
-
"cell_type": "code",
|
353 |
-
"execution_count": null,
|
354 |
-
"metadata": {},
|
355 |
-
"outputs": [],
|
356 |
-
"source": []
|
357 |
}
|
358 |
],
|
359 |
"metadata": {
|
|
|
232 |
"print(\"Test accuracy:\", score[1])"
|
233 |
]
|
234 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
{
|
236 |
"cell_type": "markdown",
|
237 |
"metadata": {},
|
238 |
"source": [
|
239 |
+
"## Save Model (h5 format)"
|
240 |
]
|
241 |
},
|
242 |
{
|
243 |
"cell_type": "code",
|
244 |
+
"execution_count": 7,
|
245 |
"metadata": {},
|
246 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
"source": [
|
248 |
+
"model.save(\"mnist.h5\")"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
}
|
251 |
],
|
252 |
"metadata": {
|