-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
223 lines (143 loc) · 5.65 KB
/
training.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import tensorflow as tf
import numpy as np
import sys
sys.path.append('game/')
import flappy as game
import cv2 as cv
import random
from collections import deque
ACTIONS = 2 #Up and down
FRAMESPERACTION=1
FINAL_EPSILON = 0.001
OBSERVE = 100000
EXPLORE = 2000000
REPLAY_MEM = 50000
GAMMA = 0.99
GAME='bird'
def weight_variable(shape):
w = tf.truncated_normal(shape=shape , stddev=0.01)
return tf.Variable(w)
def bias_variable(shape):
initial = tf.constant(0.01, shape = shape)
return tf.Variable(initial)
def createNetwork():
inp = tf.placeholder("float", [None, 80, 80, 4])
W1= weight_variable([8,8,4,32])
b1 = bias_variable([32])
W2 = weight_variable([4, 4, 32, 64])
b2 = bias_variable([64])
W3 = weight_variable([3, 3, 64, 64])
b3 = bias_variable([64])
W4 = weight_variable([1600, 512])
b4 = bias_variable([512])
W5 = weight_variable([512, ACTIONS])
b5 = bias_variable([ACTIONS])
conv1 = tf.nn.relu(tf.nn.conv2d(inp, W1, strides = [1, 4, 4, 1], padding = "SAME") + b1)
conv1 = tf.nn.max_pool(conv1, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")
conv2 = tf.nn.relu(tf.nn.conv2d(conv1, W2, strides = [1, 2, 2, 1], padding = "SAME") + b2)
conv3 = tf.nn.relu(tf.nn.conv2d(conv2, W3, strides = [1, 1, 1, 1], padding = "SAME") + b3)
conv3_flat = tf.reshape(conv3, [-1, 1600])
fc4 = tf.nn.relu(tf.matmul(conv3_flat, W4) + b4)
fc5 = tf.matmul(fc4, W5) + b5
return inp, fc5 , fc4
def trainNetwork(inp, out, sess):
argmax = tf.placeholder("float", [None, ACTIONS])
y = tf.placeholder("float", [None]) #True value
action = tf.reduce_sum(tf.multiply(out, argmax), reduction_indices = 1)
loss = tf.reduce_mean(tf.square(action - y)) #Squared error loss
train_step = tf.train.AdamOptimizer(1e-6).minimize(loss)
state = game.GameState()
prevObs = deque()
no_action = np.zeros(ACTIONS)
no_action[0]=1
img , reward , terminal = state.frame_step(no_action)
img = cv.cvtColor(cv.resize(img, (80, 80)), cv.COLOR_BGR2GRAY) #Processing image using OpenCV
_ , img = cv.threshold(img , 1 , 255 , cv.THRESH_BINARY)
imgStack = np.stack((img , img , img , img,) , axis=2)
print(imgStack.shape)
saver = tf.train.Saver()
init = tf.global_variables_initializer()
sess.run(init)
checkpoint = tf.train.get_checkpoint_state("saved_networks")
if checkpoint and checkpoint.model_checkpoint_path:
saver.restore(sess, checkpoint.model_checkpoint_path)
print("Loaded :", checkpoint.model_checkpoint_path)
else:
print("Unable to find network weights")
epsilon = 0.001
time = 0
while True:
#print(imgStack)
out_t = out.eval(feed_dict = {inp : [imgStack]})[0]
# print(out_t)
action_t = np.zeros([ACTIONS])
action_index = 0
if time % FRAMESPERACTION == 0:
prob = random.random()
if prob <=epsilon:
print("Taking a random action")
action_index = random.randrange(ACTIONS)
action_t[random.randrange(ACTIONS)]=1
else:
action_index = np.argmax(out_t)
action_t[action_index]=1
else:
action_t[0]=1
if epsilon > FINAL_EPSILON and time > OBSERVE:
epsilon = epsilon - (0.001 - FINAL_EPSILON) / EXPLORE
nextImg , nextReward , terminal = state.frame_step(action_t)
nextImg = cv.cvtColor(cv.resize(nextImg, (80, 80)), cv.COLOR_BGR2GRAY) #Processing image using OpenCV
_ , nextImg = cv.threshold(nextImg , 1 , 255 , cv.THRESH_BINARY)
nextImg = np.reshape(nextImg , (80 , 80 , 1))
nextImgStack = np.append(nextImg , imgStack[:,:,:3] , axis=2)
prevObs.append((imgStack , action_t , reward , nextImgStack , terminal))
if len(prevObs)> REPLAY_MEM:
prevObs.popleft()
if time>OBSERVE:
minibatch = random.sample(prevObs , 32)
curobs = np.zeros((32 , imgStack.shape[0] , imgStack.shape[1] , imgStack.shape[2]))
curaction=[]
curreward=[]
nextobs = np.zeros((32 , imgStack.shape[0] , imgStack.shape[1] , imgStack.shape[2]))
ybatch =[]
i = 0
for obs in minibatch:
curobs[i , : , : , :] = obs[0]
curaction.append(obs[1])
curreward.append(obs[2])
nextobs[i , : , : , :] = obs[3]
i = i+1
outbatch = out.eval(feed_dict = {inp : nextobs})
for i in range(len(minibatch)):
terminal = minibatch[i][4]
if terminal:
ybatch.append(curreward[i])
else:
ybatch.append(curreward[i] + GAMMA * np.max(outbatch[i]))
#print(curaction)
train_step.run(feed_dict = {
y : ybatch,
argmax: curaction,
inp : curobs})
imgStack = nextImgStack
time+=1
if time % 10000 == 0:
saver.save(sess, 'saved_networks/' + GAME + '-dqn', global_step = time)
s = ""
if time <= OBSERVE:
s = "observe"
elif time > OBSERVE and time <= OBSERVE + EXPLORE:
s = "explore"
else:
s = "train"
print("TIMESTEP", time, "/ STATE", s, \
"/ EPSILON", epsilon, "/ ACTION", action_index, "/ REWARD", reward, \
"/ Q_MAX %e" % np.max(out_t))
def playGame():
sess = tf.InteractiveSession()
s, readout, h_fc1 = createNetwork()
trainNetwork(s, readout, sess)
def main():
playGame()
if __name__ == "__main__":
main()