def threshold_acc(y_true, y_pred, threshold=0.7): if K.backend() == 'tensorflow': return K.mean( K.equal(y_true, K.cast(K.greater_equal(y_pred, threshold), y_true.dtype))) else: return K.mean(K.equal(y_true, K.greater_equal(y_pred, threshold)))
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
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)
def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6): """Filters YOLO boxes by thresholding on object and class confidence. Select the max probabilities for each anchor box and filter out the one if probability < threshold 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. """ box_scores = box_confidence * box_class_probs box_classes = K.argmax(box_scores, axis = -1) #5 dimension: 从80个数里挑最大的index, 每一个anchor属于的class box_class_scores = K.max(box_scores, axis=-1) #5 dimension: 从80个数里挑最大的, 每一个anchor 最大的probabilities #box_classes, box_class_scores dimension, (19,19,5) print(box_class_scores.shape) filtering_mask = K.greater_equal(box_class_scores, threshold) print(filtering_mask.shape) scores = tf.boolean_mask(box_class_scores, filtering_mask) boxes = tf.boolean_mask(boxes, filtering_mask) # if probability > threshold, 保留相应的anchor box,否则就去掉这个anchor box classes = tf.boolean_mask(box_classes, filtering_mask) return scores,boxes,classes
def call(self, inputs): step_function = K.cast(K.greater_equal(inputs, 0.0), K.floatx()) width = 1.0 - self.sharpness # width of 'non-binary' region. _lambda = 0.001 k = (4.0*math.log(2.0 + 3.0**0.5))/(width + _lambda) psigmoid = 1.0/(1.0 + K.exp(K.clip(-(inputs)*k, -40, 40))) return K.switch(K.equal(self.sharpness, 1.0), step_function, psigmoid)
def accuracy_mod(y_true, y_pred): # Squeeze the shape to (None, ) from (None, 1) as we want to apply operations directly on y_true if K.ndim(y_true) == K.ndim(y_pred): y_true = K.squeeze(y_true, -1) # Normalize the y_pred values first and then take the arg at which we have a maximum value (This is the predicted label) y_pred = K.softmax(y_pred, axis = -1) y_pred = K.argmax(y_pred, axis = -1) # Since the ground labels can also have -1s for which we don't wanna calculate accuracy, we are filtering them off defa = K.constant([0], dtype=tf.float32) #Creating a boolean tensor for labels greater or equal to 0 is_valid = K.greater_equal(y_true, defa) #Get the corresponding indices indices = tf.where(is_valid) #Gather the results of y_true and y_pred at the indices we calculated above fil_y_true = K.gather(y_true, K.reshape(indices, [-1])) fil_y_pred = K.gather(y_pred, K.reshape(indices, [-1])) # K.print_tensor(res, message='res = ') # K.print_tensor(comp, message='comp = ') fil_y_true = K.cast(fil_y_true, K.floatx()) fil_y_pred = K.cast(fil_y_pred, K.floatx()) #pdb.set_trace() return K.cast(K.equal(fil_y_true, fil_y_pred), K.floatx())
def _init_cel(self, A_graph, b_graph, c_graph, y): # Sanity Checks y = tf.check_numerics(y, 'Problem with input y') # Find intersection points between Ax-b and the line joining the c and y Ac = tf.reduce_sum(A_graph * tf.expand_dims(c_graph, axis=-2), axis=-1) bMinusAc = b_graph - Ac yMinusc = y - c_graph ADotyMinusc = tf.reduce_sum((A_graph * tf.expand_dims(yMinusc, -2)), axis=-1) intersection_alphas = bMinusAc / (ADotyMinusc + K.epsilon()) # Enforce intersection_alpha > 0 because the point must lie on the ray from c to y less_equal_0 = K.less_equal(intersection_alphas, K.zeros_like(intersection_alphas)) candidate_alpha = K.switch( less_equal_0, K.ones_like(intersection_alphas) * tf.constant(np.inf, dtype='float32'), intersection_alphas) # Find closest the intersection point closest to the interior point to get projection point intersection_alpha = K.min(candidate_alpha, axis=-1, keepdims=True) # If it is an interior point, y itself is the projection point is_interior_point = K.greater_equal(intersection_alpha, K.ones_like(intersection_alpha)) alpha = K.switch(is_interior_point, K.ones_like(intersection_alpha), intersection_alpha) # Return z = \alpha.y + (1 - \alpha).c z = alpha * y + ((1 - alpha) * c_graph) return z
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
def masked(): # pick cval beta = K.sigmoid(self.beta) cval = self.min_value * beta + self.max_value * (1 - beta) # determine a mask ratio = K.sigmoid(self.ratio) size = K.random_uniform([], maxval=0.2, dtype='float32') offset = K.random_uniform([], maxval=1 - size, dtype='float32') ''' ratio = K.concatenate([self.ratio, [0.]]) ratio = ratio + K.random_normal([3,], dtype='float32') ratio = K.softmax(ratio) ''' mask = K.arange(0., 1., 1 / freq, dtype='float32') ge = K.cast(K.greater_equal(mask, offset), dtype='float32') le = K.cast(K.less_equal(mask, size + offset), dtype='float32') mask = 1 - ge * le mask = K.reshape(mask, broadcast_shape) outputs = inputs * mask + cval * (1 - mask) return outputs
def crps(y_true, y_pred): y_pred = K.cumsum(y_pred, axis=1) ym = K.cast(K.reshape(K.argmax(y_true, axis=1) - 99, (-1, 1)), dtype='int32') n = K.arange(-99, 100) step = K.cast(K.greater_equal(n - ym, 0), dtype='float32') return K.mean(K.sum(K.square(y_pred - step), axis=1)) / 199
def compute_mask_loss(boxes, masks, annotations, masks_target, width, height, iou_threshold=0.5, mask_size=(28, 28)): """compute overlap of boxes with annotations""" iou = overlap(boxes, annotations) argmax_overlaps_inds = K.argmax(iou, axis=1) max_iou = K.max(iou, axis=1) # filter those with IoU > 0.5 indices = tf.where(K.greater_equal(max_iou, iou_threshold)) boxes = tf.gather_nd(boxes, indices) masks = tf.gather_nd(masks, indices) argmax_overlaps_inds = K.cast(tf.gather_nd(argmax_overlaps_inds, indices), 'int32') labels = K.cast(K.gather(annotations[:, 4], argmax_overlaps_inds), 'int32') # make normalized boxes x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] y2 = boxes[:, 3] boxes = K.stack([ y1 / (K.cast(height, dtype=K.floatx()) - 1), x1 / (K.cast(width, dtype=K.floatx()) - 1), (y2 - 1) / (K.cast(height, dtype=K.floatx()) - 1), (x2 - 1) / (K.cast(width, dtype=K.floatx()) - 1), ], axis=1) # crop and resize masks_target # append a fake channel dimension masks_target = K.expand_dims(masks_target, axis=3) masks_target = tf.image.crop_and_resize( masks_target, boxes, argmax_overlaps_inds, mask_size ) masks_target = masks_target[:, :, :, 0] # remove fake channel dimension # gather the predicted masks using the annotation label masks = tf.transpose(masks, (0, 3, 1, 2)) label_indices = K.stack([tf.range(K.shape(labels)[0]), labels], axis=1) masks = tf.gather_nd(masks, label_indices) # compute mask loss mask_loss = K.binary_crossentropy(masks_target, masks) normalizer = K.shape(masks)[0] * K.shape(masks)[1] * K.shape(masks)[2] normalizer = K.maximum(K.cast(normalizer, K.floatx()), 1) mask_loss = K.sum(mask_loss) / normalizer return mask_loss
def call(self, inputs, **kwargs): x = inputs[:, 1] # print('x.shape: ' + str(K.int_shape(x))) bool_mask = Lambda(lambda t: K.greater_equal(t[:, 0], t[:, 1]), output_shape=K.int_shape(x)[1:])(inputs) # print('bool_mask.shape: ' + str(K.int_shape(bool_mask))) mask = Lambda(lambda t: K.cast(t, dtype='float32'))(bool_mask) # print('mask.shape: ' + str(K.int_shape(mask))) x = Multiply()([mask, x]) # print('x.shape: ' + str(K.int_shape(x))) return x
def _calc_loss(self, upper_bound, lower_bound, prediction): mask = K.cast(K.greater_equal(upper_bound, self._tf_zero), dtype=K.floatx()) # main error to the label (the predictions outside the bounding boxes) error = mask * (K.maximum(self._tf_zero, prediction - upper_bound) + K.minimum(self._tf_zero, prediction - lower_bound)) # additional losses (independent from label) aux_loss = self._aux_losses(mask, prediction, self._tf_zero, self._tf_one) return K.abs(error) + K.mean(aux_loss, axis=0, keepdims=True) / self._aux_scale
def _kl_regularizer(y_pred): _, rows, cols, _ = K.int_shape(y_pred) vmax = K.max(y_pred, axis=(1, 2)) vmax = K.expand_dims(vmax, axis=(1)) vmax = K.expand_dims(vmax, axis=(1)) vmax = K.tile(vmax, [1, rows, cols, 1]) y_delta = K.cast(K.greater_equal(y_pred, vmax), 'float32') return rho * K.sum(y_pred * (K.log(K.clip(y_pred, K.epsilon(), 1.)) - K.log(K.clip(y_delta, K.epsilon(), 1.))) / (rows * cols) )
def apply(self, Xs, Ys, Rs, reverse_state): ##print(" in BatchNormalizationReverseLayer.apply:", reverse_state['layer'].__class__.__name__, '(nid: {})'.format(reverse_state['nid'])) input_shape = [K.int_shape(x) for x in Xs] if len(input_shape) != 1: #extend below lambda layers towards multiple parameters. raise ValueError( "BatchNormalizationReverseLayer expects Xs with len(Xs) = 1, but was len(Xs) = {}" .format(len(Xs))) input_shape = input_shape[0] # prepare broadcasting shape for layer parameters broadcast_shape = [1] * len(input_shape) broadcast_shape[self._axis] = input_shape[self._axis] broadcast_shape[0] = -1 #reweight relevances as # x * (y - beta) R # Rin = ---------------- * ---- # x - mu y # batch norm can be considered as 3 distinct layers of subtraction, # multiplication and then addition. The multiplicative scaling layer # has no effect on LRP and functions as a linear activation layer minus_mu = tensorflow.keras.layers.Lambda( lambda x: x - K.reshape(self._mean, broadcast_shape)) minus_beta = tensorflow.keras.layers.Lambda( lambda x: x - K.reshape(self._beta, broadcast_shape)) prepare_div = tensorflow.keras.layers.Lambda(lambda x: x + (K.cast( K.greater_equal(x, 0), K.floatx()) * 2 - 1) * K.epsilon()) x_minus_mu = kutils.apply(minus_mu, Xs) if self._center: y_minus_beta = kutils.apply(minus_beta, Ys) else: y_minus_beta = Ys numerator = [ tensorflow.keras.layers.Multiply()([x, ymb, r]) for x, ymb, r in zip(Xs, y_minus_beta, Rs) ] denominator = [ tensorflow.keras.layers.Multiply()([xmm, y]) for xmm, y in zip(x_minus_mu, Ys) ] return [ ilayers.SafeDivide()([n, prepare_div(d)]) for n, d in zip(numerator, denominator) ]
def compute_fd_loss(boxes, scores, annotations, iou_threshold=0.75): """compute the overlap of boxes with annotations""" iou = overlap(boxes, annotations) max_iou = K.max(iou, axis=1, keepdims=True) targets = K.cast(K.greater_equal(max_iou, iou_threshold), K.floatx()) # compute the loss loss = focal(targets, scores) # alpha=self.alpha, gamma=self.gamma) # compute the normalizer: the number of cells present in the image normalizer = K.cast(K.shape(annotations)[0], K.floatx()) normalizer = K.maximum(K.cast_to_floatx(1.0), normalizer) return K.sum(loss) / normalizer
def my_IoU(y_true, y_pred): #IOU metrics #1 for object, 0 for background y_true = y_true[:, :, :, 2] smooth = 1 #threshold to determine positive pixel y_pred = K.cast_to_floatx(K.greater_equal(y_pred, 0.5)) #flatten, (batch, pixels) y_true_f = K.batch_flatten(y_true) y_pred_f = K.batch_flatten(y_pred) #(batchs) intersection = K.sum(y_true_f * y_pred_f, axis=-1) union = K.sum(y_pred_f, axis=-1) + K.sum(y_true_f, axis=-1) - intersection return (intersection + smooth) / (union + smooth)
def discretize_with_histogram(tensor, bins): _min = K.min(tensor) _max = K.max(tensor) _len_shape = len(tensor.shape) _bins = K.cast(tf.range(bins), dtype=K.floatx()) _range = tf.linspace(_min, _max, bins + 1) for _ in range(_len_shape): _bins = K.expand_dims(_bins, axis=-1) _range = K.expand_dims(_range, axis=-1) _cond1 = K.greater_equal(tensor, _range[:-1]) _cond2 = K.less(tensor, _range[1:]) _cond3 = K.less_equal(tensor, _range[1:]) _cond4 = K.concatenate((_cond2[:-1], _cond3[-1:]), axis=0) _all_cond = K.cast(K.all(K.stack((_cond1, _cond4), axis=0), axis=0), dtype=K.floatx()) _axis = tuple([i + 1 for i in range(_len_shape)]) _discrete = K.sum(_all_cond * _bins, axis=0) _histogram = tf.count_nonzero(_all_cond, axis=_axis) return _discrete, _histogram
def apply(self, Xs, Ys, Rs, reverse_state): grad = ilayers.GradientWRT(len(Xs)) # The epsilon rule aligns epsilon with the (extended) sign: 0 is considered to be positive prepare_div = tensorflow.keras.layers.Lambda(lambda x: x + (K.cast( K.greater_equal(x, 0), K.floatx()) * 2 - 1) * self._epsilon) # Get activations. Zs = kutils.apply(self._layer_wo_act, Xs) # Divide incoming relevance by the activations. tmp = [ilayers.Divide()([a, prepare_div(b)]) for a, b in zip(Rs, Zs)] # Propagate the relevance to input neurons # using the gradient. tmp = iutils.to_list(grad(Xs + Zs + tmp)) # Re-weight relevance with the input values. return [ tensorflow.keras.layers.Multiply()([a, b]) for a, b in zip(Xs, tmp) ]
def accuracy(self, y_true, y_pred): y_pred = K.cast(y_pred, dtype=K.floatx()) output_dimensions = tf.shape(y_pred)[2] upper_bound = K.cast(y_true[:, :, :output_dimensions], dtype=K.floatx()) mask = K.cast(K.greater_equal(upper_bound, self._tf_zero), dtype=K.floatx()) accuracy_mask = K.cast(y_true[:, :, 2 * output_dimensions:], dtype=K.floatx()) # because the accuracy_mask is originally also padded with -1, we mask it accuracy_mask = mask * accuracy_mask error_with_slack = K.abs(y_pred - upper_bound) - self._slack error_with_slack = K.cast(K.less_equal(error_with_slack, self._tf_zero), dtype=K.floatx()) # number of right predicted sequences divided by count of sequences return K.sum(accuracy_mask * error_with_slack) / K.sum(accuracy_mask)
def __call__(self, w): value = K.cast(K.greater_equal(w, K.epsilon()), K.floatx()) w = w * value return w
def __call__(self, w): w = w * K.cast(K.greater_equal(w, 0.), K.floatx()) w = w / (K.epsilon() + K.sum(w)) return w * self.n_feat
def call(self, inputs): step_function = K.cast(K.greater_equal(inputs, 0.5), K.floatx()) width = 1.0 - self.sharpness # width of 'non-binary' region. _lambda = 0.001 pbrelu = K.clip((1.0/(width + _lambda))*(inputs - 0.5) + 0.5, 0.0, 1.0) return K.switch(K.equal(self.sharpness, 1.0), step_function, pbrelu)
def linear_activation(self, mem): """Linear activation.""" return k.cast(k.greater_equal(mem, self.v_thresh), k.floatx())
def call(self, x): return K.greater_equal(x, K.constant(0))
def call(self, x): a, b = x return K.greater_equal(a, b)