F_MNIST
/
ДЗ_34_(light).ipynb
450 строк · 11.7 Кб
1{
2"cells": [
3{
4"cell_type": "code",
5"execution_count": 1,
6"id": "eea2b0bd",
7"metadata": {},
8"outputs": [],
9"source": [
10"import threading\n",
11"import tensorflow\n",
12"import keras\n",
13"import tensorflow.keras.models\n",
14"from tensorflow.keras import backend as K\n",
15"import tensorflow as tf\n",
16"from tensorflow.keras.models import Sequential, model_from_json\n",
17"from tensorflow.keras.layers import Dense, Dropout, Flatten\n",
18"from tensorflow.keras.layers import Conv2D, MaxPooling2D\n",
19"from tensorflow.keras.datasets import mnist\n",
20"from tensorflow.keras.preprocessing import image\n",
21"from PIL import Image\n",
22"import PIL.ImageOps"
23]
24},
25{
26"cell_type": "code",
27"execution_count": 21,
28"id": "ed3a8c20",
29"metadata": {},
30"outputs": [],
31"source": [
32"from flask import Flask, request\n",
33"from flask import render_template\n",
34"import imageio\n",
35"import numpy as np\n",
36"import re\n",
37"import sys\n",
38"import os\n",
39"import base64\n",
40"\n",
41"app = Flask(__name__)"
42]
43},
44{
45"cell_type": "code",
46"execution_count": 22,
47"id": "bb823d36",
48"metadata": {},
49"outputs": [],
50"source": [
51"with open('model.json', 'r') as f:\n",
52" loaded_model_json = f.read()\n",
53" loaded_model = model_from_json(loaded_model_json)"
54]
55},
56{
57"cell_type": "code",
58"execution_count": 23,
59"id": "8321b113",
60"metadata": {},
61"outputs": [],
62"source": [
63"loaded_model.load_weights('model.h5')\n",
64"loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
65]
66},
67{
68"cell_type": "code",
69"execution_count": 24,
70"id": "ed65fd08",
71"metadata": {},
72"outputs": [],
73"source": [
74"def convertImage(imgData):\n",
75" imgstr = re.search(b'base64,(.*)', imgData).group(1)\n",
76" with open('output.png', 'wb') as output:\n",
77" output.write(base64.b64decode(imgstr))"
78]
79},
80{
81"cell_type": "code",
82"execution_count": 25,
83"id": "9248feae",
84"metadata": {},
85"outputs": [],
86"source": [
87"@app.route('/')\n",
88"@app.route('/index')\n",
89"def index():\n",
90" return render_template('index.html')"
91]
92},
93{
94"cell_type": "code",
95"execution_count": 26,
96"id": "130cecea",
97"metadata": {},
98"outputs": [],
99"source": [
100"@app.route('/predict/', methods=['GET', 'POST'])\n",
101"def predict():\n",
102" imgData = request.get_data()\n",
103" convertImage(imgData)\n",
104" print('debug')\n",
105" img = image.load_img('output.png', target_size=(28,28), color_mode = 'grayscale')\n",
106" img = PIL. ImageOps.invert(img)\n",
107" sample = np.array(img)\n",
108" sample = np.reshape(sample, (1, 28, 28, 1))\n",
109" sample = sample.astype('float32')\n",
110" sample = sample / 255\n",
111" out = loaded_model.predict (sample)\n",
112" print (\"debug2\")\n",
113" print (out)\n",
114" print (np.argmax(out, axis=1))\n",
115" print (\"debug3\")\n",
116" \n",
117" response = np.array_str(np.argmax(out, axis=1))\n",
118" return response"
119]
120},
121{
122"cell_type": "code",
123"execution_count": null,
124"id": "5f2f7c22",
125"metadata": {},
126"outputs": [
127{
128"name": "stdout",
129"output_type": "stream",
130"text": [
131" * Serving Flask app \"__main__\" (lazy loading)\n",
132" * Environment: production\n",
133"\u001b[31m WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
134"\u001b[2m Use a production WSGI server instead.\u001b[0m\n",
135" * Debug mode: off\n"
136]
137},
138{
139"name": "stderr",
140"output_type": "stream",
141"text": [
142" * Running on http://127.0.0.1:5035/ (Press CTRL+C to quit)\n",
143"127.0.0.1 - - [23/Jan/2023 20:15:26] \"GET / HTTP/1.1\" 200 -\n"
144]
145},
146{
147"name": "stdout",
148"output_type": "stream",
149"text": [
150"debug\n",
151"1/1 [==============================] - 0s 84ms/step\n"
152]
153},
154{
155"name": "stderr",
156"output_type": "stream",
157"text": [
158"127.0.0.1 - - [23/Jan/2023 20:15:30] \"POST /predict/ HTTP/1.1\" 200 -\n"
159]
160},
161{
162"name": "stdout",
163"output_type": "stream",
164"text": [
165"debug2\n",
166"[[9.3201059e-01 1.1428276e-11 1.0070351e-05 4.3536406e-08 6.1634209e-09\n",
167" 2.7219914e-05 3.2060329e-02 5.7727096e-12 3.5883427e-02 8.3746581e-06]]\n",
168"[0]\n",
169"debug3\n",
170"debug\n",
171"1/1 [==============================] - 0s 15ms/step\n"
172]
173},
174{
175"name": "stderr",
176"output_type": "stream",
177"text": [
178"127.0.0.1 - - [23/Jan/2023 20:15:35] \"POST /predict/ HTTP/1.1\" 200 -\n"
179]
180},
181{
182"name": "stdout",
183"output_type": "stream",
184"text": [
185"debug2\n",
186"[[5.2022106e-06 4.0624676e-07 9.6911466e-01 2.6125127e-02 2.0442321e-16\n",
187" 1.3959200e-10 2.0406446e-12 1.2396893e-11 4.7546322e-03 2.4293083e-09]]\n",
188"[2]\n",
189"debug3\n",
190"debug\n",
191"1/1 [==============================] - 0s 15ms/step\n"
192]
193},
194{
195"name": "stderr",
196"output_type": "stream",
197"text": [
198"127.0.0.1 - - [23/Jan/2023 20:15:37] \"POST /predict/ HTTP/1.1\" 200 -\n"
199]
200},
201{
202"name": "stdout",
203"output_type": "stream",
204"text": [
205"debug2\n",
206"[[2.7174503e-14 6.3781899e-16 7.5413906e-09 9.9999487e-01 1.1777393e-13\n",
207" 4.6881702e-08 3.7957003e-22 1.1922670e-07 1.6922473e-14 5.0051403e-06]]\n",
208"[3]\n",
209"debug3\n",
210"debug\n",
211"1/1 [==============================] - 0s 14ms/step\n"
212]
213},
214{
215"name": "stderr",
216"output_type": "stream",
217"text": [
218"127.0.0.1 - - [23/Jan/2023 20:15:40] \"POST /predict/ HTTP/1.1\" 200 -\n"
219]
220},
221{
222"name": "stdout",
223"output_type": "stream",
224"text": [
225"debug2\n",
226"[[3.1875302e-09 4.8050235e-09 2.3869720e-07 5.9134164e-03 1.6584538e-01\n",
227" 4.5474599e-06 5.1189738e-12 4.0587294e-04 1.2965601e-04 8.2770091e-01]]\n",
228"[9]\n",
229"debug3\n",
230"debug\n",
231"1/1 [==============================] - 0s 15ms/step\n"
232]
233},
234{
235"name": "stderr",
236"output_type": "stream",
237"text": [
238"127.0.0.1 - - [23/Jan/2023 20:15:42] \"POST /predict/ HTTP/1.1\" 200 -\n"
239]
240},
241{
242"name": "stdout",
243"output_type": "stream",
244"text": [
245"debug2\n",
246"[[1.1290436e-09 8.8604712e-15 1.1948684e-17 8.2895118e-01 6.1764491e-14\n",
247" 1.4281274e-03 1.3353132e-17 4.4147761e-13 2.4497970e-11 1.6962069e-01]]\n",
248"[3]\n",
249"debug3\n",
250"debug\n",
251"1/1 [==============================] - 0s 15ms/step\n"
252]
253},
254{
255"name": "stderr",
256"output_type": "stream",
257"text": [
258"127.0.0.1 - - [23/Jan/2023 20:15:47] \"POST /predict/ HTTP/1.1\" 200 -\n"
259]
260},
261{
262"name": "stdout",
263"output_type": "stream",
264"text": [
265"debug2\n",
266"[[4.55450264e-24 1.13084995e-17 9.58464049e-16 1.81709524e-25\n",
267" 1.00000000e+00 1.68002759e-15 5.04004041e-27 9.46734959e-14\n",
268" 8.56097619e-20 3.16767723e-10]]\n",
269"[4]\n",
270"debug3\n",
271"debug\n",
272"1/1 [==============================] - 0s 14ms/step\n"
273]
274},
275{
276"name": "stderr",
277"output_type": "stream",
278"text": [
279"127.0.0.1 - - [23/Jan/2023 20:15:50] \"POST /predict/ HTTP/1.1\" 200 -\n"
280]
281},
282{
283"name": "stdout",
284"output_type": "stream",
285"text": [
286"debug2\n",
287"[[6.3625973e-11 1.4692613e-14 2.6769520e-09 2.5775041e-02 1.6015545e-16\n",
288" 9.7420537e-01 1.9484218e-05 2.6060049e-15 3.4179902e-08 6.0795255e-20]]\n",
289"[5]\n",
290"debug3\n",
291"debug\n",
292"1/1 [==============================] - 0s 17ms/step\n"
293]
294},
295{
296"name": "stderr",
297"output_type": "stream",
298"text": [
299"127.0.0.1 - - [23/Jan/2023 20:15:52] \"POST /predict/ HTTP/1.1\" 200 -\n"
300]
301},
302{
303"name": "stdout",
304"output_type": "stream",
305"text": [
306"debug2\n",
307"[[8.0842455e-08 1.5206976e-01 4.0687528e-08 6.9201074e-04 1.4595592e-03\n",
308" 5.6558118e-09 2.1334360e-11 4.5443960e-02 5.0548377e-05 8.0028409e-01]]\n",
309"[9]\n",
310"debug3\n",
311"debug\n",
312"1/1 [==============================] - 0s 14ms/step\n"
313]
314},
315{
316"name": "stderr",
317"output_type": "stream",
318"text": [
319"127.0.0.1 - - [23/Jan/2023 20:15:55] \"POST /predict/ HTTP/1.1\" 200 -\n"
320]
321},
322{
323"name": "stdout",
324"output_type": "stream",
325"text": [
326"debug2\n",
327"[[3.6834126e-11 9.9999702e-01 1.1056134e-14 6.2640226e-11 1.0510353e-10\n",
328" 1.7886409e-15 2.2788171e-12 1.7409876e-06 3.5725026e-08 1.1992629e-06]]\n",
329"[1]\n",
330"debug3\n",
331"debug\n",
332"1/1 [==============================] - 0s 16ms/step\n"
333]
334},
335{
336"name": "stderr",
337"output_type": "stream",
338"text": [
339"127.0.0.1 - - [23/Jan/2023 20:15:58] \"POST /predict/ HTTP/1.1\" 200 -\n"
340]
341},
342{
343"name": "stdout",
344"output_type": "stream",
345"text": [
346"debug2\n",
347"[[1.0000000e+00 1.6443580e-20 5.8074293e-19 1.7929975e-23 1.3078717e-13\n",
348" 2.0822644e-22 2.3584164e-11 3.8935616e-17 1.3081929e-14 1.1000708e-16]]\n",
349"[0]\n",
350"debug3\n",
351"debug\n",
352"1/1 [==============================] - 0s 17ms/step\n"
353]
354},
355{
356"name": "stderr",
357"output_type": "stream",
358"text": [
359"127.0.0.1 - - [23/Jan/2023 20:16:00] \"POST /predict/ HTTP/1.1\" 200 -\n"
360]
361},
362{
363"name": "stdout",
364"output_type": "stream",
365"text": [
366"debug2\n",
367"[[1.1011792e-32 5.2573558e-19 7.1413356e-06 2.3974273e-18 6.5579042e-11\n",
368" 9.9999285e-01 6.8268307e-23 7.3875900e-16 1.7079189e-10 1.4159600e-18]]\n",
369"[5]\n",
370"debug3\n",
371"debug\n",
372"1/1 [==============================] - 0s 15ms/step\n"
373]
374},
375{
376"name": "stderr",
377"output_type": "stream",
378"text": [
379"127.0.0.1 - - [23/Jan/2023 20:16:28] \"POST /predict/ HTTP/1.1\" 200 -\n"
380]
381},
382{
383"name": "stdout",
384"output_type": "stream",
385"text": [
386"debug2\n",
387"[[1.04400585e-13 1.40164804e-03 9.98515189e-01 8.48558102e-06\n",
388" 1.82685803e-16 3.68405135e-17 7.12706121e-16 7.45378202e-05\n",
389" 1.44322826e-07 5.80550018e-12]]\n",
390"[2]\n",
391"debug3\n",
392"debug\n",
393"1/1 [==============================] - 0s 15ms/step\n"
394]
395},
396{
397"name": "stderr",
398"output_type": "stream",
399"text": [
400"127.0.0.1 - - [23/Jan/2023 20:16:32] \"POST /predict/ HTTP/1.1\" 200 -\n"
401]
402},
403{
404"name": "stdout",
405"output_type": "stream",
406"text": [
407"debug2\n",
408"[[9.3762967e-17 3.1366699e-15 4.2425044e-12 6.3464239e-10 9.2023604e-14\n",
409" 1.0000000e+00 6.2463825e-19 6.7069276e-15 2.6422970e-16 2.4572570e-08]]\n",
410"[5]\n",
411"debug3\n"
412]
413}
414],
415"source": [
416"port = int(os.environ.get('PORT', 5035))\n",
417"app.run(host='127.0.0.1', port=port, threaded=False)"
418]
419},
420{
421"cell_type": "code",
422"execution_count": null,
423"id": "7116c55c",
424"metadata": {},
425"outputs": [],
426"source": []
427}
428],
429"metadata": {
430"kernelspec": {
431"display_name": "Python 3 (ipykernel)",
432"language": "python",
433"name": "python3"
434},
435"language_info": {
436"codemirror_mode": {
437"name": "ipython",
438"version": 3
439},
440"file_extension": ".py",
441"mimetype": "text/x-python",
442"name": "python",
443"nbconvert_exporter": "python",
444"pygments_lexer": "ipython3",
445"version": "3.9.13"
446}
447},
448"nbformat": 4,
449"nbformat_minor": 5
450}
451