-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_collection_drone.py
51 lines (43 loc) · 1.63 KB
/
data_collection_drone.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
"""Collect human data from DroneReacher."""
import gym
import residual_shared_autonomy.drone_sim
from residual_shared_autonomy.drone_sim.joystick_agent import DroneJoystickActor
from dl.rl import ensure_vec_env
import torch
import time
import argparse
import os
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Data collection.')
parser.add_argument('datafile', type=str, help='datafile')
parser.add_argument('neps', type=int, help='# of episodes')
parser.add_argument('--overwrite', action='store_true',
help='overwrite data')
args = parser.parse_args()
env = gym.make("DroneReacherBot-v0")
env = ensure_vec_env(env)
if os.path.exists(args.datafile):
if not args.overwrite:
raise ValueError('Data file {} already exists. Add --overwrite to '
' overwrite the file.'.format(args.datafile))
actor = DroneJoystickActor(env)
transitions = []
for i in range(args.neps):
print(i)
ob = env.reset()
actor.reset()
env.render()
done = False
time.sleep(1.)
while not done:
action = actor(ob)
ob_next, reward, done, _ = env.step(action)
transitions.append([ob, action.copy(), reward, done])
ob = ob_next
time.sleep(0.5)
obs, actions, rewards, dones = zip(*transitions)
data = {'obs': torch.Tensor(obs).squeeze(),
'actions': torch.Tensor(actions).squeeze(),
'rewards': torch.Tensor(rewards).squeeze(),
'dones': torch.Tensor(dones).squeeze()}
torch.save(data, args.datafile)