-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_ur5_visual_reacher.py
285 lines (248 loc) · 11.1 KB
/
task_ur5_visual_reacher.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
import torch
import argparse
import relod.utils as utils
import time
import numpy as np
import cv2
import os
from relod.logger import Logger
from relod.algo.comm import MODE
from relod.algo.local_wrapper import LocalWrapper
from relod.algo.sac_rad_agent import SACRADLearner, SACRADPerformer
from relod.envs.ur5_visual_reacher import VisualReacherEnv, MonitorTarget
config = {
'conv': [
# in_channel, out_channel, kernel_size, stride
[-1, 32, 3, 2],
[32, 32, 3, 2],
[32, 32, 3, 2],
[32, 32, 3, 1],
],
'latent': 50,
'mlp': [
[-1, 1024], # first hidden layer
[1024, 1024],
[1024, -1] # output layer
],
}
def parse_args():
parser = argparse.ArgumentParser(description='Local remote visual UR5 Reacher')
# environment
parser.add_argument('--setup', default='Visual-UR5')
parser.add_argument('--env', default='ur5', type=str)
parser.add_argument('--ur5_ip', default='129.128.159.210', type=str)
parser.add_argument('--camera_id', default=0, type=int)
parser.add_argument('--image_width', default=160, type=int)
parser.add_argument('--image_height', default=90, type=int)
parser.add_argument('--target_type', default='size', type=str)
parser.add_argument('--random_action_repeat', default=1, type=int)
parser.add_argument('--agent_action_repeat', default=1, type=int)
parser.add_argument('--image_history', default=3, type=int)
parser.add_argument('--joint_history', default=1, type=int)
parser.add_argument('--ignore_joint', default=False, action='store_true')
parser.add_argument('--episode_length_time', default=6.0, type=float)
parser.add_argument('--dt', default=0.04, type=float)
parser.add_argument('--size_tol', default=0.015, type=float)
parser.add_argument('--center_tol', default=0.1, type=float)
parser.add_argument('--reward_tol', default=2.0, type=float)
parser.add_argument('--reset_penalty_steps', default=70, type=int)
parser.add_argument('--reward', default=-1, type=float)
parser.add_argument('--reset_type', default='zero', type=str, help=["zero", "random"])
# replay buffer
parser.add_argument('--replay_buffer_capacity', default=100000, type=int)
parser.add_argument('--rad_offset', default=0.01, type=float)
# train
parser.add_argument('--init_steps', default=2000, type=int)
parser.add_argument('--env_steps', default=100000, type=int)
parser.add_argument('--batch_size', default=256, type=int)
parser.add_argument('--sync_mode', default=False, action='store_true')
parser.add_argument('--async_buffer', default=True, action='store_true')
parser.add_argument('--max_updates_per_step', default=0.6, type=float)
parser.add_argument('--update_every', default=50, type=int)
parser.add_argument('--update_epochs', default=50, type=int)
# critic
parser.add_argument('--critic_lr', default=3e-4, type=float)
parser.add_argument('--critic_tau', default=0.005, type=float)
parser.add_argument('--critic_target_update_freq', default=1, type=int)
parser.add_argument('--bootstrap_terminal', default=0, type=int)
# actor
parser.add_argument('--actor_lr', default=3e-4, type=float)
parser.add_argument('--actor_update_freq', default=1, type=int)
# encoder
parser.add_argument('--encoder_tau', default=0.005, type=float)
# sac
parser.add_argument('--discount', default=0.99, type=float)
parser.add_argument('--init_temperature', default=0.1, type=float)
parser.add_argument('--alpha_lr', default=3e-4, type=float)
# agent
parser.add_argument('--remote_ip', default='localhost', type=str)
parser.add_argument('--port', default=9876, type=int)
parser.add_argument('--mode', default='e', type=str, help="Modes in ['r', 'l', 'rl', 'e'] ")
# misc
parser.add_argument('--run_type', default='experiment', type=str)
parser.add_argument('--description', default='', type=str)
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--work_dir', default='results/', type=str)
parser.add_argument('--save_tb', default=False, action='store_true')
parser.add_argument('--save_model', default=False, action='store_true')
parser.add_argument('--plot_learning_curve', default=False, action='store_true')
parser.add_argument('--xtick', default=1200, type=int)
parser.add_argument('--display_image', default=True, action='store_true')
parser.add_argument('--save_image', default=False, action='store_true')
parser.add_argument('--save_model_freq', default=10000, type=int)
parser.add_argument('--load_model', default=-1, type=int)
parser.add_argument('--device', default='cuda:0', type=str)
parser.add_argument('--lock', default=False, action='store_true')
args = parser.parse_args()
assert args.mode in ['r', 'l', 'rl', 'e']
assert args.reward < 0 and args.reset_penalty_steps >= 0
args.async_mode = not args.sync_mode
return args
def main():
args = parse_args()
if args.mode == 'r':
mode = MODE.REMOTE_ONLY
elif args.mode == 'l':
mode = MODE.LOCAL_ONLY
elif args.mode == 'rl':
mode = MODE.REMOTE_LOCAL
elif args.mode == 'e':
mode = MODE.EVALUATION
else:
raise NotImplementedError()
if args.device is '':
args.device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
args.work_dir += f'/{args.env}/timeout={args.episode_length_time:.0f}/seed={args.seed}'
args.model_dir = args.work_dir+'/models'
args.return_dir = args.work_dir+'/returns'
if mode != MODE.EVALUATION:
os.makedirs(args.model_dir, exist_ok=False)
os.makedirs(args.return_dir, exist_ok=False)
if mode == MODE.LOCAL_ONLY:
L = Logger(args.return_dir, use_tb=args.save_tb)
if args.save_image:
args.image_dir = args.work_dir+'/images'
if mode == MODE.LOCAL_ONLY or mode == MODE.EVALUATION:
os.makedirs(args.image_dir, exist_ok=False)
env = VisualReacherEnv(
setup = args.setup,
ip = args.ur5_ip,
seed = args.seed,
camera_id = args.camera_id,
image_width = args.image_width,
image_height = args.image_height,
target_type = args.target_type,
image_history = args.image_history,
joint_history = args.joint_history,
episode_length = args.episode_length_time,
dt = args.dt,
size_tol = args.size_tol,
center_tol = args.center_tol,
reward_tol = args.reward_tol,
reset_type=args.reset_type,
)
utils.set_seed_everywhere(args.seed, None)
mt = MonitorTarget()
mt.reset_plot()
# input('Please hit Enter to proceed...')
image, prop = env.reset()
image_to_show = np.transpose(image, [1, 2, 0])
image_to_show = image_to_show[:,:,-3:]
# cv2.imshow('raw', image_to_show)
# cv2.waitKey(1)
args.image_shape = env.image_space.shape
args.proprioception_shape = env.proprioception_space.shape
args.action_shape = env.action_space.shape
args.env_action_space = env.action_space
args.net_params = config
episode_length_step = int(args.episode_length_time / args.dt)
agent = LocalWrapper(episode_length_step, mode, remote_ip=args.remote_ip, port=args.port)
agent.send_data(args)
agent.init_performer(SACRADPerformer, args)
agent.init_learner(SACRADLearner, args, agent.performer)
# sync initial weights with remote
agent.apply_remote_policy(block=True)
if args.load_model > -1:
agent.load_policy_from_file(args.model_dir, args.load_model)
# First inference took a while (~1 min), do it before the agent-env interaction loop
if mode != MODE.REMOTE_ONLY:
agent.performer.sample_action((image, prop))
agent.performer.sample_action((image, prop))
agent.performer.sample_action((image, prop))
# Experiment block starts
returns = []
ep_lens = []
start_time = time.time()
print(f'Experiment starts at: {start_time}')
t = 0
while t <= args.env_steps:
# start a new episode
if mode == MODE.EVALUATION:
image, prop = env.reset()
mt.reset_plot()
else:
mt.reset_plot()
image, prop = env.reset()
agent.send_init_ob((image, prop))
ret = 0
ep_steps = 0
done = 0
if (mode == MODE.LOCAL_ONLY or mode == MODE.EVALUATION) and args.save_image:
episode_image_dir = args.image_dir+f'/episode={len(returns)+1}/'
os.makedirs(episode_image_dir, exist_ok=False)
epi_start_time = time.time()
while not done:
if args.display_image or ((mode == MODE.LOCAL_ONLY or mode == MODE.EVALUATION) and args.save_image):
image_to_show = np.transpose(image, [1, 2, 0])
image_to_show = image_to_show[:,:,-3:]
if (mode == MODE.LOCAL_ONLY or mode == MODE.EVALUATION) and args.save_image:
cv2.imwrite(episode_image_dir+f'ep_step={ep_steps}.png', image_to_show)
# if args.display_image:
# cv2.imshow('raw', image_to_show)
# cv2.waitKey(1)
# select an action
action = agent.sample_action((image, prop))
# step in the environment
next_image, next_prop, reward, done, _ = env.step(action)
# store
agent.push_sample((image, prop), action, reward, (next_image, next_prop), done)
stat = agent.update_policy(t)
if mode == MODE.LOCAL_ONLY and stat is not None:
for k, v in stat.items():
L.log(k, v, t)
image = next_image
prop = next_prop
# Log
ret += reward
ep_steps += 1
t += 1
if args.save_model and t % args.save_model_freq == 0:
agent.save_policy_to_file(args.model_dir, t)
# save the last image
if (mode == MODE.LOCAL_ONLY or mode == MODE.EVALUATION) and args.save_image:
image_to_show = np.transpose(image, [1, 2, 0])
image_to_show = image_to_show[:,:,-3:]
cv2.imwrite(episode_image_dir+f'ep_step={ep_steps}.png', image_to_show)
returns.append(ret)
ep_lens.append(ep_steps)
if mode != MODE.EVALUATION:
utils.save_returns(args.return_dir+'/return.txt', returns, ep_lens)
if mode == MODE.LOCAL_ONLY:
L.log('train/duration', time.time() - epi_start_time, t)
L.log('train/episode_reward', ret, t)
L.log('train/episode', len(returns), t)
L.dump(t)
if args.plot_learning_curve:
utils.show_learning_curve(args.return_dir+'/learning curve.png', returns, ep_lens, xtick=args.xtick)
duration = time.time() - start_time
agent.save_policy_to_file(args.model_dir, t)
# Clean up
env.reset()
agent.close()
env.close()
# always show a learning curve at the end
if mode == MODE.LOCAL_ONLY:
utils.show_learning_curve(args.return_dir+'/learning curve.png', returns, ep_lens, xtick=args.xtick)
print(f"Finished in {duration}s")
if __name__ == '__main__':
main()