-
Notifications
You must be signed in to change notification settings - Fork 0
/
altered_keras_play.py
157 lines (120 loc) · 4.07 KB
/
altered_keras_play.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
#! /usr/bin/env python
import random
import json
from collections import deque
import numpy as np
from keras.models import Sequential
from keras import layers
from keras.optimizers import Adam
from skimage.transform import resize
from skimage.color import rgb2gray
from skimage.filters import sobel
# from skimage import io
import retro
# NUM_ACTIONS = 12 # ['B', 'A', 'MODE', 'START', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'C', 'Y', 'X', 'Z']
# ACTION_LIST = ['B', 'A', 'MODE', 'START', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'C']
# NUM_ACTIONS = 9
ACTION_LIST = ['B', 'A', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'C']
NUM_ACTIONS = 7
MAX_MEMORY = 50000
EPOCHS = 1000
STACK_SIZE = 4
IMG_ROWS = 80
IMG_COLS = 160
OBSERVATIONS_BEFORE_TRAINING = 1000
BATCH_SIZE = 32
GAMMA = 0.99 # decay rate of past observations
INITIAL_EPSILON = 0.1
FINAL_EPSILON = 0.0001
EXPLORE = 3000 # frames over which to anneal epsilon TODO
def get_action_str(action):
actions = []
for idx, val in enumerate(action):
if val:
actions.append(ACTION_LIST[idx])
return actions
def get_preprocessed_frame(observation):
"""
"""
observation = resize(observation, (122, 160))
grey = rgb2gray(observation)
grey = sobel(grey)
return grey[20:160 - 60, :]
def fix_actions(action):
out = action[:2]
out = np.append(out, [0, 0])
out = np.append(out, action[-5:])
return out
def build_model():
print()
print("Building the model ")
model = Sequential()
model.add(layers.Convolution2D(
32, 8, 8, subsample=(4, 4), border_mode='same',
input_shape=(IMG_ROWS, IMG_COLS, STACK_SIZE)))
model.add(layers.Activation('relu'))
model.add(layers.Convolution2D(
64, 4, 4, subsample=(2, 2), border_mode='same'))
model.add(layers.Activation('relu'))
model.add(layers.Convolution2D(
64, 3, 3, subsample=(1, 1), border_mode='same'))
model.add(layers.Activation('relu'))
model.add(layers.Flatten())
model.add(layers.Dense(512))
model.add(layers.Activation('relu'))
model.add(layers.Dense(NUM_ACTIONS))
adam = Adam(lr=1e-6)
model.compile(
loss='mse',
optimizer=adam)
print("Finished building the model")
print(model.summary())
return model
def get_empty_action_space(num_actions):
return [0] * num_actions # len(env.BUTTONS)
if __name__ == "__main__":
state = 'run'
model = build_model()
model.load_weights("models/model.h5")
print ("Weight load successfully")
# Define environment/game
env = retro.make(game='AlteredBeast-Genesis')
env.reset()
game_over = False
# get initial input
obs_t, reward, game_over, info = env.step(
get_empty_action_space(NUM_ACTIONS))
obs_t = get_preprocessed_frame(obs_t)
state_stack = np.stack((obs_t,) * STACK_SIZE, axis=2)
state_stack = state_stack.reshape(
1, state_stack.shape[0], state_stack.shape[1], state_stack.shape[2])
tick = 0
epsilon = 0
health = 12
tot_reward = 0
while not game_over:
tick += 1
action = get_empty_action_space(NUM_ACTIONS)
q = model.predict(state_stack)
max_Q = np.argmax(q)
action_index = max_Q
action[max_Q] = 1
# apply action, get rewards and new state
action_fix = fix_actions(action)
obs_t, reward, game_over, info = env.step(action_fix)
if info['health'] < health:
reward -= 50
health = info['health']
# store the transition in the replay memory
obs_t = get_preprocessed_frame(obs_t)
obs_t = obs_t.reshape(
1, obs_t.shape[0], obs_t.shape[1], 1)
state_stack_t1 = np.append(
obs_t, state_stack[:, :, :, :STACK_SIZE-1], axis=3)
env.render()
action_string = get_action_str(action)
print('T: {} | State: {} | E: {:.8f} | Action: {} | Reward: {} | TotReward: {} | Q_Max: {:.8f} '.format(
tick, state, epsilon, action_string, reward, tot_reward, np.max(max_Q)))
state_stack = state_stack_t1
tot_reward += reward
print("Total reward: ", tot_reward)