-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
212 lines (192 loc) · 8.78 KB
/
play.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
import numpy as np
from envs import make_env
from algorithm.replay_buffer import goal_based_process
from utils.os_utils import make_dir
from common import get_args
import re
from ast import literal_eval
import tensorflow as tf
import os
from gym.wrappers.monitoring.video_recorder import VideoRecorder
from resource import sample_trajectory
class Player:
def __init__(self, args):
# initialize environment
self.args = args
self.env = make_env(args)
self.args.timesteps = self.env.max_episode_steps
self.env_test = make_env(args)
self.info = []
self.test_rollouts = 100
# get current policy from path (restore tf session + graph)
self.play_dir = args.play_path
self.play_epoch = args.play_epoch
self.meta_path = os.path.join(self.play_dir, "saved_policy-{}.meta".format(self.play_epoch))
self.sess = tf.Session()
self.saver = tf.train.import_meta_graph(self.meta_path)
self.saver.restore(self.sess, tf.train.latest_checkpoint(self.play_dir))
graph = tf.get_default_graph()
self.raw_obs_ph = graph.get_tensor_by_name("raw_obs_ph:0")
self.pi = graph.get_tensor_by_name("main/policy/net/pi/Tanh:0")
def my_step_batch(self, obs):
# compute actions from obs based on current policy by running tf session initialized before
actions = self.sess.run(self.pi, {self.raw_obs_ph: obs})
return actions
def play(self):
# play policy on env
env = self.env
acc_sum, obs = 0.0, []
for i in range(self.test_rollouts):
obs.append(goal_based_process(env.reset()))
for timestep in range(self.args.timesteps):
actions = self.my_step_batch(obs)
obs, infos = [], []
ob, _, _, info = env.step(actions[0])
obs.append(goal_based_process(ob))
infos.append(info)
env.render()
def demoRecordReach(self, raw_path="videos/KukaReach1"):
env = self.env
test_rollouts = 5
goals = [[0.84604588, 0.14732964, 1.35766576], [0.79483348, -0.14184732, 1.20930532],
[0.919015, -0.15907337, 1.18060975], [7.11554270e-01, 1.51756884e-03, 1.34433537e+00],
[0.70905836, 0.13042637, 1.19320888]]
recorder = VideoRecorder(env.env.env, base_path=raw_path)
for i in range(test_rollouts):
env.reset()
env.set_goal(np.array(goals[i]))
print("Rollout {}/{} ...".format(i + 1, test_rollouts))
for timestep in range(len(sample_trajectory[i])):
if timestep == 0: continue
action = np.array(sample_trajectory[i][timestep]) - np.array(sample_trajectory[i][timestep-1])
zero = np.zeros(1)
action = np.concatenate([action * 22, zero])
env.step(action)
recorder.capture_frame()
recorder.close()
def demoRecordPush(self, raw_path="videos/KukaPush"):
env = self.env
test_rollouts = 5
goals = [[0.68, -0.18, 0.85], [0.60, -0.3, 0.85], [0.72, -0.28, 0.85], [0.58, -0.3, 0.85], [0.62, -0.25, 0.85]]
recorder = VideoRecorder(env.env.env, base_path=raw_path)
acc_sum, obs = 0.0, []
test_rollouts = 5
for i in range(test_rollouts):
env.reset()
env.set_goal(np.array(goals[i]))
obs.append(goal_based_process(env.get_obs()))
print("Rollout {}/{} ...".format(i + 1, test_rollouts))
for timestep in range(self.args.timesteps):
actions = self.my_step_batch(obs)
obs, infos = [], []
ob, _, _, info = env.step(actions[0])
obs.append(goal_based_process(ob))
infos.append(info)
recorder.capture_frame()
recorder.close()
def demoRecordPickNoObstacle(self, raw_path="videos/KukaPickNoObstacle"):
env = self.env
test_rollouts = 5
goals = [[0.80948876, -0.24847823, 1.15], [0.90204398, -0.24176245, 1.15], [0.72934716, -0.19637749, 1.15], [0.8429464, -0.20765762, 1.15], [0.6970663, -0.18643907, 1.15]]
recorder = VideoRecorder(env.env.env, base_path=raw_path)
acc_sum, obs = 0.0, []
test_rollouts = 5
for i in range(test_rollouts):
env.reset()
env.set_goal(np.array(goals[i]))
obs.append(goal_based_process(env.get_obs()))
print("Rollout {}/{} ...".format(i + 1, test_rollouts))
for timestep in range(200):
actions = self.my_step_batch(obs)
obs, infos = [], []
ob, _, _, info = env.step(actions[0])
obs.append(goal_based_process(ob))
infos.append(info)
recorder.capture_frame()
recorder.close()
def demoRecordPickAndPlaceObstacle(self, raw_path="videos/KukaPickAndPlaceObstacle"):
env = self.env
test_rollouts = 5
goals = [[0.80948876, -0.24847823, 0.85], [0.90204398, -0.24176245, 0.85], [0.72934716, -0.19637749, 0.85], [0.6970663, -0.25643907, 0.85], [0.7029464, -0.18765762, 0.85]]
recorder = VideoRecorder(env.env.env, base_path=raw_path)
acc_sum, obs = 0.0, []
test_rollouts = 5
for i in range(test_rollouts):
env.reset()
env.set_goal(np.array(goals[i]))
obs.append(goal_based_process(env.get_obs()))
print("Rollout {}/{} ...".format(i + 1, test_rollouts))
for timestep in range(200):
actions = self.my_step_batch(obs)
obs, infos = [], []
ob, _, _, info = env.step(actions[0])
obs.append(goal_based_process(ob))
infos.append(info)
recorder.capture_frame()
recorder.close()
def record_video(self, raw_path="myrecord"):
env = self.env
test_rollouts = 5
# play policy on env
recorder = VideoRecorder(env.env.env, base_path=raw_path)
acc_sum, obs = 0.0, []
for i in range(test_rollouts):
obs.append(goal_based_process(env.reset()))
if hasattr(env.env.env, "set_camera_pos"):
env.env.env.set_camera_pos(i)
print("Rollout {}/{} ...".format(i + 1, test_rollouts))
for timestep in range(self.args.timesteps):
actions = self.my_step_batch(obs)
obs, infos = [], []
ob, _, _, info = env.step(actions[0])
obs.append(goal_based_process(ob))
infos.append(info)
recorder.capture_frame()
print("... done.")
recorder.close()
def demoRecordReachJoints(self, raw_path="videos/KukaReachJoints"):
file = open("KukaPushJointsTrajectory.txt", 'r')
env = self.env
test_rollouts = 5
recorder = VideoRecorder(env.env.env, base_path=raw_path)
goals = [[0.84604588, 0.14732964, 1.35766576], [0.79483348, -0.14184732, 1.20930532],
[0.919015, -0.15907337, 1.18060975], [7.11554270e-01, 1.51756884e-03, 1.34433537e+00],
[0.70905836, 0.13042637, 1.19320888]]
for i in range(test_rollouts):
jointsTrajectory = re.sub(r"([^[])\s+([^]])", r"\1 \2", file.readline())
jointsTrajectory = np.array(literal_eval(jointsTrajectory))
env.reset()
env.set_goal(np.array(goals[i]))
for j in range(len(jointsTrajectory)):
self.env.stepJoints(jointsTrajectory[j])
recorder.capture_frame()
print("... done.")
recorder.close()
def demoRecordPushJoints(self, raw_path="videos/KukaPushJoints"):
file = open("KukaPushJointsTrajectory2.txt", 'r')
env = self.env
test_rollouts = 5
goals = [[0.68, -0.18, 0.85], [0.60, -0.3, 0.85], [0.72, -0.28, 0.85], [0.58, -0.3, 0.85], [0.62, -0.25, 0.85]]
recorder = VideoRecorder(env.env.env, base_path=raw_path)
jointsTrajectory = re.sub(r"([^[])\s+([^]])", r"\1 \2", file.readline())
jointsTrajectory = np.array(literal_eval(jointsTrajectory))
env.reset()
env.set_goal(np.array(goals[1]))
for j in range(len(jointsTrajectory)):
self.env.stepJoints(jointsTrajectory[j])
recorder.capture_frame()
print("... done.")
if __name__ == "__main__":
# Call play.py in order to see current policy progress
args = get_args()
player = Player(args)
if not args.record:
player.play()
else:
player.record_video(raw_path="figures/" + args.play_path[8:])
# player.demoRecordPush()
# player.demoRecordPushJoints()
# player.demoRecordReach()
# player.demoRecordReach1()
# player.demoRecordPickNoObstacle()
# player.demoRecordPickAndPlaceObstacle()