-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsolution.py
75 lines (56 loc) · 2.45 KB
/
solution.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
#!/usr/bin/env python3
from dataclasses import dataclass
from typing import Tuple
import numpy as np
from aido_schemas import EpisodeStart, protocol_agent_duckiebot1, PWMCommands, Duckiebot1Commands, LEDSCommands, RGB, \
wrap_direct, Context, Duckiebot1Observations, JPGImage
from model import DDPG
from wrappers import DTPytorchWrapper
class PytorchRLTemplateAgent:
def __init__(self, load_model=False, model_path=None):
self.preprocessor = DTPytorchWrapper()
self.model = DDPG(state_dim=self.preprocessor.shape, action_dim=2, max_action=1, net_type="cnn")
self.current_image = np.zeros((640, 480, 3))
if load_model:
fp = model_path if model_path else "model"
self.model.load(fp, "models", for_inference=True)
def init(self, context: Context):
context.info('init()')
def on_received_seed(self, data: int):
np.random.seed(data)
def on_received_episode_start(self, context: Context, data: EpisodeStart):
context.info(f'Starting episode "{data.episode_name}".')
def on_received_observations(self, data: Duckiebot1Observations):
camera: JPGImage = data.camera
obs = jpg2rgb(camera.jpg_data)
self.current_image = self.preprocessor.preprocess(obs)
def compute_action(self, observation):
if observation.shape != self.preprocessor.transposed_shape:
observation = self.preprocessor.preprocessor(observation)
action = self.model.predict(observation)
return action.astype(float)
def on_received_get_commands(self, context: Context):
pwm_left, pwm_right = self.compute_action(self.current_image)
grey = RGB(0.0, 0.0, 0.0)
led_commands = LEDSCommands(grey, grey, grey, grey, grey)
pwm_commands = PWMCommands(motor_left=pwm_left, motor_right=pwm_right)
commands = Duckiebot1Commands(pwm_commands, led_commands)
context.write('commands', commands)
def finish(self, context: Context):
context.info('finish()')
def jpg2rgb(image_data: bytes) -> np.ndarray:
""" Reads JPG bytes as RGB"""
from PIL import Image
import io
im = Image.open(io.BytesIO(image_data))
im = im.convert('RGB')
data = np.array(im)
assert data.ndim == 3
assert data.dtype == np.uint8
return data
def main():
node = PytorchRLTemplateAgent()
protocol = protocol_agent_duckiebot1
wrap_direct(node=node, protocol=protocol)
if __name__ == '__main__':
main()