-
Notifications
You must be signed in to change notification settings - Fork 45
/
rddpg_per.py
539 lines (479 loc) · 22 KB
/
rddpg_per.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
'''
Author: Sunghoon Hong
Title: rddpg_per.py
Description:
Recurrent Deep Deterministic Policy Gradient Agent
with Prioritized Experience Replay
for Airsim
Detail:
- not use join()
- reset for zero-image error
- tensorflow v1 + keras
- soft update for target model
- PER using Sumtree & Proportional priority
'''
import os
import csv
import time
import random
import argparse
from copy import deepcopy
from collections import deque
from datetime import datetime as dt
import numpy as np
import tensorflow as tf
import keras.backend as K
from keras.layers import TimeDistributed, BatchNormalization, Flatten, Add, Lambda, Concatenate
from keras.layers import Conv2D, MaxPooling2D, Dense, GRU, Input, ELU, Activation
from keras.optimizers import Adam
from keras.models import Model
from PIL import Image
import cv2
from airsim_env import Env
from PER import Memory
np.set_printoptions(suppress=True, precision=4)
agent_name = 'rddpg_per'
class RDDPGAgent(object):
def __init__(self, state_size, action_size, actor_lr, critic_lr, tau,
gamma, lambd, batch_size, memory_size,
epsilon, epsilon_end, decay_step, load_model):
self.state_size = state_size
self.vel_size = 3
self.action_size = action_size
self.action_high = 1.5
self.action_low = -self.action_high
self.actor_lr = actor_lr
self.critic_lr = critic_lr
self.tau = tau
self.gamma = gamma
self.lambd = lambd
self.batch_size = batch_size
self.memory_size = memory_size
self.epsilon = epsilon
self.epsilon_end = epsilon_end
self.decay_step = decay_step
self.epsilon_decay = (epsilon - epsilon_end) / decay_step
self.sess = tf.Session()
K.set_session(self.sess)
self.actor, self.critic = self.build_model()
self.target_actor, self.target_critic = self.build_model()
self.actor_update = self.build_actor_optimizer()
self.critic_update = self.build_critic_optimizer()
self.sess.run(tf.global_variables_initializer())
if load_model:
self.load_model('./save_model/'+ agent_name)
self.target_actor.set_weights(self.actor.get_weights())
self.target_critic.set_weights(self.critic.get_weights())
self.memory = Memory(self.memory_size)
def build_model(self):
# shared network
# image process
image = Input(shape=self.state_size)
image_process = BatchNormalization()(image)
image_process = TimeDistributed(
Conv2D(16, (3, 3), activation='elu', padding='same', kernel_initializer='he_normal'))(image_process)
#72 128
image_process = TimeDistributed(Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal'))(
image_process)
#70 126
image_process = TimeDistributed(Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal'))(
image_process)
#68 124
image_process = TimeDistributed(MaxPooling2D((2, 2)))(image_process)
#34 62
image_process = TimeDistributed(Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal'))(
image_process)
#32 60
image_process = TimeDistributed(Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal'))(
image_process)
#30 58
image_process = TimeDistributed(MaxPooling2D((2, 2)))(image_process)
#15 29
image_process = TimeDistributed(Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal'))(
image_process)
#13 27
image_process = TimeDistributed(Conv2D(32, (4, 4), activation='elu', kernel_initializer='he_normal'))(
image_process)
#10 24
image_process = TimeDistributed(MaxPooling2D((2, 2)))(image_process)
#5 12
image_process = TimeDistributed(Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal'))(
image_process)
#3 10
image_process = TimeDistributed(Conv2D(8, (1, 1), activation='elu', kernel_initializer='he_normal'))(
image_process)
image_process = TimeDistributed(Flatten())(image_process)
image_process = GRU(48, kernel_initializer='he_normal', use_bias=False)(image_process)
image_process = BatchNormalization()(image_process)
image_process = Activation('tanh')(image_process)
# vel process
vel = Input(shape=[self.vel_size])
vel_process = Dense(48, kernel_initializer='he_normal', use_bias=False)(vel)
vel_process = BatchNormalization()(vel_process)
vel_process = Activation('tanh')(vel_process)
# state process
state_process = Add()([image_process, vel_process])
# Actor
policy = Dense(32, kernel_initializer='he_normal', use_bias=False)(state_process)
policy = BatchNormalization()(policy)
policy = ELU()(policy)
policy = Dense(32, kernel_initializer='he_normal', use_bias=False)(policy)
policy = BatchNormalization()(policy)
policy = ELU()(policy)
policy = Dense(self.action_size, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))(policy)
policy = Lambda(lambda x: K.clip(x, self.action_low, self.action_high))(policy)
actor = Model(inputs=[image, vel], outputs=policy)
# Critic
action = Input(shape=[self.action_size])
action_process = Dense(48, kernel_initializer='he_normal', use_bias=False)(action)
action_process = BatchNormalization()(action_process)
action_process = Activation('tanh')(action_process)
state_action = Add()([state_process, action_process])
# state_action = Concatenate()([state_process, action_process])
Qvalue = Dense(32, kernel_initializer='he_normal', use_bias=False)(state_action)
Qvalue = BatchNormalization()(Qvalue)
Qvalue = ELU()(Qvalue)
Qvalue = Dense(32, kernel_initializer='he_normal', use_bias=False)(Qvalue)
Qvalue = BatchNormalization()(Qvalue)
Qvalue = ELU()(Qvalue)
Qvalue = Dense(1, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))(Qvalue)
critic = Model(inputs=[image, vel, action], outputs=Qvalue)
actor._make_predict_function()
critic._make_predict_function()
return actor, critic
def build_actor_optimizer(self):
pred_Q = self.critic.output
action_grad = tf.gradients(pred_Q, self.critic.input[2])
target = -action_grad[0] / self.batch_size
params_grad = tf.gradients(
self.actor.output, self.actor.trainable_weights, target)
params_grad, global_norm = tf.clip_by_global_norm(params_grad, 5.0)
grads = zip(params_grad, self.actor.trainable_weights)
optimizer = tf.train.AdamOptimizer(self.actor_lr)
updates = optimizer.apply_gradients(grads)
train = K.function(
[self.actor.input[0], self.actor.input[1], self.critic.input[2]],
[global_norm],
updates=[updates]
)
return train
def build_critic_optimizer(self):
y = K.placeholder(shape=(None, 1), dtype='float32')
pred = self.critic.output
error = K.abs(pred - y)
loss = K.mean(K.square(error))
# Huber Loss
# error = K.abs(y - pred)
# quadratic = K.clip(error, 0.0, 1.0)
# linear = error - quadratic
# loss = K.mean(0.5 * K.square(quadratic) + linear)
optimizer = Adam(lr=self.critic_lr)
updates = optimizer.get_updates(self.critic.trainable_weights, [], loss)
train = K.function(
[self.critic.input[0], self.critic.input[1], self.critic.input[2], y],
[error, loss],
updates=updates
)
return train
def get_action(self, state):
policy = self.actor.predict(state)[0]
noise = np.random.normal(0, self.epsilon, self.action_size)
action = np.clip(policy + noise, self.action_low, self.action_high)
return action, policy
def train_model(self):
batch, idxs, _ = self.memory.sample(self.batch_size)
images = np.zeros([self.batch_size] + self.state_size)
vels = np.zeros([self.batch_size, self.vel_size])
actions = np.zeros((self.batch_size, self.action_size))
rewards = np.zeros((self.batch_size, 1))
next_images = np.zeros([self.batch_size] + self.state_size)
next_vels = np.zeros([self.batch_size, self.vel_size])
dones = np.zeros((self.batch_size, 1))
targets = np.zeros((self.batch_size, 1))
for i, sample in enumerate(batch):
images[i], vels[i] = sample[0]
actions[i] = sample[1]
rewards[i] = sample[2]
next_images[i], next_vels[i] = sample[3]
dones[i] = sample[4]
states = [images, vels]
next_states = [next_images, next_vels]
policy = self.actor.predict(states)
target_actions = self.target_actor.predict(next_states)
target_next_Qs = self.target_critic.predict(next_states + [target_actions])
targets = rewards + self.gamma * (1 - dones) * target_next_Qs
actor_loss = self.actor_update(states + [policy])
tds, critic_loss = self.critic_update(states + [actions, targets])
for i in range(self.batch_size):
idx = idxs[i]
self.memory.update(idx, tds[i])
return actor_loss[0], critic_loss
def append_memory(self, state, action, reward, next_state, done):
Q = self.critic.predict(state + [action.reshape(1, -1)])[0]
target_action = self.target_actor.predict(next_state)[0].reshape(1, -1)
target_Q = self.target_critic.predict(next_state + [target_action])[0]
td = reward + (1 - done) * self.gamma * target_Q - Q
td = abs(td[0])
self.memory.add(td, (state, action, reward, next_state, done))
return float(td)
def load_model(self, name):
if os.path.exists(name + '_actor.h5'):
self.actor.load_weights(name + '_actor.h5')
print('Actor loaded')
if os.path.exists(name + '_critic.h5'):
self.critic.load_weights(name + '_critic.h5')
print('Critic loaded')
def save_model(self, name):
self.actor.save_weights(name + '_actor.h5')
self.critic.save_weights(name + '_critic.h5')
def update_target_model(self):
self.target_actor.set_weights(
self.tau * np.array(self.actor.get_weights()) \
+ (1 - self.tau) * np.array(self.target_actor.get_weights())
)
self.target_critic.set_weights(
self.tau * np.array(self.critic.get_weights()) \
+ (1 - self.tau) * np.array(self.target_critic.get_weights())
)
'''
Environment interaction
'''
def transform_input(responses, img_height, img_width):
img1d = np.array(responses[0].image_data_float, dtype=np.float)
img1d = np.array(np.clip(255 * 3 * img1d, 0, 255), dtype=np.uint8)
img2d = np.reshape(img1d, (responses[0].height, responses[0].width))
image = Image.fromarray(img2d)
image = np.array(image.resize((img_width, img_height)).convert('L'))
cv2.imwrite('view.png', np.uint8(image))
image = np.float32(image.reshape(1, img_height, img_width, 1))
image /= 255.0
return image
def transform_action(action):
real_action = np.array(action)
real_action[1] += 0.5
return real_action
if __name__ == '__main__':
# CUDA config
# tf_config = tf.ConfigProto()
# tf_config.gpu_options.allow_growth = True
# argument parser
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--load_model', action='store_true')
parser.add_argument('--play', action='store_true')
parser.add_argument('--img_height', type=int, default=72)
parser.add_argument('--img_width', type=int, default=128)
parser.add_argument('--actor_lr', type=float, default=1e-4)
parser.add_argument('--critic_lr', type=float, default=5e-4)
parser.add_argument('--tau', type=float, default=5e-3)
parser.add_argument('--gamma', type=float, default=0.99)
parser.add_argument('--lambd', type=float, default=0.90)
parser.add_argument('--seqsize', type=int, default=5)
parser.add_argument('--epoch', type=int, default=1)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--memory_size',type=int, default=50000)
parser.add_argument('--train_start',type=int, default=5000)
parser.add_argument('--train_rate', type=int, default=4)
parser.add_argument('--epsilon', type=float, default=1)
parser.add_argument('--epsilon_end',type=float, default=0.01)
parser.add_argument('--decay_step', type=int, default=20000)
parser.add_argument('--random', type=float, default=0.05)
args = parser.parse_args()
if not os.path.exists('save_graph/'+ agent_name):
os.makedirs('save_graph/'+ agent_name)
if not os.path.exists('save_stat'):
os.makedirs('save_stat')
if not os.path.exists('save_model'):
os.makedirs('save_model')
# Make RL agent
state_size = [args.seqsize, args.img_height, args.img_width, 1]
action_size = 3
agent = RDDPGAgent(
state_size=state_size,
action_size=action_size,
actor_lr=args.actor_lr,
critic_lr=args.critic_lr,
tau=args.tau,
gamma=args.gamma,
lambd=args.lambd,
batch_size=args.batch_size,
memory_size=args.memory_size,
epsilon=args.epsilon,
epsilon_end=args.epsilon_end,
decay_step=args.decay_step,
load_model=args.load_model
)
episode = 0
env = Env()
if args.play:
while True:
try:
done = False
bug = False
# stats
bestY, timestep, score, avgvel, avgQ = 0., 0, 0., 0., 0.
observe = env.reset()
image, vel = observe
try:
image = transform_input(image, args.img_height, args.img_width)
except:
continue
history = np.stack([image] * args.seqsize, axis=1)
vel = vel.reshape(1, -1)
state = [history, vel]
while not done:
timestep += 1
# snapshot = np.zeros([0, args.img_width, 1])
# for snap in state[0][0]:
# snapshot = np.append(snapshot, snap, axis=0)
# snapshot *= 128
# snapshot += 128
# cv2.imshow('%s' % timestep, np.uint8(snapshot))
# cv2.waitKey(0)
action = agent.actor.predict(state)[0]
# noise = np.random.normal(0, args.epsilon, action_size)
# noise = np.array(noise, dtype=np.float32)
# action = np.clip(action + noise, -1, 1)
real_action = transform_action(action)
observe, reward, done, info = env.step(transform_action(real_action))
image, vel = observe
try:
image = transform_input(image, args.img_height, args.img_width)
except:
bug = True
break
history = np.append(history[:, 1:], [image], axis=1)
vel = vel.reshape(1, -1)
next_state = [history, vel]
# stats
avgQ += float(agent.critic.predict([state[0], state[1], action.reshape(1, -1)])[0][0])
avgvel += float(np.linalg.norm(real_action))
score += reward
if info['Y'] > bestY:
bestY = info['Y']
print('%s' % (real_action), end='\r', flush=True)
if args.verbose:
print('Step %d Action %s Reward %.2f Info %s:' % (timestep, real_action, reward, info['status']))
state = next_state
if bug:
continue
avgQ /= timestep
avgvel /= timestep
# done
print('Ep %d: BestY %.3f Step %d Score %.2f AvgQ %.2f AvgVel %.2f'
% (episode, bestY, timestep, score, avgQ, avgvel))
episode += 1
except KeyboardInterrupt:
env.disconnect()
break
else:
# Train
time_limit = 600
highscoreY = 0.
if os.path.exists('save_stat/'+ agent_name + '_stat.csv'):
with open('save_stat/'+ agent_name + '_stat.csv', 'r') as f:
read = csv.reader(f)
episode = int(float(next(reversed(list(read)))[0]))
print('Last episode:', episode)
episode += 1
if os.path.exists('save_stat/'+ agent_name + '_highscore.csv'):
with open('save_stat/'+ agent_name + '_highscore.csv', 'r') as f:
read = csv.reader(f)
highscoreY = float(next(reversed(list(read)))[0])
print('Best Y:', highscoreY)
global_step = 0
while True:
try:
done = False
bug = False
random = (np.random.random() < args.random)
# stats
bestY, timestep, score, avgvel, avgQ, avgAct = 0., 0, 0., 0., 0., 0.
train_num, actor_loss, critic_loss, tds, maxtd = 0, 0., 0., 0., 0.
observe = env.reset()
image, vel = observe
try:
image = transform_input(image, args.img_height, args.img_width)
except:
continue
history = np.stack([image] * args.seqsize, axis=1)
vel = vel.reshape(1, -1)
state = [history, vel]
while not done and timestep < time_limit:
timestep += 1
global_step += 1
if len(agent.memory) >= args.train_start and global_step >= args.train_rate:
for _ in range(args.epoch):
a_loss, c_loss = agent.train_model()
actor_loss += float(a_loss)
critic_loss += float(c_loss)
train_num += 1
agent.update_target_model()
global_step = 0
if random:
action = policy = np.random.uniform(-1, 1, action_size)
else:
action, policy = agent.get_action(state)
real_action, real_policy = transform_action(action), transform_action(policy)
observe, reward, done, info = env.step(real_action)
image, vel = observe
try:
if timestep < 3 and info['status'] == 'landed':
raise Exception
image = transform_input(image, args.img_height, args.img_width)
except:
bug = True
break
history = np.append(history[:, 1:], [image], axis=1)
vel = vel.reshape(1, -1)
next_state = [history, vel]
td = agent.append_memory(state, action, reward, next_state, done)
# stats
tds += td
if maxtd < td:
maxtd = td
avgQ += float(agent.critic.predict(state + [action.reshape(1, -1)])[0][0])
avgvel += float(np.linalg.norm(real_policy))
avgAct += float(np.linalg.norm(real_action))
score += reward
if info['Y'] > bestY:
bestY = info['Y']
print('%s | %s' % (real_action, real_policy), end='\r', flush=True)
if args.verbose:
print('Step %d Action %s Reward %.2f Info %s:' % (timestep, real_action, reward, info['status']))
state = next_state
if agent.epsilon > agent.epsilon_end:
agent.epsilon -= agent.epsilon_decay
if bug:
continue
if train_num:
actor_loss /= train_num
critic_loss /= train_num
avgQ /= timestep
avgvel /= timestep
avgAct /= timestep
tds /= timestep
# done
if args.verbose or episode % 10 == 0:
print('Ep %d: BestY %.3f Step %d Score %.2f Q %.2f Vel %.2f Act %.2f'
% (episode, bestY, timestep, score, avgQ, avgvel, avgAct))
stats = [
episode, timestep, score, bestY, avgvel, \
actor_loss, critic_loss, info['level'], avgQ, avgAct, info['status'],
tds, maxtd
]
# log stats
with open('save_stat/'+ agent_name + '_stat.csv', 'a', encoding='utf-8', newline='') as f:
wr = csv.writer(f)
wr.writerow(['%.4f' % s if type(s) is float else s for s in stats])
if highscoreY < bestY:
highscoreY = bestY
with open('save_stat/'+ agent_name + '_highscore.csv', 'w', encoding='utf-8', newline='') as f:
wr = csv.writer(f)
wr.writerow('%.4f' % s if type(s) is float else s for s in [highscoreY, episode, score, dt.now().strftime('%Y-%m-%d %H:%M:%S')])
agent.save_model('./save_model/'+ agent_name + '_best')
agent.save_model('./save_model/'+ agent_name)
episode += 1
except KeyboardInterrupt:
env.disconnect()
break