-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_test.py
126 lines (100 loc) · 2.94 KB
/
model_test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
from grabscreen import grab_screen
import cv2
import time
from directkeys import W, A, S, D, PressKey, ReleaseKey
from getkeys import key_check
import os
import torch
import random
from inception_v3 import InceptionV3
# device - gpu
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# hyper parameters
LR = 0.001
WIDTH = 160
HEIGHT = 120
n_classes = 5
t_time = 0.05
# time threshold
time_t = 0.09
def straight():
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)
ReleaseKey(S)
def fleft():
PressKey(W)
PressKey(A)
ReleaseKey(S)
ReleaseKey(D)
time.sleep(t_time)
ReleaseKey(A)
def fright():
PressKey(W)
PressKey(D)
ReleaseKey(S)
ReleaseKey(A)
time.sleep(t_time)
ReleaseKey(D)
def reverse():
PressKey(S)
ReleaseKey(A)
ReleaseKey(D)
ReleaseKey(W)
# loading our model weights
model = InceptionV3(n_classes)
model = model.to(device)
model.load_state_dict(torch.load('./ModelSaves/inceptv3_model.pth'))
def main():
# countdown timer
for i in list(range(4))[::-1]:
print(i+1)
time.sleep(1)
while True:
paused = False
#last_time = time.time()
while(True):
if not paused:
# 800x600 windowed mode
screen = grab_screen(region=(0,40,800,640))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen, (160,120))
# print('Frame took {} seconds'.format(time.time()-last_time))
# last_time = time.time()
screen = screen.astype(float)
image = torch.tensor(screen).unsqueeze(0)
image = image.to(device)
pred = model(image)
pred = pred.argmax().item()
#torch.nn.functional.softmax(pred, dim=1)
if pred == 0:
straight()
#print("Forward")
elif pred == 1:
fleft()
#print("Left")
elif pred == 2:
fright()
#print("Right")
elif pred == 3:
reverse()
# barely used as of now
elif pred == 4:
# do nothing
time.sleep(1)
keys = key_check()
if 'T' in keys:
if paused:
paused = False
print('Unpaused model testing. Press T again to pause.')
time.sleep(1)
else:
print('Pausing model testing. Press T to start again.')
paused = True
ReleaseKey(A)
ReleaseKey(W)
ReleaseKey(D)
ReleaseKey(S)
time.sleep(1)
main()