-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkb_agent.py
191 lines (147 loc) · 5.83 KB
/
kb_agent.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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import sys
import termios
import tty
import os
import collections
import torch
import numpy as np
from PIL import ImageDraw
from torchvision.utils import make_grid
import torchvision.transforms as transforms
import torch.nn.functional as F
from rl.common.env_utils import construct_envs, get_env_class
from rl.config import get_config
from interaction_exploration.utils import util
def get_term_character():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def add_rectangle(tensor, bbox):
img = transforms.ToPILImage()(tensor)
draw = ImageDraw.Draw(img)
draw.rectangle(bbox, outline='blue', width=3)
tensor = transforms.ToTensor()(img)
return tensor
class KBController(object):
def __init__(self, config):
self.config = config
self.command_dict = {
'\x1b[A': 'forward',
'w': 'up',
's': 'down',
'a': 'tleft',
'd': 'tright',
'e': 'interact',
}
self.config.defrost()
self.config.DEBUG = True
self.config.NUM_PROCESSES = 1
self.config.freeze()
self.envs = construct_envs(self.config, get_env_class(self.config.ENV_NAME))
self.observation = self.envs.reset()[0]
self.act_to_idx = collections.defaultdict(lambda: -1)
self.act_to_idx.update({act:idx for idx, act in enumerate(self.envs.call_at(0, 'get_actions'))})
sz = 300
N = 5
center = ((sz//N)*(N//2), (sz//N)*(N+1)//2)
self.center_box = [center[0], center[0], center[1], center[1]]
colors = {'green':[7, 212, 0], 'yellow':[255, 255, 0]}
colors = {color: torch.Tensor(colors[color]).unsqueeze(1)/255 for color in colors}
self.colors = colors
self.interactions = ['take', 'put', 'open', 'close', 'toggle-on', 'toggle-off', 'slice']
self.act_to_channel = {act: idx for idx, act in enumerate(self.interactions)}
self.render()
print ('KB controller set up.')
print ('↑: move forward, look: wsad, action: e')
def next_interact_command(self):
current_buffer = ''
while True:
commands = self.command_dict
current_buffer += get_term_character()
if current_buffer == 'q' or current_buffer == '\x03':
break
if current_buffer in commands:
yield commands[current_buffer]
current_buffer = ''
else:
match = False
for k,v in commands.items():
if k.startswith(current_buffer):
match = True
break
if not match:
current_buffer = ''
# TODO: add action names, add more actions
def render(self):
event = self.envs.call_at(0, 'last_event')
frame = torch.from_numpy(np.array(event.frame)).float().permute(2, 0, 1)/255
frame = F.interpolate(frame.unsqueeze(0), 300, mode='bilinear', align_corners=True)[0]
frame = add_rectangle(frame, self.center_box)
if not self.config.SHOW_MARKERS:
util.show_wait(frame, T=1, win='frame')
return
masks = self.envs.call_at(0, 'get_current_mask').byte()
actions = ['open']
beacon = torch.zeros(3, 300, 300)
viz_tensors = []
for action in actions:
channel = self.act_to_channel[action]
beacon.zero_()
beacon[:, masks[channel, 0]] = self.colors['yellow']
beacon[:, masks[channel, 1]] = self.colors['green']
beacon = util.blend(frame, beacon)
viz_tensors.append(beacon)
grid = make_grid(viz_tensors, nrow=len(viz_tensors))
util.show_wait(grid, T=1)
def step(self):
for action in self.next_interact_command():
if action=='interact':
prompt = ['# Options']
prompt += ['Interactions: take, put, open, close, toggle-on, toggle-off, slice']
prompt += ['Misc: done, reset']
prompt += ['>> ']
action = input('\n'.join(prompt))
yield action
def run(self):
for action in self.step():
# handle special controller actions
if action=='done':
sys.exit(0)
if action=='reset':
self.observation = self.envs.reset()
continue
act_idx = self.act_to_idx[action]
if act_idx==-1:
print ('Action not recognized')
continue
# handle environment actions
outputs = self.envs.step([act_idx])
self.observation, reward, done, info = [list(x)[0] for x in zip(*outputs)]
print (f"A: {info['action']} | S: {info['success']} | R: {info['reward']}")
display = os.environ['DISPLAY']
os.environ['DISPLAY'] = os.environ['LDISPLAY']
self.render()
os.environ['DISPLAY'] = display
if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser(description='RL')
parser.add_argument('--env-name', default='ThorInteractionCount-v0')
parser.add_argument('--markers', action='store_true')
parser.add_argument('--x_display', default='0.0')
args = parser.parse_args()
os.environ['LDISPLAY'] = os.environ['DISPLAY']
config = get_config()
config.defrost()
config.ENV_NAME = args.env_name
config.RL.PPO.num_steps = 10000000
config.X_DISPLAY = args.x_display
config.SHOW_MARKERS = args.markers
config.freeze()
controller = KBController(config)
controller.run()