From 2812a9301a1ec9f2603b33c00bbb538fc26e7796 Mon Sep 17 00:00:00 2001 From: fsalhani Date: Thu, 27 Dec 2018 13:45:22 -0200 Subject: [PATCH 1/8] remove pycache from repo --- .../akangatu/__pycache__/__init__.cpython-36.pyc | Bin 167 -> 0 bytes .../akangatu/__pycache__/views.cpython-36.pyc | Bin 570 -> 0 bytes .../__pycache__/__init__.cpython-36.pyc | Bin 173 -> 0 bytes .../__pycache__/settings.cpython-36.pyc | Bin 2299 -> 0 bytes .../__pycache__/urls.cpython-36.pyc | Bin 932 -> 0 bytes .../__pycache__/wsgi.cpython-36.pyc | Bin 590 -> 0 bytes 6 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 django_project/akangatu/__pycache__/__init__.cpython-36.pyc delete mode 100644 django_project/akangatu/__pycache__/views.cpython-36.pyc delete mode 100644 django_project/django_project/__pycache__/__init__.cpython-36.pyc delete mode 100644 django_project/django_project/__pycache__/settings.cpython-36.pyc delete mode 100644 django_project/django_project/__pycache__/urls.cpython-36.pyc delete mode 100644 django_project/django_project/__pycache__/wsgi.cpython-36.pyc diff --git a/django_project/akangatu/__pycache__/__init__.cpython-36.pyc b/django_project/akangatu/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index 8de1afe7d3931111b1cb14cc78de5d51b714491c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 167 zcmXr!<>k__7L8#5g2x~N1{i@12OutH0TL+;!3>&=ek&P@K*9*(SBQQ_er~FMT5)1d zMq*y3zDs^`X>Mv>NwL0rW=TeAl732NdS;1kQEGC2dR}H#YLR|QR$^XyetbbuepYI7 piGE@>kegUisvjSpnU`4-AFo$Xd5gm)H$SB`C)EyQTQLwb005;7E9w9M diff --git a/django_project/akangatu/__pycache__/views.cpython-36.pyc b/django_project/akangatu/__pycache__/views.cpython-36.pyc deleted file mode 100644 index bf2af2b7de92b310ec858ad9751f92457bcd23f0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 570 zcmYk2&uY~`6vk&JxkfZ4WO2}VwX9ajZz%tKKIN?;1 zhGw*38KXnbWYC0JNC|{JIcC`;i(tZop7M}SzLB10F^|?HiLdZINtmC#GphXnrc}x62Iq-z~SSx2mTVC#csvwuPIw>kB>a@n$qXIFpIYSh)A>BOR?=E~eBoL#y znW7N3L#K?_v%zO~q|Fxru8B+TajoKKx7>I*%j<^!jt-$v diff --git a/django_project/django_project/__pycache__/__init__.cpython-36.pyc b/django_project/django_project/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index a40e53a20f03e92860c288925bf6a412d5f90ff3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 173 zcmXr!<>jg~6OCa2g2x~N1{i@12OutH0TL+;!3>&=ek&P@K*9*(SCoE6er~FMT5)1d zMq*y3zDs^`X>Mv>NwL0rW=TeAl732NdS;1kQEGC2dR}H#YLR|QR$^XyetbbuepYI7 i2|71EJ~J<~BtBlRpz;=nO>TZlX-=vg$j)LQW&i*M%PzhE diff --git a/django_project/django_project/__pycache__/settings.cpython-36.pyc b/django_project/django_project/__pycache__/settings.cpython-36.pyc deleted file mode 100644 index 3eb234ea61ece181fd4eab37b6b8f1558bde53c3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2299 zcmb7FTXWM!6!t~29g~DxxD;q33KT*csUZ+Loeqt#h4J7^#6`USY6ufSD10|KnWHMkBp;HE9mEPVD+u(SFq+|t+JHrz>LT_gqg z{6h}D_?V+v{Ss_|h?X00H=Vys=X*433;JcauU~-&`c=$i9iMCXT!*jp8<5j7n-9&M zY$j8#j!e(?MMgNMp3TI8ABd33jB((Ph{eU}TPl}p#3O;p35fk;@!`a5%LJEtBI9Pj zr!L%zW@NO{Qln0+tH;9E?Z z#F%AZ`L0Yn83ASPkEzAvS>Eylo)3vQ2pvasDC43(INg`&0R|#85D!f!B<%mh3dmqC z(BH^|Oe=cQe>Q@>y~l@#cb`3n?I#bmJl}e{yY2gg2k%Hk?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!p_^oZ7;JOtKq{j#SxnOsEP?=<|7LB^Gn- z&rxp-AQdc+kL8mL7K4?um0hcHNJ5 zKV5$zzF=fwX;V`r$V**zk}GG)YvJndgfLMFw`+K*DHzMN`)z0z9aXKQ(+nknm)~+(Ar#j zLj|ykN*{?ViG#w`4UIm_Ye{7FNcrbeVdOAL1v(xkQ$#gtp@6Sdb-kok9 z-W)>iz Date: Thu, 27 Dec 2018 13:51:05 -0200 Subject: [PATCH 2/8] modify reshape data --- django_project/akangatu/core/preprocessor.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/django_project/akangatu/core/preprocessor.py b/django_project/akangatu/core/preprocessor.py index fa863df..bc2e60b 100644 --- a/django_project/akangatu/core/preprocessor.py +++ b/django_project/akangatu/core/preprocessor.py @@ -1,6 +1,10 @@ import numpy as np + +MNIST_WIDTH = 28 +MNIST_HEIGHT = 28 +COLOR_SCALE = 255 + class Preprocessor: - def reshape_data(request): - pixel_array = np.array(request) - return pixel_array.reshape(-1, 1, 28, 28) / 255.0 + def reshape_data(pixel_array): + return pixel_array.reshape(1, 1, MNIST_WIDTH, MNIST_HEIGHT) / COLOR_SCALE From 922ae52f8f87a3ca69a20ed024613daa3729930d Mon Sep 17 00:00:00 2001 From: fsalhani Date: Thu, 27 Dec 2018 14:16:34 -0200 Subject: [PATCH 3/8] convert image to mnist 28x28 array --- django_project/akangatu/core/preprocessor.py | 35 ++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/django_project/akangatu/core/preprocessor.py b/django_project/akangatu/core/preprocessor.py index bc2e60b..61c0573 100644 --- a/django_project/akangatu/core/preprocessor.py +++ b/django_project/akangatu/core/preprocessor.py @@ -1,10 +1,39 @@ import numpy as np -MNIST_WIDTH = 28 -MNIST_HEIGHT = 28 +MNIST_SIZE = 28 COLOR_SCALE = 255 class Preprocessor: + def image_to_mnist(image): + pixel_array = aggregate_to_mnist_array(image) + + return reshape_data(pixel_array) / COLOR_SCALE + def reshape_data(pixel_array): - return pixel_array.reshape(1, 1, MNIST_WIDTH, MNIST_HEIGHT) / COLOR_SCALE + return pixel_array.reshape(1, 1, MNIST_SIZE, MNIST_SIZE) + + def get_group_sizes(image): + image_width = len(image[0]) + image_height = len(image) + + return image_height // MNIST_SIZE, image_width // MNIST_SIZE + + def aggregate_to_mnist_array(image): + mnist_array = np.zeros([MNIST_SIZE, MNIST_SIZE]) + + for i in range(MNIST_SIZE): + for j in range(MNIST_SIZE): + pixel_array[i][j] = calculate_mnist_pixel(image,i,j) + + return mnist_array + + def calculate_mnist_pixel(image,line,column): + group_height, group_width = get_group_sizes(image) + + rgb_sum = 0 + for i in range(group_height): + for j in range(group_width): + rgb_sum += sum(image[line*group_height+i][column*group_width+l])//len(image[line][column]) + + return rgb_sum / (group_width * group_height) From 3403d2033ec7b2334d53be4952f8143a2574ab90 Mon Sep 17 00:00:00 2001 From: fsalhani Date: Thu, 27 Dec 2018 14:17:56 -0200 Subject: [PATCH 4/8] update predictor to use new method --- django_project/akangatu/core/predictor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_project/akangatu/core/predictor.py b/django_project/akangatu/core/predictor.py index 9a1bc2c..fe1944f 100644 --- a/django_project/akangatu/core/predictor.py +++ b/django_project/akangatu/core/predictor.py @@ -15,7 +15,7 @@ def predict(self, pixel_array): if not self._model: self._load_model() - pixels = Preprocessor.reshape_data(pixel_array) + pixels = Preprocessor.image_to_mnist(pixel_array) probs = self._model.predict(pixels) return probs.argmax(axis=1) From 46e226b4ad61c9a7424ed63e33451b0fda5b69e9 Mon Sep 17 00:00:00 2001 From: fsalhani Date: Thu, 27 Dec 2018 14:45:11 -0200 Subject: [PATCH 5/8] create instance of preprocessor on predictor --- django_project/akangatu/core/predictor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django_project/akangatu/core/predictor.py b/django_project/akangatu/core/predictor.py index fe1944f..c35ac3f 100644 --- a/django_project/akangatu/core/predictor.py +++ b/django_project/akangatu/core/predictor.py @@ -2,6 +2,8 @@ from .preprocessor import Preprocessor +preprocessor = Preprocessor() + class Predictor: _model = None @@ -15,7 +17,7 @@ def predict(self, pixel_array): if not self._model: self._load_model() - pixels = Preprocessor.image_to_mnist(pixel_array) + pixels = preprocessor.image_to_mnist(pixel_array) probs = self._model.predict(pixels) return probs.argmax(axis=1) From 463960d074b69f6577497d3294488c4b4046ec27 Mon Sep 17 00:00:00 2001 From: fsalhani Date: Thu, 27 Dec 2018 14:45:41 -0200 Subject: [PATCH 6/8] fix method calls for preprocessor --- django_project/akangatu/core/preprocessor.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/django_project/akangatu/core/preprocessor.py b/django_project/akangatu/core/preprocessor.py index 61c0573..f13cfb9 100644 --- a/django_project/akangatu/core/preprocessor.py +++ b/django_project/akangatu/core/preprocessor.py @@ -5,35 +5,35 @@ COLOR_SCALE = 255 class Preprocessor: - def image_to_mnist(image): - pixel_array = aggregate_to_mnist_array(image) + def image_to_mnist(self, image): + pixel_array = self._aggregate_to_mnist_array(image) - return reshape_data(pixel_array) / COLOR_SCALE + return self._reshape_data(pixel_array) / COLOR_SCALE - def reshape_data(pixel_array): + def _reshape_data(self, pixel_array): return pixel_array.reshape(1, 1, MNIST_SIZE, MNIST_SIZE) - def get_group_sizes(image): + def _get_group_sizes(self, image): image_width = len(image[0]) image_height = len(image) return image_height // MNIST_SIZE, image_width // MNIST_SIZE - def aggregate_to_mnist_array(image): + def _aggregate_to_mnist_array(self, image): mnist_array = np.zeros([MNIST_SIZE, MNIST_SIZE]) for i in range(MNIST_SIZE): for j in range(MNIST_SIZE): - pixel_array[i][j] = calculate_mnist_pixel(image,i,j) + mnist_array[i][j] = self._calculate_mnist_pixel(image,i,j) return mnist_array - def calculate_mnist_pixel(image,line,column): - group_height, group_width = get_group_sizes(image) + def _calculate_mnist_pixel(self, image,line,column): + group_height, group_width = self._get_group_sizes(image) rgb_sum = 0 for i in range(group_height): for j in range(group_width): - rgb_sum += sum(image[line*group_height+i][column*group_width+l])//len(image[line][column]) + rgb_sum += sum(image[line*group_height+i][column*group_width+j])//len(image[line][column]) return rgb_sum / (group_width * group_height) From 3cacc4e94e065e18cb12ac3d343de3c6b1f8fcc6 Mon Sep 17 00:00:00 2001 From: fsalhani Date: Thu, 27 Dec 2018 14:46:21 -0200 Subject: [PATCH 7/8] add image reader to predict view --- django_project/akangatu/views.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/django_project/akangatu/views.py b/django_project/akangatu/views.py index 3dbb460..981416e 100644 --- a/django_project/akangatu/views.py +++ b/django_project/akangatu/views.py @@ -2,6 +2,7 @@ from django_quicky import routing from django.views.decorators.csrf import csrf_exempt import json +import matplotlib.image as img from .models import Predictor @@ -25,11 +26,11 @@ def predict(request): if request.method.upper() != 'POST': return HttpResponseNotAllowed(['POST']) # List of allowed ones - json_data = request.POST.dict() or json.loads(request.body.decode('utf-8')) + image_file = next(iter(request.FILES.values())) - print(json_data) + image = img.imread(image_file) - pred = predictor_acessor.predict(json_data['pixels']) + pred = predictor_acessor.predict(image) answer = {'label': int(pred[0]) } From 5b9227faaf894604014fa6eb877b9a3ca128150f Mon Sep 17 00:00:00 2001 From: fsalhani Date: Tue, 5 Feb 2019 00:14:34 -0200 Subject: [PATCH 8/8] update requirements.txt with image libs --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 49cb872..af3703e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,9 @@ Keras==2.2.4 Keras-Applications==1.0.6 Keras-Preprocessing==1.0.5 Markdown==3.0.1 +matplotlib==3.0.2 numpy==1.15.4 +Pillow==5.3.0 protobuf==3.6.1 pytz==2018.7 PyYAML==3.13