Пример #1
0
def contrastive_loss_old(labels, dists):

    label_first = labels[0:1, :]
    other_labels = labels[1:, :]

    labels_shifted = K.concatenate(
        [labels, other_labels, label_first],
        axis=0)  #   [ l1 ........ ln  | l2 ... ln-1 ln ]
    labels_orig = K.concatenate(
        [labels, labels], axis=0)  #   [ l1 ........ ln  | l1 ... ln-2 ln ]
    zeros = K.zeros_like(labels_orig)  #   [ 0  ........  0  | 0  ...   0   0 ]
    h = K.cast(K.equal(labels_orig - labels_shifted, zeros),
               dtype='float32')  #   [ 1  1 ......  1  | 0  ...   1   0 ]
    # h:   ALL ONES       |    MOST ZEROS
    # h[i] = 1  where labels_orig[i] == labels_shifted[i]  (i-th image correlated with i+1-th image, i.e. same artwork)
    # h[i] = 0  where labels_orig[i] != labels_shifted[i]

    first_dist = dists[0:1]
    other_dists = dists[1:]
    shifted_dists = K.concatenate(
        [dists, other_dists, first_dist],
        axis=0)  # [ d1 ........ dn  | d1 ... dn-2 dn ]

    # equation:  Lcon = (1/2N) SUM[ h(i) d(i)^2 + (1-h(i)) max(1-d(i), 0)^2
    Z = K.zeros_like(shifted_dists)
    max_z_sd = K.max(K.stack([1 - shifted_dists, Z]), axis=0, keepdims=False)
    #max_z_sd = K.sqrt(K.cast(K.shape(shifted_dists)[0], dtype='float32')) - shifted_dists

    first_operand = h * K.square(shifted_dists)
    second_operand = (1 - h) * K.square(max_z_sd)
    tensor_sum = first_operand + second_operand
    sum = K.sum(tensor_sum, axis=0) / K.cast(K.shape(shifted_dists)[0],
                                             dtype='float32')

    return K.mean(sum)
Пример #2
0
def reweight(y_true,
             y_pred,
             tp_weight=.3,
             tn_weight=.3,
             fp_weight=4,
             fn_weight=0.7):
    # Get predictions
    y_pred_classes = K.greater_equal(y_pred, 0.5)
    y_pred_classes_float = K.cast(y_pred_classes, K.floatx())

    # Get misclassified examples
    wrongly_classified = K.not_equal(y_true, y_pred_classes_float)
    wrongly_classified_float = K.cast(wrongly_classified, K.floatx())

    # Get correctly classified examples
    correctly_classified = K.equal(y_true, y_pred_classes_float)
    correctly_classified_float = K.cast(correctly_classified, K.floatx())

    # Get tp, fp, tn, fn
    tp = correctly_classified_float * y_true
    tn = correctly_classified_float * (1 - y_true)
    fp = wrongly_classified_float * y_true
    fn = wrongly_classified_float * (1 - y_true)

    # Get weights
    weight_tensor = tp_weight * tp + fp_weight * fp + tn_weight * tn + fn_weight * fn

    loss = K.binary_crossentropy(y_true, y_pred)
    weighted_loss = loss * weight_tensor
    return weighted_loss
 def acc(y_true, y_pred):
     y_true, payoffs = splitter(y_true)
     return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)
Пример #4
0
def seq_accuracy(y_true, y_pred):
    return K.cast(K.all(K.equal(y_true, K.round(y_pred)), axis=-2), 'float32')
Пример #5
0
def binary_accuracy(y_true, y_pred):
    return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)
Пример #6
0
def accuracy(y_true, y_pred):  # Tensor上的操作
    '''Compute classification accuracy with a fixed threshold on distances.
    '''
    return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))
Пример #7
0
def binary_accuracy(y_true, y_pred):
    return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)