Skip to content

Commit 27bdea5

Browse files
removed final instances of tf use
1 parent 67b1656 commit 27bdea5

File tree

2 files changed

+97
-76
lines changed

2 files changed

+97
-76
lines changed

costs/quad_cost_with_wrapping.py

+43-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import tensorflow as tf
21
from costs.cost import DiscreteCost
32
from utils.angle_utils import angle_normalize
3+
import numpy as np
44

55

66
class QuadraticRegulatorRef(DiscreteCost):
@@ -16,7 +16,7 @@ def __init__(self, trajectory_ref, system, params):
1616
system: A system dynamics object (used for parsing trajectory objects)
1717
state that corresponds to angles and should be wrapped.
1818
"""
19-
19+
2020
# Used for parsing trajectory objects into tensors of states and actions
2121
self.params = params
2222
self.system = system
@@ -26,7 +26,8 @@ def __init__(self, trajectory_ref, system, params):
2626

2727
self.trajectory_ref = trajectory_ref
2828
self.update_shape()
29-
super(QuadraticRegulatorRef, self).__init__(x_dim=self._x_dim, u_dim=self._u_dim)
29+
super(QuadraticRegulatorRef, self).__init__(
30+
x_dim=self._x_dim, u_dim=self._u_dim)
3031

3132
self.isTimevarying = True
3233
self.isNonquadratic = False
@@ -38,52 +39,58 @@ def update_shape(self):
3839

3940
x_dim, u_dim = self._x_dim, self._u_dim
4041

41-
C_gg = tf.diag(p.quad_coeffs, name='lqr_coeffs_quad')
42-
c_g = tf.constant(p.linear_coeffs, name='lqr_coeffs_linear', dtype=tf.float32)
42+
C_gg = np.linalg.diag(p.quad_coeffs, name='lqr_coeffs_quad')
43+
c_g = np.constant(
44+
p.linear_coeffs, name='lqr_coeffs_linear', dtype=np.float32)
4345
# Check dimensions
44-
assert ((tf.reduce_all(tf.equal(C_gg[:x_dim, x_dim:], tf.transpose(C_gg[x_dim:, :x_dim]))).numpy()))
45-
assert ((x_dim + u_dim) == C_gg.shape[0].value == C_gg.shape[1].value == c_g.shape[0].value)
46+
assert ((np.reduce_all(
47+
np.equal(C_gg[:x_dim, x_dim:], np.transpose(C_gg[x_dim:, :x_dim])))))
48+
assert ((x_dim + u_dim) ==
49+
C_gg.shape[0].value == C_gg.shape[1].value == c_g.shape[0].value)
4650

4751
trajectory_ref = self.trajectory_ref
4852
n, k, g = trajectory_ref.n, trajectory_ref.k, C_gg.shape[0]
49-
self._C_nkgg = tf.broadcast_to(C_gg, (n, k, g, g))
50-
self._c_nkg = tf.broadcast_to(c_g, (n, k, g))
53+
self._C_nkgg = np.broadcast_to(C_gg, (n, k, g, g))
54+
self._c_nkg = np.broadcast_to(c_g, (n, k, g))
5155

5256
def compute_trajectory_cost(self, trajectory, trials=1):
53-
with tf.name_scope('compute_traj_cost'):
54-
z_nkg = self.construct_z(trajectory)
55-
C_nkgg, c_nkg = self._C_nkgg, self._c_nkg
56-
Cz_nkg = tf.squeeze(tf.matmul(C_nkgg, z_nkg[:, :, :, None]))
57-
zCz_nk = tf.reduce_sum(z_nkg*Cz_nkg, axis=2)
58-
cz_nk = tf.reduce_sum(c_nkg*z_nkg, axis=2)
59-
cost_nk = .5*zCz_nk + cz_nk
60-
return cost_nk, tf.reduce_sum(cost_nk, axis=1)
57+
# with tf.name_scope('compute_traj_cost'):
58+
z_nkg = self.construct_z(trajectory)
59+
C_nkgg, c_nkg = self._C_nkgg, self._c_nkg
60+
Cz_nkg = np.squeeze(np.matmul(C_nkgg, z_nkg[:, :, :, None]))
61+
zCz_nk = np.sum(z_nkg * Cz_nkg, axis=2)
62+
cz_nk = np.sum(c_nkg * z_nkg, axis=2)
63+
cost_nk = .5 * zCz_nk + cz_nk
64+
return cost_nk, np.sum(cost_nk, axis=1)
6165

6266
def quad_coeffs(self, trajectory, t=None):
6367
# Return terms H_xx, H_xu, H_uu, J_x, J_u
64-
with tf.name_scope('quad_coeffs'):
65-
H_nkgg = self._C_nkgg
66-
J_nkg = self._c_nkg
67-
z_nkg = self.construct_z(trajectory)
68-
Hz_nkg = tf.squeeze(tf.matmul(H_nkgg, z_nkg[:, :, :, None]), axis=-1)
69-
return H_nkgg[:, :, :self._x_dim, :self._x_dim], \
70-
H_nkgg[:, :, :self._x_dim, self._x_dim:], \
71-
H_nkgg[:, :, self._x_dim:, self._x_dim:], \
72-
J_nkg[:, :, :self._x_dim] + Hz_nkg[:, :, :self._x_dim], \
73-
J_nkg[:, :, self._x_dim:] + Hz_nkg[:, :, self._x_dim:]
68+
# with tf.name_scope('quad_coeffs'):
69+
H_nkgg = self._C_nkgg
70+
J_nkg = self._c_nkg
71+
z_nkg = self.construct_z(trajectory)
72+
Hz_nkg = np.squeeze(
73+
np.matmul(H_nkgg, z_nkg[:, :, :, None]), axis=-1)
74+
return H_nkgg[:, :, :self._x_dim, :self._x_dim], \
75+
H_nkgg[:, :, :self._x_dim, self._x_dim:], \
76+
H_nkgg[:, :, self._x_dim:, self._x_dim:], \
77+
J_nkg[:, :, :self._x_dim] + Hz_nkg[:, :, :self._x_dim], \
78+
J_nkg[:, :, self._x_dim:] + Hz_nkg[:, :, self._x_dim:]
7479

7580
# TODO: Currently calling numpy() here as tfe.DEVICE_PLACEMENT_SILENT is not working in the eager mode to place
7681
# non-gpu ops (i.e. mod) on the cpu turning tensors into numpy arrays is a hack around this.
7782
def construct_z(self, trajectory):
7883
""" Input: A trajectory with x_dim =d and u_dim=f
7984
Output: z_nkg - a tensor of size n,k,g where g=d+f
8085
"""
81-
with tf.name_scope('construct_z'):
82-
x_nkd, u_nkf = self.system.parse_trajectory(trajectory)
83-
x_ref_nkd, u_ref_nkf = self.system.parse_trajectory(self.trajectory_ref)
84-
delx_nkd = x_nkd - x_ref_nkd
85-
delu_nkf = u_nkf - u_ref_nkf
86-
z_nkg = tf.concat([delx_nkd[:, :, :self.angle_dims],
87-
angle_normalize(delx_nkd[:, :, self.angle_dims:self.angle_dims+1].numpy()),
88-
delx_nkd[:, :, self.angle_dims+1:], delu_nkf], axis=2)
89-
return z_nkg
86+
# with tf.name_scope('construct_z'):
87+
x_nkd, u_nkf = self.system.parse_trajectory(trajectory)
88+
x_ref_nkd, u_ref_nkf = self.system.parse_trajectory(
89+
self.trajectory_ref)
90+
delx_nkd = x_nkd - x_ref_nkd
91+
delu_nkf = u_nkf - u_ref_nkf
92+
z_nkg = np.concatenate([delx_nkd[:, :, :self.angle_dims],
93+
angle_normalize(
94+
delx_nkd[:, :, self.angle_dims:self.angle_dims + 1]),
95+
delx_nkd[:, :, self.angle_dims + 1:], delu_nkf], axis=2)
96+
return z_nkg

waypoint_grids/projected_image_space_grid.py

+54-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import tensorflow as tf
32
from waypoint_grids.uniform_sampling_grid import UniformSamplingGrid
43

54

@@ -11,36 +10,39 @@ def __init__(self, params):
1110
# Compute the image size bounds based on the focal length and the field of view
1211
params = self.compute_image_bounds(params)
1312
super(ProjectedImageSpaceGrid, self).__init__(params)
14-
13+
1514
# Compute the rotation and translation vectors from the world frame to the optical frame and vice-versa
1615
self.compute_rotation_and_translation_transformations()
17-
16+
1817
@staticmethod
1918
def compute_image_bounds(params):
2019
"""
2120
Compute the image size bounds based on the focal length and the field of view.
2221
"""
2322
eps = 1e-2
2423
# The projection point of the outermost point in the filed of view
25-
dx = params.projected_grid_params.f * np.tan(params.projected_grid_params.fov)
26-
24+
dx = params.projected_grid_params.f * \
25+
np.tan(params.projected_grid_params.fov)
26+
2727
# Compute x_min and x_max
2828
x_min = -1. * dx
2929
x_max = 1. * dx
30-
30+
3131
# Compute y_min
3232
if params.projected_grid_params.tilt > params.projected_grid_params.fov:
3333
y_min = -1. * dx
3434
else:
3535
# An eps is subtracted from the tilt to clip the far end of the camera
36-
y_min = -1. * params.projected_grid_params.f * np.tan(params.projected_grid_params.tilt - eps)
37-
36+
y_min = -1. * params.projected_grid_params.f * \
37+
np.tan(params.projected_grid_params.tilt - eps)
38+
3839
# Compute y_max
39-
if params.projected_grid_params.tilt + params.projected_grid_params.fov < np.pi/2:
40+
if params.projected_grid_params.tilt + params.projected_grid_params.fov < np.pi / 2:
4041
y_max = 1. * dx
4142
else:
4243
# An eps is subtracted from the tilt to clip the far end of the camera
43-
y_max = params.projected_grid_params.f * np.tan(np.pi/2 - params.projected_grid_params.tilt - eps)
44+
y_max = params.projected_grid_params.f * \
45+
np.tan(np.pi / 2 - params.projected_grid_params.tilt - eps)
4446

4547
params.bound_min = [x_min, y_min, params.bound_min[2]]
4648
params.bound_max = [x_max, y_max, params.bound_max[2]]
@@ -55,23 +57,24 @@ def sample_egocentric_waypoints(self, vf=0.):
5557
wf_n11 = np.zeros_like(wx_n11)
5658
# Project the (x, y, theta) points back in the world coordinates.
5759
return self.generate_worldframe_waypoints_from_imageframe_waypoints(wx_n11, wy_n11, wtheta_n11, vf_n11, wf_n11)
58-
60+
5961
def generate_worldframe_waypoints_from_imageframe_waypoints(self, wx_n11, wy_n11, wtheta_n11,
6062
vf_n11=None, wf_n11=None):
6163
"""
6264
Project the (x, y, theta) waypoints in the image space back in the world coordinates. In the world frame x
6365
correspond to Z (the depth) and y corresponds to the x direction in the image plane. The theta in the image
6466
plane is measured positively anti-clockwise from the x-axis.
6567
"""
66-
X_n1, _, Z_n1 = self.project_image_space_points_to_ground(np.hstack([wx_n11[:, :, 0], wy_n11[:, :, 0]]))
68+
X_n1, _, Z_n1 = self.project_image_space_points_to_ground(
69+
np.hstack([wx_n11[:, :, 0], wy_n11[:, :, 0]]))
6770
# Project coordinates for determining angle (take a vector of magnitude 1e-5 in the direction of angle)
6871
X_plus_delta_n1, _, Z_plus_delta_n1 = self.project_image_space_points_to_ground(
69-
np.hstack([wx_n11[:, :, 0] + 1e-5*np.cos(wtheta_n11[:, :, 0]),
70-
wy_n11[:, :, 0] + 1e-5*np.sin(wtheta_n11[:, :, 0])]))
72+
np.hstack([wx_n11[:, :, 0] + 1e-5 * np.cos(wtheta_n11[:, :, 0]),
73+
wy_n11[:, :, 0] + 1e-5 * np.sin(wtheta_n11[:, :, 0])]))
7174
return Z_n1[:, :, np.newaxis], X_n1[:, :, np.newaxis], \
72-
np.arctan2(X_plus_delta_n1 - X_n1, Z_plus_delta_n1 - Z_n1)[:, :, np.newaxis], \
73-
vf_n11, wf_n11
74-
75+
np.arctan2(X_plus_delta_n1 - X_n1, Z_plus_delta_n1 - Z_n1)[:, :, np.newaxis], \
76+
vf_n11, wf_n11
77+
7578
def generate_imageframe_waypoints_from_worldframe_waypoints(self, wx_n11, wy_n11, wtheta_n11,
7679
vf_n11=None, wf_n11=None):
7780
"""
@@ -81,24 +84,27 @@ def generate_imageframe_waypoints_from_worldframe_waypoints(self, wx_n11, wy_n11
8184
"""
8285
# Project points in the optical coordinates
8386
n = wx_n11.shape[0]
84-
XYZ_world_coordinates_n3 = np.hstack([wy_n11[:, :, 0], np.zeros((n, 1)), wx_n11[:, :, 0]])
85-
XYZ_optical_coordinates_n3 = self.convert_world_coordinates_to_optical_coordinates(XYZ_world_coordinates_n3)
86-
87+
XYZ_world_coordinates_n3 = np.hstack(
88+
[wy_n11[:, :, 0], np.zeros((n, 1)), wx_n11[:, :, 0]])
89+
XYZ_optical_coordinates_n3 = self.convert_world_coordinates_to_optical_coordinates(
90+
XYZ_world_coordinates_n3)
91+
8792
# Project points in the optical coordinates for theta transformation
8893
XYZ_plus_delta_world_coordinates_n3 = np.hstack([wy_n11[:, :, 0] + 1e-5 * np.sin(wtheta_n11[:, :, 0]),
8994
np.zeros((n, 1)),
9095
wx_n11[:, :, 0] + 1e-5 * np.cos(wtheta_n11[:, :, 0])])
9196
XYZ_plus_delta_optical_coordinates_n3 = self.convert_world_coordinates_to_optical_coordinates(
9297
XYZ_plus_delta_world_coordinates_n3)
93-
98+
9499
# Project optical coordinates into image space
95-
x_n1, y_n1 = self.project_optical_coordinates_to_image_space(XYZ_optical_coordinates_n3)
100+
x_n1, y_n1 = self.project_optical_coordinates_to_image_space(
101+
XYZ_optical_coordinates_n3)
96102
x_plus_delta_n1, y_plus_delta_n1 = self.project_optical_coordinates_to_image_space(
97103
XYZ_plus_delta_optical_coordinates_n3)
98-
104+
99105
return x_n1[:, :, np.newaxis], y_n1[:, :, np.newaxis], \
100-
np.arctan2(y_plus_delta_n1 - y_n1, x_plus_delta_n1 - x_n1)[:, :, np.newaxis], \
101-
vf_n11, wf_n11
106+
np.arctan2(y_plus_delta_n1 - y_n1, x_plus_delta_n1 - x_n1)[:, :, np.newaxis], \
107+
vf_n11, wf_n11
102108

103109
def worldframe_waypoint_direction_indicator(self, wx_n11, wy_n11, wtheta_n11, vf_n11=None,
104110
wf_n11=None):
@@ -108,18 +114,22 @@ def worldframe_waypoint_direction_indicator(self, wx_n11, wy_n11, wtheta_n11, vf
108114
-1 if "behind the camera" (Z coordinate negative).
109115
"""
110116
n = wx_n11.shape[0]
111-
XYZ_world_coordinates_n3 = np.hstack([wy_n11[:, :, 0], np.zeros((n, 1)), wx_n11[:, :, 0]])
112-
XYZ_optical_coordinates_n3 = self.convert_world_coordinates_to_optical_coordinates(XYZ_world_coordinates_n3)
113-
return tf.sign(XYZ_optical_coordinates_n3[:, 2])[:, None, None]
117+
XYZ_world_coordinates_n3 = np.hstack(
118+
[wy_n11[:, :, 0], np.zeros((n, 1)), wx_n11[:, :, 0]])
119+
XYZ_optical_coordinates_n3 = self.convert_world_coordinates_to_optical_coordinates(
120+
XYZ_world_coordinates_n3)
121+
return np.sign(XYZ_optical_coordinates_n3[:, 2])[:, None, None]
114122

115123
def project_optical_coordinates_to_image_space(self, XYZ_n3):
116124
"""
117125
Project a series of coordinates from the optical frame to the image space.
118126
"""
119-
x_n = -self.params.projected_grid_params.f * XYZ_n3[:, 0] / XYZ_n3[:, 2]
120-
y_n = -self.params.projected_grid_params.f * XYZ_n3[:, 1] / XYZ_n3[:, 2]
127+
x_n = -self.params.projected_grid_params.f * \
128+
XYZ_n3[:, 0] / XYZ_n3[:, 2]
129+
y_n = -self.params.projected_grid_params.f * \
130+
XYZ_n3[:, 1] / XYZ_n3[:, 2]
121131
return x_n[:, np.newaxis], y_n[:, np.newaxis]
122-
132+
123133
def project_image_space_points_to_ground(self, xy_n2):
124134
"""
125135
Project a series of points in the image space to the ground plane.
@@ -132,12 +142,14 @@ def project_image_space_points_to_ground(self, xy_n2):
132142
tilt = self.params.projected_grid_params.tilt
133143
h = self.params.projected_grid_params.h
134144
n = xy_n2.shape[0]
135-
deno_n1 = self.params.projected_grid_params.f * np.sin(tilt) + xy_n2[:, 1:2] * np.cos(tilt)
136-
X_n1 = -1. * xy_n2[:, 0:1] * h/deno_n1
145+
deno_n1 = self.params.projected_grid_params.f * \
146+
np.sin(tilt) + xy_n2[:, 1:2] * np.cos(tilt)
147+
X_n1 = -1. * xy_n2[:, 0:1] * h / deno_n1
137148
Y_n1 = np.zeros((n, 1))
138-
Z_n1 = h * (self.params.projected_grid_params.f * np.cos(tilt) - xy_n2[:, 1:2] * np.sin(tilt)) / deno_n1
149+
Z_n1 = h * (self.params.projected_grid_params.f *
150+
np.cos(tilt) - xy_n2[:, 1:2] * np.sin(tilt)) / deno_n1
139151
return X_n1, Y_n1, Z_n1
140-
152+
141153
def convert_world_coordinates_to_optical_coordinates(self, xyz_n3):
142154
"""
143155
Convert a series of coordinates from the world frame to the optical frame (the one that is aligned with the
@@ -146,15 +158,15 @@ def convert_world_coordinates_to_optical_coordinates(self, xyz_n3):
146158
# First translate the points and then rotate them.
147159
xyz_n3 = xyz_n3 - self.T_world_optical
148160
return xyz_n3.dot(self.R_world_optical.transpose())
149-
161+
150162
def convert_optical_coordinates_to_world_coordinates(self, xyz_n3):
151163
"""
152164
Convert a series of coordinates from the optical frame to the world frame.
153165
"""
154166
# First rotate and then translate the points.
155167
xyz_n3 = xyz_n3.dot(self.R_optical_world.transpose())
156168
return xyz_n3 - self.T_optical_world
157-
169+
158170
def compute_rotation_and_translation_transformations(self):
159171
"""
160172
Compute the rotation and translation matrices from the world frame to the optical frame and vice-versa.
@@ -170,9 +182,11 @@ def compute_rotation_and_translation_transformations(self):
170182
self.R_optical_world = np.array([[1., 0., 0.],
171183
[0., np.cos(tilt), -np.sin(tilt)],
172184
[0., np.sin(tilt), np.cos(tilt)]])
173-
self.T_world_optical = np.array([0., self.params.projected_grid_params.h, 0.])
174-
self.T_optical_world = np.array([0., -self.params.projected_grid_params.h, 0.])
175-
185+
self.T_world_optical = np.array(
186+
[0., self.params.projected_grid_params.h, 0.])
187+
self.T_optical_world = np.array(
188+
[0., -self.params.projected_grid_params.h, 0.])
189+
176190
@property
177191
def descriptor_string(self):
178192
"""Returns a unique string identifying

0 commit comments

Comments
 (0)