def loss(y_true, y_pred): prob = K.sum(y_true * y_pred, axis=-1) # Multiply with the one hot encoded taken action old_prob = K.sum(y_true * old_prediction, axis=-1) r = prob / (old_prob + 1e-10) return -K.mean(K.minimum(r * advantage, K.clip( r, min_value=1 - loss_clipping, max_value=1 + loss_clipping) * advantage) + entropy_loss * -( prob * K.log(prob + 1e-10)))
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 # 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): prob = K.sum(y_true * y_pred) old_prob = K.sum(y_true * old_prediction) r = prob / (old_prob + 1e-10) return -K.log(prob + 1e-10) * K.mean( K.minimum(r * advantage, K.clip(r, min_value=0.8, max_value=1.2) * advantage))
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 dice(y_true, y_pred): eps = K.constant(1e-6) truelabels = tf.argmax(y_true, axis=-1, output_type=tf.int32) predictions = tf.argmax(y_pred, axis=-1, output_type=tf.int32) # cast->型変換,minimum2つのテンソルの要素ごとの最小値,equal->boolでかえってくる intersection = K.cast(K.sum(K.minimum(K.cast(K.equal(predictions, truelabels), tf.int32), truelabels)), tf.float32) union = tf.count_nonzero(predictions, dtype=tf.float32) + tf.count_nonzero(truelabels, dtype=tf.float32) dice = 2. * intersection / (union + eps) return dice
def weighted_loss(y_true, y_pred): cross_entropy_loss = K.binary_crossentropy(y_true, y_pred, from_logits=False) n_classes_present = K.sum(y_true, axis=1, keepdims=True) cross_entropy_loss = K.minimum(80 / n_classes_present * y_true, 1) * cross_entropy_loss return K.mean(cross_entropy_loss, axis=-1)
def ppo_loss(y_true, y_pred): eps = 0.2 entropy_loss = 0.001 * K.mean( K.sum(y_pred * K.log(y_pred + 1e-10), axis=1, keepdims=True) ) # Danger : le masque des actions possibles n'est pas pris en compte !!! r = y_pred * y_true / (old_pred * y_true + 1e-10) policy_loss = -K.mean( K.minimum(r * advantages, K.clip(r, 1 - eps, 1 + eps) * advantages) ) return policy_loss + entropy_loss
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 loss(y_true, y_pred): var = K.variable(1.0) pi = K.variable(3.1415926) denom = K.sqrt(2 * pi * var) prob_num = K.exp(-K.square(y_true - y_pred) / (2 * var)) old_prob_num = K.exp(-K.square(y_true - old_prediction) / (2 * var)) prob = prob_num / denom old_prob = old_prob_num / denom r = prob / (old_prob + 1e-10) return -K.mean( K.minimum( r * advantage, K.clip(r, min_value=1 - 1e-5, max_value=1 + 1e-5) * advantage))
def dice_2(y_true, y_pred): K = tf.keras.backend eps = K.constant(1e-6) truelabels = K.cast(y_true[:, :, :, 2], tf.int32) predictions = K.cast(y_pred[:, :, :, 2], tf.int32) intersection = K.cast( K.sum( K.minimum(K.cast(K.equal(predictions, truelabels), tf.int32), truelabels)), tf.float32) union = tf.count_nonzero(predictions, dtype=tf.float32) + tf.count_nonzero( truelabels, dtype=tf.float32) dice_2 = 2 * intersection / (union + eps) return dice_2
def call(self, x): y_pred = x[0] y_recont_gt = x[1] y_prob_pred = tf.squeeze(x[2], axis=3) y_prob_gt = x[3] visible = tf.cast(y_prob_gt > 0.5, y_pred.dtype) visible = tf.squeeze(visible, axis=3) #generate transformed values using sym if (len(self.sym) > 1): #if(True): for sym_id, transform in enumerate(self.sym): #3x3 matrix tf_mat = tf.convert_to_tensor(transform, y_recont_gt.dtype) y_gt_transformed = tf.transpose( tf.matmul(tf_mat, tf.transpose(tf.reshape(y_recont_gt, [-1, 3])))) y_gt_transformed = tf.reshape(y_gt_transformed, [-1, 128, 128, 3]) loss_xyz_temp = K.sum(K.abs(y_gt_transformed - y_pred), axis=3) / 3 loss_sum = K.sum(loss_xyz_temp, axis=[1, 2]) if (sym_id > 0): loss_sums = tf.concat( [loss_sums, tf.expand_dims(loss_sum, axis=0)], axis=0) loss_xyzs = tf.concat( [loss_xyzs, tf.expand_dims(loss_xyz_temp, axis=0)], axis=0) else: loss_sums = tf.expand_dims(loss_sum, axis=0) loss_xyzs = tf.expand_dims(loss_xyz_temp, axis=0) min_values = tf.reduce_min(loss_sums, axis=0, keepdims=True) loss_switch = tf.cast(tf.equal(loss_sums, min_values), y_pred.dtype) loss_xyz = tf.expand_dims(tf.expand_dims(loss_switch, axis=2), axis=3) * loss_xyzs loss_xyz = K.sum(loss_xyz, axis=0) else: loss_xyz = K.sum(K.abs(y_recont_gt - y_pred), axis=3) / 3 prob_loss = K.square(y_prob_pred - K.minimum(loss_xyz, 1)) loss_invisible = (1 - visible) * loss_xyz loss_visible = visible * loss_xyz loss = loss_visible * 3 + loss_invisible + 0.5 * prob_loss loss = K.mean(loss, axis=[1, 2]) return loss
def dice_1(y_true, y_pred): K = tf.keras.backend eps = K.constant(1e-6) # truelabels = tf.argmax(y_true, axis=-1, output_type=tf.int32) # predictions = tf.argmax(y_pred, axis=-1, output_type=tf.int32) truelabels = K.cast(y_true[:, :, :, 1], tf.int32) predictions = K.cast(y_pred[:, :, :, 1], tf.int32) # truelabels = tf.where(tf.equal(truelabels, 1), truelabels, 0) # predictions = tf.where(tf.equal(predictions, 1), predictions, 0) intersection = K.cast(K.sum(K.minimum(K.cast(K.equal(predictions, truelabels), tf.int32), truelabels)), tf.float32) union = tf.count_nonzero(predictions, dtype=tf.float32) + tf.count_nonzero(truelabels, dtype=tf.float32) dice_1 = 2 * intersection / (union + eps) return dice_1
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 yolo_loss(yolo_output, true_boxes, detectors_mask, matching_true_boxes, anchors, num_classes, rescore_confidence=False, print_loss=False): """YOLO localization loss function. Parameters ---------- yolo_output : tf.Tensor Final convolutional layer features. true_boxes : tf.Tensor Ground truth boxes tensor with shape [batch, num_true_boxes, 5] containing box x_center, y_center, width, height, and class. detectors_mask : np.ndarray 0/1 mask for detector positions where there is a matching ground truth. matching_true_boxes : np.ndarray Corresponding ground truth boxes for positive detector positions. Already adjusted for conv height and width. anchors : np.ndarray Anchor boxes for model. num_classes : int Number of object classes. rescore_confidence : bool, default=False If true then set confidence target to IOU of best predicted box with the closest matching ground truth box. print_loss : bool, default=False If True then print the loss components. Returns ------- mean_loss : float Mean localization loss across minibatch """ num_anchors = len(anchors) object_scale = 5 no_object_scale, class_scale, coordinates_scale = 1, 1, 1 pred_xy, pred_wh, pred_confidence, pred_class_prob = yolo_head( yolo_output, anchors, num_classes) # Unadjusted box predictions for loss. # TODO: Remove extra computation shared with yolo_head. yolo_output_shape = K.shape(yolo_output) feats = K.reshape(yolo_output, [ -1, yolo_output_shape[1], yolo_output_shape[2], num_anchors, num_classes + 5 ]) pred_boxes = K.concatenate((K.sigmoid(feats[..., 0:2]), feats[..., 2:4]), axis=-1) # TODO: Adjust predictions by image width/height for non-square images? # IOUs may be off due to different aspect ratio. # Expand pred x,y,w,h to allow comparison with ground truth. # batch, conv_height, conv_width, num_anchors, num_true_boxes, box_params pred_xy = K.expand_dims(pred_xy, 4) pred_wh = K.expand_dims(pred_wh, 4) pred_wh_half = pred_wh / 2. pred_mins = pred_xy - pred_wh_half pred_maxes = pred_xy + pred_wh_half true_boxes_shape = K.shape(true_boxes) # batch, conv_height, conv_width, num_anchors, num_true_boxes, box_params true_boxes = K.reshape(true_boxes, [ true_boxes_shape[0], 1, 1, 1, true_boxes_shape[1], true_boxes_shape[2] ]) true_xy = true_boxes[..., 0:2] true_wh = true_boxes[..., 2:4] # Find IOU of each predicted box with each ground truth box. true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half intersect_mins = K.maximum(pred_mins, true_mins) intersect_maxes = K.minimum(pred_maxes, true_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] pred_areas = pred_wh[..., 0] * pred_wh[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = intersect_areas / union_areas # Best IOUs for each location. best_ious = K.max(iou_scores, axis=4) # Best IOU scores. best_ious = K.expand_dims(best_ious) # A detector has found an object if IOU > thresh for some true box. object_detections = K.cast(best_ious > 0.6, K.dtype(best_ious)) # TODO: Darknet region training includes extra coordinate loss for early # TODO: training steps to encourage predictions to match anchor priors. # Determine confidence weights from object and no_object weights. # NOTE: YOLO does not use binary cross-entropy here. no_object_weights = (no_object_scale * (1 - object_detections) * (1 - detectors_mask)) no_objects_loss = no_object_weights * K.square(-pred_confidence) if rescore_confidence: objects_loss = (object_scale * detectors_mask * K.square(best_ious - pred_confidence)) else: objects_loss = (object_scale * detectors_mask * K.square(1 - pred_confidence)) confidence_loss = objects_loss + no_objects_loss # Classification loss for matching detections. # NOTE: YOLO does not use categorical cross-entropy loss here. matching_classes = K.cast(matching_true_boxes[..., 4], 'int32') matching_classes = K.one_hot(matching_classes, num_classes) classification_loss = (class_scale * detectors_mask * K.square(matching_classes - pred_class_prob)) # Coordinate loss for matching detection boxes. matching_boxes = matching_true_boxes[..., 0:4] coordinates_loss = (coordinates_scale * detectors_mask * K.square(matching_boxes - pred_boxes)) confidence_loss_sum = K.sum(confidence_loss) classification_loss_sum = K.sum(classification_loss) coordinates_loss_sum = K.sum(coordinates_loss) total_loss = 0.5 * (confidence_loss_sum + classification_loss_sum + coordinates_loss_sum) if print_loss: # TODO: printing Tensor values. Maybe use eval function or session? print( 'yolo_loss: {}, conf_loss: {}, class_loss: {}, box_coord_loss: {}'. format(total_loss, confidence_loss_sum, classification_loss_sum, coordinates_loss_sum)) return total_loss
def filter_detections(boxes, classification, other=[], class_specific_filter=True, nms=True, score_threshold=0.05, max_detections=300, nms_threshold=0.5): """Filter detections using the boxes and classification values. Args: boxes: Tensor of shape (num_boxes, 4) containing the boxes in (x1, y1, x2, y2) format. classification: Tensor of shape (num_boxes, num_classes) containing the classification scores. other: List of tensors of shape (num_boxes, ...) to filter along with the boxes and classification scores. class_specific_filter: Whether to perform filtering per class, or take the best scoring class and filter those. nms: Flag to enable/disable non maximum suppression. score_threshold: Threshold used to prefilter the boxes with. max_detections: Maximum number of detections to keep. nms_threshold: Threshold for the IoU value to determine when a box should be suppressed. Returns: A list of [boxes, scores, labels, other[0], other[1], ...]. boxes is shaped (max_detections, 4) and contains the (x1, y1, x2, y2) of the non-suppressed boxes. scores is shaped (max_detections,) and contains the scores of the predicted class. labels is shaped (max_detections,) and contains the predicted label. other[i] is shaped (max_detections, ...) and contains the filtered other[i] data. In case there are less than max_detections detections, the tensors are padded with -1's. """ def _filter_detections(scores, labels): # threshold based on score indices = tf.where(K.greater(scores, score_threshold)) if nms: filtered_boxes = tf.gather_nd(boxes, indices) filtered_scores = K.gather(scores, indices)[:, 0] # perform NMS nms_indices = tf.image.non_max_suppression( filtered_boxes, filtered_scores, max_output_size=max_detections, iou_threshold=nms_threshold) # filter indices based on NMS indices = K.gather(indices, nms_indices) # add indices to list of all indices labels = tf.gather_nd(labels, indices) indices = K.stack([indices[:, 0], labels], axis=1) return indices if class_specific_filter: all_indices = [] # perform per class filtering for c in range(K.int_shape(classification)[1]): scores = classification[:, c] labels = c * tf.ones((K.shape(scores)[0], ), dtype='int64') all_indices.append(_filter_detections(scores, labels)) # concatenate indices to single tensor indices = K.concatenate(all_indices, axis=0) else: scores = K.max(classification, axis=1) labels = K.argmax(classification, axis=1) indices = _filter_detections(scores, labels) # select top k scores = tf.gather_nd(classification, indices) labels = indices[:, 1] scores, top_indices = tf.nn.top_k(scores, k=K.minimum(max_detections, K.shape(scores)[0])) # filter input using the final set of indices indices = K.gather(indices[:, 0], top_indices) boxes = K.gather(boxes, indices) labels = K.gather(labels, top_indices) other_ = [K.gather(o, indices) for o in other] # zero pad the outputs pad_size = K.maximum(0, max_detections - K.shape(scores)[0]) boxes = tf.pad(boxes, [[0, pad_size], [0, 0]], constant_values=-1) scores = tf.pad(scores, [[0, pad_size]], constant_values=-1) labels = tf.pad(labels, [[0, pad_size]], constant_values=-1) labels = K.cast(labels, 'int32') pads = lambda x: [[0, pad_size]] + [[0, 0] for _ in range(1, K.ndim(x))] other_ = [tf.pad(o, pads(o), constant_values=-1) for o in other_] # set shapes, since we know what they are boxes.set_shape([max_detections, 4]) scores.set_shape([max_detections]) labels.set_shape([max_detections]) for o, s in zip(other_, [list(K.int_shape(o)) for o in other]): o.set_shape([max_detections] + s[1:]) return [boxes, scores, labels] + other_
def get_updates(self, loss, params): grads = self.get_gradients(loss, params) self.updates = [K.update_add(self.iterations, 1)] t = K.cast(self.iterations, K.floatx()) + 1 lr = K.switch( t <= self.warmup_steps, self.lr * (t / self.warmup_steps), self.min_lr + (self.lr - self.min_lr) * (1.0 - K.minimum(t, self.decay_steps) / self.decay_steps), ) 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), name='m_{}'.format(i)) for i, p in enumerate(params) ] vs = [ K.zeros(K.int_shape(p), dtype=K.dtype(p), name='v_{}'.format(i)) for i, p in enumerate(params) ] if self.amsgrad: vhats = [ K.zeros(K.int_shape(p), dtype=K.dtype(p), name='vh_{}'.format(i)) for i, p in enumerate(params) ] else: vhats = [ K.zeros(1, dtype=K.dtype(p), name='vh_{}'.format(i)) for i, p in enumerate(params) ] self.weights = [self.iterations] + ms + vs + vhats for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats): 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 = m_t / (K.sqrt(vhat_t) + self.epsilon) self.updates.append(K.update(vhat, vhat_t)) else: p_t = m_t / (K.sqrt(v_t) + self.epsilon) if self.initial_weight_decay > 0.0: if self.weight_decay_pattern is None: p_t += self.weight_decay * p else: for pattern in self.weight_decay_pattern: if pattern in p.name: p_t += self.weight_decay * p break p_t = p - lr_t * p_t self.updates.append(K.update(m, m_t)) self.updates.append(K.update(v, v_t)) new_p = p_t 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 _aabb_intersected_area(aabb_1, aabb_2): min_distance = K.minimum(aabb_1, aabb_2) return _aabb_box_area(min_distance)
def _resource_apply_sparse(self, grad, var, indices): var_dtype = var.dtype.base_dtype lr_t = self._decayed_lr(var_dtype) beta_1_t = self._get_hyper('beta_1', var_dtype) beta_2_t = self._get_hyper('beta_2', var_dtype) epsilon_t = ops.convert_to_tensor(self.epsilon, var_dtype) local_step = math_ops.cast(self.iterations + 1, var_dtype) beta_1_power = math_ops.pow(beta_1_t, local_step) beta_2_power = math_ops.pow(beta_2_t, local_step) if self._initial_total_steps > 0: total_steps = self._get_hyper('total_steps', var_dtype) warmup_steps = total_steps * self._get_hyper('warmup_proportion', var_dtype) min_lr = self._get_hyper('min_lr', var_dtype) decay_steps = K.maximum(total_steps - warmup_steps, 1) decay_rate = (min_lr - lr_t) / decay_steps lr_t = tf.where( local_step <= warmup_steps, lr_t * (local_step / warmup_steps), lr_t + decay_rate * K.minimum(local_step - warmup_steps, decay_steps), ) sma_inf = 2.0 / (1.0 - beta_2_t) - 1.0 sma_t = sma_inf - 2.0 * local_step * beta_2_power / (1.0 - beta_2_power) m = self.get_slot(var, 'm') m_scaled_g_values = grad * (1 - beta_1_t) m_t = state_ops.assign(m, m * beta_1_t, use_locking=self._use_locking) with ops.control_dependencies([m_t]): m_t = self._resource_scatter_add(m, indices, m_scaled_g_values) m_corr_t = m_t / (1.0 - beta_1_power) v = self.get_slot(var, 'v') v_scaled_g_values = (grad * grad) * (1 - beta_2_t) v_t = state_ops.assign(v, v * beta_2_t, use_locking=self._use_locking) with ops.control_dependencies([v_t]): v_t = self._resource_scatter_add(v, indices, v_scaled_g_values) if self.amsgrad: vhat = self.get_slot(var, 'vhat') vhat_t = state_ops.assign(vhat, math_ops.maximum(vhat, v_t), use_locking=self._use_locking) v_corr_t = math_ops.sqrt(vhat_t / (1.0 - beta_2_power)) else: vhat_t = None v_corr_t = math_ops.sqrt(v_t / (1.0 - beta_2_power)) r_t = math_ops.sqrt((sma_t - 4.0) / (sma_inf - 4.0) * (sma_t - 2.0) / (sma_inf - 2.0) * sma_inf / sma_t) var_t = tf.where(sma_t >= 5.0, r_t * m_corr_t / (v_corr_t + epsilon_t), m_corr_t) if self._initial_weight_decay > 0.0: var_t += self._get_hyper('weight_decay', var_dtype) * var var_update = self._resource_scatter_add(var, indices, tf.gather(-lr_t * var_t, indices)) updates = [var_update, m_t, v_t] if self.amsgrad: updates.append(vhat_t) return control_flow_ops.group(*updates)
def loss_layer(x, anchors, num_classes=4, input_shape=(416, 416)): in_shape = tf.constant(input_shape, dtype=tf.float32) # anchors = K.constant(anchors) detectors_mask = x[:3] matching_true_boxes = x[3:6] true_box_all = x[6] ys = x[7:10] object_scale = 5 no_object_scale = 1 class_scale = 1 coordinates_scale = 1 m = K.shape(ys[0])[0] # batch size, tensor loss = 0 mf = K.cast(m, K.dtype(ys[0])) for index, y in enumerate(ys): box_xy_predict, box_wh_predict, box_confidence_predict, box_classes_predict, box_coord_predict = \ yolo_parse_output(y, anchors=anchors[(index*3):(index*3+3), :], input_shape=in_shape, num_classes=num_classes) box_xy_predict = K.expand_dims(box_xy_predict, 4) box_wh_predict = K.expand_dims(box_wh_predict, 4) pred_wh_half = box_wh_predict / 2. pred_mins = box_xy_predict - pred_wh_half pred_maxes = box_xy_predict + pred_wh_half true_box = true_box_all[:, index, :, :] true_boxes_shape = K.shape(true_box) # batch, conv_height, conv_width, num_anchors, num_true_boxes, box_params true_boxes = K.reshape(true_box, [ true_boxes_shape[0], 1, 1, 1, true_boxes_shape[1], true_boxes_shape[2] ]) true_xy = true_boxes[..., 0:2] true_wh = true_boxes[..., 2:4] # Find IOU of each predicted box with each ground truth box. true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half intersect_mins = K.maximum(pred_mins, true_mins) intersect_maxes = K.minimum(pred_maxes, true_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] pred_areas = box_wh_predict[..., 0] * box_wh_predict[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = intersect_areas / union_areas # Best IOUs for each location. best_ious = K.max(iou_scores, axis=4) # Best IOU scores. best_ious = K.expand_dims(best_ious) object_detections = K.cast(best_ious > 0.4, dtype=K.dtype(best_ious)) no_object_weights = (no_object_scale * (1 - object_detections) * (1 - detectors_mask[index])) no_objects_loss = no_object_weights * K.binary_crossentropy( detectors_mask[index], box_confidence_predict, from_logits=False) object_loss = object_scale * detectors_mask[ index] * K.binary_crossentropy(detectors_mask[index], box_confidence_predict, from_logits=False) xy_loss = (coordinates_scale * detectors_mask[index] * K.binary_crossentropy(matching_true_boxes[index][..., 0:2], box_coord_predict[..., 0:2], from_logits=False)) wh_loss = (coordinates_scale * detectors_mask[index] * K.square(matching_true_boxes[index][..., 2:4] - box_coord_predict[..., 2:4])) matching_classes = K.cast(matching_true_boxes[index][..., 4], 'int32') matching_classes = K.one_hot(matching_classes, num_classes) classification_loss = ( class_scale * detectors_mask[index] * K.binary_crossentropy( matching_classes, box_classes_predict, from_logits=False)) no_objects_loss = K.sum(no_objects_loss) object_loss = K.sum(object_loss) xy_loss = K.sum(xy_loss) wh_loss = K.sum(wh_loss) classification_loss = K.sum(classification_loss) total_loss = no_objects_loss + object_loss + xy_loss + wh_loss + classification_loss loss += total_loss losses = loss / mf return losses