-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
160 lines (122 loc) · 4.51 KB
/
utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import random
import math
import numpy as np
import ipdb
from PIL import ImageGrab
import pytesseract
import scipy
import pickle
pytesseract.pytesseract.tesseract_cmd = r'D:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
def img_arr_capture(run_bbox, step=0, down_sample_rate=1.0, is_img_save=False):
"""
Capture game env (running picture) and convert to np.array
:param run_bbox:
:param step:
:param down_sample_rate:
:param is_img_save:
:return:
"""
# convet to black/white (0, 1)
im = ImageGrab.grab(bbox=run_bbox).convert('1')
# image down sampling
original_size = im.size
downsampled_size = (
math.floor(down_sample_rate * original_size[0]), math.floor(down_sample_rate * original_size[1]))
im = im.resize(downsampled_size)
# get the array
arr = np.array(im).astype(int)
arr_shape = arr.shape
if is_img_save:
im.save('running_screen_shots/test_{}.png'.format(step))
# ipdb.set_trace()
return arr, arr_shape
def score_capture(run_bbox):
"""
Recognize the game score by OCR
"""
im = ImageGrab.grab(bbox=run_bbox)
score = int(pytesseract.image_to_string(im))
return score
def compare_images(img1, img2):
"""
compare the difference between two images
:param img1: np.ndarray
:return: diff
"""
def _to_grayscale(arr):
"If arr is a color image (3D array), convert it to grayscale (2D array)."
if len(arr.shape) == 3:
return scipy.average(arr, -1) # average over the last axis (color channels)
else:
return arr
# def _normalize(arr):
# rng = arr.max() - arr.min()
# amin = arr.min()
# return (arr - amin) * 255 / rng
img1 = _to_grayscale(img1)
img2 = _to_grayscale(img2)
# # normalize to compensate for exposure difference
# img1 = _normalize(img1)
# img2 = _normalize(img2)
# calculate the difference and its norms
diff = img1 - img2 # elementwise for scipy arrays
m_norm = np.sum(abs(diff)) # Manhattan norm
# z_norm = norm(diff.ravel(), 0) # Zero norm
img_size = img1.size
n_m = m_norm / img_size
return n_m
def load_replays(replays_paths,batch_size, game_index_now, pos_sample_factor=1.0, max_N=None,
valid_game_index_range=float('inf'), verbose=True):
"""
Load replays & get shuffled data
:return:
"""
replays = pickle.load(open(replays_paths, 'rb'))
neg_replays = [x for x in replays if x[2] <= 0 and game_index_now - x[-2] <= valid_game_index_range]
pos_replays = [x for x in replays if x[2] > 0 and game_index_now - x[-2] <= valid_game_index_range]
assert len(neg_replays) < len(pos_replays)
neg_N = len(neg_replays)
pos_N = int(len(neg_replays) * pos_sample_factor)
random_samples = random.sample(neg_replays, neg_N) + random.sample(pos_replays, pos_N)
random.shuffle(random_samples)
if max_N:
random_samples = random_samples[:max_N]
step_size = int(math.ceil(len(random_samples) / batch_size))
if verbose:
print("total sample: {}, neg_N: {}, pos_N: {}, step_size: {}"
.format(len(random_samples), neg_N, pos_N, step_size))
return random_samples, step_size
def data_loader(batch_size, random_samples, step_size):
"""
Generator for CNN
:param random_samples: training data for CNN
"""
X = [x[0] for x in random_samples]
Y = [x[1] for x in random_samples]
# print("X: ", len(X))
# print("Y: ", len(Y))
while 1:
for i in range(step_size):
batch_s_i = i * batch_size
if i == step_size - 1:
batch_e_i = len(random_samples)
else:
batch_e_i = batch_s_i + batch_size
# print("batch_s_i: ", batch_s_i)
# print("batch_e_i: ", batch_e_i)
x_batch = X[batch_s_i:batch_e_i]
y_batch = Y[batch_s_i:batch_e_i]
x_batch = np.concatenate(x_batch, axis=0)
y_batch = np.concatenate(y_batch, axis=0)
yield x_batch, y_batch
if __name__ == '__main__':
from models import ConvNet
game_index_now = 10
replays_paths = 'replays'
batch_size = 2
epoch = 20
cnn = ConvNet(num_classes=2, lr=1e-3)
random_samples, step_size = load_replays(game_index_now, pos_sample_factor=1.0,
max_N=None, valid_game_index_range=float('inf'))
cnn_data_loader = data_loader(batch_size, random_samples, step_size)
cnn.train_model(cnn_data_loader, epoch, step_size)