-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·55 lines (42 loc) · 1.7 KB
/
app.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
#!/usr/bin/python
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import caffe2.python.onnx.backend as backend
import numpy as np
import onnx
import torch
import torchvision
PORT_NUMBER = 8080
start = time.time()
# Temporarily create and save model here: TODO: Create mechanism to load ONNX Model from a Repo.
dummy_input = torch.randn(10, 3, 224, 224, device='cpu') # or cuda
model = torchvision.models.alexnet(pretrained=True).cpu() # or cuda()
input_names = ["actual_input_1"] + ["learned_%d" % i for i in range(16)]
output_names = ["output1"]
torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)
# Load the ONNX model
model = onnx.load("alexnet.onnx")
# Check that the IR is well formed
onnx.checker.check_model(model)
# Print a human readable representation of the graph
onnx.helper.printable_graph(model.graph)
rep = backend.prepare(model, device="CPU") # or "CUDA:0"
end = time.time()
print("Loading time: {0:f} secs)".format(end - start))
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
"""Handler for GET requests"""
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
outputs = rep.run(np.random.randn(10, 3, 224, 224).astype(np.float32))
# To run networks with more than one input, pass a tuple
# rather than a single numpy ndarray.
self.wfile.write(bytes(str(outputs[0]), "utf8"))
try:
server = HTTPServer(('', PORT_NUMBER), MyHandler)
print('Started httpserver on port', PORT_NUMBER)
server.serve_forever()
except KeyboardInterrupt:
server.server_close()
print('Stopping server')