-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmnist_predictor.py
48 lines (41 loc) · 1.16 KB
/
mnist_predictor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision.transforms.functional as TF
from model.model import MNISTModel
from PIL import Image
size = (28, 28)
def preprocess_img(image):
'''
This method processes the image into the correct expected shape in the model (28, 28).
'''
if (image.mode == 'RGB'):
# Convert RGB to grayscale.
image = image.convert('L')
image = image.resize(size)
return image
def image_loader(image):
'''
This method loads the image into a PyTorch tensor.
'''
image = TF.to_tensor(image)
image = image.unsqueeze(0)
return image
class MNIST_predictor:
def __init__(self):
self.model = MNISTModel()
self.model.net.load_state_dict(torch.load('model/results/model.pth'))
def predict(self, request):
'''
This method reads the file uploaded from the Flask application POST request,
and performs a prediction using the MNIST model.
'''
f = request.files['image']
image = Image.open(f)
image = preprocess_img(image)
image = image_loader(image)
model_output = self.model.net(image)
prediction = torch.argmax(model_output)
return prediction.item()