-
Notifications
You must be signed in to change notification settings - Fork 20
/
dagger.py
127 lines (103 loc) · 3.51 KB
/
dagger.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
from gym_torcs import TorcsEnv
import numpy as np
img_dim = [64,64,3]
action_dim = 1
steps = 1000
batch_size = 32
nb_epoch = 100
def get_teacher_action(ob):
steer = ob.angle*10/np.pi
steer -= ob.trackPos*0.10
return np.array([steer])
def img_reshape(input_img):
_img = np.transpose(input_img, (1, 2, 0))
_img = np.flipud(_img)
_img = np.reshape(_img, (1, img_dim[0], img_dim[1], img_dim[2]))
return _img
images_all = np.zeros((0, img_dim[0], img_dim[1], img_dim[2]))
actions_all = np.zeros((0,action_dim))
rewards_all = np.zeros((0,))
img_list = []
action_list = []
reward_list = []
env = TorcsEnv(vision=True, throttle=False)
ob = env.reset(relaunch=True)
print('Collecting data...')
for i in range(steps):
if i == 0:
act = np.array([0.0])
else:
act = get_teacher_action(ob)
if i%100 == 0:
print(i)
ob, reward, done, _ = env.step(act)
img_list.append(ob.img)
action_list.append(act)
reward_list.append(np.array([reward]))
env.end()
print('Packing data into arrays...')
for img, act, rew in zip(img_list, action_list, reward_list):
images_all = np.concatenate([images_all, img_reshape(img)], axis=0)
actions_all = np.concatenate([actions_all, np.reshape(act, [1,action_dim])], axis=0)
rewards_all = np.concatenate([rewards_all, rew], axis=0)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import Adam
#model from https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='same',
input_shape=img_dim))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(action_dim))
model.add(Activation('tanh'))
model.compile(loss='mean_squared_error',
optimizer=Adam(lr=1e-4),
metrics=['mean_squared_error'])
model.fit(images_all, actions_all,
batch_size=batch_size,
nb_epoch=nb_epoch,
shuffle=True)
output_file = open('results.txt', 'w')
#aggregate and retrain
dagger_itr = 5
for itr in range(dagger_itr):
ob_list = []
env = TorcsEnv(vision=True, throttle=False)
ob = env.reset(relaunch=True)
reward_sum = 0.0
for i in range(steps):
act = model.predict(img_reshape(ob.img))
ob, reward, done, _ = env.step(act)
if done is True:
break
else:
ob_list.append(ob)
reward_sum += reward
print(i, reward, reward_sum, done, str(act[0]))
print('Episode done ', itr, i, reward_sum)
output_file.write('Number of Steps: %02d\t Reward: %0.04f\n'%(i, reward_sum))
env.end()
if i==(steps-1):
break
for ob in ob_list:
images_all = np.concatenate([images_all, img_reshape(ob.img)], axis=0)
actions_all = np.concatenate([actions_all, np.reshape(get_teacher_action(ob), [1,action_dim])], axis=0)
model.fit(images_all, actions_all,
batch_size=batch_size,
nb_epoch=nb_epoch,
shuffle=True)