-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
executable file
·89 lines (68 loc) · 3.19 KB
/
demo.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import numpy as np
import cv2
import torch
from torchvision import transforms
from homnist.visualization import DrawingCanvas
from homnist.learning import MinMaxScale, ConvertToBlackWhite
from homnist.network import HONet, HACCNet
IMAGE_SIZE = [28, 28] # MNIST input size
PENCIL_THICKNESS = 2.5
SIZE_MULTIPLIER = 20 # So things are not as small
def main():
# Demo settings
parser = argparse.ArgumentParser(description='Hardware Oriented MNIST test')
parser.add_argument('--model-path', type=str, default="./models/mnist_quantized_converted.pth",
help='MNIST trained model after quantization and conversion')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--live', action='store_true', default=False,
help='predict while drawing')
parser.add_argument('--hardware', action='store_true', default=False,
help='Tests on real hardware')
parser.add_argument('--serial-port', type=str, default="/dev/ttyUSB2",
help='Serial port for connection to FPGA')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
if use_cuda:
device = torch.device("cuda")
else:
device = torch.device("cpu")
if not args.hardware:
model = HONet()
state_dict = torch.load(args.model_path)
model.load_state_dict(state_dict, strict=True)
model = model.to(device)
model.eval()
transform_test=transforms.Compose([
transforms.ToTensor(),
ConvertToBlackWhite(),
transforms.Resize([16, 16], interpolation=transforms.InterpolationMode.NEAREST),
MinMaxScale()
])
def process_image(image):
# cv2.imwrite("my_drawing.png", image)
with torch.no_grad():
transformed_image = transform_test(image)
transformed_image = transformed_image.unsqueeze(0) # We add the "batch" dimension
results_logits = model(transformed_image)
results = model.softmax(results_logits)
results = results[0] # We remove the "batch" dimension
results = results.detach().cpu().numpy()
return results
else:
model = HACCNet(weights_dir=args.model_path, port=args.serial_port)
def process_image(image):
# cv2.imwrite("my_drawing.png", image)
results = model.forward(image)
results = model.softmax(results)
return results
canvas = DrawingCanvas(title='MNIST Draw', save_event=process_image, size=IMAGE_SIZE, pencil_thickness=PENCIL_THICKNESS, display_multiplier=SIZE_MULTIPLIER, save_while_drawing=args.live)
canvas.mainloop()
if __name__ == "__main__":
main()