Skip to content

Commit 1857f99

Browse files
committed
flappyBird深度学习
1 parent 56ac0e9 commit 1857f99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+648
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Using Deep Q-Network to Learn How To Play Flappy Bird
2+
3+
<img src="./images/flappy_bird_demp.gif" width="250">
4+
5+
7 mins version: [DQN for flappy bird](https://www.youtube.com/watch?v=THhUXIhjkCM)
6+
7+
## Overview
8+
This project follows the description of the Deep Q Learning algorithm described in Playing Atari with Deep Reinforcement Learning [2] and shows that this learning algorithm can be further generalized to the notorious Flappy Bird.
9+
10+
## Installation Dependencies:
11+
* Python 2.7 or 3
12+
* TensorFlow 0.7
13+
* pygame
14+
* OpenCV-Python
15+
16+
## How to Run?
17+
```
18+
git clone https://github.com/yenchenlin1994/DeepLearningFlappyBird.git
19+
cd DeepLearningFlappyBird
20+
python deep_q_network.py
21+
```
22+
23+
## What is Deep Q-Network?
24+
It is a convolutional neural network, trained with a variant of Q-learning, whose input is raw pixels and whose output is a value function estimating future rewards.
25+
26+
For those who are interested in deep reinforcement learning, I highly recommend to read the following post:
27+
28+
[Demystifying Deep Reinforcement Learning](http://www.nervanasys.com/demystifying-deep-reinforcement-learning/)
29+
30+
## Deep Q-Network Algorithm
31+
32+
The pseudo-code for the Deep Q Learning algorithm, as given in [1], can be found below:
33+
34+
```
35+
Initialize replay memory D to size N
36+
Initialize action-value function Q with random weights
37+
for episode = 1, M do
38+
Initialize state s_1
39+
for t = 1, T do
40+
With probability ϵ select random action a_t
41+
otherwise select a_t=max_a Q(s_t,a; θ_i)
42+
Execute action a_t in emulator and observe r_t and s_(t+1)
43+
Store transition (s_t,a_t,r_t,s_(t+1)) in D
44+
Sample a minibatch of transitions (s_j,a_j,r_j,s_(j+1)) from D
45+
Set y_j:=
46+
r_j for terminal s_(j+1)
47+
r_j+γ*max_(a^' ) Q(s_(j+1),a'; θ_i) for non-terminal s_(j+1)
48+
Perform a gradient step on (y_j-Q(s_j,a_j; θ_i))^2 with respect to θ
49+
end for
50+
end for
51+
```
52+
53+
## Experiments
54+
55+
#### Environment
56+
Since deep Q-network is trained on the raw pixel values observed from the game screen at each time step, [3] finds that remove the background appeared in the original game can make it converge faster. This process can be visualized as the following figure:
57+
58+
<img src="./images/preprocess.png" width="450">
59+
60+
#### Network Architecture
61+
According to [1], I first preprocessed the game screens with following steps:
62+
63+
1. Convert image to grayscale
64+
2. Resize image to 80x80
65+
3. Stack last 4 frames to produce an 80x80x4 input array for network
66+
67+
The architecture of the network is shown in the figure below. The first layer convolves the input image with an 8x8x4x32 kernel at a stride size of 4. The output is then put through a 2x2 max pooling layer. The second layer convolves with a 4x4x32x64 kernel at a stride of 2. We then max pool again. The third layer convolves with a 3x3x64x64 kernel at a stride of 1. We then max pool one more time. The last hidden layer consists of 256 fully connected ReLU nodes.
68+
69+
<img src="./images/network.png">
70+
71+
The final output layer has the same dimensionality as the number of valid actions which can be performed in the game, where the 0th index always corresponds to doing nothing. The values at this output layer represent the Q function given the input state for each valid action. At each time step, the network performs whichever action corresponds to the highest Q value using a ϵ greedy policy.
72+
73+
74+
#### Training
75+
At first, I initialize all weight matrices randomly using a normal distribution with a standard deviation of 0.01, then set the replay memory with a max size of 500,00 experiences.
76+
77+
I start training by choosing actions uniformly at random for the first 10,000 time steps, without updating the network weights. This allows the system to populate the replay memory before training begins.
78+
79+
Note that unlike [1], which initialize ϵ = 1, I linearly anneal ϵ from 0.1 to 0.0001 over the course of the next 3000,000 frames. The reason why I set it this way is that agent can choose an action every 0.03s (FPS=30) in our game, high ϵ will make it **flap** too much and thus keeps itself at the top of the game screen and finally bump the pipe in a clumsy way. This condition will make Q function converge relatively slow since it only start to look other conditions when ϵ is low.
80+
However, in other games, initialize ϵ to 1 is more reasonable.
81+
82+
During training time, at each time step, the network samples minibatches of size 32 from the replay memory to train on, and performs a gradient step on the loss function described above using the Adam optimization algorithm with a learning rate of 0.000001. After annealing finishes, the network continues to train indefinitely, with ϵ fixed at 0.001.
83+
84+
## FAQ
85+
86+
#### Checkpoint not found
87+
Change [first line of `saved_networks/checkpoint`](https://github.com/yenchenlin1994/DeepLearningFlappyBird/blob/master/saved_networks/checkpoint#L1) to
88+
89+
`model_checkpoint_path: "saved_networks/bird-dqn-2920000"`
90+
91+
#### How to reproduce?
92+
1. Comment out [these lines](https://github.com/yenchenlin1994/DeepLearningFlappyBird/blob/master/deep_q_network.py#L108-L112)
93+
94+
2. Modify `deep_q_network.py`'s parameter as follow:
95+
```python
96+
OBSERVE = 10000
97+
EXPLORE = 3000000
98+
FINAL_EPSILON = 0.0001
99+
INITIAL_EPSILON = 0.1
100+
```
101+
102+
## References
103+
104+
[1] Mnih Volodymyr, Koray Kavukcuoglu, David Silver, Andrei A. Rusu, Joel Veness, Marc G. Bellemare, Alex Graves, Martin Riedmiller, Andreas K. Fidjeland, Georg Ostrovski, Stig Petersen, Charles Beattie, Amir Sadik, Ioannis Antonoglou, Helen King, Dharshan Kumaran, Daan Wierstra, Shane Legg, and Demis Hassabis. **Human-level Control through Deep Reinforcement Learning**. Nature, 529-33, 2015.
105+
106+
[2] Volodymyr Mnih, Koray Kavukcuoglu, David Silver, Alex Graves, Ioannis Antonoglou, Daan Wierstra, and Martin Riedmiller. **Playing Atari with Deep Reinforcement Learning**. NIPS, Deep Learning workshop
107+
108+
[3] Kevin Chen. **Deep Reinforcement Learning for Flappy Bird** [Report](http://cs229.stanford.edu/proj2015/362_report.pdf) | [Youtube result](https://youtu.be/9WKBzTUsPKc)
109+
110+
## Disclaimer
111+
This work is highly based on the following repos:
112+
113+
1. [sourabhv/FlapPyBird] (https://github.com/sourabhv/FlapPyBird)
114+
2. [asrivat1/DeepLearningVideoGames](https://github.com/asrivat1/DeepLearningVideoGames)
115+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#!/usr/bin/env python
2+
from __future__ import print_function
3+
4+
import tensorflow as tf
5+
import cv2
6+
import sys
7+
sys.path.append("game/")
8+
import wrapped_flappy_bird as game
9+
import random
10+
import numpy as np
11+
from collections import deque
12+
13+
GAME = 'bird' # the name of the game being played for log files
14+
ACTIONS = 2 # number of valid actions
15+
GAMMA = 0.99 # decay rate of past observations
16+
OBSERVE = 100000. # timesteps to observe before training
17+
EXPLORE = 2000000. # frames over which to anneal epsilon
18+
FINAL_EPSILON = 0.0001 # final value of epsilon
19+
INITIAL_EPSILON = 0. # starting value of epsilon
20+
REPLAY_MEMORY = 50000 # number of previous transitions to remember
21+
BATCH = 32 # size of minibatch
22+
FRAME_PER_ACTION = 1
23+
24+
def weight_variable(shape):
25+
initial = tf.truncated_normal(shape, stddev = 0.01)
26+
return tf.Variable(initial)
27+
28+
def bias_variable(shape):
29+
initial = tf.constant(0.01, shape = shape)
30+
return tf.Variable(initial)
31+
32+
def conv2d(x, W, stride):
33+
return tf.nn.conv2d(x, W, strides = [1, stride, stride, 1], padding = "SAME")
34+
35+
def max_pool_2x2(x):
36+
return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")
37+
38+
def createNetwork():
39+
# network weights
40+
W_conv1 = weight_variable([8, 8, 4, 32])
41+
b_conv1 = bias_variable([32])
42+
43+
W_conv2 = weight_variable([4, 4, 32, 64])
44+
b_conv2 = bias_variable([64])
45+
46+
W_conv3 = weight_variable([3, 3, 64, 64])
47+
b_conv3 = bias_variable([64])
48+
49+
W_fc1 = weight_variable([1600, 512])
50+
b_fc1 = bias_variable([512])
51+
52+
W_fc2 = weight_variable([512, ACTIONS])
53+
b_fc2 = bias_variable([ACTIONS])
54+
55+
# input layer
56+
s = tf.placeholder("float", [None, 80, 80, 4])
57+
58+
# hidden layers
59+
h_conv1 = tf.nn.relu(conv2d(s, W_conv1, 4) + b_conv1)
60+
h_pool1 = max_pool_2x2(h_conv1)
61+
62+
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2, 2) + b_conv2)
63+
#h_pool2 = max_pool_2x2(h_conv2)
64+
65+
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3, 1) + b_conv3)
66+
#h_pool3 = max_pool_2x2(h_conv3)
67+
68+
#h_pool3_flat = tf.reshape(h_pool3, [-1, 256])
69+
h_conv3_flat = tf.reshape(h_conv3, [-1, 1600])
70+
71+
h_fc1 = tf.nn.relu(tf.matmul(h_conv3_flat, W_fc1) + b_fc1)
72+
73+
# readout layer
74+
readout = tf.matmul(h_fc1, W_fc2) + b_fc2
75+
76+
return s, readout, h_fc1
77+
78+
def trainNetwork(s, readout, h_fc1, sess):
79+
# define the cost function
80+
a = tf.placeholder("float", [None, ACTIONS])
81+
y = tf.placeholder("float", [None])
82+
readout_action = tf.reduce_sum(tf.multiply(readout, a), reduction_indices=1)
83+
cost = tf.reduce_mean(tf.square(y - readout_action))
84+
train_step = tf.train.AdamOptimizer(1e-6).minimize(cost)
85+
86+
# open up a game state to communicate with emulator
87+
game_state = game.GameState()
88+
89+
# store the previous observations in replay memory
90+
D = deque()
91+
92+
# printing
93+
a_file = open("logs_" + GAME + "/readout.txt", 'w')
94+
h_file = open("logs_" + GAME + "/hidden.txt", 'w')
95+
96+
# get the first state by doing nothing and preprocess the image to 80x80x4
97+
do_nothing = np.zeros(ACTIONS)
98+
do_nothing[0] = 1
99+
x_t, r_0, terminal = game_state.frame_step(do_nothing)
100+
x_t = cv2.cvtColor(cv2.resize(x_t, (80, 80)), cv2.COLOR_BGR2GRAY)
101+
ret, x_t = cv2.threshold(x_t,1,255,cv2.THRESH_BINARY)
102+
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
103+
104+
# saving and loading networks
105+
saver = tf.train.Saver()
106+
sess.run(tf.initialize_all_variables())
107+
checkpoint = tf.train.get_checkpoint_state("saved_networks")
108+
if checkpoint and checkpoint.model_checkpoint_path:
109+
saver.restore(sess, checkpoint.model_checkpoint_path)
110+
print("Successfully loaded:", checkpoint.model_checkpoint_path)
111+
else:
112+
print("Could not find old network weights")
113+
114+
# start training
115+
epsilon = INITIAL_EPSILON
116+
t = 0
117+
while "flappy bird" != "angry bird":
118+
# choose an action epsilon greedily
119+
readout_t = readout.eval(feed_dict={s : [s_t]})[0]
120+
a_t = np.zeros([ACTIONS])
121+
action_index = 0
122+
if t % FRAME_PER_ACTION == 0:
123+
if random.random() <= epsilon:
124+
print("----------Random Action----------")
125+
action_index = random.randrange(ACTIONS)
126+
a_t[random.randrange(ACTIONS)] = 1
127+
else:
128+
action_index = np.argmax(readout_t)
129+
a_t[action_index] = 1
130+
else:
131+
a_t[0] = 1 # do nothing
132+
133+
# scale down epsilon
134+
if epsilon > FINAL_EPSILON and t > OBSERVE:
135+
epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE
136+
137+
# run the selected action and observe next state and reward
138+
x_t1_colored, r_t, terminal = game_state.frame_step(a_t)
139+
x_t1 = cv2.cvtColor(cv2.resize(x_t1_colored, (80, 80)), cv2.COLOR_BGR2GRAY)
140+
ret, x_t1 = cv2.threshold(x_t1, 1, 255, cv2.THRESH_BINARY)
141+
x_t1 = np.reshape(x_t1, (80, 80, 1))
142+
#s_t1 = np.append(x_t1, s_t[:,:,1:], axis = 2)
143+
s_t1 = np.append(x_t1, s_t[:, :, :3], axis=2)
144+
145+
# store the transition in D
146+
D.append((s_t, a_t, r_t, s_t1, terminal))
147+
if len(D) > REPLAY_MEMORY:
148+
D.popleft()
149+
150+
# only train if done observing
151+
if t > OBSERVE:
152+
# sample a minibatch to train on
153+
minibatch = random.sample(D, BATCH)
154+
155+
# get the batch variables
156+
s_j_batch = [d[0] for d in minibatch]
157+
a_batch = [d[1] for d in minibatch]
158+
r_batch = [d[2] for d in minibatch]
159+
s_j1_batch = [d[3] for d in minibatch]
160+
161+
y_batch = []
162+
readout_j1_batch = readout.eval(feed_dict = {s : s_j1_batch})
163+
for i in range(0, len(minibatch)):
164+
terminal = minibatch[i][4]
165+
# if terminal, only equals reward
166+
if terminal:
167+
y_batch.append(r_batch[i])
168+
else:
169+
y_batch.append(r_batch[i] + GAMMA * np.max(readout_j1_batch[i]))
170+
171+
# perform gradient step
172+
train_step.run(feed_dict = {
173+
y : y_batch,
174+
a : a_batch,
175+
s : s_j_batch}
176+
)
177+
178+
# update the old values
179+
s_t = s_t1
180+
t += 1
181+
182+
# save progress every 10000 iterations
183+
if t % 10000 == 0:
184+
saver.save(sess, 'saved_networks/' + GAME + '-dqn', global_step = t)
185+
186+
# print info
187+
state = ""
188+
if t <= OBSERVE:
189+
state = "observe"
190+
elif t > OBSERVE and t <= OBSERVE + EXPLORE:
191+
state = "explore"
192+
else:
193+
state = "train"
194+
195+
print("TIMESTEP", t, "/ STATE", state, \
196+
"/ EPSILON", epsilon, "/ ACTION", action_index, "/ REWARD", r_t, \
197+
"/ Q_MAX %e" % np.max(readout_t))
198+
# write info to files
199+
'''
200+
if t % 10000 <= 100:
201+
a_file.write(",".join([str(x) for x in readout_t]) + '\n')
202+
h_file.write(",".join([str(x) for x in h_fc1.eval(feed_dict={s:[s_t]})[0]]) + '\n')
203+
cv2.imwrite("logs_tetris/frame" + str(t) + ".png", x_t1)
204+
'''
205+
206+
def playGame():
207+
sess = tf.InteractiveSession()
208+
s, readout, h_fc1 = createNetwork()
209+
trainNetwork(s, readout, h_fc1, sess)
210+
211+
def main():
212+
playGame()
213+
214+
if __name__ == "__main__":
215+
main()

0 commit comments

Comments
 (0)