Ejemplo n.º 1
0
def _dense_alphabeta_dtd(layer, R, beta, parameter2):
    print('_dense_alphabeta_dtd')

    if layer.activation.__name__ == 'softmax':
        raise NotImplementedError(
            'Please use softmax only on Activation layer '
            'and not directly on Dense layer')

    alpha = 1 + beta

    Z = layer.kernel * K.expand_dims(layer.input, axis=-1)

    if not alpha == 0:
        Zp = K.maximum(Z, 0)
        Zsp = K.sum(Zp, axis=1, keepdims=True)
        Zsp += 1e-12 * K.switch(K.greater_equal(Zsp, 0),
                                K.ones_like(Zsp, dtype=K.floatx()),
                                K.ones_like(Zsp, dtype=K.floatx()) * -1.)
        Ralpha = alpha * K.sum((Zp / Zsp) * K.expand_dims(R, axis=1), axis=2)
    else:
        Ralpha = 0

    if not beta == 0:
        Zn = K.minimum(Z, 0)
        Zsn = K.sum(Zn, axis=1, keepdims=True)
        Zsn += 1e-12 * K.switch(K.greater_equal(Zsn, 0),
                                K.ones_like(Zsn, dtype=K.floatx()),
                                K.ones_like(Zsn, dtype=K.floatx()) * -1.)
        Rbeta = beta * K.sum((Zn / Zsn) * K.expand_dims(R, axis=1), axis=2)
    else:
        Rbeta = 0

    return Ralpha - Rbeta
Ejemplo n.º 2
0
def recall_binary(y_true, y_pred):
    """ Keras metric for computing recall for a binary classification task during training

        Recall = true positives / (true positives + false negatives)

        Parameters
        ----------
        y_true : K.variable
            Ground truth N-dimensional Keras variable of float type with only 0's and 1's.
        y_pred : K.variable
            Predicted Keras variable of float type with predicted values between 0 and 1.

        Returns
        -------
        K.variable
            A single value indicating the recall.

    """

    logical_and = K.cast(
        K.all(K.stack([K.cast(y_true, 'bool'),
                       K.greater_equal(y_pred, 0.5)],
                      axis=0),
              axis=0), 'float32')
    logical_or = K.cast(
        K.any(K.stack([K.cast(y_true, 'bool'),
                       K.greater_equal(y_pred, 0.5)],
                      axis=0),
              axis=0), 'float32')
    tp = K.sum(logical_and)
    fn = K.sum(logical_or - K.cast(K.greater_equal(y_pred, 0.5), 'float32'))
    return K.switch(K.equal(tp, K.variable(0)), K.variable(0), tp / (tp + fn))
    def is_in_geo_range(patch_x_and_y):

        patch_x_tensor = patch_x_and_y[0]
        patch_y_tensor = patch_x_and_y[1]

        base_leaner_x_max = K.constant(geo_range[0], dtype='float32')
        base_leaner_x_min = K.constant(geo_range[1], dtype='float32')
        base_leaner_y_max = K.constant(geo_range[2], dtype='float32')
        base_leaner_y_min = K.constant(geo_range[3], dtype='float32')

        layer_output = patch_x_and_y[2]
        #coef_geo_range = K.ones((K.int_shape(img_input)[0],1),dtype='float32')
        coef_geo_range = layer_output

        coef_geo_range = K.switch(
            K.less_equal(patch_x_tensor, base_leaner_x_max),
            coef_geo_range * 1.0, coef_geo_range * 0.0)

        coef_geo_range = K.switch(
            K.greater_equal(patch_x_tensor, base_leaner_x_min),
            coef_geo_range * 1.0, coef_geo_range * 0.0)

        coef_geo_range = K.switch(
            K.less_equal(patch_y_tensor, base_leaner_y_max),
            coef_geo_range * 1.0, coef_geo_range * 0.0)

        coef_geo_range = K.switch(
            K.greater_equal(patch_y_tensor, base_leaner_y_min),
            coef_geo_range * 1.0, coef_geo_range * 0.0)

        return coef_geo_range
Ejemplo n.º 4
0
def get_jaccard_index1_from_sigmoid(y_true, y_pred, smooth=default_smooth):
    """Calculate jaccard index but from signed-dist vectors"""
    thresh = K.constant(0.5)
    ji = get_jaccard_index1(K.cast(K.greater_equal(y_true, thresh), 'float32'),
                            K.cast(K.greater_equal(y_pred, thresh), 'float32'),
                            smooth=K.constant(smooth))
    return ji
Ejemplo n.º 5
0
def berHu_loss_elementwise_w_border(y_true, y_pred):
    ''' Proposed Lpano as described in our paper. '''
    ret_loss = 0

    y_diff = y_true - y_pred
    y_diff_abs = K.abs(y_diff)
    c = (1.0/5.0)*K.max(y_diff_abs)

    L2_berHu = (K.pow(y_diff_abs, 2) + c**2) / (2*c)
    berHu_tensor = tf.where(K.less_equal(y_diff_abs, c), y_diff_abs, L2_berHu)

    ## regular reverse huber
    n_pixels = tf.to_float(tf.size(y_true))
    berHu_overall = K.sum(berHu_tensor) / n_pixels

    ## add extra weight to the borders
    ## build boolean mask by combining boundary conditions for the rows and cols
    shape = tf.shape(berHu_tensor)
    bs, R, C, chans = tf.meshgrid(tf.range(shape[0]), tf.range(shape[1]), tf.range(shape[2]), tf.range(shape[3])) ## batch_size, width, height, channels
    row_lines = tf.logical_or(K.less_equal(R, 16), K.greater_equal(R, 112)) # these numbers will need to change when moving to larger image sizes or different border width
    col_lines = tf.logical_or(K.less_equal(C, 16), K.greater_equal(C, 240))
    border_mask = tf.logical_or(row_lines, col_lines)
    border_berHu_vals = tf.boolean_mask(berHu_tensor, border_mask)

    n_border_pixels = tf.to_float(tf.size(border_berHu_vals))
    berHu_border = K.sum(border_berHu_vals) / n_border_pixels

    lambda_frac = 0.5 # default amount for the weight matrix
    ret_loss = berHu_overall + lambda_frac*berHu_border 

    return ret_loss
Ejemplo n.º 6
0
def dice_coef_for_sigmoid(y_true, y_pred, threshold=0.5):
    y_true_bi = K.cast(K.greater_equal(y_true, threshold), 'float32')
    y_pred_bi = K.cast(K.greater_equal(y_pred, threshold), 'float32')
    inter = K.sum(y_true_bi*y_pred_bi, axis=[1, 2])
    union = K.sum(y_true_bi, axis=[1, 2]) + K.sum(y_pred_bi, axis=[1, 2])
    dice_coef = (2.0*inter + K.epsilon()) / (union + K.epsilon())
    return K.mean(dice_coef, axis=-1)
Ejemplo n.º 7
0
def greater_equal(f, other):
    """Element-wise comparison applied to the `Functional` objects.

    # Arguments
        f: Functional object.
        other: A python number or a tensor or a functional object.

    # Returns
        A Functional.
    """
    validate_functional(f)

    inputs = f.inputs.copy()
    if is_functional(other):
        inputs += to_list(other.inputs)
        lmbd = [Lambda(lambda x: K.cast_to_floatx(K.greater_equal(x[0], x[1])), name=graph_unique_name("greater_equal")) for X in f.outputs]
    else:
        _warn_for_ndarray(other)
        lmbd = [Lambda(lambda x: K.cast_to_floatx(K.greater_equal(x, other)), name=graph_unique_name("greater_equal")) for X in f.outputs]

    Functional = f.get_class()
    res = Functional(
        inputs=unique_tensors(inputs),
        outputs=_apply_operation(lmbd, f, other),
        layers=lmbd
    )
    return res
Ejemplo n.º 8
0
    def call(self, x):

        y = x[1]
        BinPop = {}

        id_bin_1 = K.cast(K.less(x, self.ValueRange[0]), K.floatx())
        id_bin_last = K.cast(K.greater_equal(x, self.ValueRange[1]),
                             K.floatx())

        BinPop[1] = K.sum(id_bin_1, axis=1, keepdims=True)
        BinPop[self.nbins] = K.sum(id_bin_last, axis=1, keepdims=True)

        start = self.ValueRange[0]
        stop = start + self.binwidth
        for i in range(2, self.nbins):

            id_bin = K.tf.multiply(
                K.cast(K.greater_equal(x, start), K.floatx()),
                K.cast(K.less(x, stop), K.floatx()))
            BinPop[i] = K.sum(id_bin, axis=1, keepdims=True)

            for i2 in range(2, self.nbins):
                id_bin_i2 = K.tf.multiply(
                    K.cast(K.greater_equal(y, start), K.floatx()),
                    K.cast(K.less(y, stop), K.floatx()))

            start = stop
            stop = start + self.binwidth

        hist = K.concatenate(list(BinPop.values()))
        return hist
Ejemplo n.º 9
0
 def call(self, x):
     min_cat = K.less(x, self.thresholds[0])
     max_cat = K.greater_equal(x, self.thresholds[-1])
     other_cat = map(
         lambda (th1, th2): K.all(K.stack([K.greater_equal(x, th1), K.less(x, th2)], axis=0), axis=0),
         zip(self.thresholds[:-1], self.thresholds[1:])
     )
     return K.cast(K.concatenate([min_cat] + other_cat + [max_cat]), K.floatx())
Ejemplo n.º 10
0
def EuiLoss(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    d = K.sum(K.sqrt(K.square(y_true_f - y_pred_f) + 1e-12))
    a = K.cast(K.greater_equal(d, 0.5), dtype='float32')
    b = K.cast(K.greater_equal(0.12, d), dtype='float32')
    c = K.cast(K.greater_equal(0.3, d), dtype='float32')
    loss = (2 + 4 * a - 0.5 * b - 1 * c) * d + 0.2 * y_pred_f *d
    return loss
Ejemplo n.º 11
0
def Checking_if_object(x1_window, y1_window, x2_window, y2_window, x_max_true,
                       x_min_true, y_max_true, y_min_true):
    x_middle_true = (x_max_true + x_min_true) / 2.0
    y_middle_true = (y_max_true + y_min_true) / 2.0
    matching_x = tf.logical_and(K.greater_equal(x=x_middle_true, y=x1_window),
                                K.greater_equal(x=x2_window, y=x_middle_true))
    matching_y = tf.logical_and(K.greater_equal(x=y_middle_true, y=y1_window),
                                K.greater_equal(x=y2_window, y=y_middle_true))
    matching = tf.logical_and(matching_x, matching_y)
    return matching
Ejemplo n.º 12
0
def get_jaccard_index1_from_sdt(y_true, y_pred, smooth=default_smooth):
    """Calculate jaccard index but from signed-dist vectors"""
    thresh = K.constant(0)
    y_true = K.cast(K.greater_equal(y_true, thresh), 'float32')
    y_pred = K.cast(K.greater_equal(y_pred, thresh), 'float32')

    ji = get_jaccard_index1(K.cast(K.argmax(y_true, axis=-1), 'int64'),
                            K.cast(K.argmax(y_pred, axis=-1), 'int64'),
                            smooth=K.constant(smooth))
    return ji
Ejemplo n.º 13
0
    def get_spikes(self, new_mem):
        """Linear activation."""

        thr = self._v_thresh
        pos_spikes = k.cast(
            tf.logical_and(k.less(self.mem, thr),
                           k.greater_equal(new_mem, thr)), k.floatx())
        neg_spikes = k.cast(
            tf.logical_and(k.less(new_mem, thr),
                           k.greater_equal(self.mem, thr)), k.floatx())
        return pos_spikes - neg_spikes
Ejemplo n.º 14
0
def intersection_over_union(y_true, y_pred):
    smooth = 1.
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    y_t = K.greater_equal(y_true_f, 0.5)
    y_p = K.greater_equal(y_pred_f, 0.5)
    union1 = [i for i, j in zip(y_true_f, y_pred_f) if i or j]
    union2 = [j for i, j in zip(y_true_f, y_pred_f) if i or j]
    intersection = [i for i, j in zip(y_true_f, y_pred_f) if i and j]
    unionAll = union1 + union2 + intersection
    return (np.sum(intersection) + smooth) / float(np.sum(unionAll) + smooth)
Ejemplo n.º 15
0
    def loss(y, y_pred):
        data['steps'] += 1
        data['epoch'] = int(data['steps'] / steps_per_epoch) + 1  # which epoch is running
        decay = pow(rate, max(1, data['epoch'] - epochs_without_decay))
        assert decay > 1
        if data['epoch'] > epochs_without_decay:
            data['ones'] = tf.ones_like(y_pred) if data['ones'] is None else data['ones']
            y_pred = K.switch(K.greater_equal(y_pred, 1 - smoothing_threshold), data['ones'], y_pred)
            y_pred = K.switch(K.greater_equal(y_pred, 0.5), K.pow(y_pred, 1 / decay), y_pred)
            y_pred = K.switch(K.less(y_pred, 0.5), K.pow(y_pred, decay), y_pred)

        return K.categorical_crossentropy(y, y_pred)
Ejemplo n.º 16
0
def Padlayer1(x):
    print("before cropping: ", K.shape(x))
    image_rows = K.shape(x)[1]
    image_cols = K.shape(x)[2]
    image_batch = K.shape(x)[3]
    difsizeZ = lz - image_batch
    difsizeY = ly - image_cols
    difsizeX = lx - image_rows
    Vy = tf.cond(K.greater_equal(difsizeZ, 0),
                 lambda: K.cast(difsizeZ, 'int32'), lambda: 0)
    Xy = tf.cond(K.greater_equal(difsizeX, 0),
                 lambda: K.cast(difsizeX, 'int32'), lambda: 0)
    Yy = tf.cond(K.greater_equal(difsizeY, 0),
                 lambda: K.cast(difsizeY, 'int32'), lambda: 0)
    return K.reshape(K.stack([Xy, Yy, Vy]), (1, 3))
Ejemplo n.º 17
0
def EuiLoss_new(y_true, y_pred):
    y_true_t = y_true[:, 0:1]
    y_pred_t = y_pred[:, 0:1]
    y_true_f = K.flatten(y_true_t)
    y_pred_f = K.flatten(y_pred_t)

    d = K.mean(K.sqrt(K.square(y_true_f - y_pred_f) + 1e-12))
    a = K.cast(K.greater_equal(d, 0.5), dtype='float32')
    b = K.cast(K.greater_equal(0.12, d), dtype='float32')
    c = K.cast(K.greater_equal(0.3, d), dtype='float32')
    #e = K.cast(y_pred_f, dtype='float32')

    loss = (2 + 4 * a - 0.5 * b - 1 * c) * d + 0.2 * y_pred_f * d

    return loss
Ejemplo n.º 18
0
def m_accuracy(true_y,pred_y):
    treshold = 0
    mask = Lambda(lambda x:K.greater_equal(x, treshold))(true_y)
    mask = Lambda(lambda x:K.cast(x, 'float32'))(mask)
    pred_label = Lambda(lambda x: x * mask)(pred_y)
    true_label = Lambda(lambda x: x * mask)(true_y)
    return K.mean(K.equal(K.argmax(true_label, axis=-1), K.argmax(pred_label, axis=-1)))
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6):
    
    '''
    Filters the classified YOLO boxes by thresholding on the objects classified and on the basis of box_confidence
    
    Returns values of the filter:
    scores -- tensor of shape (None,), containing the class probability score for selected boxes
    boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes
    classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes
    
    Note: "None" is here because we don't know the exact number of selected boxes, as it depends on the threshold. 
    For example, the actual output size of scores would be (10,) if there are 10 boxes.
    
    '''
    
    # Computing the box scores
    box_scores = np.multiply(box_confidence,box_class_probs)
    
    # Finding the box_classes thanks to the max box_scores, keep track of the corresponding score
    box_classes = K.argmax(box_scores, axis=-1)      # gives the index of the maximum number in the obtained box_score matrix/array (1D)
    box_class_scores = K.max(box_scores, axis=-1)    # gives the maximum value from the box_score
    
    # Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the
    # same dimension as box_class_scores, and the boxes with probability >= threshold are classified as True or 1 and remaining False or 0
    filtering_mask = K.greater_equal(box_class_scores, threshold)
    
    # Applying the mask to scores, boxes and classes
    scores = tf.boolean_mask(box_class_scores,filtering_mask)       # the scores grater than corresponding threshold are selected
    boxes = tf.boolean_mask(boxes,filtering_mask)                   # the boxes grater than corresponding threshold are selected
    classes = tf.boolean_mask(box_classes,filtering_mask)           # the classes grater than corresponding threshold are selected
    
    return scores, boxes, classes
def _simple_lrp(layer, R, parameter):
    '''
  LRP according to Eq(56) in DOI: 10.1371/journal.pone.0130140
  '''
    print('_maxpooling3d_simple_lrp')

    volume_patches = patches.extract_volume_patches(layer.input,
                                                    layer.pool_size,
                                                    layer.strides,
                                                    layer.padding)
    Z = K.equal(
        K.reshape(layer.output,
                  (-1, layer.output_shape[1], layer.output_shape[2],
                   layer.output_shape[3], 1, 1, 1, layer.output_shape[4])),
        volume_patches)
    Z = K.switch(Z, K.ones_like(Z, dtype=K.floatx()),
                 K.zeros_like(Z, dtype=K.floatx()))
    Zs = K.sum(Z, axis=[4, 5, 6], keepdims=True)
    Zs += 1e-12 * K.switch(K.greater_equal(Zs, 0),
                           K.ones_like(Zs, dtype=K.floatx()),
                           K.ones_like(Zs, dtype=K.floatx()) * -1.)
    result = (Z / Zs) * K.reshape(
        R, (-1, layer.output_shape[1], layer.output_shape[2],
            layer.output_shape[3], 1, 1, 1, layer.output_shape[4]))
    return patches.restitch_volume_patches(result, layer.input_shape,
                                           layer.pool_size, layer.strides,
                                           layer.padding)
Ejemplo n.º 21
0
def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    mask = K.cast(K.greater_equal(y_true_f, -0.5), dtype='float32')
    intersection = K.sum(y_true_f * y_pred_f * mask)
    return (2. * intersection + ss) / (K.sum(y_true_f * mask) +
                                       K.sum(y_pred_f * mask) + ss)
Ejemplo n.º 22
0
def binary_crossentropy_with_ranking(y_true, y_pred):
    """ Trying to combine ranking loss with numeric precision"""
    # first get the log loss like normal
    logloss = K.mean(K.binary_crossentropy(y_pred, y_true), axis=-1)

    # next, build a rank loss

    # clip the probabilities to keep stability
    y_pred_clipped = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())

    # translate into the raw scores before the logit
    y_pred_score = K.log(y_pred_clipped / (1 - y_pred_clipped))

    # determine what the maximum score for a zero outcome is
    y_pos = K.cast(K.greater_equal(y_true, 0.5), 'float32')
    y_neg = K.cast(K.less(y_true, 0.5), 'float32')

    y_pred_score_zerooutcome_max = K.max(y_pred_score * y_neg)

    # determine how much each score is above or below it
    rankloss = y_pred_score - y_pred_score_zerooutcome_max

    # only keep losses for positive outcomes
    rankloss = rankloss * y_pos

    # only keep losses where the score is below the max
    rankloss = K.square(K.clip(rankloss, -10, 0))

    # average the loss for just the positive outcomes
    rankloss = K.sum(rankloss, axis=-1) / (K.sum(y_pos) + 1)

    # return (rankloss + 1) * logloss - an alternative to try
    return rankloss + logloss
Ejemplo n.º 23
0
def BooleanMask(x):

    output = K.greater_equal(x[0], x[1])
    output = K.cast(output, dtype='float32')
    output = K.tf.multiply(output, x[1])

    return output
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold=.6):
    """Filters YOLO boxes by thresholding on object and class confidence.
    
    Arguments:
    box_confidence -- tensor of shape (19, 19, 5, 1)
    boxes -- tensor of shape (19, 19, 5, 4)
    box_class_probs -- tensor of shape (19, 19, 5, 80)
    threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box
    
    Returns:
    scores -- tensor of shape (None,), containing the class probability score for selected boxes
    boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes
    classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes
    
    Note: "None" is here because you don't know the exact number of selected boxes, as it depends on the threshold. 
    For example, the actual output size of scores would be (10,) if there are 10 boxes.
    """

    # Step 1: Compute box scores
    box_scores = box_confidence * box_class_probs

    # Step 2: Find the box_classes using the max box_scores, keep track of the corresponding score
    box_classes = K.argmax(box_scores, axis=-1)
    box_class_scores = K.max(box_scores, axis=-1)

    # Step 3: Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the
    # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)
    filtering_mask = K.greater_equal(box_class_scores, threshold)

    # Step 4: Apply the mask to box_class_scores, boxes and box_classes
    scores = tf.boolean_mask(box_class_scores, filtering_mask)
    boxes = tf.boolean_mask(boxes, filtering_mask)
    classes = tf.boolean_mask(box_classes, filtering_mask)

    return scores, boxes, classes
Ejemplo n.º 25
0
    def zeroing_function(orig_prediction_tensor):
        """Zeroes out predicted heating rate at top of profile.

        :param orig_prediction_tensor: Keras tensor with model predictions.
        :return: new_prediction_tensor: Same as input but with top heating rate
            zeroed out.
        """

        num_outputs = orig_prediction_tensor.get_shape().as_list()[-1]

        zero_tensor = K.greater_equal(
            orig_prediction_tensor[..., top_heating_rate_index], 1e12)
        zero_tensor = K.cast(zero_tensor, dtype=K.floatx())

        new_prediction_tensor = K.concatenate(
            (orig_prediction_tensor[..., :top_heating_rate_index],
             K.expand_dims(zero_tensor, axis=-1)),
            axis=-1)

        if top_heating_rate_index == num_outputs - 1:
            return new_prediction_tensor

        return K.concatenate(
            (new_prediction_tensor,
             orig_prediction_tensor[..., (top_heating_rate_index + 1):]),
            axis=-1)
Ejemplo n.º 26
0
    def loss(y_true, y_pred):
        L = _categorical_crossentropy(target=y_true,
                                      output=y_pred,
                                      axis=axis,
                                      from_logits=from_logits)
        _epsilon = K.epsilon()
        y_true_p = K.argmax(y_true, axis=axis)
        y_pred_bin = K.cast(K.greater_equal(y_pred, threshold),
                            K.dtype(y_true)) if from_logits else K.argmax(
                                y_pred, axis=axis)
        y_pred_probs = y_preds if from_logits else K.max(y_pred, axis=axis)
        for c in range(classes):
            c_true = K.cast(K.equal(y_true_p, c), K.dtype(y_pred))
            w = 1. / (K.sum(c_true) + _epsilon)
            C = K.sum(L * c_true * w) if c == 0 else C + K.sum(L * c_true * w)

            # Calc. FP Rate Correction
            c_false_p = K.cast(K.not_equal(
                y_true_p, c), K.dtype(y_pred)) * K.cast(
                    K.equal(y_pred_bin, c),
                    K.dtype(y_pred))  # Calculate false predictions
            gamma = 0.5 + (K.sum(K.abs((c_false_p * y_pred_probs) - 0.5)) /
                           (K.sum(c_false_p) + _epsilon))  # Calculate Gamme
            wc = w * gamma  # gamma / |Y+|
            C = C + K.sum(L * c_false_p * wc)  # Add FP Correction

        return C
Ejemplo n.º 27
0
def accuracy_m(y_true, y_pred):
    y_true = K.round(y_true)
    y_pred = K.round(y_pred)
    correct_predictions = K.sum(K.cast(K.equal(y_true, y_pred), K.floatx()))
    all_instances = K.sum(K.cast(K.greater_equal(y_true, 0.), K.floatx()))
    accuracy = correct_predictions / all_instances
    return accuracy
 def __call__(self, w):
     other_weights = K.cast(K.greater_equal(w, 0)[:-1], K.floatx())
     last_weight = K.cast(
         K.equal(K.reshape(w[-1, :], (1, K.shape(w)[1])), 0.), K.floatx())
     appended = K.concatenate([other_weights, last_weight], axis=0)
     w *= appended
     return w
Ejemplo n.º 29
0
def get_binary_sat_loss(state, state_pred):

    sat_threshold = 0.105
    sat = K.expand_dims(state[:, :, :, 0], -1)
    sat_pred = K.expand_dims(state_pred[:, :, :, 0], -1)

    sat_bool = K.greater_equal(sat, sat_threshold)  #will return boolean values
    sat_bin = K.cast(sat_bool, dtype=K.floatx())  #will convert bool to 0 and 1

    sat_pred_bool = K.greater_equal(sat_pred,
                                    sat_threshold)  #will return boolean values
    sat_pred_bin = K.cast(sat_pred_bool,
                          dtype=K.floatx())  #will convert bool to 0 and 1

    binary_loss = losses.binary_crossentropy(sat_bin, sat_pred_bin)
    return K.mean(binary_loss)
Ejemplo n.º 30
0
def dice_coef_for_softmax(y_true, y_pred, threshold=0.5):
    y_true_bi = K.cast(K.greater_equal(y_true, threshold), 'float32')[:, :, :, :4]
    y_pred_bi = K.cast(tf.one_hot(K.argmax(y_pred), tf.shape(y_pred)[-1]), 'float32')[:, :, :, :4]
    inter = K.sum(y_true_bi*y_pred_bi, axis=[1, 2])
    union = K.sum(y_true_bi, axis=[1, 2]) + K.sum(y_pred_bi, axis=[1, 2])
    dice_coef = (2.0*inter + K.epsilon()) / (union + K.epsilon())
    return K.mean(dice_coef, axis=-1)
Ejemplo n.º 31
0
def simple_test(image_path):
	image = cv2.imread(image_path, cv2.IMREAD_COLOR)
	height = image.shape[1]
	width = image.shape[0]
	image = cv2.resize(image, (image_w,image_h))
	image = image.reshape((1,image_w,image_h,3))
	prediction = model.predict(image, batch_size=1)
	print(prediction.shape)
	# 1, 13, 13, 125
	# Reshape it to 1,13,13,5,25
	# 5 anchor boxes at every grid in 13 x 13
	# 25 elements of reach anchorbox
	# probabiliity if an object is present, bx, by, w, h, 20 dim vector for each class
	p_resh = prediction.reshape(1, 13, 13, 5, 25)
	print(p_resh.shape)

	for box_i in range(5):
		box = p_resh[0][0][0][box_i]
		pc   = box[0]
		c_scores = box[5:]
		res = pc * c_scores
		idx = np.argmax(res)
		p = class_dict[idx]
		print("Box No {} score {} box {},{},{},{} class {} ".format(box_i, res[idx], box[1],box[2],box[3],box[4], p))

	box_confidence = p_resh[:,:,:,:,0]
	box_confidence = box_confidence.reshape(1,13,13,5,1)
	boxes = p_resh[:,:,:,:,1:5]
	boxes = boxes.reshape(1,13,13,5,4)
	box_class_prob = p_resh[:,:,:,:,5:]
	box_class_prob = box_class_prob.reshape(1,13,13,5,20)

	# Filter the boxes
	threshold = 0.6
	box_scores = np.multiply(box_confidence, box_class_prob)
	print(box_scores.shape)
	box_class = K.argmax(box_scores, axis =-1)
	box_class_scores = K.max(box_scores, axis=-1)
	# Filtering mask
	filtering_mask = K.greater_equal(box_class_scores, threshold)
	with K.get_session() as test:
		scores = tf.boolean_mask(box_class_scores, filtering_mask).eval()
		boxes = tf.boolean_mask(boxes, filtering_mask).eval()
		classes = tf.boolean_mask(box_class, filtering_mask).eval()
	
		print(boxes.shape)
		print(classes.shape)
		print(scores.shape)


		max_boxes = 5
		iou_threshold = 0.6


		max_boxes_tensor = K.variable(max_boxes, dtype='int32')     # tensor to be used in tf.image.non_max_suppression()
		test.run(tf.variables_initializer([max_boxes_tensor]))# initialize variable max_boxes_tensor
	# Use tf.image.non_max_suppression() to get the list of indices corresponding to boxes you keep


		nms_indices = tf.image.non_max_suppression(boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold)
		scores = K.gather(scores, nms_indices).eval()
		boxes = K.gather(boxes, nms_indices).eval()
		classes = K.gather(classes, nms_indices).eval()

		print(boxes.shape)
		print(classes.shape)
		print(scores.shape)

		# scale the boxes
		image_dims = K.stack([height, width, height, width])
		image_dims = K.reshape(image_dims, [1, 4])
		boxes = boxes * image_dims

		print(boxes.eval())
Ejemplo n.º 32
0
 def __call__(self, w):
     w *= K.cast(K.greater_equal(w, 0.), K.floatx()) # Ensure non-negativity
     return w / (K.epsilon() + K.sum(w, axis=0, keepdims=True)) # Ensure columns sum to 1