def _meshgrid(height, width):
with tf.variable_scope('_meshgrid'):
x_t = tf.matmul(tf.ones(shape=tf.stack([height, 1])), tf.transpose(
tf.expand_dims(tf.linspace(-1.0, 1.0, width), 1), [1, 0]))
y_t = tf.matmul(tf.expand_dims(
tf.linspace(-1.0, 1.0, height), 1), tf.ones(shape=tf.stack([1, width])))
x_t_flat = tf.reshape(x_t, (1, -1))
y_t_flat = tf.reshape(y_t, (1, -1))
ones = tf.ones_like(x_t_flat)
grid = tf.concat([x_t_flat, y_t_flat, ones], 0)
return grid
python类linspace()的实例源码
def uv_grid(shape):
u, v = tf.meshgrid(tf.linspace(0.0, 1.0, shape[1]), tf.linspace(0.0, 1.0, shape[0]))
return u, v
def tf_percentile(images):
min = tf.reduce_min(tf.log(1.0 + images))
max = tf.reduce_max(tf.log(1.0 + images))
histogram = tf.histogram_fixed_width(tf.reshape(images, [-1]), [min, max])
values = tf.linspace(min, max, 100)
csum = tf.cumsum(histogram)
csum_float = tf.cast(csum, tf.float32) / tf.cast(tf.size(csum), tf.float32)
argmin_index = tf.cast(tf.argmin((csum_float - 0.95) ** 2.0, axis = 0), tf.int32)
return tf.exp(values[argmin_index]) - 1.0
def lat_long_grid(shape, epsilon = 1.0e-12):
return tf.meshgrid(tf.linspace(-np.pi, np.pi, shape[1]),
tf.linspace(-np.pi / 2.0 + epsilon, np.pi / 2.0 - epsilon, shape[0]))
def uv_grid(shape):
return tf.meshgrid(tf.linspace(-0.5, 0.5, shape[1]),
tf.linspace(-0.5, 0.5, shape[0]))
# Restricted rotations of (a, b, c) to (x, y, z), implemented using
# permutations and negations.
def xyz_grid(shape, face = "front"):
a, b = tf.meshgrid(tf.linspace(-1.0, 1.0, shape[1]),
tf.linspace(-1.0, 1.0, shape[0]))
c = tf.constant(1.0, dtype = tf.float32, shape = shape)
return switch_face(a, b, c, face)
# Convert Cartesian coordinates (x, y, z) to latitude (T) and longitude (S).
def backproject_cubic(depth, shape, face):
a, b = tf.meshgrid(tf.linspace(-1.0, 1.0, shape[2]),
tf.linspace(-1.0, 1.0, shape[1]))
A = depth * tf.expand_dims(tf.tile(tf.expand_dims(a, 0), [shape[0], 1, 1]), 3)
B = depth * tf.expand_dims(tf.tile(tf.expand_dims(b, 0), [shape[0], 1, 1]), 3)
C = depth
x, y, z = switch_face(A, B, C, face)
return tf.sqrt(x ** 2.0 + z ** 2.0)
def backproject_rectilinear(depth, K, shape, face):
u, v = tf.meshgrid(tf.linspace(-1.0, 1.0, shape[2]),
tf.linspace(-1.0, 1.0, shape[1]))
u = tf.expand_dims(tf.tile(tf.expand_dims(u, 0), [shape[0], 1, 1]), 3)
v = tf.expand_dims(tf.tile(tf.expand_dims(v, 0), [shape[0], 1, 1]), 3)
A = (u - K[2]) * depth / K[0]
B = (v - K[3]) * depth / K[1]
C = depth
x, y, z = switch_face(A, B, C, face)
return tf.sqrt(x ** 2.0 + z ** 2.0)
def rectilinear_xyz(K, shape, face = "front"):
u, v = tf.meshgrid(tf.linspace(-1.0, 1.0, shape[1]),
tf.linspace(-1.0, 1.0, shape[0]))
# X = (u - c_x) * z / f_x
# Y = (v - c_y) * z / f_y
a = (u - K[2]) / K[0]
b = (v - K[3]) / K[1]
c = tf.ones([shape[1], shape[0]], dtype = tf.float32)
return switch_face(a, b, c, face)
def spatial_expected_softmax(x, temp=1):
assert len(x.get_shape()) == 4
vals = []
for dim in [0, 1]:
dim_val = x.get_shape()[dim + 1].value
lin = tf.linspace(-1.0, 1.0, dim_val)
lin = tf.expand_dims(lin, 1 - dim)
lin = tf.expand_dims(lin, 0)
lin = tf.expand_dims(lin, 3)
m = tf.reduce_max(x, [1, 2], keep_dims=True)
e = tf.exp((x - m) / temp) + 1e-5
val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
vals.append(tf.expand_dims(val, 2))
return tf.reshape(tf.concat(2, vals), [-1, x.get_shape()[-1].value * 2])
def get_output_for(self, input, **kwargs):
return spatial_expected_softmax(input) # , self.temp)
# max_ = tf.reduce_max(input, reduction_indices=[1, 2], keep_dims=True)
# exp = tf.exp(input - max_) + 1e-5
# vals = []
#
# for dim in [0, 1]:
# dim_val = input.get_shape()[dim + 1].value
# lin = tf.linspace(-1.0, 1.0, dim_val)
# lin = tf.expand_dims(lin, 1 - dim)
# lin = tf.expand_dims(lin, 0)
# lin = tf.expand_dims(lin, 3)
# m = tf.reduce_max(input, [1, 2], keep_dims=True)
# e = tf.exp(input - m) + 1e-5
# val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
# vals.append(tf.expand_dims(val, 2))
#
# return tf.reshape(tf.concat(2, vals), [-1,
# input.get_shape()[-1].value * 2])
# import ipdb; ipdb.set_trace()
# input.get_shape()
# exp / tf.reduce_sum(exp, reduction_indices=[1, 2], keep_dims=True)
# import ipdb;
# ipdb.set_trace()
# spatial softmax?
# for dim in range(2):
# val = obs.get_shape()[dim + 1].value
# lin = tf.linspace(-1.0, 1.0, val)
# lin = tf.expand_dims(lin, 1 - dim)
# lin = tf.expand_dims(lin, 0)
# lin = tf.expand_dims(lin, 3)
# m = tf.reduce_max(e, [1, 2], keep_dims=True)
# e = tf.exp(e - m) + 1e-3
# val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
spatial_transformer_network.py 文件源码
项目:New_Layers-Keras-Tensorflow
作者: WeidiXie
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def _meshgrid(height, width):
x_t_flat, y_t_flat = tf.meshgrid(tf.linspace(-1., 1., width), tf.linspace(-1., 1., height))
ones = tf.ones_like(x_t_flat)
grid = tf.concat(values=[x_t_flat, y_t_flat, ones], axis=0)
return grid
spatial_warping_network.py 文件源码
项目:New_Layers-Keras-Tensorflow
作者: WeidiXie
项目源码
文件源码
阅读 129
收藏 0
点赞 0
评论 0
def warping_meshgrid(height, width):
x_t_flat, y_t_flat = tf.meshgrid(tf.linspace(-1., 1., width), tf.linspace(-1., 1., height))
grid = tf.concat(values=[x_t_flat, y_t_flat], axis=0)
return grid
def spatial_expected_softmax(x, temp=1):
assert len(x.get_shape()) == 4
vals = []
for dim in [0, 1]:
dim_val = x.get_shape()[dim + 1].value
lin = tf.linspace(-1.0, 1.0, dim_val)
lin = tf.expand_dims(lin, 1 - dim)
lin = tf.expand_dims(lin, 0)
lin = tf.expand_dims(lin, 3)
m = tf.reduce_max(x, [1, 2], keep_dims=True)
e = tf.exp((x - m) / temp) + 1e-5
val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
vals.append(tf.expand_dims(val, 2))
return tf.reshape(tf.concat(axis=2, values=vals), [-1, x.get_shape()[-1].value * 2])
def get_output_for(self, input, **kwargs):
return spatial_expected_softmax(input)#, self.temp)
# max_ = tf.reduce_max(input, reduction_indices=[1, 2], keep_dims=True)
# exp = tf.exp(input - max_) + 1e-5
# vals = []
#
# for dim in [0, 1]:
# dim_val = input.get_shape()[dim + 1].value
# lin = tf.linspace(-1.0, 1.0, dim_val)
# lin = tf.expand_dims(lin, 1 - dim)
# lin = tf.expand_dims(lin, 0)
# lin = tf.expand_dims(lin, 3)
# m = tf.reduce_max(input, [1, 2], keep_dims=True)
# e = tf.exp(input - m) + 1e-5
# val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
# vals.append(tf.expand_dims(val, 2))
#
# return tf.reshape(tf.concat(2, vals), [-1, input.get_shape()[-1].value * 2])
# import ipdb; ipdb.set_trace()
# input.get_shape()
# exp / tf.reduce_sum(exp, reduction_indices=[1, 2], keep_dims=True)
# import ipdb;
# ipdb.set_trace()
# spatial softmax?
# for dim in range(2):
# val = obs.get_shape()[dim + 1].value
# lin = tf.linspace(-1.0, 1.0, val)
# lin = tf.expand_dims(lin, 1 - dim)
# lin = tf.expand_dims(lin, 0)
# lin = tf.expand_dims(lin, 3)
# m = tf.reduce_max(e, [1, 2], keep_dims=True)
# e = tf.exp(e - m) + 1e-3
# val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
def test_LinSpace(self):
t = tf.linspace(0., 10., 15)
self.check(t)
def spatial_expected_softmax(x, temp=1):
assert len(x.get_shape()) == 4
vals = []
for dim in [0, 1]:
dim_val = x.get_shape()[dim + 1].value
lin = tf.linspace(-1.0, 1.0, dim_val)
lin = tf.expand_dims(lin, 1 - dim)
lin = tf.expand_dims(lin, 0)
lin = tf.expand_dims(lin, 3)
m = tf.reduce_max(x, [1, 2], keep_dims=True)
e = tf.exp((x - m) / temp) + 1e-5
val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
vals.append(tf.expand_dims(val, 2))
return tf.reshape(tf.concat(axis=2, values=vals), [-1, x.get_shape()[-1].value * 2])
def get_output_for(self, input, **kwargs):
return spatial_expected_softmax(input)#, self.temp)
# max_ = tf.reduce_max(input, reduction_indices=[1, 2], keep_dims=True)
# exp = tf.exp(input - max_) + 1e-5
# vals = []
#
# for dim in [0, 1]:
# dim_val = input.get_shape()[dim + 1].value
# lin = tf.linspace(-1.0, 1.0, dim_val)
# lin = tf.expand_dims(lin, 1 - dim)
# lin = tf.expand_dims(lin, 0)
# lin = tf.expand_dims(lin, 3)
# m = tf.reduce_max(input, [1, 2], keep_dims=True)
# e = tf.exp(input - m) + 1e-5
# val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
# vals.append(tf.expand_dims(val, 2))
#
# return tf.reshape(tf.concat(axis=2, values=vals), [-1, input.get_shape()[-1].value * 2])
# import ipdb; ipdb.set_trace()
# input.get_shape()
# exp / tf.reduce_sum(exp, reduction_indices=[1, 2], keep_dims=True)
# import ipdb;
# ipdb.set_trace()
# spatial softmax?
# for dim in range(2):
# val = obs.get_shape()[dim + 1].value
# lin = tf.linspace(-1.0, 1.0, val)
# lin = tf.expand_dims(lin, 1 - dim)
# lin = tf.expand_dims(lin, 0)
# lin = tf.expand_dims(lin, 3)
# m = tf.reduce_max(e, [1, 2], keep_dims=True)
# e = tf.exp(e - m) + 1e-3
# val = tf.reduce_sum(e * lin, [1, 2]) / (tf.reduce_sum(e, [1, 2]))
Dense_Transformer_Networks_3D.py 文件源码
项目:3D_Dense_Transformer_Networks
作者: JohnYC1995
项目源码
文件源码
阅读 36
收藏 0
点赞 0
评论 0
def _makeT(self,cp):
with tf.variable_scope('_makeT'):
cp = tf.reshape(cp,(-1,3,self.X_controlP_number*self.Y_controlP_number*self.Z_controlP_number))
cp = tf.cast(cp,'float32')
N_f = tf.shape(cp)[0]
#c_s
x,y,z = tf.linspace(-1.,1.,self.X_controlP_number),tf.linspace(-1.,1.,self.Y_controlP_number),tf.linspace(-1.,1.,self.Z_controlP_number)
x = tf.tile(x,[self.Y_controlP_number*self.Z_controlP_number])
y = tf.tile(self._repeat(y,self.X_controlP_number,'float32'),[self.Z_controlP_number])
z = self._repeat(z,self.X_controlP_number*self.Y_controlP_number,'float32')
xs,ys,zs = tf.transpose(tf.reshape(x,(-1,1))),tf.transpose(tf.reshape(y,(-1,1))),tf.transpose(tf.reshape(z,(-1,1)))
cp_s = tf.concat([xs,ys,zs],0)
cp_s_trans = tf.transpose(cp_s)
# (4*4*4)*3 -> 64 * 3
##===Compute distance R
xs_trans,ys_trans,zs_trans = tf.transpose(tf.stack([xs],axis=2),perm=[1,0,2]),tf.transpose(tf.stack([ys],axis=2),perm=[1,0,2]),tf.transpose(tf.stack([zs],axis=2),perm=[1,0,2])
xs, xs_trans = tf.meshgrid(xs,xs_trans);ys, ys_trans = tf.meshgrid(ys,ys_trans);zs, zs_trans = tf.meshgrid(zs,zs_trans)
Rx,Ry, Rz = tf.square(tf.subtract(xs,xs_trans)),tf.square(tf.subtract(ys,ys_trans)),tf.square(tf.subtract(zs,zs_trans))
R = tf.add_n([Rx,Ry,Rz])
#print("R",sess.run(R))
R = tf.multiply(R,tf.log(tf.clip_by_value(R,1e-10,1e+10)))
#print("R",sess.run(R))
ones = tf.ones([self.Y_controlP_number*self.X_controlP_number*self.Z_controlP_number,1],tf.float32)
ones_trans = tf.transpose(ones)
zeros = tf.zeros([4,4],tf.float32)
Deltas1 = tf.concat([ones, cp_s_trans, R],1)
Deltas2 = tf.concat([ones_trans,cp_s],0)
Deltas2 = tf.concat([zeros,Deltas2],1)
Deltas = tf.concat([Deltas1,Deltas2],0)
#print("Deltas",sess.run(Deltas))
##get deltas_inv
Deltas_inv = tf.matrix_inverse(Deltas)
Deltas_inv = tf.expand_dims(Deltas_inv,0)
Deltas_inv = tf.reshape(Deltas_inv,[-1])
Deltas_inv_f = tf.tile(Deltas_inv,tf.stack([N_f]))
Deltas_inv_f = tf.reshape(Deltas_inv_f,tf.stack([N_f,self.X_controlP_number*self.Y_controlP_number*self.Z_controlP_number+4, -1]))
cp_trans =tf.transpose(cp,perm=[0,2,1])
zeros_f_In = tf.zeros([N_f,4,3],tf.float32)
cp = tf.concat([cp_trans,zeros_f_In],1)
#print("cp",sess.run(cp))
#print("Deltas_inv_f",sess.run(Deltas_inv_f))
T = tf.transpose(tf.matmul(Deltas_inv_f,cp),[0,2,1])
#print("T",sess.run(T))
return T
Dense_Transformer_Networks_3D.py 文件源码
项目:3D_Dense_Transformer_Networks
作者: JohnYC1995
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def _makeT(self,cp):
with tf.variable_scope('_makeT'):
cp = tf.reshape(cp,(-1,3,self.X_controlP_number*self.Y_controlP_number*self.Z_controlP_number))
cp = tf.cast(cp,'float32')
N_f = tf.shape(cp)[0]
#c_s
x,y,z = tf.linspace(-1.,1.,self.X_controlP_number),tf.linspace(-1.,1.,self.Y_controlP_number),tf.linspace(-1.,1.,self.Z_controlP_number)
x = tf.tile(x,[self.Y_controlP_number*self.Z_controlP_number])
y = tf.tile(self._repeat(y,self.X_controlP_number,'float32'),[self.Z_controlP_number])
z = self._repeat(z,self.X_controlP_number*self.Y_controlP_number,'float32')
xs,ys,zs = tf.transpose(tf.reshape(x,(-1,1))),tf.transpose(tf.reshape(y,(-1,1))),tf.transpose(tf.reshape(z,(-1,1)))
cp_s = tf.concat([xs,ys,zs],0)
cp_s_trans = tf.transpose(cp_s)
# (4*4*4)*3 -> 64 * 3
##===Compute distance R
xs_trans,ys_trans,zs_trans = tf.transpose(tf.stack([xs],axis=2),perm=[1,0,2]),tf.transpose(tf.stack([ys],axis=2),perm=[1,0,2]),tf.transpose(tf.stack([zs],axis=2),perm=[1,0,2])
xs, xs_trans = tf.meshgrid(xs,xs_trans);ys, ys_trans = tf.meshgrid(ys,ys_trans);zs, zs_trans = tf.meshgrid(zs,zs_trans)
Rx,Ry, Rz = tf.square(tf.subtract(xs,xs_trans)),tf.square(tf.subtract(ys,ys_trans)),tf.square(tf.subtract(zs,zs_trans))
R = tf.add_n([Rx,Ry,Rz])
#print("R",sess.run(R))
R = tf.multiply(R,tf.log(tf.clip_by_value(R,1e-10,1e+10)))
#print("R",sess.run(R))
ones = tf.ones([self.Y_controlP_number*self.X_controlP_number*self.Z_controlP_number,1],tf.float32)
ones_trans = tf.transpose(ones)
zeros = tf.zeros([4,4],tf.float32)
Deltas1 = tf.concat([ones, cp_s_trans, R],1)
Deltas2 = tf.concat([ones_trans,cp_s],0)
Deltas2 = tf.concat([zeros,Deltas2],1)
Deltas = tf.concat([Deltas1,Deltas2],0)
#print("Deltas",sess.run(Deltas))
##get deltas_inv
Deltas_inv = tf.matrix_inverse(Deltas)
Deltas_inv = tf.expand_dims(Deltas_inv,0)
Deltas_inv = tf.reshape(Deltas_inv,[-1])
Deltas_inv_f = tf.tile(Deltas_inv,tf.stack([N_f]))
Deltas_inv_f = tf.reshape(Deltas_inv_f,tf.stack([N_f,self.X_controlP_number*self.Y_controlP_number*self.Z_controlP_number+4, -1]))
cp_trans =tf.transpose(cp,perm=[0,2,1])
zeros_f_In = tf.zeros([N_f,4,3],tf.float32)
cp = tf.concat([cp_trans,zeros_f_In],1)
#print("cp",sess.run(cp))
#print("Deltas_inv_f",sess.run(Deltas_inv_f))
T = tf.transpose(tf.matmul(Deltas_inv_f,cp),[0,2,1])
#print("T",sess.run(T))
return T