Beispiel #1
0
    def ctc_lambda_func(args, x):
        """
        Create cost function (CTC)
        """
        y_pred, labels, input_length, label_length = x

        return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
Beispiel #2
0
def ctc_loss_v2(labels, y_pred):
    '''
        Arguments:
            labels: arrays with shape [batch_size, time_steps]  
                            Each time step is an interger,
            y_pred: arrays with shape [batch_size, time_steps, num_of_class] 
                            Each time step is a numpy array with length of num_of_class
        Return:
            loss: single float number # ex: 0.12312
    '''
    def caculate_y_pred_lengths(y_pred):    
        x = tf.shape(y_pred)[1]
        x = tf.tile([x], tf.shape(y_pred)[:1])
        x = tf.reshape(tf.squeeze(x), [-1, 1])    
        x = tf.identity(x, 'y_pred_length')
        return x
    
    def caculate_label_lengths(labels, padding_value=-1):
        x = labels
        x = tf.logical_not(tf.equal(x, padding_value))
        x = tf.cast(x, tf.int32)
        x = tf.reduce_sum(x, -1)
        x =  tf.reshape(x, [-1, 1])
        x = tf.identity(x, 'label_length')
        return  x 
    
    labels = tf.identity(labels, 'labels')
    y_pred = tf.identity(y_pred, 'y_preds')
    input_length = caculate_y_pred_lengths(y_pred)
    
    label_length = caculate_label_lengths(labels)
#     for x in [labels, y_pred, input_length, label_length]: print(x.name, x.shape)
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
Beispiel #3
0
def ctc_loss(y_true, y_pred):
    """
    Runs CTC Loss Algorithm on each batch element

    :param y_true: tensor (samples, max_string_length) containing the truth labels.
    :param y_pred: tensor (samples, time_steps, num_categories) containing the prediction, or output of the softmax.

    * caution

    input_length : tensor (samples, 1) containing the sequence length for each batch item in y_pred
    label_length : tensor (samples, 1) containing the sequence length for each batch item in y_true

    y_true는 [3,7,12,1,2,-1,-1,-1,-1] 와 같은 형태로 구성되어 있음. -1은 Blank를 의미
    처음 등장하는 -1의 인덱스가 y_true의 sequnece length와 동일

    y_pred의 총 width와 input_length는 동일

    """

    # Get the Length of Prediction
    shape = tf.shape(y_pred)
    batch_size = shape[0]
    max_length = shape[1, None, None]
    input_length = tf.tile(max_length, [batch_size, 1])

    # Get the Length of Input
    label_length = tf.argmin(y_true, axis=-1)[:, None]

    return K.ctc_batch_cost(y_true, y_pred, input_length, label_length)
def focal_ctc_lambda_func(args):
    alpha = 0.75
    gamma = 0.5
    labels, y_pred, input_length, label_length = args
    ctc_loss = K.ctc_batch_cost(labels, y_pred, input_length, label_length)
    p = tf.exp(-ctc_loss)
    focal_ctc_loss = alpha * tf.pow((1 - p), gamma) * ctc_loss
    return focal_ctc_loss
Beispiel #5
0
def ctc_loss(labels, y_pred):
    '''
        Arguments:
            labels: arrays with shape [batch_size, time_steps]  
                            Each time step is an interger,
            y_pred: arrays with shape [batch_size, time_steps, num_of_class] 
                            Each time step is a numpy array with length of num_of_class
        Return:
            loss: single float number # ex: 0.12312
    '''
    def caculate_y_pred_lengths(y_pred):    
        x = tf.shape(y_pred)[1]
        x = tf.tile([x], tf.shape(y_pred)[:1])
        return tf.reshape(tf.squeeze(x), [-1, 1])    
    
    def caculate_label_lengths(labels, padding_value=-1):
        i0 = tf.constant(0)
        m0 = tf.constant([1], shape=[1,1])
        c = lambda i, m: i < tf.shape(labels)[0]
        def b(i, m):
            label_i = labels[i]
            invalid_indexes = tf.reshape(tf.where(tf.equal(label_i, padding_value)), [-1])        
            l = tf.shape(invalid_indexes)[0]
            li = tf.cond(tf.equal(l, 0), 
                         lambda: tf.shape(label_i)[0], 
                         lambda: tf.cast(invalid_indexes[0], tf.int32)
                        )
            li = tf.reshape(li, [1,1])
            m = tf.concat([m, li], axis=0)
            m = tf.reshape(m, [-1, 1])
            return [i+1, m]


        out_loop = tf.while_loop(
            c, b, loop_vars=[i0, m0],
            shape_invariants=[i0.get_shape(), tf.TensorShape([None, 1])])
        out = out_loop[1][1:]
        out = tf.cast(out, tf.int32)
        return out
    
    labels = tf.concat([labels,tf.ones(shape=[tf.shape(labels)[0], 1])*-1], axis=-1)
    input_length = caculate_y_pred_lengths(y_pred)
    label_length = caculate_label_lengths(labels)
    label_length = tf.reshape(label_length, [-1, 1])
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
Beispiel #6
0
def ctc_lambda_func(args):
    y_pred, labels, input_length, label_length = args
    # the 2 is critical here since the first couple outputs of the RNN
    # tend to be garbage:
    y_pred = y_pred[:, 2:, :]
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
Beispiel #7
0
def ctc_lambda(args):
    labels, y_pred, input_length, label_length = args
    y_pred = y_pred[:, :, :]
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def ctc_lambda_func(args):
    iy_pred, ilabels, iinput_length, ilabel_length = args
    # the 2 is critical here since the first couple outputs of the RNN
    # tend to be garbage:
    iy_pred = iy_pred[:, 2:, :]  # no such influence
    return K.ctc_batch_cost(ilabels, iy_pred, iinput_length, ilabel_length)
def ctc_lambda_func(args):
    y_pred, labels, input_length, label_length = args
    y_pred = y_pred
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)