Exemplo n.º 1
0
def extract_feature(waveforms, params):
    '''extract fbank with delta-delta and do cmvn
     waveforms: [batch, samples]
  '''
    p = params
    with tf.variable_scope('feature_extractor'):
        mel_fbanks = extract_logfbank_with_delta(waveforms, params)
        # shape: [1, nframes, nbins, nchannels]
        fbank_size = utils.shape_list(mel_fbanks)
        #assert fbank_size[0] == 1

        # This replaces CMVN estimation on data
        if not p.audio_global_cmvn:
            mean = tf.reduce_mean(mel_fbanks, keepdims=True, axis=1)
            variance = tf.reduce_mean(tf.square(mel_fbanks - mean),
                                      keepdims=True,
                                      axis=1)
        else:
            assert p.audio_cmvn_path, p.audio_cmvn_path
            mean, variance = utils.load_cmvn(p.audio_cmvn_path)

        var_epsilon = 1e-09
        mel_fbanks = utils.apply_cmvn(mel_fbanks, mean, variance, var_epsilon)

        # Later models like to flatten the two spatial dims. Instead, we add a
        # unit spatial dim and flatten the frequencies and channels.
        batch_size = fbank_size[0]
        feats = tf.concat([
            tf.reshape(
                mel_fbanks,
                [batch_size, fbank_size[1], fbank_size[2], fbank_size[3]]),
            tf.zeros((batch_size, p.num_zeropad_frames, fbank_size[2],
                      fbank_size[3]))
        ], 1)
    return feats  # shape [batch_size, nframes, featue_size, chnanels]
Exemplo n.º 2
0
        def grow_finished(finished_seq, finished_scores, finished_flags,
                          curr_seq, curr_scores, curr_finished):
            """
        Given sequences and scores from finished sequence and current finished sequence
        , will gather the top k=beam size sequences to update finished seq.
      """
            # padding zero for finished seq
            finished_seq = tf.concat(
                [finished_seq,
                 tf.zeros([batch_size, beam_size, 1], tf.int32)],
                axis=2)

            # mask unfinished curr seq
            curr_scores += (1. - tf.to_float(curr_finished)) * -INF

            # concatenating the sequences and scores along beam axis
            # (batch_size, 2xbeam_size, seq_len)
            curr_finished_seq = tf.concat([finished_seq, curr_seq], axis=1)
            curr_finished_scores = tf.concat([finished_scores, curr_scores],
                                             axis=1)
            curr_finished_flags = tf.concat([finished_flags, curr_finished],
                                            axis=1)
            return utils.compute_topk_scores_and_seq(
                curr_finished_seq, curr_finished_scores, curr_finished_scores,
                curr_finished_flags, beam_size, batch_size, "grow_finished")
Exemplo n.º 3
0
def _create_topk_unique(inputs, k):
    """Creates the top k values in sorted order with indices."""
    height = inputs.shape[0]
    width = inputs.shape[1]
    neg_inf_r0 = tf.constant(-np.inf, dtype=tf.float32)
    ones = tf.ones([height, width], dtype=tf.float32)
    neg_inf_r2 = ones * neg_inf_r0
    inputs = tf.where(tf.is_nan(inputs), neg_inf_r2, inputs)

    tmp = inputs
    topk_r2 = tf.zeros([height, k], dtype=tf.float32)
    for i in range(k):
        kth_order_statistic = tf.reduce_max(tmp, axis=1, keepdims=True)
        k_mask = tf.tile(
            tf.expand_dims(tf.equal(tf.range(k), tf.fill([k], i)), 0),
            [height, 1])
        topk_r2 = tf.where(k_mask, tf.tile(kth_order_statistic, [1, k]),
                           topk_r2)
        ge_r2 = tf.greater_equal(inputs,
                                 tf.tile(kth_order_statistic, [1, width]))
        tmp = tf.where(ge_r2, neg_inf_r2, inputs)

    log2_ceiling = int(math.ceil(math.log(float(int(width)), 2)))
    next_power_of_two = 1 << log2_ceiling
    count_mask = next_power_of_two - 1
    mask_r0 = tf.constant(count_mask)
    mask_r2 = tf.fill([height, k], mask_r0)
    topk_r2_s32 = tf.bitcast(topk_r2, tf.int32)
    topk_indices_r2 = tf.bitwise.bitwise_and(topk_r2_s32, mask_r2)
    return topk_r2, topk_indices_r2
Exemplo n.º 4
0
def _create_make_unique(inputs):
    """Replaces the lower bits of each element with iota."""
    if inputs.shape.ndims != 2:
        raise ValueError("Input of top_k_with_unique must be rank-2 "
                         "but got: %s" % inputs.shape)

    height = inputs.shape[0]
    width = inputs.shape[1]
    zeros = tf.zeros([height, width], dtype=tf.int32)

    log2_ceiling = int(math.ceil(math.log(int(width), 2)))
    next_power_of_two = 1 << log2_ceiling
    count_mask = ~(next_power_of_two - 1)
    count_mask_r0 = tf.constant(count_mask)
    count_mask_r2 = tf.fill([height, width], count_mask_r0)

    smallest_normal = 1 << 23
    smallest_normal_r0 = tf.constant(smallest_normal, dtype=tf.int32)
    smallest_normal_r2 = tf.fill([height, width], smallest_normal_r0)

    low_bit_mask = ~(1 << 31)
    low_bit_mask_r0 = tf.constant(low_bit_mask, dtype=tf.int32)
    low_bit_mask_r2 = tf.fill([height, width], low_bit_mask_r0)

    iota = tf.tile(tf.expand_dims(tf.range(width, dtype=tf.int32), 0),
                   [height, 1])

    input_r2 = tf.bitcast(inputs, tf.int32)
    abs_r2 = tf.bitwise.bitwise_and(input_r2, low_bit_mask_r2)
    if_zero_r2 = tf.equal(abs_r2, zeros)
    smallest_normal_preserving_sign_r2 = tf.bitwise.bitwise_or(
        input_r2, smallest_normal_r2)
    input_no_zeros_r2 = tf.where(if_zero_r2,
                                 smallest_normal_preserving_sign_r2, input_r2)

    and_r2 = tf.bitwise.bitwise_and(input_no_zeros_r2, count_mask_r2)
    or_r2 = tf.bitwise.bitwise_or(and_r2, iota)
    return tf.bitcast(or_r2, tf.float32)
Exemplo n.º 5
0
    def beam_search(symbols_to_logits_fn,
                    initial_ids,
                    beam_size,
                    decode_length,
                    vocab_size,
                    alpha,
                    eos_id,
                    states=None,
                    stop_early=True,
                    INF=1. * 1e20):
        """Beam search with length penalties."""
        batch_size = utils.shape_list(initial_ids)[0]

        initial_log_probs = tf.constant([[0.] + [-INF] * (beam_size - 1)])
        # (batch_size, beam_size)
        alive_log_probs = tf.tile(initial_log_probs, [batch_size, 1])

        alive_seq = utils.expand_to_beam_size(initial_ids, beam_size)
        # (batch_size, beam_size, 1)
        alive_seq = tf.expand_dims(alive_seq, axis=2)
        if states:
            states = nest.map_structure(
                lambda state: utils.expand_to_beam_size(state, beam_size),
                states)
        else:
            states = {}

        # (batch_size, beam_size, 1)
        finished_seq = tf.zeros(utils.shape_list(alive_seq), tf.int32)
        # (batch_size, beam_size)
        finished_scores = tf.ones([batch_size, beam_size]) * -INF
        # (batch_size, beam_size)
        finished_flags = tf.zeros([batch_size, beam_size], tf.bool)

        def grow_finished(finished_seq, finished_scores, finished_flags,
                          curr_seq, curr_scores, curr_finished):
            """
        Given sequences and scores from finished sequence and current finished sequence
        , will gather the top k=beam size sequences to update finished seq.
      """
            # padding zero for finished seq
            finished_seq = tf.concat(
                [finished_seq,
                 tf.zeros([batch_size, beam_size, 1], tf.int32)],
                axis=2)

            # mask unfinished curr seq
            curr_scores += (1. - tf.to_float(curr_finished)) * -INF

            # concatenating the sequences and scores along beam axis
            # (batch_size, 2xbeam_size, seq_len)
            curr_finished_seq = tf.concat([finished_seq, curr_seq], axis=1)
            curr_finished_scores = tf.concat([finished_scores, curr_scores],
                                             axis=1)
            curr_finished_flags = tf.concat([finished_flags, curr_finished],
                                            axis=1)
            return utils.compute_topk_scores_and_seq(
                curr_finished_seq, curr_finished_scores, curr_finished_scores,
                curr_finished_flags, beam_size, batch_size, "grow_finished")

        def grow_alive(curr_seq, curr_scores, curr_log_probs, curr_finished,
                       states):
            """Given sequences and scores, will gather the top k=beam size sequences."""
            curr_scores += tf.to_float(curr_finished) * -INF
            return utils.compute_topk_scores_and_seq(curr_seq, curr_scores,
                                                     curr_log_probs,
                                                     curr_finished, beam_size,
                                                     batch_size, "grow_alive",
                                                     states)

        def grow_topk(i, alive_seq, alive_log_probs, states):
            """Inner beam search loop."""
            flat_ids = tf.reshape(alive_seq, [batch_size * beam_size, -1])

            # (batch_size * beam_size, decoded_length)
            if states:
                flat_states = nest.map_structure(utils.merge_beam_dim, states)
                flat_logits, flat_states = symbols_to_logits_fn(
                    flat_ids, i, flat_states)
                states = nest.map_structure(
                    lambda t: utils.unmerge_beam_dim(t, batch_size, beam_size),
                    flat_states)
            else:
                flat_logits = symbols_to_logits_fn(flat_ids)

            logits = tf.reshape(flat_logits, [batch_size, beam_size, -1])
            candidate_log_probs = utils.log_prob_from_logits(logits)
            log_probs = candidate_log_probs + tf.expand_dims(alive_log_probs,
                                                             axis=2)

            length_penalty = tf.pow(((5. + tf.to_float(i + 1)) / 6.), alpha)

            curr_scores = log_probs / length_penalty
            flat_curr_scores = tf.reshape(curr_scores,
                                          [-1, beam_size * vocab_size])

            topk_scores, topk_ids = tf.nn.top_k(flat_curr_scores,
                                                k=beam_size * 2)
            topk_log_probs = topk_scores * length_penalty

            topk_beam_index = topk_ids // vocab_size
            topk_ids %= vocab_size  # Unflatten the ids
            batch_pos = utils.compute_batch_indices(batch_size, beam_size * 2)
            topk_coordinates = tf.stack([batch_pos, topk_beam_index], axis=2)

            topk_seq = tf.gather_nd(alive_seq, topk_coordinates)
            if states:
                states = nest.map_structure(
                    lambda state: tf.gather_nd(state, topk_coordinates),
                    states)
            topk_seq = tf.concat(
                [topk_seq, tf.expand_dims(topk_ids, axis=2)], axis=2)

            topk_finished = tf.equal(topk_ids, eos_id)

            return topk_seq, topk_log_probs, topk_scores, topk_finished, states

        def inner_loop(i, alive_seq, alive_log_probs, finished_seq,
                       finished_scores, finished_flags, states):
            """Inner beam search loop."""
            topk_seq, topk_log_probs, topk_scores, topk_finished, states = grow_topk(
                i, alive_seq, alive_log_probs, states)
            alive_seq, alive_log_probs, _, states = grow_alive(
                topk_seq, topk_scores, topk_log_probs, topk_finished, states)
            finished_seq, finished_scores, finished_flags, _ = grow_finished(
                finished_seq, finished_scores, finished_flags, topk_seq,
                topk_scores, topk_finished)

            return (i + 1, alive_seq, alive_log_probs, finished_seq,
                    finished_scores, finished_flags, states)

        def _is_finished(i, unused_alive_seq, alive_log_probs,
                         unused_finished_seq, finished_scores,
                         unused_finished_in_finished, unused_states):
            """Checking termination condition.
      """
            max_length_penalty = tf.pow(
                ((5. + tf.to_float(decode_length)) / 6.), alpha)
            lower_bound_alive_scores = alive_log_probs[:,
                                                       0] / max_length_penalty

            if not stop_early:
                lowest_score_of_finished_in_finished = tf.reduce_min(
                    finished_scores)
            else:
                lowest_score_of_finished_in_finished = tf.reduce_max(
                    finished_scores, axis=1)

            bound_is_met = tf.reduce_all(
                tf.greater(lowest_score_of_finished_in_finished,
                           lower_bound_alive_scores))

            return tf.logical_and(tf.less(i, decode_length),
                                  tf.logical_not(bound_is_met))

        inner_shape = tf.TensorShape([None, None, None])

        state_struc = nest.map_structure(utils.get_state_shape_invariants,
                                         states)
        (_, alive_seq, alive_log_probs, finished_seq, finished_scores,
         finished_flags, states) = tf.while_loop(
             _is_finished,
             inner_loop, [
                 tf.constant(0), alive_seq, alive_log_probs, finished_seq,
                 finished_scores, finished_flags, states
             ],
             shape_invariants=[
                 tf.TensorShape([]), inner_shape,
                 alive_log_probs.get_shape(), inner_shape,
                 finished_scores.get_shape(),
                 finished_flags.get_shape(), state_struc
             ],
             parallel_iterations=1,
             back_prop=False)

        alive_seq.set_shape((None, beam_size, None))
        finished_seq.set_shape((None, beam_size, None))
        finished_seq = tf.where(tf.reduce_any(finished_flags, 1), finished_seq,
                                alive_seq)
        finished_scores = tf.where(tf.reduce_any(finished_flags, 1),
                                   finished_scores, alive_log_probs)
        return finished_seq, finished_scores, states