Skip to content

Commit

Permalink
Minor code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
0xc0392b committed Jun 1, 2017
1 parent b3a5996 commit 1f728dc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 180 deletions.
83 changes: 29 additions & 54 deletions Versions/v0.03/1. collect_data.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,66 @@
import os, time, cv2
import numpy as np
from grabscreen import grab_screen
import cv2
import time
from getkeys import key_check
import os

w = [1,0,0,0,0,0,0,0,0]
s = [0,1,0,0,0,0,0,0,0]
a = [0,0,1,0,0,0,0,0,0]
d = [0,0,0,1,0,0,0,0,0]
wa = [0,0,0,0,1,0,0,0,0]
wd = [0,0,0,0,0,1,0,0,0]
sa = [0,0,0,0,0,0,1,0,0]
sd = [0,0,0,0,0,0,0,1,0]
nk = [0,0,0,0,0,0,0,0,1]

key_map = {
'W': [1, 0, 0, 0, 0, 0, 0, 0, 0],
'S': [0, 1, 0, 0, 0, 0, 0, 0, 0],
'A': [0, 0, 1, 0, 0, 0, 0, 0, 0],
'D': [0, 0, 0, 1, 0, 0, 0, 0, 0],
'WS': [0, 0, 0, 0, 1, 0, 0, 0, 0],
'WD': [0, 0, 0, 0, 0, 1, 0, 0, 0],
'SA': [0, 0, 0, 0, 0, 0, 1, 0, 0],
'SD': [0, 0, 0, 0, 0, 0, 0, 1, 0],
'NK': [0, 0, 0, 0, 0, 0, 0, 0, 1],
'default': [0, 0, 0, 0, 0, 0, 0, 0, 0],
}

starting_value = 1058

while True:
file_name = 'training_data-{}.npy'.format(starting_value)

file_name = 'training_data-{0}.npy'.format(starting_value)
if os.path.isfile(file_name):
print('File exists, moving along',starting_value)
print('File exists, moving along', starting_value)
starting_value += 1
else:
print('File does not exist, starting fresh!',starting_value)

print('File does not exist, starting fresh!', starting_value)
break


def keys_to_output(keys):
'''
Convert keys to a ...multi-hot... array
0 1 2 3 4 5 6 7 8
[W, S, A, D, WA, WD, SA, SD, NOKEY] boolean values.
'''
output = [0,0,0,0,0,0,0,0,0]

if 'W' in keys and 'A' in keys:
output = wa
elif 'W' in keys and 'D' in keys:
output = wd
elif 'S' in keys and 'A' in keys:
output = sa
elif 'S' in keys and 'D' in keys:
output = sd
elif 'W' in keys:
output = w
elif 'S' in keys:
output = s
elif 'A' in keys:
output = a
elif 'D' in keys:
output = d
else:
output = nk
return output
if ''.join(keys) in key_map:
return key_map[''.join(keys)]
return key_map['default']


def main(file_name, starting_value):
file_name = file_name
starting_value = starting_value
training_data = []
for i in list(range(4))[::-1]:
print(i+1)
print(i + 1)
time.sleep(1)

last_time = time.time()
paused = False
print('STARTING!!!')
while(True):

if not paused:
screen = grab_screen(region=(0,40,1920,1120))
screen = grab_screen(region = (0, 40, 1920, 1120))
last_time = time.time()
# resize to something a bit more acceptable for a CNN
screen = cv2.resize(screen, (480,270))
screen = cv2.resize(screen, (480, 270))
# run a color convert:
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

keys = key_check()
output = keys_to_output(keys)
training_data.append([screen,output])

#print('loop took {} seconds'.format(time.time()-last_time))
## print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
## cv2.imshow('window',cv2.resize(screen,(640,360)))
## if cv2.waitKey(25) & 0xFF == ord('q'):
Expand All @@ -92,25 +69,23 @@ def main(file_name, starting_value):

if len(training_data) % 100 == 0:
print(len(training_data))

if len(training_data) == 500:
np.save(file_name,training_data)
np.save(file_name, training_data)
print('SAVED')
training_data = []
starting_value += 1
file_name = 'X:/pygta5/phase7-larger-color/training_data-{}.npy'.format(starting_value)
file_name = 'X:/pygta5/phase7-larger-color/training_data-{0}.npy'.format(starting_value)


keys = key_check()
if 'T' in keys:
if paused:
paused = False
print('unpaused!')
print('Unpaused!')
time.sleep(1)
else:
print('Pausing!')
paused = True
time.sleep(1)


main(file_name, starting_value)
if __name__ == "__main__":
main(file_name, starting_value)
78 changes: 18 additions & 60 deletions Versions/v0.03/2. train_model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os, cv2
import pandas as pd
import numpy as np
from grabscreen import grab_screen
import cv2
import time
import os
import pandas as pd
from tqdm import tqdm
from collections import deque
from models import inception_v3 as googlenet
Expand All @@ -19,56 +17,30 @@

MODEL_NAME = ''
PREV_MODEL = ''

LOAD_MODEL = True

wl = 0
sl = 0
al = 0
dl = 0

wal = 0
wdl = 0
sal = 0
sdl = 0
nkl = 0

w = [1,0,0,0,0,0,0,0,0]
s = [0,1,0,0,0,0,0,0,0]
a = [0,0,1,0,0,0,0,0,0]
d = [0,0,0,1,0,0,0,0,0]
wa = [0,0,0,0,1,0,0,0,0]
wd = [0,0,0,0,0,1,0,0,0]
sa = [0,0,0,0,0,0,1,0,0]
sd = [0,0,0,0,0,0,0,1,0]
nk = [0,0,0,0,0,0,0,0,1]

model = googlenet(WIDTH, HEIGHT, 3, LR, output=9, model_name=MODEL_NAME)

if LOAD_MODEL:
model.load(PREV_MODEL)
print('We have loaded a previous model!!!!')


# iterates through the training files


for e in range(EPOCHS):
#data_order = [i for i in range(1,FILE_I_END+1)]
data_order = [i for i in range(1,FILE_I_END+1)]
## data_order = [i for i in range(1,FILE_I_END+1)]
data_order = [i for i in range(1, FILE_I_END + 1)]
shuffle(data_order)
for count,i in enumerate(data_order):

for count, i in enumerate(data_order):
try:
file_name = 'J:/phase10-random-padded/training_data-{}.npy'.format(i)
file_name = 'J:/phase10-random-padded/training_data-{0}.npy'.format(i)
# full file info
train_data = np.load(file_name)
print('training_data-{}.npy'.format(i),len(train_data))
print('training_data-{0}.npy'.format(i), len(train_data))

## # [ [ [FRAMES], CHOICE ] ]
## # [ [ [FRAMES], CHOICE ] ]
## train_data = []
## current_frames = deque(maxlen=HM_FRAMES)
##
##
## for ds in data:
## screen, choice = ds
## gray_screen = cv2.cvtColor(screen, cv2.COLOR_RGB2GRAY)
Expand All @@ -78,40 +50,26 @@
## if len(current_frames) == HM_FRAMES:
## train_data.append([list(current_frames),choice])


# #
# always validating unique data:
#shuffle(train_data)
# always validating unique data:
## shuffle(train_data)
train = train_data[:-50]
test = train_data[-50:]

X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,3)
X = np.array([i[0] for i in train]).reshape(-1, WIDTH, HEIGHT, 3)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,3)
test_x = np.array([i[0] for i in test]).reshape(-1, WIDTH, HEIGHT, 3)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=1, validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=2500, show_metric=True, run_id=MODEL_NAME)

model.fit({'input': X}, {'targets': Y}, n_epoch = 1, validation_set = ({'input': test_x}, {'targets': test_y}),
snapshot_step = 2500, show_metric = True, run_id = MODEL_NAME)

if count%10 == 0:
if count % 10 == 0:
print('SAVING MODEL!')
model.save(MODEL_NAME)

except Exception as e:
print(str(e))









except Exception as e:
print(e)

#

#tensorboard --logdir=foo:J:/phase10-code/log

Loading

0 comments on commit 1f728dc

Please sign in to comment.