'Encode a box and keypoint collection with respect to anchor collection.
Args:
boxes: BoxList holding N boxes and keypoints to be encoded. Boxes are
tensors with the shape [N, 4], and keypoints are tensors with the shape
[N, num_keypoints, 2].
anchors: BoxList of anchors.
Returns:
a tensor representing N anchor-encoded boxes of the format
[ty, tx, th, tw, tky0, tkx0, tky1, tkx1, ...] where tky0 and tkx0
represent the y and x coordinates of the first keypoint, tky1 and tkx1
represent the y and x coordinates of the second keypoint, and so on.'

def _encode(self, boxes, anchors):

    (ycenter_a, xcenter_a, ha, wa) = anchors.get_center_coordinates_and_sizes()
    (ycenter, xcenter, h, w) = boxes.get_center_coordinates_and_sizes()
    keypoints = boxes.get_field(fields.BoxListFields.keypoints)
    keypoints = tf.transpose(tf.reshape(keypoints, [(-1), (self._num_keypoints * 2)]))
    num_boxes = boxes.num_boxes()
    ha += EPSILON
    wa += EPSILON
    h += EPSILON
    w += EPSILON
    tx = ((xcenter - xcenter_a) / wa)
    ty = ((ycenter - ycenter_a) / ha)
    tw = tf.log((w / wa))
    th = tf.log((h / ha))
    tiled_anchor_centers = tf.tile(tf.stack([ycenter_a, xcenter_a]), [self._num_keypoints, 1])
    tiled_anchor_sizes = tf.tile(tf.stack([ha, wa]), [self._num_keypoints, 1])
    tkeypoints = ((keypoints - tiled_anchor_centers) / tiled_anchor_sizes)
    if self._scale_factors:
        ty *= self._scale_factors[0]
        tx *= self._scale_factors[1]
        th *= self._scale_factors[2]
        tw *= self._scale_factors[3]
        tkeypoints *= tf.tile(self._keypoint_scale_factors, [1, num_boxes])
    tboxes = tf.stack([ty, tx, th, tw])
    return tf.transpose(tf.concat([tboxes, tkeypoints], 0))
