Example #1
0
def depth_to_xyz(depth, input_shape=(1, 224, 224), fov="kinect"):
    """
    Convert depth map to a cartesian point cloud map

    :param depth: a depth map tensor of shape [b, h, w]
    :param focal_lengths: a tuple of focal lengths as (fx, fy)
    :param input_shape: a tuple of input shape as (b, h, w)
    :param fov: which type of camera used one of three ["kinect", "webcam", "intel"]
    :return: a tensor of points in cartesian 3-space [b, h, w, 3]
    """
    x = tf.constant([i - (input_shape[1] // 2)
                     for i in range(input_shape[1])])  # [h,] int32
    x = tf.tile(tf.expand_dims(x, axis=0), (input_shape[2], 1))  # [h, w] int32
    x = tf.expand_dims(x, axis=0)  # [1, h, w] int32
    x = tf.cast(x, tf.float32)
    x = (tf.tan(cfg[fov + "_h_fov"] * pi / 360) /
         (input_shape[2] / 2)) * tf.multiply(x, depth)

    y = tf.constant([i - (input_shape[2] // 2) for i in range(input_shape[2])])
    y = tf.tile(tf.expand_dims(y, axis=-1), (1, input_shape[1]))  # [h, w]
    y = tf.expand_dims(y, axis=0)  # [1, h, w]
    y = tf.cast(y, tf.float32)
    y = (tf.tan(cfg[fov + "_v_fov"] * pi / 360) /
         (input_shape[1] / 2)) * tf.multiply(y, depth)

    z = depth

    x = tf.expand_dims(x, -1)  # [b, h, w, 1]
    y = tf.expand_dims(y, -1)  # [b, h, w, 1]
    z = tf.expand_dims(z, -1)  # [b, h, w, 1]
    p = tf.concat((x, y, z), -1)  # [b, h, w, 3]
    return p
Example #2
0
def shears_to_projective_transforms(shears, height, width):
    """Returns shear transform matrices for a batched input.

  The shear is applied relative to the image center, instead of (0, 0).

  Args:
    shears: 2-element tensor [sx, sy], or a [B, 2]-tensor reprenting a batch of
      such inputs. `sx` and `sy` are the shear angle (in radians) in x and y
      respectively.
    height: Image height, in pixels.
    width: Image width, in pixels.

  Returns:
    A [B, 8]-tensor representing the transform that can be passed to
    `tensorflow_addons.image.transform`.
  """
    shears = tf.convert_to_tensor(shears)
    if tf.rank(shears) == 1:
        shears = shears[None, :]
    shears_x = tf.reshape(tf.tan(shears[:, 0]), (-1, 1))
    shears_y = tf.reshape(tf.tan(shears[:, 1]), (-1, 1))
    ones = tf.ones_like(shears_x)
    zeros = tf.zeros_like(shears_x)
    transform = tf.concat(
        [ones, shears_x, zeros, shears_y, ones, zeros, zeros, zeros], axis=-1)
    return _center_transform(transform, height, width)
Example #3
0
def perspectivey_grid(output_size,
                      ulim=(1, 8),
                      vlim=(-0.99 * np.pi / 2, 0.99 * np.pi / 2)):
    """Vertical perspective coordinate system.

    Args:
        output_size: (int, int), number of sampled values for the v-coordinate and u-coordinate respectively
        ulim: (float, float), y^{-1} "radial" coordinate limits
        vlim: (float, float), angular coordinate limits

    Returns:
        tf.Tensor, type tf.float32, shape (output_size[0], output_size[1], 2),
        tensor where entry (i, j) gives the (x, y) coordinate of the grid point.
    """
    nv, nu = output_size[0], output_size[1]
    vs, us = _grid_prepare((nv // 2, nu), ulim, vlim)

    yl = -tf.math.reciprocal(tf.reverse(us, [1]))
    yr = tf.math.reciprocal(us)
    xl = -yl * tf.tan(vs)
    xr = yr * tf.tan(vs)

    if nv % 2 == 0:
        xs = tf.concat([xl, xr], axis=0)
        ys = tf.concat([yl, yr], axis=0)
    else:
        xs = tf.concat([xl, xl[-1:], xr], axis=0)
        ys = tf.concat([yl, yl[-1:], yr], axis=0)
    return tf.stack([xs, ys], 2)
Example #4
0
    def angular_loss_2(y_true, y_pred):
        y_pred = K.clip(y_pred, _EPSILON, 1.0 - _EPSILON)
        loss = tf.convert_to_tensor(0, dtype=tf.float32)
        g = tf.constant(1.0, shape=[1], dtype=tf.float32)
        c = tf.constant(4.0, shape=[1], dtype=tf.float32)
        d = tf.constant(2.0, shape=[1], dtype=tf.float32)
        alpha = tf.constant(45.0, shape=[1], dtype=tf.float32)

        losses = []
        losses2 = []
        for i in range(0, batch_size, 3):
            try:
                xa = y_pred[i + 0]
                xp = y_pred[i + 1]
                xn = y_pred[i + 2]

                fapn = c * (tf.tan(alpha * K.transpose(xa + xp) * xn)**
                            2) - d * (g +
                                      tf.tan(alpha)**2) * K.transpose(xa) * xp
                losses.append(fapn)

                losses2.append(K.transpose(xa) * xn - K.transpose(xa) * xp)

                loss = (loss + g + _loss)
            except:
                continue
        loss = K.sum(K.log(1 + 2 * K.sum([K.exp(v) for v in losses])))
        loss2 = K.sum(K.log(1 + 2 * K.sum([K.exp(v) for v in losses2])))
        loss = loss + 2 * loss2
        loss = loss / (batch_size / 3)
        zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
        return tf.maximum(loss, zero)
Example #5
0
 def disparity_to_depth(self, disparity, position, epsilon=1e-6):
     baseline_distance = self.params.baseline
     S, T = lat_long_grid([tf.shape(disparity)[1], tf.shape(disparity)[2]])
     _, T_grids = self.expand_grids(S, -T, tf.shape(disparity)[0])
     if position == "top":
         t1 = tf.tan(T_grids)
         t2 = tf.tan(T_grids + np.pi * disparity)
     else:
         t1 = tf.tan(T_grids)
         t2 = tf.tan(T_grids - np.pi * disparity)
     return baseline_distance / (tf.abs(t2 - t1) + epsilon)
Example #6
0
 def attenuate_equirectangular(self, disparity, position):
     S, T = lat_long_grid([tf.shape(disparity)[1], tf.shape(disparity)[2]])
     _, T_grids = self.expand_grids(S, -T, tf.shape(disparity)[0])
     if position == "top":
         attenuated_disparity = (1.0 / np.pi) * (
             tf.atan(tf.tan(np.pi * disparity) + tf.tan(T_grids)) - T_grids)
     else:
         attenuated_disparity = (1.0 / np.pi) * (
             T_grids - tf.atan(tf.tan(T_grids) - tf.tan(np.pi * disparity)))
     return tf.clip_by_value(
         tf.where(tf.is_finite(attenuated_disparity), attenuated_disparity,
                  tf.zeros_like(attenuated_disparity)), 1e-6, 0.75)
Example #7
0
 def depth_to_disparity(self, depth, position):
     baseline_distance = self.params.baseline
     S, T = lat_long_grid([tf.shape(depth)[1], tf.shape(depth)[2]])
     _, T_grids = self.expand_grids(S, T, tf.shape(depth)[0])
     if position == "top":
         return self.disparity_scale * (np.pi / 2.0 - atan2(
             baseline_distance * depth, (1.0 + tf.tan(-T_grids)**2.0) *
             (depth**2.0) + baseline_distance * depth * tf.tan(-T_grids)))
     else:
         return self.disparity_scale * (atan2(
             baseline_distance * depth, (1.0 + tf.tan(-T_grids)**2.0) *
             (depth**2.0) - baseline_distance * depth * tf.tan(-T_grids)) -
                                        np.pi / 2.0)
Example #8
0
    def camera_perspective(self, aspect_ratio, fov_y, near_clip, far_clip):
        """Computes perspective transformation matrices.

        Functionality mimes gluPerspective (third_party/GL/glu/include/GLU/glu.h).

        Args:
          aspect_ratio: float value specifying the image aspect ratio (width/height).
          fov_y: 1-D float32 Tensor with shape [batch_size] specifying output vertical
              field of views in degrees.
          near_clip: 1-D float32 Tensor with shape [batch_size] specifying near
              clipping plane distance.
          far_clip: 1-D float32 Tensor with shape [batch_size] specifying far clipping
              plane distance.

        Returns:
          A [batch_size, 4, 4] float tensor that maps from right-handed points in eye
          space to left-handed points in clip space.
        """
        # The multiplication of fov_y by pi/360.0 simultaneously converts to radians
        # and adds the half-angle factor of .5.
        focal_lengths_y = 1.0 / tf.tan(fov_y * (math.pi / 360.0))
        depth_range = far_clip - near_clip
        p_22 = -(far_clip + near_clip) / depth_range
        p_23 = -2.0 * (far_clip * near_clip / depth_range)

        zeros = tf.zeros_like(p_23, dtype=tf.float32)
        perspective_transform = tf.concat([
            focal_lengths_y / aspect_ratio, zeros, zeros, zeros, zeros,
            focal_lengths_y, zeros, zeros, zeros, zeros, p_22, p_23, zeros,
            zeros, -tf.ones_like(p_23, dtype=tf.float32), zeros
        ],
                                          axis=0)
        perspective_transform = tf.reshape(perspective_transform, [4, 4, -1])

        return tf.transpose(perspective_transform, [2, 0, 1])
Example #9
0
def coherence_loss(phase, target):
    # This loss function refers to wiki page of Directional Statistics
    # So first you have to find Re^(i*theta) within the group
    # not tough to implement
    mask = tf.cast(target, tf.float32)
    masked_phase = tf.multiply(tf.expand_dims(phase, axis=1), mask)

    # self.masked_phase.shape=(batch, groups, N)
    sin_vec = tf.sin(masked_phase)
    cos_vec = tf.where(tf.equal(mask, tf.constant(0.)), masked_phase,
                       tf.cos(masked_phase))

    sin_mean = tf.div_no_nan(tf.reduce_sum(sin_vec, axis=2),
                             tf.reduce_sum(mask, axis=2))
    cos_mean = tf.div_no_nan(tf.reduce_sum(cos_vec, axis=2),
                             tf.reduce_sum(mask, axis=2))

    std = tf.sqrt(
        -2 * log_no_nan(tf.sqrt(tf.square(sin_mean) + tf.square(cos_mean))))
    sync_loss = mean_no_zero(std, axis=1)
    # sync_loss.shape=(batch,)

    desync_loss = tf.tan(
        tf.sqrt(
            tf.square(mean_no_zero(sin_mean, axis=1)) +
            tf.square(mean_no_zero(cos_mean, axis=1))))

    # desync_loss.shape=(batch,)
    sync_loss_mean = tf.reduce_mean(sync_loss)
    desync_loss_mean = tf.reduce_mean(desync_loss)
    tot_loss_mean = 0.2 * sync_loss_mean + desync_loss_mean
    return sync_loss_mean, desync_loss_mean, tot_loss_mean
Example #10
0
 def fov(self, value):
     self._fov = value
     fov_factor = 1.0 / tf.tan(transform.radians(0.5 * self._fov))
     o = tf.ones([1], dtype=tf.float32)
     diag = tf.concat([fov_factor, fov_factor, o], 0)
     self._cam_to_ndc = tf.diag(diag)
     self.ndc_to_cam = tf.linalg.inv(self._cam_to_ndc)
Example #11
0
def get_unary_op(x, option):
    unary_ops = {
        'log': tf.log(x),
        'exp': tf.exp(x),
        'neg': tf.negative(x),
        'ceil': tf.ceil(x),
        'floor': tf.floor(x),
        'log1p': tf.log1p(x),
        'sqrt': tf.sqrt(x),
        'square': tf.square(x),
        'abs': tf.abs(x),
        'relu': tf.nn.relu(x),
        'elu': tf.nn.elu(x),
        'selu': tf.nn.selu(x),
        'leakyRelu': tf.nn.leaky_relu(x),
        'sigmoid': tf.sigmoid(x),
        'sin': tf.sin(x),
        'cos': tf.cos(x),
        'tan': tf.tan(x),
        'asin': tf.asin(x),
        'acos': tf.acos(x),
        'atan': tf.atan(x),
        'sinh': tf.sinh(x),
        'cosh': tf.cosh(x),
        'tanh': tf.tanh(x),
    }

    assert option in unary_ops, 'Unary option not found: ' + option
    return unary_ops[option]
    def model_v1(self, inpt):

        self.input = tf.Variable(
            initial_value=inpt,
            dtype=tf.float32,
            shape=[None, None, None, 3 + self.num_classes],
            trainable=False)

        current_input = self.input

        self.predictions = []
        self.logits = []

        for n_layer in range(self.num_layers):

            h_conv1 = self.conv2d(current_input, self.w_conv1,
                                  1) + self.b_conv1
            h_pool1 = self.maxpool(h_conv1, 2, 2)

            tanh1 = tf.tan(h_pool1)

            h_conv2 = self.conv2d(tanh1, self.w_conv2, 1) + self.b_conv2
            h_pool2 = self.maxpool(h_conv2, 2, 2)
            tanh2 = tf.tanh(h_pool2)

            logits = self.conv2d(tanh2, self.w_conv3, 1) + self.b_conv3
            predictions = tf.nn.softmax(logits, axis=None, name=None)

            self.logits.append(logits)
            self.predictions.append(predictions)

            rgb = tf.strided_slice(current_input, [0, 0, 0, 0], [0, 0, 0, 3],
                                   strides=[1, 4, 4, 1],
                                   end_mask=7)
            current_input = tf.concat(values=[rgb, predictions], axis=3)
Example #13
0
    def test_cwise_unary_grad(self):
        """
        Ensure that all component-wise unary functions in the math op library yield an identical gradient to tensorflow
        """
        test_config = tf.ConfigProto(allow_soft_placement=False)
        test_config.graph_options.optimizer_options.opt_level = -1
        with tf.Session(config=test_config) as s:
            arg_np = np.random.random(100)
            grad_above = tf.constant(np.random.random(100))

            arg = tf.constant(arg_np)

            def test_grad(fcn, tf_fcn):
                ovl_out = as_tensorflow(fcn(arg))
                tf_out = tf_fcn(arg)

                ovl_grad = tf.gradients(ovl_out, arg, grad_above)[0]
                tf_grad = tf.gradients(tf_out, arg, grad_above)[0]
                ovl_out, tf_out, ovl_grad, tf_grad = s.run([ovl_out, tf_out, ovl_grad, tf_grad])

                assert np.allclose(ovl_out, tf_out)
                assert np.allclose(ovl_grad, tf_grad)

            test_grad(lambda x: neg(x), lambda x: tf.neg(x))
            test_grad(lambda x: tanh(x), lambda x: tf.tanh(x))
            test_grad(lambda x: sin(x), lambda x: tf.sin(x))
            test_grad(lambda x: cos(x), lambda x: tf.cos(x))
            test_grad(lambda x: tan(x), lambda x: tf.tan(x))
            test_grad(lambda x: sigmoid(x), lambda x: tf.sigmoid(x))
Example #14
0
 def test_tan(self):
     shape = [3, 4, 5]
     graph = tf.Graph()
     with graph.as_default():
         a = tf.placeholder(tf.float32, shape=shape)
         out = tf.tan(a)
     self._test_tf_model_constant(graph, {a.op.name: shape}, [out.op.name])
Example #15
0
def backproject(S, T, depth):
    # Convert to Cartesian for modified depth input.
    # depth = sqrt(x^2 + z^2).
    x = depth * tf.sin(S)
    y = depth * tf.tan(T)
    z = depth * tf.cos(S)
    return x, y, z
Example #16
0
def logMap(SE3):
    '''logarithmic mapping
    :param SE3: [B, 4, 4]
    :return: ksai [B, 6], trn after rot
    '''
    def deltaR(R):
        '''
        :param R:  [B, 3, 3]
        :return:
        '''
        v_0 = R[:, 2, 1] - R[:, 1, 2]
        v_1 = R[:, 0, 2] - R[:, 2, 0]
        v_2 = R[:, 1, 0] - R[:, 0, 1]
        v = tf.stack([v_0, v_1, v_2], axis=-1)
        return v

    batch, _, _ = SE3.get_shape().as_list()
    _R = SE3[:, :3, :3]
    _t = SE3[:, :3, 3:]
    d = 0.5 * (_R[:, 0, 0] + _R[:, 1, 1] + _R[:, 2, 2] - 1)
    d = tf.expand_dims(d, axis=-1)
    dR = deltaR(_R)
    theta = tf.acos(d)
    omega = theta * dR / (2 * tf.sqrt(1 - d * d))
    Omega = skew(omega)
    identities = tf.tile([tf.eye(3)], multiples=[batch, 1, 1])
    V_inv = identities - 0.5 * Omega + \
            ( 1 - theta / ( 2 * tf.tan ( theta/2 ) ) ) * tf.matmul( Omega , Omega ) / (theta * theta)
    upsilon = tf.matmul(V_inv, _t)
    upsilon = tf.reshape(upsilon, [batch, -1])
    ksai = tf.concat([omega, upsilon], axis=-1)
    return ksai
Example #17
0
 def _build(self, inputs):
     net = build_common_network('critic', inputs)
     net = BatchApply(linear('input_layer', 128))(net)
     net = tf.tanh(net)
     net = BatchApply(linear('output_layer', 1))(net)
     net = tf.tan(net)
     return tf.reduce_mean(net, axis=1)
Example #18
0
    def test_cwise_unary_grad(self):
        """
        Ensure that all component-wise unary functions in the math op library yield an identical gradient to tensorflow
        """
        test_config = tf.ConfigProto(allow_soft_placement=False)
        test_config.graph_options.optimizer_options.opt_level = -1
        with tf.Session(config=test_config) as s:
            arg_np = np.random.random(100)
            grad_above = tf.constant(np.random.random(100))

            arg = tf.constant(arg_np)

            def test_grad(fcn, tf_fcn):
                ovl_out = as_tensorflow(fcn(arg))
                tf_out = tf_fcn(arg)

                ovl_grad = tf.gradients(ovl_out, arg, grad_above)[0]
                tf_grad = tf.gradients(tf_out, arg, grad_above)[0]
                ovl_out, tf_out, ovl_grad, tf_grad = s.run(
                    [ovl_out, tf_out, ovl_grad, tf_grad])

                assert np.allclose(ovl_out, tf_out)
                assert np.allclose(ovl_grad, tf_grad)

            test_grad(lambda x: neg(x), lambda x: tf.neg(x))
            test_grad(lambda x: tanh(x), lambda x: tf.tanh(x))
            test_grad(lambda x: sin(x), lambda x: tf.sin(x))
            test_grad(lambda x: cos(x), lambda x: tf.cos(x))
            test_grad(lambda x: tan(x), lambda x: tf.tan(x))
            test_grad(lambda x: sigmoid(x), lambda x: tf.sigmoid(x))
Example #19
0
def generate_vl(w=3264, h=2448):

    # d = w/(2*tf.tan(alpha)), alpha = 33/180*pi
    d = w / 1.29876
    view_pos = tf.constant([w / 2, h / 2, d], dtype=tf.float32, shape=[1, 3])
    view_pos = tf.expand_dims(view_pos, axis=1)

    wgrid = tf.linspace(0.0, w, w)
    hgrid = tf.linspace(0.0, h, h)
    plane_coor = tf.concat(tf.meshgrid(wgrid, hgrid, 0.0), axis=2)
    planc_expand = tf.expand_dims(plane_coor, axis=0)  #[1, h, w, 3]

    xy = planc_expand[:, :, :, 0:2]
    t = tf.sqrt(
        tf.reduce_sum(tf.square(
            xy - tf.constant([w / 2, h / 2], dtype=tf.float32, shape=[1, 2])),
                      keepdims=True,
                      axis=-1))
    I = tf.exp(tf.square(tf.tan(t / d * 1.6)) * (-0.5))
    I = tf.concat([I, I, I], axis=-1)

    view_pos_expand = tf.expand_dims(view_pos, axis=1)
    view_vec = view_pos_expand - planc_expand

    view_norm = tf.sqrt(tf.reduce_sum(tf.square(view_vec), axis=-1))
    view_expand = tf.expand_dims(view_norm, axis=-1)
    newview_vec = tf.concat([view_expand, view_expand, view_expand], axis=-1)
    eview_vec = view_vec / newview_vec

    return eview_vec, I
Example #20
0
def look_at_origin(fow):
    """
	This function gives some useful intrinsic and 
	extrinsic camera matrices K and T.
	When you render a unit ball using K and T, you will get
	a circle that tightly fits into the image.

	Input:
	fow: [B] tf.float32, field of views in radians, where 0<fow<pi/2
		for each entry

	Output:
	K: [B, 3, 3] tf.float32, intrinsic camera matrices
		The 3D points [0,-1,f] and [0,1,f] are projected to [0,-1] and [0,1].
		The render will convert the 2D coordinates into pixel coordinates
		depending on the resolution.
	T: [B, 3, 4] tf.float32, rigid transformation matrices
		It moves the origin to the focal plane.
	"""

    f = 1. / tf.tan(fow / 2.)  # focal length
    _0 = tf.zeros_like(f)
    _1 = tf.ones_like(f)
    K = tf.reshape(tf.stack([f, _0, _0, _0, f, _0, _0, _0, _1], -1),
                   [-1, 3, 3])

    T = tf.reshape(tf.stack([_1, _0, _0, _0, _0, _1, _0, _0, _0, _0, _1, f]),
                   [-1, 3, 4])

    return K, T
Example #21
0
    def __init__(self, learning_rate):

        self.learning_rate = learning_rate

        self.tv_select = tf.placeholder(tf.int32, shape=[], name="tv_select")

        perturb_state = []

        # Iterate over each prior Variable and trainable variable pair...
        for tv_num, v in enumerate(tf.trainable_variables()):

            # TODO: Determine if conv ops have 0s we're overwriting

            do = tf.tan(np.pi * (tf.random_uniform(v.get_shape()) - 0.5))

            do_not = tf.zeros(v.get_shape())

            p = tf.cond(tf.equal(self.tv_select, tv_num), lambda: do,
                        lambda: do_not)

            scaled_p = tf.multiply(self.learning_rate,
                                   p,
                                   name="make_perturbation")

            # Add an op that perturbs this trainable var
            perturb_state.append(
                tf.assign_add(v, scaled_p, name="perturb_state_rlfsa"))

        self.perturb_state = perturb_state
def matern_12(points):
    """
    Matern 12
    Ref: Sampling Student''s T distribution-use of the inverse cumulative distribution function
    :param points:
    :return:
    """
    return tf.tan(np.pi * (points - 0.5))
Example #23
0
def get_projection_matrix():

    near_plane_distance = 0.05
    fov_angle = 100. * np.pi / 180.
    near_plane_right = near_plane_distance * tf.tan(fov_angle / 2.)
    return dirt.matrices.perspective_projection(
        near_plane_distance, far=100., right=near_plane_right,
        aspect=72 / 96).numpy()  # :: x/y/x/z (in), x/y/z/w (out)
def test07():
    sess = tf.Session()
    d0 = tf.Variable(1, dtype=tf.float32, name='number1')
    d1 = tf.tan(d0)
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    B = sess.run(d1)
    print(B)
def angular_loss(y_true, y_pred):
    alpha = K.constant(ALPHA)
    a_p = y_pred[:, 0, 0]
    n_c = y_pred[:, 1, 0]
    return K.mean(
        K.maximum(
            K.constant(0),
            K.square(a_p) -
            K.constant(4) * K.square(tf.tan(alpha)) * K.square(n_c)))
Example #26
0
 def fov(self, value):
     with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())):
         self._fov = tf.identity(value).cpu()
         fov_factor = 1.0 / tf.tan(transform.radians(0.5 * self._fov))
         o = tf.convert_to_tensor(np.ones([1], dtype=np.float32),
                                  dtype=tf.float32)
         diag = tf.concat([fov_factor, fov_factor, o], 0)
         self._cam_to_ndc = tf.linalg.tensor_diag(diag)
         self.ndc_to_cam = tf.linalg.inv(self._cam_to_ndc)
def steer_to_yawrate(steer, v):
    '''
    Use Ackerman formula to compute yawrate from steering angle and forward
    velocity v:
      r = wheelbase / tan(steer)
      omega = v / r
    '''
    # assert steer.get_shape().as_list() == v.get_shape().as_list(), "steer = {}, v = {}".format(steer, v)
    # print steer, v
    return v * tf.tan(steer) / FLAGS.wheelbase
Example #28
0
def get_focal_length(dim_length, fov, angle_units='rad'):
    with tf.name_scope('focal_length'):
        if angle_units == 'deg':
            angle = fov * math.pi / 360
        elif angle_units == 'rad':
            angle = fov / 2
        else:
            raise KeyError('Invalid angle_units')
        focal_length = dim_length / (2*tf.tan(angle))
    return focal_length
Example #29
0
    def __call__(self, x):

        x = tf.cast(x, tf.float32)
        batch_size = tf.shape(x)[0]
        ref_conf = tf.gather(x, self.ref_index, axis=1)
        mob_conf = tf.gather(x, self.mob_index, axis=1)
        ref_T = calcTransformMatrix_tf(self.ini_ref_conf, ref_conf,
                                       self.ref_weights)  # shape (B, 4, 4)

        ini_mob_conf = tf.gather(tf.reshape(
            applyTransformMatrix_tf(ref_T, self.ini_conf), [batch_size, -1]),
                                 self.mob_index,
                                 axis=1)
        com1 = calcCOM_tf(ini_mob_conf, self.mob_weights)  # shape (B, 1, 3)
        com2 = calcCOM_tf(mob_conf, self.mob_weights)  # shape (B, 1, 3)
        pl = (com2 - com1) / tf.linalg.norm(com2 - com1, axis=2,
                                            keepdims=True)  # shape (B, 1, 3)

        mob_T = calcTransformMatrix_tf(ini_mob_conf, mob_conf,
                                       self.mob_weights)  # shape (B, 4, 4)
        t21 = tf.reshape(tf.tile(tf.eye(3), [batch_size, 1]),
                         [batch_size, 3, 3])
        t21 = tf.concat([t21, tf.reshape(com1 - com2, [batch_size, 3, 1])],
                        axis=2)
        t21 = tf.concat([
            t21,
            tf.tile(tf.constant([[[0., 0., 0., 1.]]]), [batch_size, 1, 1])
        ],
                        axis=1)
        rot2 = tf.matmul(mob_T, t21)

        p1 = applyTransformMatrix_tf(rot2, com1)  # shape (B, 1, 3)
        p2 = applyTransformMatrix_tf(rot2, p1)  # shape (B, 1, 3)
        rideal = tf.cross((com1 - p2), (com1 - p1))
        rideal = rideal / tf.linalg.norm(rideal, axis=2,
                                         keepdims=True)  # shape (B, 1, 3)
        new = com2 - tf.matmul(rideal, tf.transpose(
            com2 - com1, [0, 2, 1])) * rideal  # shape (B, 1, 3)

        cosine = tf.matmul(
            (new - com1) / tf.linalg.norm(new - com1, axis=2, keepdims=True),
            tf.transpose(
                (new - p1) / tf.linalg.norm(new - p1, axis=2, keepdims=True),
                [0, 2, 1]))
        angl = tf.acos(cosine)

        perp = tf.matmul(rideal, tf.transpose(pl,
                                              [0, 2, 1]))  # shape (B, 1, 1)
        angp = tf.abs(tf.asin(perp))  # shape (B, 1, 1)
        pro = rideal - perp * pl  # shape (B, 1, 3)

        tang = tf.cos(angp) * tf.tan(0.5 * angl)  # shape (B, 1, 1)
        angle = tf.reshape(2.0 * tf.atan(tang),
                           [-1]) * 180.0 / np.pi  # 度数に変換している
        return angle
Example #30
0
def gen_perspective_matrix(fov, clip_near, clip_far):
    clip_dist = clip_far - clip_near
    cot = 1 / tf.tan(radians(fov / 2.0))
    z = tf.constant(np.zeros([1]), dtype=tf.float32)
    o = tf.convert_to_tensor(np.ones([1], dtype=np.float32), dtype=tf.float32)
    return tf.stack([
        tf.concat([cot, z, z, z], 0),
        tf.concat([z, cot, z, z], 0),
        tf.concat([z, z, 1 / clip_dist, -clip_near / clip_dist], 0),
        tf.concat([z, z, o, z], 0)
    ])
Example #31
0
 def fov(self, value):
     if value is not None:
         self._fov = value
         with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())):
             fov_factor = 1.0 / tf.tan(transform.radians(0.5 * self._fov))
             o = tf.ones([1], dtype=tf.float32)
             diag = tf.concat([fov_factor, fov_factor, o], 0)
             self._intrinsic_mat = tf.linalg.tensor_diag(diag)
             self.intrinsic_mat_inv = tf.linalg.inv(self._intrinsic_mat)
     else:
         self._fov = None
Example #32
0
 def _quantile(self, p):
   return self.loc + self.scale * tf.tan(np.pi * (p - 0.5))
def run():
  """Run custom scalar demo and generate event files."""
  step = tf.placeholder(tf.float32, shape=[])

  with tf.name_scope('loss'):
    # Specify 2 different loss values, each tagged differently.
    summary_lib.scalar('foo', tf.pow(0.9, step))
    summary_lib.scalar('bar', tf.pow(0.85, step + 2))

    # Log metric baz as well as upper and lower bounds for a margin chart.
    middle_baz_value = step + 4 * tf.random_uniform([]) - 2
    summary_lib.scalar('baz', middle_baz_value)
    summary_lib.scalar('baz_lower',
                       middle_baz_value - 6.42 - tf.random_uniform([]))
    summary_lib.scalar('baz_upper',
                       middle_baz_value + 6.42 + tf.random_uniform([]))

  with tf.name_scope('trigFunctions'):
    summary_lib.scalar('cosine', tf.cos(step))
    summary_lib.scalar('sine', tf.sin(step))
    summary_lib.scalar('tangent', tf.tan(step))

  merged_summary = tf.summary.merge_all()

  with tf.Session() as sess, tf.summary.FileWriter(LOGDIR) as writer:
    # We only need to specify the layout once (instead of per step).
    layout_summary = summary_lib.custom_scalar_pb(
        layout_pb2.Layout(category=[
            layout_pb2.Category(
                title='losses',
                chart=[
                    layout_pb2.Chart(
                        title='losses',
                        multiline=layout_pb2.MultilineChartContent(
                            tag=[r'loss(?!.*margin.*)'],)),
                    layout_pb2.Chart(
                        title='baz',
                        margin=layout_pb2.MarginChartContent(
                            series=[
                                layout_pb2.MarginChartContent.Series(
                                    value='loss/baz/scalar_summary',
                                    lower='loss/baz_lower/scalar_summary',
                                    upper='loss/baz_upper/scalar_summary'
                                ),
                            ],)),
                ]),
            layout_pb2.Category(
                title='trig functions',
                chart=[
                    layout_pb2.Chart(
                        title='wave trig functions',
                        multiline=layout_pb2.MultilineChartContent(
                            tag=[
                                r'trigFunctions/cosine', r'trigFunctions/sine'
                            ],)),
                    # The range of tangent is different. Give it its own chart.
                    layout_pb2.Chart(
                        title='tan',
                        multiline=layout_pb2.MultilineChartContent(
                            tag=[r'trigFunctions/tangent'],)),
                ],
                # This category we care less about. Make it initially closed.
                closed=True),
        ]))
    writer.add_summary(layout_summary)

    for i in xrange(42):
      summary = sess.run(merged_summary, feed_dict={step: i})
      writer.add_summary(summary, global_step=i)
Example #34
0
 def tan(self,x):
     return tf.tan(x)
Example #35
0
 def _quantile(self, p):
   return self.loc + self.scale * tf.tan((np.pi / 2) * p)
# div() vs truediv() vs floordiv()
print(sess.run(tf.div(3, 4)))
print(sess.run(tf.truediv(3, 4)))
print(sess.run(tf.floordiv(3.0, 4.0)))

# Mod function
print(sess.run(tf.mod(22.0, 5.0)))

# Cross Product
print(sess.run(tf.cross([1., 0., 0.], [0., 1., 0.])))

# Trig functions
print(sess.run(tf.sin(3.1416)))
print(sess.run(tf.cos(3.1416)))
print(sess.run(tf.tan(3.1416/4.)))

# Custom operation
test_nums = range(15)


def custom_polynomial(x_val):
    # Return 3x^2 - x + 10
    return tf.subtract(3 * tf.square(x_val), x_val) + 10

print(sess.run(custom_polynomial(11)))

# What should we get with list comprehension
expected_output = [3*x*x-x+10 for x in test_nums]
print(expected_output)