/
KivyUI.py
524 lines (420 loc) · 17.7 KB
/
KivyUI.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
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.uix.slider import Slider
from kivy.core.window import Window
from qlearning import DeepQLearner
from qlearning.visualize import plot_weights
from World import World
from time import sleep
from time import clock
from random import randint
from multiprocessing.managers import BaseManager
from threading import Thread
import cPickle
import theano
import numpy as np
import cv2
import warnings
import multiprocessing
import os
import copy
mp_lock = multiprocessing.Lock()
def show_cv_frame(picture, window_title):
if picture is not None:
cv2.imshow(window_title, picture)
cv2.waitKey(30)
else:
print("Preview image returned empty")
class LastState(object):
def __init__(self):
self.state = None
self.image = None
def get_last_state(self):
return self.state
def get_last_image(self):
return self.image
def set_last_state(self, st):
self.state = st
def set_last_image(self, im):
self.image = im
class StateManager(BaseManager):
pass
StateManager.register('last_state', LastState)
class KivyApp(App):
title = 'RALVINN3.0 Q-Learning Rover'
def build(self):
widget = WorldManip()
return widget
class WorldManip(Widget):
lower_color = ObjectProperty(None)
upper_color = ObjectProperty(None)
btn_qlearning = ObjectProperty(None)
btn_qlearning_load = ObjectProperty(None)
btn_manual = ObjectProperty(None)
btn_debug = ObjectProperty(None)
def __init__(self, **kwargs):
super(WorldManip, self).__init__(**kwargs)
self.kc = None
self.qc = None
self.mode = None
self.quit = False
self.world = None
self.active = False
self.q_process = None
self.state_mgr = StateManager()
self.state_mgr.start()
self.last_state = self.state_mgr.last_state()
self.btn_qlearning.on_press = self.q_callback
self.btn_manual.on_press = self.m_callback
self.btn_debug.on_press = self.d_callback
self.btn_qlearning_load.on_press = self.ql_callback
def q_callback(self):
self.mode = "Q"
Clock.schedule_interval(self.update, 1.0/20.0)
self.world = World("R")
self.kc = KeyboardControl(self.world)
self.disable_buttons()
def ql_callback(self):
self.mode = "L"
Clock.schedule_interval(self.update, 1.0/20.0)
self.world = World("R")
self.kc = KeyboardControl(self.world)
self.disable_buttons()
def m_callback(self):
self.mode = "M"
Clock.schedule_interval(self.update, 1.0/20.0)
self.world = World("R")
self.kc = KeyboardControl(self.world)
self.disable_buttons()
def d_callback(self):
self.mode = "D"
Clock.schedule_interval(self.update, 1.0/20.0)
self.world = World("R")
self.kc = KeyboardControl(self.world)
self.disable_buttons()
self.lower_color.disabled = False
self.upper_color.disabled = False
def disable_buttons(self):
self.btn_qlearning.disabled = True
self.btn_manual.disabled = True
self.btn_debug.disabled = True
def update(self, dt):
if self.world is not None:
if self.mode == "M" or self.mode == "Q" or self.mode == "L":
_temp_state = np.zeros((1, 1, 4, 3), dtype='int32')
mp_lock.acquire()
_temp_state[0][0], _temp_img = self.world.get_current_state(True)
self.last_state.set_last_state(_temp_state)
self.last_state.set_last_image(_temp_img)
mp_lock.release()
if not self.active and self.mode == "Q":
self.active = True
self.q_process = multiprocessing.Process(target=self.run_episodes)
self.q_process.start()
if not self.active and self.mode == "L":
self.active = True
self.q_process = multiprocessing.Process(target=self.run_loaded_agent)
self.q_process.start()
else:
lower_cv_color = self.calculate_debug_colors(self.lower_color)
upper_cv_color = self.calculate_debug_colors(self.upper_color)
print(lower_cv_color, upper_cv_color)
_, _temp_img = self.world.get_current_state_from_color_range(lower_cv_color, upper_cv_color)
show_cv_frame(_temp_img, 'Live Rover Feed')
def run_episodes(self):
#print('module name:', __name__)
#print('process id:', os.getpid())
# universal learning parameters
input_width = 3
input_height = 4
n_actions = 2
discount = 0.9
learn_rate = .005
batch_size = 4
rng = np.random
replay_size = 16
max_iter = 175
epsilon = 0.2
#TODO: Make this settable from GUI
beginning_state = np.array([[[[0, 0, 0], #pink
[0, 0, 0], #orange
[0, 1, 0], #blue
[0, 0, 0]]]]) #green
print('Starting in 5 seconds... prepare rover opposite to pink flag.')
sleep(5)
# initialize replay memory D <s, a, r, s', t> to replay size with random policy
print('Initializing replay memory ... ')
replay_memory = (
np.zeros((replay_size, 1, input_height, input_width), dtype='int32'),
np.zeros((replay_size, 1), dtype='int32'),
np.zeros((replay_size, 1), dtype=theano.config.floatX),
np.zeros((replay_size, 1, input_height, input_width), dtype=theano.config.floatX),
np.zeros((replay_size, 1), dtype='int32')
)
s1_middle_thirds = beginning_state[0][0][[0, 1, 2, 3], [1, 1, 1, 1]]
terminal = 0
#TODO: STEP 1: Fill with random weights
for step in range(replay_size):
print(step)
mp_lock.acquire()
state = self.last_state.get_last_state()
mp_lock.release()
action = np.random.randint(2)
self.world.act(action)
sleep(0.2)
mp_lock.acquire()
state_prime = self.last_state.get_last_state()
show_cv_frame(self.last_state.get_last_image(), "state_prime")
mp_lock.release()
# get the reward and terminal value of new state
reward, terminal = self.calculate_reward_and_terminal(state_prime)
self.print_color_states(state_prime)
print ('Lead to reward of: {}').format(reward)
sequence = [state, action, reward, state_prime, terminal]
for entry in range(len(replay_memory)):
replay_memory[entry][step] = sequence[entry]
if terminal == 1:
print("Terminal reached, reset rover to opposite red flag. Starting again in 5 seconds...")
print("Resetting back to s1:")
self.reset_rover_to_start(s1_middle_thirds)
print('done')
# build the reinforcement-learning agent
print('Building RL agent ... ')
agent = DeepQLearner(input_width, input_height, n_actions, discount, learn_rate, batch_size, rng)
print('Training RL agent ... Reset rover to opposite pink flag.')
self.reset_rover_to_start(s1_middle_thirds)
print('Starting in 5 seconds...')
sleep(5)
running_loss = []
#TODO: STEP 2: Optimize network
for i in range(max_iter):
mp_lock.acquire()
state = self.last_state.get_last_state()
mp_lock.release()
action = agent.choose_action(state, epsilon) # choose an action using epsilon-greedy policy
# get the new state, reward and terminal value from world
self.world.act(action)
sleep(0.2)
mp_lock.acquire()
state_prime = self.last_state.get_last_state()
show_cv_frame(self.last_state.get_last_image(), "state_prime")
mp_lock.release()
self.print_color_states(state_prime)
reward, terminal = self.calculate_reward_and_terminal(state_prime)
sequence = [state, action, reward, state_prime, terminal] # concatenate into a sequence
print "Found state: "
print state_prime
print ('Lead to reward of: {}').format(reward)
for entry in range(len(replay_memory)):
np.delete(replay_memory[entry], 0, 0) # delete the first entry along the first axis
np.append(replay_memory[entry], sequence[entry]) # append the new sequence at the end
batch_index = np.random.permutation(batch_size) # get random mini-batch indices
loss = agent.train(replay_memory[0][batch_index], replay_memory[1][batch_index],
replay_memory[2][batch_index], replay_memory[3][batch_index],
replay_memory[4][batch_index])
running_loss.append(loss)
#if i % 100 == 0:
print("Loss at iter %i: %f" % (i, loss))
state = state_prime
if terminal == 1:
print("Terminal reached, reset rover to opposite red flag. Starting again in 5 seconds...")
print("Resetting back to s1:")
self.reset_rover_to_start(s1_middle_thirds)
print('... done training')
# test to see if it has learned best route
print("Testing whether optimal path is learned ... set rover to start.\n")
self.reset_rover_to_start(s1_middle_thirds)
filename = "agent_max_iter-{}-width-{}-height-{}-discount-{}-lr-{}-batch-{}.npz".format(max_iter,
input_width,
input_height,
discount,
learn_rate,
batch_size)
agent.save(filename)
#TODO: STEP 3: Test
self.test_agent(agent, input_height, input_width)
def run_loaded_agent(self):
input_width = 3
input_height = 4
n_actions = 2
max_iter = 100
discount = 0.9
learn_rate = .005
batch_size = 4
rng = np.random
filename = "agent_max_iter-{}-width-{}-height-{}-discount-{}-lr-{}-batch-{}.npz".format(max_iter,
input_width,
input_height,
discount,
learn_rate,
batch_size)
agent_obj = DeepQLearner(input_width, input_height, n_actions, discount, learn_rate, batch_size, rng)
try:
agent_obj.load(filename)
except:
print "Failed to Load file. Aborting."
return
self.test_agent(agent_obj, input_height, input_width)
def test_agent(self, agent, input_height, input_width):
max_test_iter = 12
shortest_path = 4
j = 0
mp_lock.acquire()
state = self.last_state.get_last_state()
mp_lock.release()
paths = np.zeros((max_test_iter + 1, 1, 1, input_height, input_width), dtype='int32')
paths[j] = state
rewards = []
# Begin test phase
while True:
action = agent.choose_action(state, 0)
self.world.act(action)
sleep(0.2)
mp_lock.acquire()
state_prime = self.last_state.get_last_state()
mp_lock.release()
reward, terminal = self.calculate_reward_and_terminal(state_prime)
state = state_prime
j += 1
paths[j] = state
rewards.append(reward)
if j == max_test_iter and reward < 10:
print('not successful, no reward found after {} moves').format(max_test_iter)
break
elif terminal == 1:
print('path found.')
break
reward_total = 0
for i in range(j + 1):
print paths[i]
for num in rewards:
reward_total += num
print "Total Reward: {}".format(reward_total)
if j <= shortest_path + 1 and reward_total >= 10:
print('success!')
else:
print('fail :(')
# visualize the weights for each of the action nodes
weights = agent.get_weights()
plot_weights(weights)
@staticmethod
def print_color_states(state_prime):
print "Found state: "
print("{} {}").format("Pink:", state_prime[0][0][0])
print("{} {}").format("Orange:", state_prime[0][0][1])
print("{} {}").format("Blue:", state_prime[0][0][2])
print("{} {}").format("Green:", state_prime[0][0][3])
@staticmethod
def calculate_reward_and_terminal(state_prime):
if state_prime[0][0][0][1] == 1:
reward = 10
terminal = 1
elif state_prime[0][0][0][0] == 1 or state_prime[0][0][0][2] == 1:
reward = 2
terminal = 0
elif state_prime[0][0][1][1] == 1:
reward = -10
terminal = 0
elif state_prime[0][0][1][0] == 1 or state_prime[0][0][1][2] == 1:
reward = -2
terminal = 0
else:
reward = 0
terminal = 0
return reward, terminal
def reset_rover_to_start(self, s1_middle_thirds):
print s1_middle_thirds
sleep(1)
# Get middle thirds of each color state
mp_lock.acquire()
sc = self.last_state.get_last_state()[0][0][[0, 1, 2, 3], [1, 1, 1, 1]]
mp_lock.release()
while (not np.array_equal(sc, s1_middle_thirds)):
t = Thread(target=self.world.rover.turn_right, args=(0.1, 0.5))
t.start()
t.join()
sleep(0.1)
mp_lock.acquire()
sc = self.last_state.get_last_state()[0][0][[0, 1, 2, 3], [1, 1, 1, 1]]
print sc
mp_lock.release()
@staticmethod
def calculate_debug_colors(controller):
h = int(controller.hue.value / 1.42)
s = int(controller.sat.value * 2.55)
v = int(controller.value.value * 2.55)
hsv = np.array([h, s, v])
return hsv
class ColorController(Widget):
hue = ObjectProperty(None)
sat = ObjectProperty(None)
value = ObjectProperty(None)
class KeyboardControl(Widget):
def __init__(self, world, **kwargs):
self.world = world
super(KeyboardControl, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
def _keyboard_closed(self):
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard.unbind(on_key_up=self._on_keyboard_up)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'w':
print("Forward")
self.world.rover.set_wheel_treads(.5, .5)
elif keycode[1] == 's':
print("Backwards")
self.world.rover.set_wheel_treads(-.5, -.5)
elif keycode[1] == 'a':
print("Left")
self.world.rover.set_wheel_treads(-.5, .5)
elif keycode[1] == 'd':
print("Right")
self.world.rover.set_wheel_treads(.5, -.5)
elif keycode[1] == 'q':
print("Left")
self.world.rover.set_wheel_treads(.1, 1)
elif keycode[1] == 'e':
print("Right")
self.world.rover.set_wheel_treads(1, .1)
elif keycode[1] == 'z':
print("Reverse Left")
self.world.rover.set_wheel_treads(-.1, -1)
elif keycode[1] == 'c':
print("Reverse Right")
self.world.rover.set_wheel_treads(-1, -.1)
elif keycode[1] == 'j':
print("Camera Up")
self.world.rover.move_camera_in_vertical_direction(1)
elif keycode[1] == 'k':
print("Camera Down")
self.world.rover.move_camera_in_vertical_direction(-1)
elif keycode[1] == 'u':
print("Lights On")
self.world.rover.turn_the_lights_on()
elif keycode[1] == 'i':
print("Lights Off")
self.world.rover.turn_the_lights_off()
elif keycode[1] == 'g':
print("Stelth On")
self.world.rover.turn_stealth_on()
elif keycode[1] == 'h':
print("Stealth Off")
self.world.rover.turn_stealth_off()
elif keycode[1] == 'escape':
keyboard.release()
self.world.rover.close()
Window.close()
return True
def _on_keyboard_up(self, *args):
self.world.rover.move_camera_in_vertical_direction(0)
self.world.rover.set_wheel_treads(0, 0)
return True
if __name__ == '__main__':
kv = KivyApp()
kv.run()