Exemple #1
0
def transform(data, network, time_factor=1):
    x = data.placeholder
    from returnn.tf.compat import v1 as tf
    # summary("features", x)
    step = network.global_train_step
    step1 = tf.where(tf.greater_equal(step, 1000), 1, 0)
    step2 = tf.where(tf.greater_equal(step, 2000), 1, 0)

    def get_masked():
        x_masked = x
        x_masked = random_mask(
            x_masked,
            batch_axis=data.batch_dim_axis,
            axis=data.time_dim_axis,
            min_num=step1 + step2,
            max_num=tf.maximum(tf.shape(x)[data.time_dim_axis] // 100, 2) *
            (1 + step1 + step2 * 2),
            max_dims=20 // time_factor)
        x_masked = random_mask(x_masked,
                               batch_axis=data.batch_dim_axis,
                               axis=data.feature_dim_axis,
                               min_num=step1 + step2,
                               max_num=2 + step1 + step2 * 2,
                               max_dims=data.dim // 5)
        #summary("features_mask", x_masked)
        return x_masked

    x = network.cond_on_train(get_masked, lambda: x)
    return x
Exemple #2
0
def transform(data, network, time_factor=1):
    x = data.placeholder
    from returnn.tf.compat import v1 as tf
    # summary("features", x)
    step = network.global_train_step
    step1 = tf.where(tf.greater_equal(step, 1000), 1, 0)
    step2 = tf.where(tf.greater_equal(step, 2000), 1, 0)

    def get_masked():
        x_masked = x
        x_masked = random_mask(
            x_masked,
            batch_axis=data.batch_dim_axis,
            axis=data.time_dim_axis,
            min_num=step1 + step2,
            max_num=tf.maximum(tf.shape(x)[data.time_dim_axis] // 100, 2) *
            (1 + step1 + step2 * 2),
            max_dims=20 // time_factor)
        x_masked = random_mask(x_masked,
                               batch_axis=data.batch_dim_axis,
                               axis=data.feature_dim_axis,
                               min_num=step1 + step2,
                               max_num=2 + step1 + step2 * 2,
                               max_dims=data.dim // 5)
        #summary("features_mask", x_masked)
        return x_masked

    cond1 = network.train_flag
    cond2 = tf.greater_equal(
        tf.shape(x)[data.time_dim_axis],
        20)  # ignore specaug for utterances less than 20 frames
    x = tf.cond(tf.logical_and(cond1, cond2), get_masked, lambda: x)
    return x
def transform(source, **kwargs):
    from returnn.tf.compat import v1 as tf
    data = source(0, as_data=True)
    time_factor = 1  #  for switchout == 6
    x = data.placeholder
    network = kwargs["self"].network
    from returnn.tf.compat import v1 as tf
    step = network.global_train_step
    step1 = tf.where(tf.greater_equal(step, 1000), 1, 0)
    step2 = tf.where(tf.greater_equal(step, 2000), 1, 0)

    def get_masked():
        x_masked = x
        x_masked = random_mask(
            x_masked,
            batch_axis=data.batch_dim_axis,
            axis=data.time_dim_axis,
            min_num=step1 + step2,
            max_num=tf.maximum(tf.shape(x)[data.time_dim_axis] // 100, 2) *
            (1 + step1 + step2 * 2),
            max_dims=20 // time_factor)
        x_masked = random_mask(x_masked,
                               batch_axis=data.batch_dim_axis,
                               axis=data.feature_dim_axis,
                               min_num=step1 + step2,
                               max_num=2 + step1 + step2 * 2,
                               max_dims=data.dim // 5)
        return x_masked

    x = network.cond_on_train(get_masked, lambda: x)
    return x
def random_mask(x, axis, min_num, max_num, max_dims):
    """
  :param tf.Tensor x: (batch,time,feature)
  :param int axis:
  :param int|tf.Tensor min_num:
  :param int|tf.Tensor max_num: inclusive
  :param int max_dims: inclusive
  """
    from returnn.tf.compat import v1 as tf
    n_batch = tf.shape(x)[0]
    num = tf.random_uniform(shape=(n_batch, ),
                            minval=min_num,
                            maxval=max_num + 1,
                            dtype=tf.int32)
    # https://github.com/tensorflow/tensorflow/issues/9260
    # https://timvieira.github.io/blog/post/2014/08/01/gumbel-max-trick-and-weighted-reservoir-sampling/
    z = -tf.log(-tf.log(tf.random_uniform((n_batch, tf.shape(x)[axis]), 0, 1)))
    _, indices = tf.nn.top_k(z, tf.reduce_max(num))
    # indices should be sorted, and of shape (batch,num), entries (int32) in [0,dim)
    # indices = tf.Print(indices, ["indices", indices, tf.shape(indices)])
    _, x = tf.while_loop(
        cond=lambda i, _: tf.less(i, tf.reduce_max(num)),
        body=lambda i, x:
        (i + 1,
         tf.where(tf.less(i, num),
                  _mask(x, axis=axis, pos=indices[:, i], max_amount=max_dims),
                  x)),
        loop_vars=(0, x))
    return x
    def _random_mask(x, axis, min_num, max_num, max_dims):
        from returnn.tf.compat import v1 as tf
        n_batch = tf.shape(x)[0]
        num = tf.random_uniform(shape=(n_batch, ),
                                minval=min_num,
                                maxval=max_num + 1,
                                dtype=tf.int32)
        # https://github.com/tensorflow/tensorflow/issues/9260
        # https://timvieira.github.io/blog/post/2014/08/01/gumbel-max-trick-and-weighted-reservoir-sampling/
        z = -tf.log(
            -tf.log(tf.random_uniform((n_batch, tf.shape(x)[axis]), 0, 1)))
        _, indices = tf.nn.top_k(z, tf.reduce_max(num))
        # indices should be sorted, and of shape (batch,num), entries (int32) in [0,dim)
        # indices = tf.Print(indices, ["indices", indices, tf.shape(indices)])

        res_mask = tf.zeros(shape=[n_batch, tf.shape(x)[axis]],
                            dtype=tf.bool)  # all False
        _, res_mask = tf.while_loop(
            cond=lambda i, _: tf.less(i, tf.reduce_max(num)),
            body=lambda i, res_mask:
            (i + 1,
             tf.where(
                 tf.less(i, num),
                 tf.math.logical_or(
                     res_mask,
                     _get_mask(
                         x, axis=axis, pos=indices[:, i], max_amount=max_dims)
                 ), res_mask)),
            loop_vars=(0, res_mask))
        return res_mask  # (batch,dim)