def map_label_to_angle_32(label):
    """
        return a list of tensors, each is the rotation angle corresponding to label [l]
    """
    batch_size = label.shape[0]
    label = tf.dtypes.cast(label, dtype=tf.float32)
    label = tf.split(label, batch_size, 0)
    label = [tf.squeeze(l) for l in label]
    angles = []
    for l in label:
        cond0 = tm.equal(tflt(0), l)
        cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(5)))
        cond2 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(10)))
        cond3 = tm.logical_and(tm.less_equal(tflt(11), l), tm.less_equal(l, tflt(20)))
        cond4 = tm.logical_and(tm.less_equal(tflt(21), l), tm.less_equal(l, tflt(25)))
        cond5 = tm.logical_and(tm.less_equal(tflt(26), l), tm.less_equal(l, tflt(30)))
        cond6 = tm.equal(tflt(31), l)

        call0= re(0., 0., 0.)
        call1= re(0.646, (l-1)*np.pi/2.5, 0)
        call2 = re(1.108, (l*2-11)*np.pi/5, 0)
        call3 = re(np.pi/2, (l-11)*np.pi/5, 0)
        call4 = re(2.033, (l*2-11)*np.pi/5, 0)
        call5 = re(2.496, (l-26)*np.pi/2.5, 0)
        call6 = re(np.pi, 0, 0)

        conditions = [cond0, cond1, cond2, cond3, cond4, cond5, cond6]
        callables = [call0, call1, call2, call3, call4, call5, call6]
        angle = nested_tf_cond(conditions, callables)
        angles.append(angle)
    return angles
def map_label_to_angle_18(label):
    """
        return a list of tensors, each is the rotation angle corresponding to label [l]
    """
    batch_size = label.shape[0]
    label = tf.dtypes.cast(label, dtype=tf.float32)
    label = tf.split(label, batch_size, 0)
    label = [tf.squeeze(l) for l in label]
    angles = []
    for l in label:
        cond0 = tm.equal(tflt(0), l)
        cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(3)))
        cond2 = tm.logical_and(tm.less_equal(tflt(4), l), tm.less_equal(l, tflt(5)))
        cond3 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(9)))
        cond4 = tm.logical_and(tm.less_equal(tflt(10), l), tm.less_equal(l, tflt(13)))
        cond5 = tm.logical_and(tm.less_equal(tflt(14), l), tm.less_equal(l, tflt(17)))

        call0= re(0., 0., 0.)
        call1= re(l*np.pi/2, 0, 0)
        call2 = re(0, 0, (l*2-7)*np.pi/2)
        call3 = re((l*2-11)*np.pi/4, 0, 0)
        call4 = re(0, 0, (l*2-19)*np.pi/4)
        call5 = re(np.pi/2, 0, (l*2-27)*np.pi/4)

        conditions = [cond0, cond1, cond2, cond3, cond4, cond5]
        callables = [call0, call1, call2, call3, call4, call5]
        angle = nested_tf_cond(conditions, callables)
        angles.append(angle)
    return angles
 def update(self, features, labels, pred_mean):
     test_for_ybar0_s1 = tfm.logical_and(
         tfm.equal(features['ybar'], 0), tfm.equal(features['sensitive'],
                                                   1))
     accepted = tf.gather_nd(tf.cast(pred_mean < 0.5, tf.float32),
                             tf.where(test_for_ybar0_s1))
     self.mean(accepted)
Esempio n. 4
0
 def update(self, features, labels, pred_mean):
     test_for_ybar1_s1 = tfm.logical_and(
         tfm.equal(features['ybar'], 1), tfm.equal(features['sensitive'],
                                                   1))
     accepted = tft.gather_nd(tf.cast(pred_mean > 0.5, tf.float32),
                              tf.where(test_for_ybar1_s1))
     return self._return_and_store(self.mean(accepted))
def rotate_tensor_by_label_32(batch_data, label, graph):
    """ Rotate a batch of points by the label
        32 possible directions:
            vertices of a regular icosahedron
            vertices of a regular dodecahedron
        Input:
          BxNx3 array
        Return:
          BxNx3 array
    """
    with graph.as_default():
        batch_size = batch_data.get_shape().as_list()[0]
        rotated_data = [None] * batch_size
        splits = tf.split(batch_data, batch_size, 0)
        label = tf.dtypes.cast(label, dtype=tf.float32)
        label = tf.split(label, batch_size, 0)
        label = [tf.squeeze(l) for l in label]
        for k in range(batch_size):
            shape_pc = splits[k] 
            l = label[k]
            cond0 = tm.equal(tflt(0), l)
            cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(5)))
            cond2 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(10)))
            cond3 = tm.logical_and(tm.less_equal(tflt(11), l), tm.less_equal(l, tflt(20)))
            cond4 = tm.logical_and(tm.less_equal(tflt(21), l), tm.less_equal(l, tflt(25)))
            cond5 = tm.logical_and(tm.less_equal(tflt(26), l), tm.less_equal(l, tflt(30)))
            cond6 = tm.equal(tflt(31), l)

            call0= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=0)
            call1= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=0.646, angle_y=(l-1)*np.pi/2.5)
            call2 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=1.108, angle_y=(l*2-11)*np.pi/5)
            call3 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/2, angle_y=(l-11)*np.pi/5)
            call4 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=2.033, angle_y=(l*2-11)*np.pi/5)
            call5 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=2.496, angle_y=(l-26)*np.pi/2.5)
            call6 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi)

            conditions = [cond0, cond1, cond2, cond3, cond4, cond5, cond6]
            callables = [call0, call1, call2, call3, call4, call5, call6]
            shape_pc = nested_tf_cond(conditions, callables)

            rotated_data[k] = shape_pc

        rotated_data = tf.squeeze(tf.stack(rotated_data, axis=0))
        return rotated_data
def rotate_tensor_by_label(batch_data, label, graph):
    """ Rotate a batch of points by [label] to 6 or 18 possible angles
        [label] is a tensor
        Input:
          BxNx3 array
        Return:
          BxNx3 array
        
    """
    with graph.as_default():
        batch_size = batch_data.get_shape().as_list()[0]
        rotated_data = [None] * batch_size
        splits = tf.split(batch_data, batch_size, 0)
        label = tf.dtypes.cast(label, dtype=tf.float32)
        label = tf.split(label, batch_size, 0)
        label = [tf.squeeze(l) for l in label]
        for k in range(batch_size):
            shape_pc = splits[k] 
            l = label[k]
            cond0 = tm.equal(tflt(0), l)
            cond1 = tm.logical_and(tm.less_equal(tflt(1), l), tm.less_equal(l, tflt(3)))
            cond2 = tm.logical_and(tm.less_equal(tflt(4), l), tm.less_equal(l, tflt(5)))
            cond3 = tm.logical_and(tm.less_equal(tflt(6), l), tm.less_equal(l, tflt(9)))
            cond4 = tm.logical_and(tm.less_equal(tflt(10), l), tm.less_equal(l, tflt(13)))
            cond5 = tm.logical_and(tm.less_equal(tflt(14), l), tm.less_equal(l, tflt(17)))

            call0= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=0)
            call1= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=(l)*np.pi/4)
            call2 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_z=(l*2-7)*np.pi/2)
            call3 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=(l*2-11)*np.pi/4)
            call4 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_z=(l*2-19)*np.pi/4)
            call5 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/2, angle_z=(l*2-27)*np.pi/4)

            conditions = [cond0, cond1, cond2, cond3, cond4, cond5]
            callables = [call0, call1, call2, call3, call4, call5]
            shape_pc = nested_tf_cond(conditions, callables)
   
            rotated_data[k] = shape_pc

        rotated_data = tf.squeeze(tf.stack(rotated_data, axis=0))
        return rotated_data
def mask_for(features, **kwargs):
    """Create a 'mask' that filters for certain values

    Args:
        features: a dictionary of tensors
        **kwargs: entries of the dictionary with the values, only the first two are used
    Returns:
        a mask
    """
    entries = list(kwargs.items())
    return tf.where(
        tfm.logical_and(tfm.equal(features[entries[0][0]], entries[0][1]),
                        tfm.equal(features[entries[1][0]], entries[1][1])))
def rotate_tensor_by_label_54(batch_data, label, graph):
    """ Rotate a batch of points by the label
        54 possible directions:
            vertices of a regular icosahedron
            vertices of a regular dodecahedron
        Input:
          BxNx3 array
        Return:
          BxNx3 array
    """
    with graph.as_default():
        batch_size = batch_data.get_shape().as_list()[0]
        rotated_data = [None] * batch_size
        splits = tf.split(batch_data, batch_size, 0)
        label = tf.dtypes.cast(label, dtype=tf.float32)
        label = tf.split(label, batch_size, 0)
        label = [tf.squeeze(l) for l in label]
        for k in range(batch_size):
            shape_pc = splits[k] 
            l = label[k]
            cond0 = tm.logical_or(tm.equal(tflt(0), l), tm.equal(tflt(1), l))
            cond1 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(2), l), tm.less_equal(l, tflt(15))), tm.equal(tflt(0), tm.mod(l, tflt(2))))
            cond2 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(2), l), tm.less_equal(l, tflt(15))), tm.equal(tflt(1), tm.mod(l, tflt(2))))
            cond3 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(16), l), tm.less_equal(l, tflt(39))), tm.equal(tflt(0), tm.mod(l, tflt(2))))
            cond4 = tm.logical_and(tm.logical_and(tm.less_equal(tflt(16), l), tm.less_equal(l, tflt(39))), tm.equal(tflt(1), tm.mod(l, tflt(2))))
            cond5 = tm.logical_and(tm.less_equal(tflt(40), l), tm.less_equal(l, tflt(53)))

            call0= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi*l)
            call1= rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/6, angle_z=2*(l-2)*np.pi/7)
            call2 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=5*np.pi/6, angle_z=2*(l-2)*np.pi/7)
            call3 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/3, angle_z=2*(l-16)*np.pi/12)
            call4 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=2*np.pi/3, angle_z=2*(l-16)*np.pi/12)
            call5 = rotate_tensor_by_angle_xyz(shape_pc, graph, angle_x=np.pi/2, angle_z=2*(l-40)*np.pi/14)

            conditions = [cond0, cond1, cond2, cond3, cond4, cond5]
            callables = [call0, call1, call2, call3, call4, call5]
            shape_pc = nested_tf_cond(shape_pc, conditions, callables)

            rotated_data[k] = shape_pc

        rotated_data = tf.squeeze(tf.stack(rotated_data, axis=0))
        return rotated_data
Esempio n. 9
0
        def body(i, extras, n_set, current_val, seg):
            def case1(i, extras, n_set, current_val: tf.Tensor, seg):
                # current_val += 0
                extras -= 1
                n_set += 1
                seg = tf.sparse.add(
                    seg,
                    tf.SparseTensor(indices=[[0, i]],
                                    values=[current_val],
                                    dense_shape=[1, n]))
                return extras, n_set, current_val, seg

            def case2(i, extras, n_set, current_val, seg):
                current_val += 1
                n_set -= n_set
                n_set += 1
                seg = tf.sparse.add(
                    seg,
                    tf.SparseTensor(indices=[[0, i]],
                                    values=[current_val],
                                    dense_shape=[1, n]))
                return extras, n_set, current_val, seg

            def case3(i, extras, n_set, current_val, seg):
                # current_val += 0
                n_set += 1
                seg = tf.sparse.add(
                    seg,
                    tf.SparseTensor(indices=[[0, i]],
                                    values=[current_val],
                                    dense_shape=[1, n]))
                return extras, n_set, current_val, seg

            extras, n_set, current_val, seg = tf.cond(
                logical_and(greater(extras, 0), equal(n_set, y)),
                lambda: case1(i, extras, n_set, current_val, seg),
                lambda: tf.cond(
                    greater_equal(n_set, y), lambda: case2(
                        i, extras, n_set, current_val, seg), lambda: case3(
                            i, extras, n_set, current_val, seg)))
            return b(i), extras, n_set, current_val, seg
Esempio n. 10
0
    def basis2(self, x):
        #         b_2_k = K.variable(np.zeros(x.shape[0], ))

        k_1 = tf.constant(self.breakpoints[self.k - 1], dtype=tf.float32)
        #         print(k_1.shape)
        k = tf.constant(self.breakpoints[self.k], dtype=tf.float32)

        def f1():
            return tfm.add(
                tfm.subtract(tfm.divide(tfm.multiply(x, x), 2),
                             tfm.multiply(k_1, x)),
                tfm.divide(tfm.multiply(k_1, k_1), 2))

        def f2():
            val1 = tfm.divide(
                tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(k, k_1)), 2)
            val2 = tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(x, k))
            val = tfm.add(val1, val2)
            return val

        b2ks = [0] * x.shape[0]
        for i in range(x.shape[0]):
            b2ks[i] = tf.cond(
                tfm.logical_and(tfm.greater(x[i, 0], k_1),
                                tfm.less(x[i, 0], k)), lambda: f1(),
                lambda: f2())


#             print(b2ks[i].shape)

        b_2_k = K.concatenate(b2ks)
        #         if tfm.greater(x, k_1) and tfm.less(x, k):

        #         elif tfm.less(k, x):

        return tfm.multiply(self.w[self.i][self.k - 1], b_2_k)
 def update(self, features, labels, pred_mean):
     test_for_ybar0_s0 = tfm.logical_and(
         tfm.equal(features['ybar'], 0), tfm.equal(features['sensitive'],
                                                   0))
     accepted = tf.gather_nd(1 - labels, tf.where(test_for_ybar0_s0))
     self.mean(accepted)