-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathserver.py
63 lines (54 loc) · 1.7 KB
/
server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import json
import numpy as np
from flask import Flask, request
import torch
import torch.nn as nn
import torch.nn.functional as F
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.cn1 = nn.Conv2d(1, 16, 3, 1)
self.cn2 = nn.Conv2d(16, 32, 3, 1)
self.dp1 = nn.Dropout2d(0.10)
self.dp2 = nn.Dropout2d(0.25)
self.fc1 = nn.Linear(4608, 64) # 4608 is basically 12 X 12 X 32
self.fc2 = nn.Linear(64, 10)
def forward(self, x):
x = self.cn1(x)
x = F.relu(x)
x = self.cn2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dp1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dp2(x)
x = self.fc2(x)
op = F.log_softmax(x, dim=1)
return op
model = ConvNet()
PATH_TO_MODEL = "./convnet.pth"
model.load_state_dict(torch.load(PATH_TO_MODEL, map_location="cpu"))
model.eval()
def run_model(input_tensor):
model_input = input_tensor.unsqueeze(0)
with torch.no_grad():
model_output = model(model_input)[0]
model_prediction = model_output.detach().numpy().argmax()
return model_prediction
def post_process(output):
return str(output)
app = Flask(__name__)
@app.route("/test", methods=["POST"])
def test():
data = request.files['data'].read()
md = json.load(request.files['metadata'])
input_array = np.frombuffer(data, dtype=np.float32)
input_image_tensor = torch.from_numpy(input_array).view(md["dims"])
output = run_model(input_image_tensor)
final_output = post_process(output)
return final_output
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8890)