def mean_per_class_accuracy(y_true, y_pred): class_id_true = K.argmax(y_true, axis=-1) class_id_preds = K.argmax(y_pred, axis=-1) # Replace class_id_preds with class_id_true for recall here interesting_class_id = 0 accuracy_mask = K.cast(K.equal(class_id_true, interesting_class_id), 'int32') class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask class0_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1) interesting_class_id = 1 accuracy_mask = K.cast(K.equal(class_id_true, interesting_class_id), 'int32') class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask class1_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1) interesting_class_id = 2 accuracy_mask = K.cast(K.equal(class_id_true, interesting_class_id), 'int32') class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask class2_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1) return (class0_acc + class1_acc + class2_acc) / 3
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.initial_decay > 0: lr = lr * (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 # Applies bounds on actual learning rate step_size = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))) final_lr = self.final_lr * lr / self.base_lr lower_bound = final_lr * (1. - 1. / (self.gamma * t + 1.)) upper_bound = final_lr * (1. + 1. / (self.gamma * t)) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] if self.amsbound: vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] else: vhats = [K.zeros(1) for _ in params] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): # apply weight decay if self.weight_decay != 0.: g += self.weight_decay * K.stop_gradient(p) m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) if self.amsbound: vhat_t = K.maximum(vhat, v_t) denom = (K.sqrt(vhat_t) + self.epsilon) self.updates.append(K.update(vhat, vhat_t)) else: denom = (K.sqrt(v_t) + self.epsilon) # Compute the bounds step_size_p = step_size * K.ones_like(denom) step_size_p_bound = step_size_p / denom bounded_lr_t = m_t * K.minimum( K.maximum(step_size_p_bound, lower_bound), upper_bound) p_t = p - bounded_lr_t self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
def loss(y_true, y_pred, q=quantile, margin=margin, alpha=alpha): from tensorflow.python.keras import backend as K error = y_true - y_pred quantile_loss = K.mean(K.maximum(q * error, (q - 1) * error)) diff = y_pred[:, 1:] - y_pred[:, :-1] penalty = K.mean(K.maximum(0.0, margin - diff)) * alpha return quantile_loss + penalty
def box_iou(b1, b2): '''Return IOU tensor b1: tensor, shape=(..., 4) x, y, w, h b2: tensor, shape=(j, 4) Return: iou: tensor(..., j) ''' b1 = K.expand_dims(b1, axis=-2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half b2 = K.expand_dims(b2, axis=0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] return intersect_area / (b1_area + b2_area - intersect_area)
def overlap(a, b): """Computes the IoU overlap of boxes in a and b. Args: a: np.array of shape (N, 4) of boxes. b: np.array of shape (K, 4) of boxes. Returns: A np.array of shape (N, K) of overlap between boxes from a and b. """ area = (b[:, 2] - b[:, 0]) * (b[:, 3] - b[:, 1]) iw = K.minimum(K.expand_dims(a[:, 2], axis=1), b[:, 2]) - \ K.maximum(K.expand_dims(a[:, 0], axis=1), b[:, 0]) ih = K.minimum(K.expand_dims(a[:, 3], axis=1), b[:, 3]) - \ K.maximum(K.expand_dims(a[:, 1], axis=1), b[:, 1]) iw = K.maximum(iw, 0) ih = K.maximum(ih, 0) ua = K.expand_dims((a[:, 2] - a[:, 0]) * (a[:, 3] - a[:, 1]), axis=1) + \ area - iw * ih ua = K.maximum(ua, K.epsilon()) intersection = iw * ih return intersection / ua
def calculate_iou(target_boxes, pred_boxes): xA = K.maximum(target_boxes[..., 0], pred_boxes[..., 0]) yA = K.maximum(target_boxes[..., 1], pred_boxes[..., 1]) xB = K.minimum(target_boxes[..., 2], pred_boxes[..., 2]) yB = K.minimum(target_boxes[..., 3], pred_boxes[..., 3]) interArea = K.maximum(0.0, xB - xA) * K.maximum(0.0, yB - yA) boxAArea = (target_boxes[..., 2] - target_boxes[..., 0]) * ( target_boxes[..., 3] - target_boxes[..., 1]) boxBArea = (pred_boxes[..., 2] - pred_boxes[..., 0]) * ( pred_boxes[..., 3] - pred_boxes[..., 1]) iou = interArea / (boxAArea + boxBArea - interArea) return iou
def triplet_loss(y_true, y_pred): margin = K.constant(1) return K.mean( K.maximum( K.constant(0), K.square(y_pred[:, 0, 0]) - 0.5 * (K.square(y_pred[:, 1, 0]) + K.square(y_pred[:, 2, 0])) + margin))
def call(self, inputs, **kwargs): inputs, memory_length = inputs memory_length = K.cast(memory_length[0][0], 'int32') batch_size = K.cast(K.shape(inputs)[0], 'int32') seq_len = K.cast(K.shape(inputs)[1], 'int32') # Build new memory pad = K.tile(inputs[0:1, ...], (self.batch_size - batch_size, 1, 1)) padded = K.concatenate([inputs, pad], axis=0) # (self.batch_size, seq_len, output_dim) new_memory = K.concatenate([self.memory, padded], axis=1) # (self.batch_size, self.memory_len + self.target_len + seq_len, ...) new_memory = tf.slice( # (self.batch_size, self.memory_len + self.target_len, output_dim) new_memory, (0, seq_len, 0), (self.batch_size, self.memory_len + self.target_len, self.output_dim), ) self.add_update(K.update(self.memory, new_memory), inputs) # Build output old_memory = tf.slice( # (batch_size, memory_length, output_dim) new_memory, (0, K.maximum(0, self.memory_len + self.target_len - seq_len - memory_length), 0), (batch_size, K.minimum(self.memory_len, memory_length), self.output_dim), ) return old_memory
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] lr = self.lr if self.initial_decay > 0: lr = lr * (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] if self.amsgrad: vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] else: vhats = [K.zeros(1) for _ in params] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): # Learning rate multipliers if self.multipliers: multiplier = [ mult for mult in self.multipliers if mult in p.name ] else: multiplier = None if multiplier: new_lr_t = lr_t * self.multipliers[multiplier[0]] if self.debug_verbose: print('Setting {} to learning rate {}'.format( multiplier[0], new_lr_t)) print(K.get_value(new_lr_t)) else: new_lr_t = lr_t if self.debug_verbose: print('No change in learning rate {}'.format(p.name)) print(K.get_value(new_lr_t)) m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) if self.amsgrad: vhat_t = K.maximum(vhat, v_t) p_t = p - new_lr_t * m_t / (K.sqrt(vhat_t) + self.epsilon) self.updates.append(K.update(vhat, vhat_t)) else: p_t = p - new_lr_t * m_t / (K.sqrt(v_t) + self.epsilon) self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) new_p = p_t # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates
def triplet_loss(y_true, y_pred, alpha=0.6): """ Implementation of the triplet loss function Arguments: y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. y_pred -- python list containing three objects: anchor -- the encodings for the anchor data positive -- the encodings for the positive data (similar to anchor) negative -- the encodings for the negative data (different from anchor) Returns: loss -- real number, value of the loss """ print('y_pred.shape = ', y_pred) total_length = y_pred.shape.as_list()[-1] # print('total_length=', total_length) # total_length =12 anchor = y_pred[:, 0:int(total_length * 1 / 3)] positive = y_pred[:, int(total_length * 1 / 3):int(total_length * 2 / 3)] negative = y_pred[:, int(total_length * 2 / 3):int(total_length * 3 / 3)] # distance between the anchor and the positive pos_dist = K.abs(K.sum(K.square(anchor - positive), axis=1)) print(pos_dist) # distance between the anchor and the negative neg_dist = K.abs(K.sum(K.square(anchor - negative), axis=1)) print(neg_dist) # compute loss basic_loss = pos_dist - (neg_dist + alpha) loss = K.maximum(basic_loss, 0.0) return loss
def define_deepDream_model_layerBased(model): dream = model.input print('Model loaded.') # Get the symbolic outputs of each "key" layer (we gave them unique names). layer_dict = dict([(layer.name, layer) for layer in model.layers]) # Define the loss. loss = K.variable(0.) for layer_name in settings['features']: # Add the L2 norm of the features of a layer to the loss. if layer_name not in layer_dict: raise ValueError('Layer ' + layer_name + ' not found in model.') coeff = settings['features'][layer_name] x = layer_dict[layer_name].output # We avoid border artifacts by only involving non-border pixels in the loss. scaling = K.prod(K.cast(K.shape(x), 'float32')) if K.image_data_format() == 'channels_first': loss = loss + coeff * K.sum(K.square(x[:, :, 2:-2, 2:-2])) / scaling else: loss = loss + coeff * K.sum(K.square(x[:, 2:-2, 2:-2, :])) / scaling # Compute the gradients of the dream wrt the loss. grads = K.gradients(loss, dream)[0] # Normalize gradients. grads /= K.maximum(K.mean(K.abs(grads)), K.epsilon()) # Set up function to retrieve the value # of the loss and gradients given an input image. outputs = [loss, grads] fetch_loss_and_grads = K.function([dream], outputs)
def contrastive_loss(y_true, y_pred): '''Contrastive loss from Hadsell-et-al.'06 http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf ''' margin = 1 return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
def __init__(self, model, layer_name): self.model = model self.layer_name = layer_name dream = model.input # Get the symbolic outputs of each "key" layer (we gave them unique names). layers_all = [layer.name for layer in model.layers] if layer_name not in layers_all: raise ValueError('Layer ' + layer_name + ' not found in model.') # Define the loss. loss = K.variable(0.) for layer_local in model.layers: if layer_local.name == layer_name: x = layer_local.output # We avoid border artifacts by only involving non-border pixels in the loss. if K.image_data_format() == 'channels_first': scaling = K.prod(K.cast(K.shape(x), 'float32')) loss = loss + K.sum(K.square(x[:, :, 2:-2, 2:-2])) / scaling else: scaling = K.prod(K.cast(K.shape(x), 'float32')) loss = loss + K.sum(K.square(x[:, 2:-2, 2:-2, :])) / scaling # Compute the gradients of the dream wrt the loss. grads = K.gradients(loss, dream)[0] # Normalize gradients. grads /= K.maximum(K.mean(K.abs(grads)), K.epsilon()) # Set up function to retrieve the value # of the loss and gradients given an input image. outputs = [loss, grads] self.fetch_loss_and_grads = K.function([dream], outputs)
def triplet_loss(y_true, y_pred): # Euclidean dist between all pairs dist = K.expand_dims(y_pred, axis=1) - K.expand_dims(y_pred, axis=0) dist_mat = K.cast(K.sqrt(K.sum(K.square(dist), axis=-1) + K.epsilon()), dtype='float32') self_mask = K.cast(K.equal(K.expand_dims(y_true, axis=1), K.expand_dims(y_true, axis=0)), dtype='float32') # Reverse the the positive mask neg_mask = K.cast(tf.abs(self_mask - 1), dtype='float32') # Make the sample do not match with itself diag = tf.linalg.diag_part(self_mask) - tf.linalg.diag_part(self_mask) pos_mask = K.cast(tf.linalg.set_diag(self_mask, diag), dtype='float32') # Pick the top K pairs for each positive/negative example(furthest positives and closest negatives) top_k_pos = tf.nn.top_k(dist_mat * pos_mask, k).values top_k_neg = tf.abs( tf.nn.top_k(-1 * (dist_mat * neg_mask + 1e10 * self_mask), k).values) loss = K.mean(margin + K.expand_dims(top_k_pos, axis=-1) - K.expand_dims(top_k_neg, axis=-2)) loss = K.maximum(loss, 0) return loss
def class2_accuracy(y_true, y_pred): class_id_true = K.argmax(y_true, axis=-1) class_id_preds = K.argmax(y_pred, axis=-1) accuracy_mask = K.cast(K.equal(class_id_preds, 2), 'int32') class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask class_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1) return class_acc
def _triplet_loss(_, y_pred): margin = K.constant(MARGIN) positive_dist = y_pred[:, 0] negative_dist = y_pred[:, 1] basic_loss = K.square(positive_dist) - K.square(negative_dist) + margin return K.mean(K.maximum(K.constant(0), basic_loss))
def masked_mse(y_true, y_pred): # masked function mask_true = K.cast(K.not_equal(y_true, 0), K.floatx()) # masked squared error masked_squared_error = K.square(mask_true * (y_true - y_pred)) masked_mse = K.sum(masked_squared_error, axis=-1) / K.maximum( K.sum(mask_true, axis=-1), 1) return masked_mse
def mean_squared_logarithmic_error(y_true, y_pred): """Computes the mean squared logarithmic error between `y_true` and `y_pred`. `loss = square(log(y_true) - log(y_pred))` Args: y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`. y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`. Returns: Mean squared logarithmic error values. shape = `[batch_size, d0, .. dN-1]`. """ y_pred = ops.convert_to_tensor(y_pred) y_true = math_ops.cast(y_true, y_pred.dtype) first_log = math_ops.log(K.maximum(y_pred, K.epsilon()) + 1.) second_log = math_ops.log(K.maximum(y_true, K.epsilon()) + 1.) return K.mean(math_ops.squared_difference(first_log, second_log), axis=-1)
def distance(inputs): ap, an, margin, gthr = inputs ap_l2n = K.sqrt(K.sum(K.square(ap), axis=1, keepdims=True)) an_l2n = K.sqrt(K.sum(K.square(an), axis=1, keepdims=True)) d = K.minimum((an_l2n - ap_l2n), margin) g = K.maximum(ap_l2n, gthr) y = K.concatenate([d, g], axis=1) return y
def contrastive_loss(y_true, y_pred): """Contrastive loss from Hadsell-et-al.'06 http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf """ margin = 1 sqaure_pred = K.square(y_pred) margin_square = K.square(K.maximum(margin - y_pred, 0)) return K.mean(y_true * sqaure_pred + (1 - y_true) * margin_square)
def mask_acc(y_true, y_pred): y_true_class = K.argmax(y_true, axis=-1) y_pred_class = K.argmax(y_pred, axis=-1) ignore_mask = K.cast(K.not_equal(y_true_class, 0), "int32") matches = K.cast(K.equal(y_true_class, y_pred_class), "int32") * ignore_mask accuracy = K.sum(matches) / K.maximum(K.sum(ignore_mask), 1) return accuracy
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 weighted_bce_loss(y_true, y_pred, weight): # avoiding overflow epsilon = 1e-7 y_pred = K.clip(y_pred, epsilon, 1. - epsilon) logit_y_pred = K.log(y_pred / (1. - y_pred)) # https://www.tensorflow.org/api_docs/python/tf/nn/weighted_cross_entropy_with_logits loss = (1. - y_true) * logit_y_pred + (1. + (weight - 1.) * y_true) * \ (K.log(1. + K.exp(-K.abs(logit_y_pred))) + K.maximum(-logit_y_pred, 0.)) return K.sum(loss) / K.sum(weight)
def f(y_true, y_pred): class_id_true = K.argmax(y_true, axis=-1) class_id_preds = K.argmax(y_pred, axis=-1) # Replace class_id_preds with class_id_true for recall here accuracy_mask = K.cast(K.equal(class_id_preds, interested_class_id), 'int32') class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask class_acc = K.sum(class_acc_tensor) / K.maximum( K.sum(accuracy_mask), 1) return class_acc
def correlation_coefficient_loss(y_true, y_pred): x = y_true y = y_pred mx = K.mean(x) my = K.mean(y) xm, ym = x - mx, y - my r_num = K.sum(tf.multiply(xm, ym)) r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym)))) r = r_num / r_den r = K.maximum(K.minimum(r, 1.0), -1.0) return 1 - K.square(r)
def correlation_coefficient_loss(y_true, y_pred): x = y_true y = y_pred mx = K.mean(x) my = K.mean(y) xm, ym = x-mx, y-my r_num = K.sum(tf.multiply(xm,ym)) r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym)))) r = r_num / r_den r = K.maximum(K.minimum(r, 1.0), -1.0) return 1 - K.square(r)
def box_iou(b1, b2): '''Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) ''' # Expand dim to apply broadcasting. # b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh / 2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. # b2 = K.expand_dims(b2, 0) # b2 = K.expand_dims(b2, -2) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh / 2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
def pairwiseRankingHingeLoss(yTrue, yPred): ''' @brief: This function gets net predictions for ranking output and calculate the pairwise ranking hinge loss. @param yTrue: dummy tensor, not used. @param yPred: net predictions for ranking output. @return: pairwise ranking hinge loss for ranking output. ''' # yPred = tf.Print(yPred, [yPred], message='\nRanking_yPred = ', summarize=25) M_tensor = K.constant(M) differences = K.dot(M_tensor, yPred) zeros_tensor = K.zeros(shape=(50, 1)) max_tensor = K.maximum(differences, zeros_tensor) ranking_loss = K.sum(max_tensor) return ranking_loss
def masked_rmse(y_true, y_pred): """ Function to define the masked root mean squared error :param y_true: true label :param y_pred: predicted label :return: masked root mean squared error """ # Masked function mask_true = K.cast(K.not_equal(y_true, 0), K.floatx()) # Masked squared error masked_squared_error = K.square(mask_true * (y_true - y_pred)) masked_rmse = K.sqrt( K.sum(masked_squared_error, axis=-1) / K.maximum(K.sum(mask_true, axis=-1), 1)) return masked_rmse
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 get_updates_Padam(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] base_lr = self._optimizer.learning_rate if self.initial_decay > 0: base_lr = base_lr * (1. / (1. + self.decay * K.cast( self.iterations, K.dtype(self.decay)))) t = K.cast(self.iterations, K.floatx()) + 1 lr_t = base_lr * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))) ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] if self.amsgrad: vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params] else: vhats = [K.zeros(1) for _ in params] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): if self._get_multiplier(p) is None: multiplier = 1.0 else: multiplier = self._get_multiplier(p) m_t = (self.beta_1 * m) + (1. - self.beta_1) * g v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g) if self.amsgrad: vhat_t = K.maximum(vhat, v_t) denom = (K.sqrt(vhat_t) + self.epsilon) self.updates.append(K.update(vhat, vhat_t)) else: denom = (K.sqrt(v_t) + self.epsilon) self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) # Partial momentum adaption. new_p = p - (lr_t * multiplier * (m_t / (denom**(self.partial * 2)))) # Apply constraints. if getattr(p, 'constraint', None) is not None: new_p = p.constraint(new_p) self.updates.append(K.update(p, new_p)) return self.updates