(labels, bboxes, anchors, num_classes)
return input_mask, labels_input, box_delta_input, box_input
Encode annotations for losses computations.
  All the output tensors have a fix shape(none dynamic dimention).

  Args:
    labels: 1-D with shape `[num_bounding_boxes]`.
    bboxes: 2-D with shape `[num_bounding_boxes, 4]`. Format [ymin, xmin, ymax, xmax]
    anchors: 4-D tensor with shape `[num_anchors, 4]`. Format [cx, cy, w, h]

  Returns:
    input_mask: 2-D with shape `[num_anchors, 1]`, indicate which anchor to be used to cal loss.
    labels_input: 2-D with shape `[num_anchors, num_classes]`, one hot encode for every anchor.
    box_delta_input: 2-D with shape `[num_anchors, 4]`. Format [dcx, dcy, dw, dh]
    box_input: 2-D with shape '[num_anchors, 4]'. Format [ymin, xmin, ymax, xmax]
Encode annotations for losses computations.
  All the output tensors have a fix shape(none dynamic dimention).
Encode annotations for losses computations . All the output tensors have a fix shape ( none dynamic dimention ) .
def encode_annos(labels, bboxes, anchors, num_classes):
  """Encode annotations for losses computations.
  All the output tensors have a fix shape(none dynamic dimention).

  Args:
    labels: 1-D with shape `[num_bounding_boxes]`.
    bboxes: 2-D with shape `[num_bounding_boxes, 4]`. Format [ymin, xmin, ymax, xmax]
    anchors: 4-D tensor with shape `[num_anchors, 4]`. Format [cx, cy, w, h]

  Returns:
    input_mask: 2-D with shape `[num_anchors, 1]`, indicate which anchor to be used to cal loss.
    labels_input: 2-D with shape `[num_anchors, num_classes]`, one hot encode for every anchor.
    box_delta_input: 2-D with shape `[num_anchors, 4]`. Format [dcx, dcy, dw, dh]
    box_input: 2-D with shape '[num_anchors, 4]'. Format [ymin, xmin, ymax, xmax]
  """
  with tf.name_scope("Encode_annotations") as scope:
    num_anchors = config.ANCHORS
    # num_bboxes = tf.shape(bboxes)[0]

    # Cal iou, find the target anchor
    with tf.name_scope("Matching") as subscope:
      ious = batch_iou_fast(xywh_to_yxyx(anchors), bboxes)
      anchor_indices = tf.reshape(tf.arg_max(ious, dimension=1), shape=[-1, 1])  # target anchor indices
      # anchor_indices = tf.Print(anchor_indices, [anchor_indices], "anchor_indices", summarize=100)

      # discard duplicate # unique_idx wrong
      anchor_indices, idx, count = tf.unique_with_counts(tf.reshape(anchor_indices, shape=[-1]))
      ori_idx = tf.cumsum(tf.pad(count, [[1, 0]]))[:-1]
      anchor_indices = tf.reshape(anchor_indices, shape=[-1, 1])
      bboxes = tf.gather(bboxes, tf.unique(ori_idx)[0])
      labels = tf.gather(labels, tf.unique(ori_idx)[0])
      ious = tf.gather(ious, tf.unique(ori_idx)[0])
      num_bboxes = tf.shape(anchor_indices)[0]

      # TODO(shizehao):deal with duplicate
      # with tf.name_scope("Deal_with_duplicate"):
      #   dup_anchor_indices, indices_in_a, dup_anchor_indices_with_dup = find_dup(tf.reshape(anchor_indices, shape=[-1]))
      #
      #   # reset duplicated corresponding anchor
      #   conflicted_ious = tf.gather(ious, indices_in_a)
      #   top_k_anchor_indices = tf.nn.top_k(conflicted_ious, k=20).indices  # shape = [num_conflicted_bboxes, 20]
      #   dup_group_idx = tf.where(tf.equal(dup_anchor_indices_with_dup, tf.reshape(dup_anchor_indices, shape=[-1, 1])))
      #   seg_group = tf.unstack(dup_group_idx, axis=1)[0]


      with tf.name_scope("Deal_with_noneoverlap"):
        # find the none-overlap bbox
        bbox_indices = tf.reshape(tf.range(num_bboxes), shape=[-1, 1])
        # bbox_indices = tf.Print(bbox_indices, [bbox_indices], "bbox_indices", summarize=100)

        # anchor_indices = tf.Print(anchor_indices, [anchor_indices], "anchor_indices", summarize=100)
        iou_indices = tf.concat([bbox_indices, tf.cast(anchor_indices, dtype=tf.int32)], axis=1)
        # iou_indices = tf.Print(iou_indices, [iou_indices], "iou_indices", summarize=100)

        target_iou = tf.gather_nd(ious, iou_indices)
        # target_iou = tf.Print(target_iou,[target_iou],"target_iou",summarize=100)

        none_overlap_bbox_indices = tf.where(target_iou <= 0)  # 1-D
        # none_overlap_bbox_indices = tf.Print(none_overlap_bbox_indices, [none_overlap_bbox_indices], "none_overlap_bbox_indices", summarize=100)

        # find it's corresponding anchor
        target_bbox = tf.gather_nd(bboxes, none_overlap_bbox_indices)
        # target_bbox = tf.Print(target_bbox, [target_bbox], "target_bbox", summarize=100)

        closest_anchor_indices = arg_closest_anchor(target_bbox, xywh_to_yxyx(anchors))  # 1-D
        # closest_anchor_indices = tf.Print(closest_anchor_indices, [closest_anchor_indices, tf.gather(anchors, closest_anchor_indices)], "closest_anchor_indices", summarize=100)

      with tf.name_scope("Update_anchor_indices"):
        anchor_indices = tf.reshape(anchor_indices, shape=[-1])
        anchor_indices = update_tensor(anchor_indices, none_overlap_bbox_indices, closest_anchor_indices)
        anchor_indices = tf.reshape(anchor_indices, shape=[-1, 1])


    with tf.name_scope("Delta") as subscope:
      target_anchors = tf.gather_nd(anchors, anchor_indices)
      bboxes = yxyx_to_xywh(bboxes)
      delta = batch_delta(bboxes, target_anchors)



    with tf.name_scope("Scattering") as subscope:
      # bbox
      box_input = tf.scatter_nd(anchor_indices,
                                bboxes,
                                shape=[num_anchors, 4]
                                )

      # label
      labels_input = tf.scatter_nd(anchor_indices,
                                   tf.one_hot(labels, num_classes),
                                   shape=[num_anchors, num_classes]
                                   )

      # delta
      box_delta_input = tf.scatter_nd(anchor_indices,
                                      delta,
                                      shape=[num_anchors, 4]
                                      )





      # anchor mask
      # unique_indices, _ = tf.unique(tf.reshape(anchor_indices, shape=[-1]))
      # unique_indices = tf.Print(unique_indices, [unique_indices], summarize=100)
      # num_bboxes = tf.Print(num_bboxes, [num_bboxes])
      input_mask = tf.scatter_nd(anchor_indices,
                                 tf.ones([num_bboxes]),
                                 shape=[num_anchors])
      input_mask = tf.reshape(input_mask, shape=[-1, 1])

  return input_mask, labels_input, box_delta_input, box_input
def encode_annos ( labels , bboxes , anchors , num_classes ) : with tf . name_scope ( "Encode_annotations" ) as scope : num_anchors = config . ANCHORS # num_bboxes = tf.shape(bboxes)[0] # Cal iou, find the target anchor with tf . name_scope ( "Matching" ) as subscope : ious = batch_iou_fast ( xywh_to_yxyx ( anchors ) , bboxes ) anchor_indices = tf . reshape ( tf . arg_max ( ious , dimension = 1 ) , shape = [ - 1 , 1 ] ) # target anchor indices # anchor_indices = tf.Print(anchor_indices, [anchor_indices], "anchor_indices", summarize=100) # discard duplicate # unique_idx wrong anchor_indices , idx , count = tf . unique_with_counts ( tf . reshape ( anchor_indices , shape = [ - 1 ] ) ) ori_idx = tf . cumsum ( tf . pad ( count , [ [ 1 , 0 ] ] ) ) [ : - 1 ] anchor_indices = tf . reshape ( anchor_indices , shape = [ - 1 , 1 ] ) bboxes = tf . gather ( bboxes , tf . unique ( ori_idx ) [ 0 ] ) labels = tf . gather ( labels , tf . unique ( ori_idx ) [ 0 ] ) ious = tf . gather ( ious , tf . unique ( ori_idx ) [ 0 ] ) num_bboxes = tf . shape ( anchor_indices ) [ 0 ] # TODO(shizehao):deal with duplicate # with tf.name_scope("Deal_with_duplicate"): #   dup_anchor_indices, indices_in_a, dup_anchor_indices_with_dup = find_dup(tf.reshape(anchor_indices, shape=[-1])) # #   # reset duplicated corresponding anchor #   conflicted_ious = tf.gather(ious, indices_in_a) #   top_k_anchor_indices = tf.nn.top_k(conflicted_ious, k=20).indices  # shape = [num_conflicted_bboxes, 20] #   dup_group_idx = tf.where(tf.equal(dup_anchor_indices_with_dup, tf.reshape(dup_anchor_indices, shape=[-1, 1]))) #   seg_group = tf.unstack(dup_group_idx, axis=1)[0] with tf . name_scope ( "Deal_with_noneoverlap" ) : # find the none-overlap bbox bbox_indices = tf . reshape ( tf . range ( num_bboxes ) , shape = [ - 1 , 1 ] ) # bbox_indices = tf.Print(bbox_indices, [bbox_indices], "bbox_indices", summarize=100) # anchor_indices = tf.Print(anchor_indices, [anchor_indices], "anchor_indices", summarize=100) iou_indices = tf . concat ( [ bbox_indices , tf . cast ( anchor_indices , dtype = tf . int32 ) ] , axis = 1 ) # iou_indices = tf.Print(iou_indices, [iou_indices], "iou_indices", summarize=100) target_iou = tf . gather_nd ( ious , iou_indices ) # target_iou = tf.Print(target_iou,[target_iou],"target_iou",summarize=100) none_overlap_bbox_indices = tf . where ( target_iou <= 0 ) # 1-D # none_overlap_bbox_indices = tf.Print(none_overlap_bbox_indices, [none_overlap_bbox_indices], "none_overlap_bbox_indices", summarize=100) # find it's corresponding anchor target_bbox = tf . gather_nd ( bboxes , none_overlap_bbox_indices ) # target_bbox = tf.Print(target_bbox, [target_bbox], "target_bbox", summarize=100) closest_anchor_indices = arg_closest_anchor ( target_bbox , xywh_to_yxyx ( anchors ) ) # 1-D # closest_anchor_indices = tf.Print(closest_anchor_indices, [closest_anchor_indices, tf.gather(anchors, closest_anchor_indices)], "closest_anchor_indices", summarize=100) with tf . name_scope ( "Update_anchor_indices" ) : anchor_indices = tf . reshape ( anchor_indices , shape = [ - 1 ] ) anchor_indices = update_tensor ( anchor_indices , none_overlap_bbox_indices , closest_anchor_indices ) anchor_indices = tf . reshape ( anchor_indices , shape = [ - 1 , 1 ] ) with tf . name_scope ( "Delta" ) as subscope : target_anchors = tf . gather_nd ( anchors , anchor_indices ) bboxes = yxyx_to_xywh ( bboxes ) delta = batch_delta ( bboxes , target_anchors ) with tf . name_scope ( "Scattering" ) as subscope : # bbox box_input = tf . scatter_nd ( anchor_indices , bboxes , shape = [ num_anchors , 4 ] ) # label labels_input = tf . scatter_nd ( anchor_indices , tf . one_hot ( labels , num_classes ) , shape = [ num_anchors , num_classes ] ) # delta box_delta_input = tf . scatter_nd ( anchor_indices , delta , shape = [ num_anchors , 4 ] ) # anchor mask # unique_indices, _ = tf.unique(tf.reshape(anchor_indices, shape=[-1])) # unique_indices = tf.Print(unique_indices, [unique_indices], summarize=100) # num_bboxes = tf.Print(num_bboxes, [num_bboxes]) input_mask = tf . scatter_nd ( anchor_indices , tf . ones ( [ num_bboxes ] ) , shape = [ num_anchors ] ) input_mask = tf . reshape ( input_mask , shape = [ - 1 , 1 ] ) return input_mask , labels_input , box_delta_input , box_input