Beispiel #1
0
    def call(self, inputs):
        #implementation from tf.contrib.layers.spatial_softmax
        #the follwoing line does not work because of a pesky
        #temperature variable creation
        #return tf.contrib.layers.spatial_softmax(inputs, temperature=1.0, trainable=True)

        #TODO maybe add temperature here
        #softmax_attention = nn.softmax(features / temperature)
        #import IPython; IPython.embed()
        shape = tf.shape(inputs)
        static_shape = inputs.shape
        height, width, num_channels = shape[1], shape[2], static_shape[3]
        pos_x, pos_y = tf.meshgrid(tf.lin_space(-1., 1., num=height),
                                   tf.lin_space(-1., 1., num=width),
                                   indexing='ij')
        pos_x = tf.reshape(pos_x, [height * width])
        pos_y = tf.reshape(pos_y, [height * width])

        inputs = tf.reshape(tf.transpose(inputs, [0, 3, 1, 2]),
                            [-1, height * width])

        softmax_attention = tf.nn.softmax(inputs)
        expected_x = tf.reduce_sum(pos_x * softmax_attention, [1],
                                   keepdims=True)
        expected_y = tf.reduce_sum(pos_y * softmax_attention, [1],
                                   keepdims=True)
        expected_xy = tf.concat([expected_x, expected_y], 1)
        feature_keypoints = tf.reshape(expected_xy,
                                       [-1, num_channels.value * 2])
        feature_keypoints.set_shape([None, num_channels.value * 2])
        return feature_keypoints
Beispiel #2
0
def get_position_signal(sequence_length, position_dim=8):
    """Return fixed position signal as sine waves.

  Sine waves frequencies are linearly spaced so that shortest is 2 and
  longest is half the maximum length. That way the longest frequency
  is long enough to be monotonous over the whole sequence length.
  Sine waves are also shifted so that they don't all start with the same
  value.
  We don't use learned positional embeddings because these embeddings are
  projected linearly along with the original embeddings, and the projection is
  learned.

  Args:
    sequence_length: int, T, length of the sequence..
    position_dim: int, P, number of sine waves.

  Returns:
    A [T, P] tensor, position embeddings.
  """
    # Compute the frequencies.
    periods = tf.exp(
        tf.lin_space(tf.log(2.0), tf.log(tf.to_float(sequence_length)),
                     position_dim))
    frequencies = 1.0 / periods  # Shape [T, P].

    # Compute the sine waves.
    xs = frequencies[None, :] * tf.to_float(tf.range(sequence_length)[:, None])
    shifts = tf.lin_space(0.0, 2.0, position_dim)[None, :]  # [1, P]
    positions = tf.math.cos(math.pi * (xs + shifts))  # [T, P]
    positions.shape.assert_is_compatible_with([sequence_length, position_dim])
    return positions
Beispiel #3
0
        def _rand_gradation(image, max_delta):
            left_scale = tf.random_uniform((),
                                           minval=1.0 - max_delta,
                                           maxval=1.0 + max_delta,
                                           dtype=tf.float32)
            right_scale = tf.random_uniform((),
                                            minval=1.0 - max_delta,
                                            maxval=1.0 + max_delta,
                                            dtype=tf.float32)
            top_scale = tf.random_uniform((),
                                          minval=1.0 - max_delta,
                                          maxval=1.0 + max_delta,
                                          dtype=tf.float32)
            bottom_scale = tf.random_uniform((),
                                             minval=1.0 - max_delta,
                                             maxval=1.0 + max_delta,
                                             dtype=tf.float32)

            horizontal_gradation = tf.reshape(tf.lin_space(left_scale,
                                                           right_scale,
                                                           num=IM_WIDTH),
                                              shape=(1, IM_WIDTH, 1))
            vertical_gradation = tf.reshape(tf.lin_space(top_scale,
                                                         bottom_scale,
                                                         num=IM_HEIGHT),
                                            shape=(IM_HEIGHT, 1, 1))
            horizontal_gradation = tf.tile(horizontal_gradation,
                                           (IM_HEIGHT, 1, 1))
            vertical_gradation = tf.tile(vertical_gradation, (1, IM_WIDTH, 1))
            image = image * (horizontal_gradation * vertical_gradation)
            # image = tf.tile((horizontal_gradation * vertical_gradation),(1,1,3))
            return image
Beispiel #4
0
    def mtf_model_fn(self, features, mesh):
        hparams = self._hparams
        hparams.batch_size = 10
        hparams.io_size = 4
        hparams.hidden_size = 2
        tf_x = tf.matmul(
            tf.reshape(tf.lin_space(0., 1.0, hparams.batch_size),
                       [hparams.batch_size, 1]),
            tf.reshape(tf.lin_space(0., 1.0, hparams.io_size),
                       [1, hparams.io_size]))
        # tf_x = tf.random_uniform([hparams.batch_size, hparams.io_size])

        hidden_1_variable = tf.get_variable(
            "a",
            shape=[hparams.io_size, hparams.hidden_size],
            initializer=tf.random_normal_initializer())
        hidden_2_variable = tf.get_variable(
            "b",
            shape=[hparams.hidden_size, hparams.io_size],
            initializer=tf.random_normal_initializer())

        hidden_layer_1 = tf.matmul(tf_x, hidden_1_variable)
        hidden_layer_2 = tf.matmul(hidden_layer_1, hidden_2_variable)
        hidden_layer_2 = tpu_ops.cross_replica_sum(hidden_layer_2)
        loss = tf.reduce_mean(tf.square(hidden_layer_2 - tf_x))
        return None, loss
Beispiel #5
0
def add_coord_channels(image_tensor):
    """Adds channels containing pixel indices (x and y coordinates) to an image.

  Note: This has nothing to do with keypoint coordinates. It is just a data
  augmentation to allow convolutional networks to learn non-translation-
  equivariant outputs. This is similar to the "CoordConv" layers:
  https://arxiv.org/abs/1603.09382.

  Args:
    image_tensor: [batch_size, H, W, C] tensor.

  Returns:
    [batch_size, H, W, C + 2] tensor with x and y coordinate channels.
  """

    batch_size = tf.shape(image_tensor)[0]
    x_size = tf.shape(image_tensor)[2]
    y_size = tf.shape(image_tensor)[1]

    x_grid = tf.lin_space(-1.0, 1.0, x_size)
    x_map = tf.tile(x_grid[tf.newaxis, tf.newaxis, :, tf.newaxis],
                    (batch_size, y_size, 1, 1))

    y_grid = tf.lin_space(1.0, -1.0, y_size)
    y_map = tf.tile(y_grid[tf.newaxis, :, tf.newaxis, tf.newaxis],
                    (batch_size, 1, x_size, 1))

    return tf.concat([image_tensor, x_map, y_map], axis=-1)
Beispiel #6
0
def spatial_soft_argmax(x):
    ''' Implementation of the spatial softmax layer
        in Deep Spatial Autoencoders for Visuomotor Learning.
        See paper at https://arxiv.org/pdf/1509.06113.pdf.
        Code inspired by https://github.com/tensorflow/tensorflow/issues/6271.

    Args:
        x: input of shape [N, H, W, C]
    '''
    N, C, H, W = x.get_shape().as_list()

    # convert softmax to shape [N, H, W, C, 1]
    features = tf.reshape(tf.transpose(x, [0, 3, 1, 2]), [N * C, H * W])
    softmax = tf.nn.softmax(features)
    softmax = tf.transpose(tf.reshape(softmax, [N, C, H, W]), [0, 2, 3, 1])
    softmax = tf.expand_dims(softmax, -1)

    # get image coordinates in shape [H, W, 1, 2]
    posx, posy = tf.meshgrid(tf.lin_space(-1., 1., num=H),
                             tf.lin_space(-1., 1., num=W),
                             indexing='ij')
    image_coords = tf.stack([posx, posy], -1)
    image_coords = tf.expand_dims(image_coords, 2)

    # return argmax coordinates of [N, C * 2]
    res = tf.reduce_sum(softmax * image_coords, reduction_indices=[1, 2])
    res = tf.reshape(res, [N, C * 2])
    return res
Beispiel #7
0
def draw_color_wheel_tf(width, height):
    color_wheel_x = tf.lin_space(-width / 2., width / 2., width)
    color_wheel_y = tf.lin_space(-height / 2., height / 2., height)
    color_wheel_X, color_wheel_Y = tf.meshgrid(color_wheel_x, color_wheel_y)
    color_wheel_flow = tf.stack([color_wheel_X, color_wheel_Y], axis=2)
    color_wheel_flow = tf.expand_dims(color_wheel_flow, 0)
    color_wheel_rgb, flow_norm, flow_ang = flow_viz_tf(color_wheel_flow)
    return color_wheel_rgb
Beispiel #8
0
def disection_march(
        sdf_fn, offset, directions, max_length, n_splits_initial=16,
        n_splits_loop=8, n_iterations=5):
    raise NotImplementedError()
    with tf.name_scope('disection_march'):
        d_shape = tf.shape(directions)
        batch_size = d_shape[0]
        n_rays = d_shape[1]
        n = n_splits_initial
        sdf0 = sdf_fn(offset)
        gap = max_length - sdf0
        zero = tf.zeros((), dtype=tf.float32)
        one = tf.ones((), dtype=tf.float32)
        g0 = tf.lin_space(zero, one, n)
        g0 = tf.expand_dims(g0, axis=0)
        lengths = tf.expand_dims(sdf0, axis=-1) + \
            tf.expand_dims(gap, axis=1)*g0
        grid = tf.expand_dims(tf.expand_dims(lengths, axis=-1), axis=1)
        directions = tf.expand_dims(directions, axis=-2)
        offset = tf.expand_dims(tf.expand_dims(offset, axis=-2), axis=-2)
        points = offset + directions*grid
        sdf = sdf_fn(points)
        signs = tf.sign(sdf)
        changes = tf.not_equal(signs[..., :-1], signs[..., 1:])
        ones = tf.expand_dims(
            tf.ones(shape=tf.shape(changes)[:-1], dtype=tf.bool), axis=-1)
        changes = tf.concat((changes, ones), axis=-1)
        i = tf.argmax(
            tf.cast(changes, dtype=tf.uint8), axis=-1, output_type=tf.int32)
        missed = tf.equal(i, n)
        batch_index = tf.tile(
            tf.expand_dims(tf.range(batch_size, dtype=tf.int32), axis=-1),
            (1, n_rays))

        bi = tf.stack((batch_index, i), axis=-1)
        lengths = tf.gather_nd(lengths, bi)

        n = n_splits_loop
        g0 = tf.reshape(tf.lin_space(zero, one, n), (1, 1, -1))

        def cond(*args, **kwargs):
            return True

        def body(lengths, missed):
            gap = max_length - lengths
            lengths = tf.expand_dims(gap, axis=-1)
            # points = offset + lengths*directions
            # TODO: or maybe just call linear solver??
            raise NotImplementedError('TODO')

        lengths, missed = tf.while_loop(
            cond, body, (lengths,  missed), back_prop=False,
            maximum_iterations=n_iterations)
        hit = tf.logical_not(missed)

    return lengths, hit, missed
Beispiel #9
0
def generateMeshGrid(out_height, out_width):
    x_t, y_t = tf.meshgrid(tf.lin_space(-1.0, 1.0, out_width),
        tf.lin_space(-1.0, 1.0, out_height)
    )
    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)
    grids = tf.concat((x_t_flat, y_t_flat, ones), axis=0)
    print(grids.shape)
    return grids
Beispiel #10
0
def tf_random_gen_points():
    # boxes: (N, 4) min_x, min_y, max_x, max_y

    num_points = 25
    x = tf.lin_space(0.0, 1.0, num_points)
    y = tf.lin_space(0.0, 1.0, num_points)
    xx,yy = tf.meshgrid(x,y)
    xx = tf.reshape(xx, [-1, 1])
    yy = tf.reshape(yy, [-1, 1])
    grid_points = tf.concat([xx,yy], axis=1)

    return grid_points
Beispiel #11
0
def _load_img_with_depth(filename):
    image = tf.cast(_load_img(filename, channels=1), tf.float32)
    depth = tf.tile(
        tf.reshape(tf.lin_space(0.0, 255.0, ORIG_HEIGHT),
                   shape=(ORIG_HEIGHT, 1, 1)), (1, ORIG_WIDTH, 1))
    near_right = tf.reshape(tf.lin_space(0.0, 1.0, ORIG_WIDTH),
                            shape=(1, ORIG_WIDTH, 1))
    near_left = tf.reshape(tf.lin_space(1.0, 0.0, ORIG_WIDTH),
                           shape=(1, ORIG_WIDTH, 1))
    near_edge = tf.tile(near_right * near_left * 255, (ORIG_HEIGHT, 1, 1))
    image = tf.concat([tf.cast(image, tf.float32), depth, near_edge], axis=2)
    return image
Beispiel #12
0
def concat_xygrad_2d(input_tensor):
    input_shape = [int(x) for i, x in enumerate(input_tensor.get_shape())]
    xgrad = tf.reshape(
        tf.tile([tf.lin_space(0.0, 1.0, input_shape[-2])],
                [input_shape[-3], 1]), np.concatenate([input_shape[0:-1],
                                                       [1]]))
    ygrad = tf.reshape(
        tf.tile(
            tf.reshape([tf.lin_space(0.0, 1.0, input_shape[-3])],
                       [input_shape[-3], 1]), [1, input_shape[-2]]),
        np.concatenate([input_shape[0:-1], [1]]))
    return tf.concat([input_tensor, xgrad, ygrad], axis=-1)
Beispiel #13
0
def center_weight(shape, base=0.005, boundary_penalty=3.0):
    with tf.variable_scope('center_weight'):
        temp = boundary_penalty - base
        x = tf.pow(tf.abs(tf.lin_space(-1.0, 1.0, shape[1])), 8)
        y = tf.pow(tf.abs(tf.lin_space(-1.0, 1.0, shape[2])), 8)
        X, Y = tf.meshgrid(y, x)
        X = tf.expand_dims(X, axis=2)
        Y = tf.expand_dims(Y, axis=2)
        dist2cent = temp * tf.sqrt(
            tf.reduce_sum(tf.square(tf.concat([X, Y], axis=2)), axis=2)) + base
        dist2cent = tf.expand_dims(tf.tile(tf.expand_dims(dist2cent, axis=0),
                                           [shape[0], 1, 1]),
                                   axis=3)
        return dist2cent
Beispiel #14
0
def get_random_scale(min_scale_factor, max_scale_factor, step_size):
  """Gets a random scale value.

  Args:
    min_scale_factor: Minimum scale value.
    max_scale_factor: Maximum scale value.
    step_size: The step size from minimum to maximum value.

  Returns:
    A random scale value selected between minimum and maximum value.

  Raises:
    ValueError: min_scale_factor has unexpected value.
  """
  if min_scale_factor < 0 or min_scale_factor > max_scale_factor:
    raise ValueError('Unexpected value of min_scale_factor.')

  if min_scale_factor == max_scale_factor:
    return tf.cast(min_scale_factor, tf.float32)

  # When step_size = 0, we sample the value uniformly from [min, max).
  if step_size == 0:
    return tf.random_uniform([1],
                             minval=min_scale_factor,
                             maxval=max_scale_factor)

  # When step_size != 0, we randomly select one discrete value from [min, max].
  num_steps = int((max_scale_factor - min_scale_factor) / step_size + 1)
  scale_factors = tf.lin_space(min_scale_factor, max_scale_factor, num_steps)
  shuffled_scale_factors = tf.random_shuffle(scale_factors)
  return shuffled_scale_factors[0]
Beispiel #15
0
    def stereo_network_cat(self, Ts, images, intrinsics):
        """3D Matching Network with view concatenation"""

        cfg = self.cfg
        depths = tf.lin_space(cfg.MIN_DEPTH, cfg.MAX_DEPTH, cfg.COST_VOLUME_DEPTH)
        intrinsics = intrinsics_vec_to_matrix(intrinsics / 4.0)

        with tf.variable_scope("stereo", reuse=self.reuse) as sc:
            # extract 2d feature maps from images and build cost volume
            fmaps = self.encoder(images)
            volume = operators.backproject_cat(Ts, depths, intrinsics, fmaps)

            self.spreds = []
            with slim.arg_scope([slim.batch_norm], **self.batch_norm_params):
                with slim.arg_scope([slim.conv3d],
                                    weights_regularizer=slim.l2_regularizer(0.00005),
                                    normalizer_fn=None,
                                    activation_fn=None):


                    x = slim.conv3d(volume, 48, [3, 3, 3])
                    x = tf.add(x, conv3d(conv3d(x, 48), 48))

                    self.pred_logits = []
                    for i in range(self.cfg.HG_COUNT):
                        with tf.variable_scope("hg1_%d"%i):
                            x = hg.hourglass_3d(x, 4, 48)
                            self.pred_logits.append(self.stereo_head(x))

        return self.soft_argmax(self.pred_logits[-1])
Beispiel #16
0
def concat_xygrad_2d(input_tensor):
    input_shape = [int(x) for i, x in enumerate(input_tensor.get_shape())]
    print('input shape of concat_xygrad:')
    print(input_shape)
    #What's the shape of the tensor? What does every dimention means for the input?
    xgrad = tf.reshape(
        tf.tile([tf.lin_space(0.0, 1.0, input_shape[-2])],
                [input_shape[-3], 1]), np.concatenate([input_shape[0:-1],
                                                       [1]]))
    #What does input_shape[-2] means? [input_shape[-3],1] has two nums in the list, which violate the requirement of tf.tile.
    ygrad = tf.reshape(
        tf.tile(
            tf.reshape([tf.lin_space(0.0, 1.0, input_shape[-3])],
                       [input_shape[-3], 1]), [1, input_shape[-2]]),
        np.concatenate([input_shape[0:-1], [1]]))
    return tf.concat([input_tensor, xgrad, ygrad], axis=-1)
Beispiel #17
0
    def __init__(
            self,
            state_shapes,
            action_size,
            actor,
            critic,
            actor_optimizer,
            critic_optimizer,
            n_step=1,
            actor_grad_val_clip=1.0,
            actor_grad_norm_clip=None,
            critic_grad_val_clip=None,
            critic_grad_norm_clip=None,
            gamma=0.99,
            target_actor_update_rate=1.0,
            target_critic_update_rate=1.0):
        super(CategoricalDDPG, self).__init__(
            state_shapes, action_size, actor, critic,
            actor_optimizer, critic_optimizer, n_step,
            actor_grad_val_clip, actor_grad_norm_clip,
            critic_grad_val_clip, critic_grad_norm_clip,
            gamma, target_actor_update_rate, target_critic_update_rate)

        self.num_atoms = self._critic.num_atoms
        self.v_min, self.v_max = self._critic.v
        self.delta_z = (self.v_max - self.v_min) / (self.num_atoms - 1)
        self.z = tf.lin_space(
            start=self.v_min, stop=self.v_max, num=self.num_atoms)
        self.create_placeholders()
        self.build_graph()
Beispiel #18
0
def part2():
    X, y = load_data('part2')
    X = tf.constant(X)
    y = tf.constant(y)
    # Define optimizer
    optimizer = tf.train.GradientDescentOptimizer(5e-1)
    model = two_layers_nn(output_size=2)
    num_epochs = 5
    # Train the model with gradient descent
    model.fit(X, y, optimizer, num_epochs=num_epochs)
    # More thresholds means higher granularity for the area under the curve approximation
    # Feel free to experiment with the number of thresholds
    num_thresholds = 1000
    thresholds = tf.lin_space(0.0, 1.0, num_thresholds).numpy()

    # Apply Softmax on our predictions as the output of the model is unnormalized# Apply
    # Select the predictions of our positive class (the class with less samples)
    preds = tf.nn.softmax(model.predict(X))[:, 1]

    # Compute the ROC-AUC score and get the TPR and FPR of each threshold
    auc_score, fpr_list, tpr_list = roc_auc(y, preds, thresholds)
    print('模型的ROC-AUC : ', auc_score.numpy())
    plt.figure(figsize=(10, 8))
    plt.plot(fpr_list, tpr_list, label='AUC score: %.2f' % auc_score)
    plt.xlabel('假正率', fontsize=15)
    plt.ylabel('真正率', fontsize=15)
    plt.title('ROC曲线')
    plt.legend(fontsize=15)
    plt.show()
Beispiel #19
0
 def call(self, x, mask=None):
     # parse input feature shape
     bsize, nb_rows, nb_cols, nb_feats = K.int_shape(x)
     nb_maps = nb_rows * nb_cols
     # normalization on channel-wise
     x_std = std_norm_along_chs(x)
     # self correlation
     x_3d = K.reshape(x_std, tf.stack([-1, nb_maps, nb_feats]))
     x_corr_3d = (tf.matmul(
         x_3d, x_3d, transpose_a=False, transpose_b=True) / nb_feats)
     x_corr = K.reshape(x_corr_3d, tf.stack([-1, nb_rows, nb_cols,
                                             nb_maps]))
     # argsort response maps along the translaton dimension
     if self.nb_pools is not None:
         ranks = K.cast(
             K.round(tf.lin_space(1.0, nb_maps - 1, self.nb_pools)),
             "int32")
     else:
         ranks = tf.range(1, nb_maps, dtype="int32")
     x_sort, _ = tf.nn.top_k(x_corr, k=nb_maps, sorted=True)
     # pool out x features at interested ranks
     x_f1st_sort = K.permute_dimensions(x_sort, (3, 0, 1, 2))
     x_f1st_pool = tf.gather(x_f1st_sort, ranks)
     x_pool = K.permute_dimensions(x_f1st_pool, (1, 2, 3, 0))
     return x_pool
Beispiel #20
0
 def loop_2_step(i, E, Rp, Tp, Cr, likelihood):
     l = E_ti[i]
     J = tf.lin_space(0., tf.cast(l - 1, tf.float32), l) / tf.cast(
         l, tf.float32)
     Dm = Cr[i] - J * Tp[i]
     likelihood = likelihood + Rp[i] - tf.reduce_sum(tf.log(Dm))
     return i + 1, E, Rp, Tp, Cr, likelihood
Beispiel #21
0
def get_random_scale(min_scale_factor, max_scale_factor, step_size):
    """获得一个随机尺度值

    Args:
    :param min_scale_factor:尺度最小值
    :param max_scale_factor: 尺度最大值
    :param step_size: 从最小到最大的步长尺寸

    :return: 在最大最小值之间选择一个可选的随机尺度

    Raises:
    ValueError:min_scale_factor 有不期望的值
    """
    if min_scale_factor < 0 or min_scale_factor > max_scale_factor:
        raise ValueError('Unexpected value of min_scale_factor.')

    if min_scale_factor == max_scale_factor:
        return tf.to_float(min_scale_factor)

    #如果step_size=0,均匀采样值从[min,max]
    if step_size == 0:
        return tf.random_uniform([1],
                                 minval=min_scale_factor,
                                 maxval=max_scale_factor)

    #当step_size !=0时,我们随机选取一个离散的值
    num_steps = int((max_scale_factor - min_scale_factor) / step_size + 1)
    scale_factors = tf.lin_space(min_scale_factor, max_scale_factor, num_steps)
    shuffled_scale_factors = tf.random_shuffle(scale_factors)
    return shuffled_scale_factors[0]
Beispiel #22
0
def gen_1d_grid(num_grid_point):
    """
    output [num_grid_point, 2]
    """
    x = tf.lin_space(-0.2, 0.2, num_grid_point)
    grid = tf.reshape(x, [-1, 1])  # [2, 2, 2] -> [4, 2]
    return grid
Beispiel #23
0
def tf_sh_kernel(X,
                 sq_dist,
                 nr,
                 l_max,
                 sigma,
                 radial_weights_fn,
                 normalize_patch=False,
                 dtype=tf.float32):
    # Y = unnormalized_sh(X, l_max, dtype=dtype)
    Y = normalized_sh(X, l_max, dtype=dtype, eps=0.0001)

    dist = tf.sqrt(tf.maximum(sq_dist, 0.0001))

    if normalize_patch:
        radius = tf.reduce_max(dist, axis=2, keepdims=True)
        dist = tf.divide(dist, radius + 0.0001)

    dist = tf.expand_dims(dist, axis=-1)
    r = (2. * nr * sigma) * tf.reshape(tf.lin_space(
        start=0., stop=1. - 1. / float(nr), num=nr),
                                       shape=(1, 1, 1, nr))
    r = tf.subtract(dist, r)
    radial_weights = radial_weights_fn(r, sigma)
    Y = tf.expand_dims(Y, axis=-1)
    radial_weights = tf.expand_dims(radial_weights, axis=-2)
    y = tf.multiply(Y, radial_weights)

    # y = tf.expand_dims(Y, axis=-1)
    # y = tf.tile(y, multiples=(1, 1, 1, 1, 3))
    return y
Beispiel #24
0
def get_random_scale(min_scale_factor, max_scale_factor, step_size):
    """Gets a random scale value.

  Args:
    min_scale_factor: Minimum scale value.
    max_scale_factor: Maximum scale value.
    step_size: The step size from minimum to maximum value.

  Returns:
    A tensor with random scale value selected between minimum and maximum value.
    If `min_scale_factor` and `max_scale_factor` are the same, a number is
    returned instead.

  Raises:
    ValueError: min_scale_factor has unexpected value.
  """
    if min_scale_factor < 0 or min_scale_factor > max_scale_factor:
        raise ValueError('Unexpected value of min_scale_factor.')

    if min_scale_factor == max_scale_factor:
        return np.float32(min_scale_factor)

    # When step_size = 0, we sample the value uniformly from [min, max).
    if step_size == 0:
        return tf.random.uniform([1],
                                 minval=min_scale_factor,
                                 maxval=max_scale_factor)

    # When step_size != 0, we randomly select one discrete value from [min, max].
    num_steps = int((max_scale_factor - min_scale_factor) / step_size + 1)
    scale_factors = tf.lin_space(min_scale_factor, max_scale_factor, num_steps)
    shuffled_scale_factors = tf.compat.v1.random_shuffle(scale_factors)
    return shuffled_scale_factors[0]
Beispiel #25
0
def spectral_centroid_bandwidth(mag_spectrogram, sample_rate, p, name=None):
    """Calculate the logarithmized spectral centroid from a batch of magnitude
    spectrograms.
  
  Args:
    mag_spectrogram: A `Tensor` of shape `[frames, spectrogram_bins]`.
    sample_rate: Python float. Samples per second of the input signal.
    p: Power to raise deviation from spectral centroid.
    name: `string`, name of the operation.
    
  Returns:
    A `Tensor` with shape `[frames, 1]` containing the spectral centroid.
    A `Tensor` with shape `[1]` containing the feature id.
  """
    with tf.name_scope(name, "spectral_centroid_bandwidth"):
        eps = 0.00000001
        n_spectrogram_bins = mag_spectrogram.shape[-1].value
        center_frequencies = tf.lin_space(0.0, sample_rate / 2.0,
                                          int(n_spectrogram_bins))
        length = tf.reduce_sum(mag_spectrogram, axis=-1, keepdims=True)
        normalized_mag_spectrogram = tf.divide(mag_spectrogram, length + eps)
        weighted_spectrogram = tf.multiply(center_frequencies,
                                           normalized_mag_spectrogram)
        centroid = tf.reduce_sum(weighted_spectrogram, axis=-1, keepdims=True)
        deviation = tf.abs(tf.subtract(center_frequencies, centroid))
        bandwidth = tf.pow(
            tf.reduce_sum(tf.multiply(mag_spectrogram, tf.pow(deviation, p)),
                          axis=-1,
                          keepdims=True), tf.reciprocal(p))
        feature_ids = tf.constant(["spectral_centroid", "spectral_bandwidth"])
        return (feature_ids, tf.log(tf.concat([centroid, bandwidth], axis=-1)))
Beispiel #26
0
def get_random_scale(min_scale_factor, max_scale_factor, step_size):
    """
    在一定范围内得到随机值,范围为min_scale_factor到max_scale_factor,间隔为step_size
    Args:
        min_scale_factor(float): 随机尺度下限,大于0
        max_scale_factor(float): 随机尺度上限,不小于下限值
        step_size(float): 尺度间隔,非负, 等于为0时直接返回min_scale_factor到max_scale_factor范围内任一值
    Returns:
        随机尺度值
    """

    if min_scale_factor < 0 or min_scale_factor > max_scale_factor:
        raise ValueError('Unexpected value of min_scale_factor.')

    if min_scale_factor == max_scale_factor:
        return tf.cast(min_scale_factor, tf.float32)

        # When step_size = 0, we sample the value uniformly from [min, max).
    if step_size == 0:
        return tf.random_uniform([1],
                                 minval=min_scale_factor,
                                 maxval=max_scale_factor)

        # When step_size != 0, we randomly select one discrete value from [min, max].
    num_steps = int((max_scale_factor - min_scale_factor) / step_size + 1)
    scale_factors = tf.lin_space(min_scale_factor, max_scale_factor, num_steps)
    shuffled_scale_factors = tf.random_shuffle(scale_factors)
    return shuffled_scale_factors[0]
def get_random_scale(min_scale_factor, max_scale_factor, step_size=0):
    """Gets a random scale value.

    Args:
      min_scale_factor: Minimum scale value.
      max_scale_factor: Maximum scale value.
      step_size: The step size from minimum to maximum value.

    Returns:
      A random scale value selected between minimum and maximum value.

    Raises:
      ValueError: min_scale_factor has unexpected value.
    """
    if min_scale_factor is None or max_scale_factor is None or step_size is None:
        return 1.0

    if min_scale_factor < 0 or min_scale_factor > max_scale_factor:
        raise ValueError('Unexpected value of min_scale_factor.')

    if min_scale_factor == max_scale_factor:
        return tf.to_float(min_scale_factor)

    # When step_size = 0, we sample the value uniformly from [min, max).
    if step_size == 0:
        return tf.random_uniform([],
                                 minval=min_scale_factor,
                                 maxval=max_scale_factor)

    # When step_size != 0, we randomly select one discrete value from [min, max].
    num_steps = int((max_scale_factor - min_scale_factor) / step_size + 1)
    scale_factors = tf.lin_space(min_scale_factor, max_scale_factor, num_steps)
    shuffled_scale_factors = tf.random_shuffle(scale_factors)
    return shuffled_scale_factors[0]
Beispiel #28
0
    def _waveform(self, phase: float, amplitude: float,
                  frequency: float) -> np.ndarray:
        x = tf.lin_space(start=0.0,
                         stop=2 * np.pi * self.time,
                         num=int(self.time * self.s_rate)) + phase
        waveform = amplitude * tf.sin(x * frequency)

        return waveform
Beispiel #29
0
def gen_grid(up_ratio):
    import math
    """
    output [num_grid_point, 2]
    """
    sqrted = int(math.sqrt(up_ratio))+1
    for i in range(1,sqrted+1).__reversed__():
        if (up_ratio%i) == 0:
            num_x = i
            num_y = up_ratio//i
            break
    grid_x = tf.lin_space(-0.2, 0.2, num_x)
    grid_y = tf.lin_space(-0.2, 0.2, num_y)

    x, y = tf.meshgrid(grid_x, grid_y)
    grid = tf.reshape(tf.stack([x, y], axis=-1), [-1, 2])  # [2, 2, 2] -> [4, 2]
    return grid
Beispiel #30
0
 def video_summary(name, video):
     if not thread_id:
         indices = tf.lin_space(0.0,
                                tf.cast(tf.constant(step_length - 1),
                                        tf.float32),
                                num=5)
         sampled_frame = tf.nn.embedding_lookup(video,
                                                tf.cast(indices, tf.int32))
         tf.summary.image(name, video, max_outputs=5)
Beispiel #31
0
def gen_grid(num_grid_point):
    """
    output [num_grid_point, 2]
    """
    x = tf.lin_space(-0.2, 0.2, num_grid_point)
    x, y = tf.meshgrid(x, x)
    grid = tf.reshape(tf.stack([x, y], axis=-1),
                      [-1, 2])  # [2, 2, 2] -> [4, 2]
    return grid
Beispiel #32
0
def get_logits_over_interval(sess, model, x_data, fgsm_params,
                             min_epsilon=-10., max_epsilon=10.,
                             num_points=21):
    """Get logits when the input is perturbed in an interval in adv direction.

    Args:
        sess: Tf session
        model: Model for which we wish to get logits.
        x_data: Numpy array corresponding to single data.
                point of shape [height, width, channels].
        fgsm_params: Parameters for generating adversarial examples.
        min_epsilon: Minimum value of epsilon over the interval.
        max_epsilon: Maximum value of epsilon over the interval.
        num_points: Number of points used to interpolate.

    Returns:
        Numpy array containing logits.

    Raises:
        ValueError if min_epsilon is larger than max_epsilon.
    """
    # Get the height, width and number of channels
    height = x_data.shape[0]
    width = x_data.shape[1]
    channels = x_data.shape[2]
    size = height * width * channels

    x_data = np.expand_dims(x_data, axis=0)
    import tensorflow as tf
    from cleverhans.attacks import FastGradientMethod

    # Define the data placeholder
    x = tf.placeholder(dtype=tf.float32,
                       shape=[1, height,
                              width,
                              channels],
                       name='x')
    # Define adv_x
    fgsm = FastGradientMethod(model, sess=sess)
    adv_x = fgsm.generate(x, **fgsm_params)

    if min_epsilon > max_epsilon:
        raise ValueError('Minimum epsilon is less than maximum epsilon')

    eta = tf.nn.l2_normalize(adv_x - x, dim=0)
    epsilon = tf.reshape(tf.lin_space(float(min_epsilon),
                                      float(max_epsilon),
                                      num_points),
                         (num_points, 1, 1, 1))
    lin_batch = x + epsilon * eta
    logits = model.get_logits(lin_batch)
    with sess.as_default():
        log_prob_adv_array = sess.run(logits,
                                      feed_dict={x: x_data})
    return log_prob_adv_array
Beispiel #33
0
 def when_nonsingular():
   bucket_width = range_ / tf.cast(bucket_count, tf.float64)
   offsets = data - min_
   bucket_indices = tf.cast(tf.floor(offsets / bucket_width),
                            dtype=tf.int32)
   clamped_indices = tf.minimum(bucket_indices, bucket_count - 1)
   one_hots = tf.one_hot(clamped_indices, depth=bucket_count)
   bucket_counts = tf.cast(tf.reduce_sum(one_hots, axis=0),
                           dtype=tf.float64)
   edges = tf.lin_space(min_, max_, bucket_count + 1)
   left_edges = edges[:-1]
   right_edges = edges[1:]
   return tf.transpose(tf.stack(
       [left_edges, right_edges, bucket_counts]))
    def __call__(self, inputs, state, scope=None):
        if self._hidden_features is None:
            self._attn_length = math_ops.reduce_max(self._attention_length)
            attention_states = tf.slice(self._attention_states, [0,0,0], tf.pack([-1, self._attn_length, -1]))
            # To calculate W1 * h_t we use a 1-by-1 convolution, need to reshape before.
            self._hidden = array_ops.reshape(attention_states, tf.pack([-1, self._attn_length, 1, self._num_units]))
            hidden_features = []

            for a in range(self._num_heads):
                k = tf.get_variable("AttnW_%d" % a, [1, 1, self._num_units, self._num_units])
                hidden_features.append(nn_ops.conv2d(self._hidden, k, [1, 1, 1, 1], "SAME"))
            self._hidden_features = hidden_features


        ds = []  # Results of attention reads will be stored here.

        batch_size = tf.shape(inputs)[0]
        mask = tf.tile(tf.reshape(tf.lin_space(1.0, tf.cast(self._attn_length, tf.float32), self._attn_length), [1, -1]),
                       tf.pack([batch_size, 1]))
        batch_size_scale = batch_size // tf.shape(self._attention_length)[0] # used in decoding
        lengths = tf.tile(tf.expand_dims(tf.cast(self._attention_length, tf.float32), 1),
                          tf.pack([batch_size_scale, self._attn_length]))

        mask = tf.cast(tf.greater(mask, lengths), tf.float32) * -1000.0
        # some parts are copied from tensorflow attention code-base
        for a in range(self._num_heads):
            with tf.variable_scope("Attention_%d" % a):
                y = tf.contrib.layers.fully_connected(inputs, self._num_units, activation_fn=None)
                y = array_ops.reshape(y, [-1, 1, 1, self._num_units])
                # Attention mask is a softmax of v^T * tanh(...).
                v = tf.get_variable("AttnV_%d" % a, [self._num_units])
                hf = tf.cond(tf.equal(batch_size_scale,1),
                             lambda: self._hidden_features[a],
                             lambda: tf.tile(self._hidden_features[a], tf.pack([batch_size_scale,1,1,1])))
                s = math_ops.reduce_sum(v * math_ops.tanh(hf + y), [2, 3])

                a = nn_ops.softmax(s + mask)
                # Now calculate the attention-weighted vector d.
                d = math_ops.reduce_sum(
                    array_ops.reshape(a, tf.pack([-1, self._attn_length, 1, 1])) * self._hidden,
                    [1, 2])
                ds.append(array_ops.reshape(d, [-1, self._num_units]))

        return tf.concat(1, ds), None
def get_centroids_by_percentile(x, levels):
  """Get the custom centroids by percentiles of the per-pixel distribution of x.

  Args:
    x: Input data set of shape [-1, height, width, channels]
        whose centroids we wish to compute.
    levels: Number of centroids to compute.

  Returns:
    Custom centroids as a tensor.
  """

  def quantile(q):
    return tf.contrib.distributions.percentile(x, q=q, axis=None)

  start = 0.
  end = 100.
  quantile_range = tf.lin_space(start, end, levels)
  centroids = tf.map_fn(quantile, quantile_range)
  return centroids