def compute_regression_loss(output, target, labels): condition = keras.backend.not_equal(labels, -1) indices = tensorflow_backend.where(condition) output = tensorflow_backend.gather_nd(output, indices) target = tensorflow_backend.gather_nd(target, indices) labels = tensorflow_backend.gather_nd(labels, indices) condition = keras.backend.greater(labels, 0) x = keras.backend.zeros_like(labels) + 1 y = keras.backend.zeros_like(labels) p_star_i = tensorflow_backend.where(condition, x, y) p_star_i = keras.backend.expand_dims(p_star_i, 0) output = keras.backend.expand_dims(output, 0) target = keras.backend.expand_dims(target, 0) a_y = tensorflow_backend.smooth_l1(output, target, anchored=True) a = p_star_i * a_y a = keras.backend.sum(a) # Divided by anchor overlaps b = keras.backend.sum(p_star_i + keras.backend.epsilon()) loss = 1.0 * (a / b) return loss
def compute_loss(output, target): condition = keras.backend.not_equal(target, -1) indices = tensorflow_backend.where(condition) output = tensorflow_backend.gather_nd(output, indices) target = tensorflow_backend.gather_nd(target, indices) loss = keras.backend.binary_crossentropy(target, output) loss = keras.backend.mean(loss) return loss
def unmap(data, count, inds_inside, fill=0): """ Unmap a subset of item (data) back to the original set of items (of size count) """ if keras.backend.ndim(data) == 1: ret = tensorflow.ones((count, ), dtype=keras.backend.floatx()) * fill inds_nd = keras.backend.expand_dims(inds_inside) else: ret = (count, keras.backend.shape(data)[1]) ret = tensorflow.ones(ret, dtype=keras.backend.floatx()) * fill data = keras.backend.transpose(data) data = keras.backend.reshape(data, (-1, )) inds_ii = keras.backend.tile(inds_inside, [4]) inds_ii = keras.backend.expand_dims(inds_ii) ones = keras.backend.expand_dims(keras.backend.ones_like(inds_inside), 1) inds_coords = keras.backend.concatenate( [ones * 0, ones, ones * 2, ones * 3], 0) inds_nd = keras.backend.concatenate([inds_ii, inds_coords], 1) inverse_ret = tensorflow_backend.gather_nd(-1 * ret, inds_nd) inverse_ret = tensorflow_backend.squeeze(inverse_ret) updates = inverse_ret + data ret = tensorflow_backend.scatter_add_tensor(ret, inds_nd, updates) return ret
def overlapping(anchors, gt_boxes, inds_inside): """ overlaps between the anchors and the gt boxes :param anchors: Generated anchors :param gt_boxes: Ground truth bounding boxes :param inds_inside: :return: """ assert keras.backend.ndim(anchors) == 2 assert keras.backend.ndim(gt_boxes) == 2 reference = common.overlap(anchors, gt_boxes) gt_argmax_overlaps_inds = keras.backend.argmax(reference, axis=0) argmax_overlaps_inds = keras.backend.argmax(reference, axis=1) arranged = keras.backend.arange(0, keras.backend.shape(inds_inside)[0]) indices = keras.backend.stack( [arranged, keras.backend.cast(argmax_overlaps_inds, "int32")], axis=0) indices = keras.backend.transpose(indices) max_overlaps = tensorflow_backend.gather_nd(reference, indices) return argmax_overlaps_inds, max_overlaps, gt_argmax_overlaps_inds
def detections(num_output): proposals, deltas, scores, metadata = x[0], x[1], x[2], x[3] proposals = keras.backend.reshape(proposals, (-1, 4)) # unscale back to raw image space boxes = proposals / metadata[0][2] num_objects = keras.backend.shape(proposals)[0] deltas = keras.backend.reshape(deltas, (num_objects, -1)) # Apply bounding-box regression deltas pred_boxes = common.bbox_transform_inv(boxes, deltas) pred_boxes = common.clip(pred_boxes, metadata[0][:2]) scores = keras.backend.reshape(scores, (num_objects, -1)) # Arg max inds = keras.backend.expand_dims( keras.backend.arange(0, num_objects, dtype='int64')) top_classes = keras.backend.expand_dims( keras.backend.argmax(scores, axis=1)) coordinate_0 = keras.backend.concatenate([inds, top_classes * 4], 1) coordinate_1 = keras.backend.concatenate( [inds, top_classes * 4 + 1], 1) coordinate_2 = keras.backend.concatenate( [inds, top_classes * 4 + 2], 1) coordinate_3 = keras.backend.concatenate( [inds, top_classes * 4 + 3], 1) pred_boxes = tensorflow_backend.gather_nd( pred_boxes, keras.backend.reshape( keras.backend.concatenate([ coordinate_0, coordinate_1, coordinate_2, coordinate_3 ], 1), (-1, 2))) pred_boxes = keras.backend.reshape(pred_boxes, (-1, 4)) max_scores = keras.backend.max(scores, axis=1) nms_indices = tensorflow_backend.non_maximum_suppression( boxes=pred_boxes, scores=max_scores, maximum=num_objects, threshold=0.7) pred_boxes = keras.backend.gather(pred_boxes, nms_indices) scores = keras.backend.gather(scores, nms_indices) detections = [ keras.backend.expand_dims(pred_boxes, 0), keras.backend.expand_dims(scores, 0) ] return detections[num_output]
def set_label_background(self, labels): # Clamp labels for the background RoIs to 0 update_indices = keras.backend.arange(self.fg_rois_per_this_image, keras.backend.shape(labels)[0]) update_indices = keras.backend.reshape(update_indices, (-1, 1)) # By making the label = background inverse_labels = tensorflow_backend.gather_nd(labels, update_indices) * -1 labels = tensorflow_backend.scatter_add_tensor(labels, update_indices, inverse_labels) return labels