-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
162 lines (127 loc) · 4.2 KB
/
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
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
161
162
import numpy as np
import sys
sys.path.append("game/")
import pygame
# import wrapped_flappy_bird_to_watch as game
# import wrapped_flappy_bird_slow as game
import wrapped_flappy_bird as game
import skimage
from skimage import transform, color, exposure
import keras
from keras.models import Sequential, Model
from keras.layers.core import Dense, Flatten, Activation
from keras.layers.convolutional import Convolution2D
from keras.optimizers import RMSprop
import keras.backend as K
from keras.backend import pool2d
import pydot
from keras.utils import plot_model
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.1
# config.gpu_options.per_process_gpu_memory_fraction = 0.7
set_session(tf.Session(config=config))
graph = tf.get_default_graph()
game_state = game.GameState()
frame,__,__ = game_state.frame_step( [1,0] )
frame = frame[ 58: -70 ]
x,y,_ = frame.shape
RES_HORIZONTAL = x / 4 / 1 # pipes move 4 pixels per frame
RES_VERTICAL = y / 2 / 1 # -> 200 # the bird moves vertically, 10 when jumping
# Seems like if it doesn't just stall,
# minimum movement is 2 and (at least nearly)
# with any movement %2 == 0
# Also, everything is painted in pixel-art style
STATE_CHANNELS = 2
frame_mean = 0.571706774615 # values that my net was trained with based on its first data file
frame_st_deviation = 1.0674802648 # values that my net was trained with based on its first data file
def load_Model( path ):
model = keras.models.load_model(path)
plot_model (
model
,
show_shapes = True
,
to_file = (
'tested-model.png'
)
)
print 'Saved model visualization in tested-model.png'
return model
def preprocess( frame ):
global RES_HORIZONTAL
global RES_VERTICAL
global adjust_mean_and_variation
frame = frame[ 58: -70 ] # some parts of our view are irrelevant
frame = skimage.transform.resize(
frame
, (RES_HORIZONTAL , RES_VERTICAL)
, mode='constant'
)
frame = np.delete(frame , [1,2] , axis=2) # we don't need blue
frame *= 4 # scale the values after we made them smaller through subtraction.
# also, variance should get up, closer to 1, which might be good.
# Best practice known to me from supervised learning on images:
# variance -> 1, mean value -> 0.
# We're getting closer to this, I think.
# But I can't say for sure if it does make a difference for better in here.
frame = frame.reshape( 1 , RES_HORIZONTAL , RES_VERTICAL , STATE_CHANNELS / 2 )
frame -= frame_mean
frame *= 1 / frame_st_deviation
return np.float16( frame )
model = load_Model (
# "saved_models/"
# +
'immortal-model'
)
decision_only_model = Model (
inputs = model.input
, outputs = model.get_layer('models_decision').output
)
terminal = True
current_move = 0
try:
while True:
if terminal:
f1,__,__ = game_state.frame_step( [1,0] ) # do nothing
f2,__,__ = game_state.frame_step( [1,0] )
state = np.concatenate (
(
preprocess (
f2
)
, preprocess (
f1
)
)
, axis=3
)
terminal = False
with graph.as_default():
model_output = decision_only_model.predict(state)
# print model_output
action = model_output[0]
jumping = True if action > 0.5 else False # top "deterministic" choice
next_frame, __, terminal = game_state.frame_step (
[0,1] if jumping else [1,0]
)
# if not terminal:
# next_frame, __, terminal = game_state.frame_step( [1,0] )
state = np.append (
preprocess (
next_frame
)
,
state[:, :, :, : STATE_CHANNELS /2 ]
,
axis=3
)
current_move += 1
if current_move % 50000 == 0:
print 'Move' , move
if terminal:
print 'Died =( Move' , current_move
current_move = 0
except KeyboardInterrupt:
print('Bye!')