def graph_fn(): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.matched_iou(boxes1, boxes2) return iou
def _compute_loss(self, prediction_tensor, target_tensor, decoded_prediction_tensor, decoded_target_tensor, weights): """Compute loss function. Args: prediction_tensor: A float tensor of shape [batch_size, num_anchors, code_size] representing the (encoded) predicted locations of objects. target_tensor: A float tensor of shape [batch_size, num_anchors, code_size] representing the regression targets weights: a float tensor of shape [batch_size, num_anchors] Returns: loss: a float tensor of shape [batch_size, num_anchors] tensor representing the value of the loss function. """ ''' target_w = target_tensor[:,:,2:3] # [batch_size, num_anchors,w,h] target_h = target_tensor[:,:,3:4] # [batch_size, num_anchors,w,h] predict_w = prediction_tensor[:,:,2:3] # [batch_size, num_anchors,w,h] predict_h = prediction_tensor[:,:,3:4] # [batch_size, num_anchors,w,h] predict_ratio = tf.truediv(predict_w + 1e-8,predict_h + 1e-8) target_ratio = tf.truediv(target_w + 1e-8,target_h + 1e-8) diff = predict_ratio - target_ratio ratio_diff = tf.reduce_sum(diff, 2) ''' SmoothL1loss = tf.reduce_sum(tf.losses.huber_loss( target_tensor, prediction_tensor, delta=self._delta, weights=tf.expand_dims(weights, axis=2), loss_collection=None, reduction=tf.losses.Reduction.NONE), axis=2) predicted_boxes = box_list.BoxList( tf.reshape(decoded_prediction_tensor, [-1, 4])) target_boxes = box_list.BoxList( tf.reshape(decoded_target_tensor, [-1, 4])) per_anchor_iou_loss = 1.0 - box_list_ops.matched_iou( predicted_boxes, target_boxes) IoUloss = tf.reshape(weights, [-1]) * per_anchor_iou_loss IoUloss = tf.reshape(IoUloss, [SmoothL1loss.get_shape()[0], -1]) # from IPython import embed;embed() if tf.to_int32(tf.is_nan(IoUloss)) == 1 or tf.to_int32( tf.is_inf(IoUloss)) == 1: return SmoothL1loss else: return tf.add(SmoothL1loss, IoUloss)
def test_matched_iou(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) exp_output = [2.0 / 16.0, 0] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.matched_iou(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou) self.assertAllClose(iou_output, exp_output)
def test_matched_iou(self): corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) exp_output = [2.0 / 16.0, 0] boxes1 = box_list.BoxList(corners1) boxes2 = box_list.BoxList(corners2) iou = box_list_ops.matched_iou(boxes1, boxes2) with self.test_session() as sess: iou_output = sess.run(iou) self.assertAllClose(iou_output, exp_output)
def _compute_loss(self, prediction_tensor, target_tensor, weights): """Compute loss function. Args: prediction_tensor: A float tensor of shape [batch_size, num_anchors, 4] representing the decoded predicted boxes target_tensor: A float tensor of shape [batch_size, num_anchors, 4] representing the decoded target boxes weights: a float tensor of shape [batch_size, num_anchors] Returns: loss: a (scalar) tensor representing the value of the loss function """ predicted_boxes = box_list.BoxList( tf.reshape(prediction_tensor, [-1, 4])) target_boxes = box_list.BoxList(tf.reshape(target_tensor, [-1, 4])) per_anchor_iou_loss = 1.0 - box_list_ops.matched_iou( predicted_boxes, target_boxes) return tf.reduce_sum(tf.reshape(weights, [-1]) * per_anchor_iou_loss)
def _compute_loss(self, prediction_tensor, target_tensor, weights): """Compute loss function. Args: prediction_tensor: A float tensor of shape [batch_size, num_anchors, 4] representing the decoded predicted boxes target_tensor: A float tensor of shape [batch_size, num_anchors, 4] representing the decoded target boxes weights: a float tensor of shape [batch_size, num_anchors] Returns: loss: a (scalar) tensor representing the value of the loss function """ predicted_boxes = box_list.BoxList(tf.reshape(prediction_tensor, [-1, 4])) target_boxes = box_list.BoxList(tf.reshape(target_tensor, [-1, 4])) per_anchor_iou_loss = 1.0 - box_list_ops.matched_iou(predicted_boxes, target_boxes) return tf.reduce_sum(tf.reshape(weights, [-1]) * per_anchor_iou_loss)
def _compute_loss(self, prediction_tensor, target_tensor, weights): predicted_boxes = box_list.BoxList( tf.reshape(prediction_tensor[:, :, 0:4], [-1, 4])) target_boxes = box_list.BoxList( tf.reshape(target_tensor[:, :, 0:4], [-1, 4])) ious = box_list_ops.matched_iou(predicted_boxes, target_boxes) ious = tf.Print( ious, [tf.reduce_min(ious), tf.reduce_max(ious), tf.reduce_mean(ious)], message='ious=') ### iou_loss = -1.0 * tf.log(tf.clip_by_value(ious, 1e-10, 1.0)) predicted_angle = tf.reshape(prediction_tensor[:, :, 4], [-1]) target_angle = tf.reshape(target_tensor[:, :, 4], [-1]) angle_loss = 1.0 - tf.cos(predicted_angle - target_angle) return tf.reduce_sum(tf.reshape(weights, [-1]) * iou_loss) \ + tf.reduce_sum(tf.reshape(weights, [-1]) * angle_loss) * self._alpha
def coordinates_to_iou(y_grid, x_grid, blist, channels_onehot, weights=None): """Computes a per-pixel IoU with groundtruth boxes. At each pixel, we return the IoU assuming that we predicted the ideal height and width for the box at that location. Args: y_grid: A 2D tensor with shape [height, width] which contains the grid y-coordinates given in the (output) image dimensions. x_grid: A 2D tensor with shape [height, width] which contains the grid x-coordinates given in the (output) image dimensions. blist: A BoxList object with `num_instances` number of boxes. channels_onehot: A 2D tensor with shape [num_instances, num_channels] representing the one-hot encoded channel labels for each point. weights: A 1D tensor with shape [num_instances] corresponding to the weight of each instance. Returns: iou_heatmap: A [height, width, num_channels] shapes float tensor denoting the IoU based heatmap. """ image_height, image_width = tf.shape(y_grid)[0], tf.shape(y_grid)[1] num_pixels = image_height * image_width _, _, height, width = blist.get_center_coordinates_and_sizes() num_boxes = tf.shape(height)[0] per_pixel_ymin = (y_grid[tf.newaxis, :, :] - (height[:, tf.newaxis, tf.newaxis] / 2.0)) per_pixel_xmin = (x_grid[tf.newaxis, :, :] - (width[:, tf.newaxis, tf.newaxis] / 2.0)) per_pixel_ymax = (y_grid[tf.newaxis, :, :] + (height[:, tf.newaxis, tf.newaxis] / 2.0)) per_pixel_xmax = (x_grid[tf.newaxis, :, :] + (width[:, tf.newaxis, tf.newaxis] / 2.0)) # [num_boxes, height, width] -> [num_boxes * height * width] per_pixel_ymin = tf.reshape( per_pixel_ymin, [num_pixels * num_boxes]) per_pixel_xmin = tf.reshape( per_pixel_xmin, [num_pixels * num_boxes]) per_pixel_ymax = tf.reshape( per_pixel_ymax, [num_pixels * num_boxes]) per_pixel_xmax = tf.reshape( per_pixel_xmax, [num_pixels * num_boxes]) per_pixel_blist = box_list.BoxList( tf.stack([per_pixel_ymin, per_pixel_xmin, per_pixel_ymax, per_pixel_xmax], axis=1)) target_boxes = tf.tile( blist.get()[:, tf.newaxis, :], [1, num_pixels, 1]) # [num_boxes, height * width, 4] -> [num_boxes * height * wdith, 4] target_boxes = tf.reshape(target_boxes, [num_pixels * num_boxes, 4]) target_blist = box_list.BoxList(target_boxes) ious = box_list_ops.matched_iou(target_blist, per_pixel_blist) ious = tf.reshape(ious, [num_boxes, image_height, image_width]) per_class_iou = ( ious[:, :, :, tf.newaxis] * channels_onehot[:, tf.newaxis, tf.newaxis, :]) if weights is not None: per_class_iou = ( per_class_iou * weights[:, tf.newaxis, tf.newaxis, tf.newaxis]) per_class_iou = tf.maximum(per_class_iou, 0.0) return tf.reduce_max(per_class_iou, axis=0)