code: def anchor_targets_bbox(
      anchors,
      image_group,
      annotations_group,
      num_classes,
      negative_overlap=0.4,
      positive_overlap=0.5,
  ):
      """Generate anchor targets for bbox detection.
  
      Args
          anchors: np.array of annotations of shape (N, 4) for (x1, y1, x2, y2).
          image_group: List of BGR images.
          annotations_group: List of annotation dictionaries with each annotation containing 'labels' and 'bboxes' of an image.
          num_classes: Number of classes to predict.
          mask_shape: If the image is padded with zeros, mask_shape can be used to mark the relevant part of the image.
          negative_overlap: IoU overlap for negative anchors (all anchors with overlap < negative_overlap are negative).
          positive_overlap: IoU overlap or positive anchors (all anchors with overlap > positive_overlap are positive).
  
      Returns
          labels_batch: batch that contains labels & anchor states (np.array of shape (batch_size, N, num_classes + 1),
                        where N is the number of anchors for an image and the last column defines the anchor state (-1 for ignore, 0 for bg, 1 for fg).
          regression_batch: batch that contains bounding-box regression targets for an image & anchor states (np.array of shape (batch_size, N, 4 + 1),
                        where N is the number of anchors for an image, the first 4 columns define regression targets for (x1, y1, x2, y2) and the
                        last column defines anchor states (-1 for ignore, 0 for bg, 1 for fg).
      """
  
      assert len(image_group) == len(
          annotations_group
      ), "The length of the images and annotations need to be equal."
      assert (
          len(annotations_group) > 0
      ), "No data received to compute anchor targets for."
      for annotations in annotations_group:
          assert (
              "bboxes" in annotations
          ), "Annotations should contain bboxes."
          assert (
              "labels" in annotations
          ), "Annotations should contain labels."
  
      batch_size = len(image_group)
  
      regression_batch = np.zeros(
          (batch_size, anchors.shape[0], 4 + 1),
          dtype=keras.backend.floatx(),
      )
      labels_batch = np.zeros(
          (batch_size, anchors.shape[0], num_classes + 1),
          dtype=keras.backend.floatx(),
      )
  
      # compute labels and regression targets
      for index, (image, annotations) in enumerate(
          zip(image_group, annotations_group)
      ):
          if annotations["bboxes"].shape[0]:
              # obtain indices of gt annotations with the greatest overlap
              (
                  positive_indices,
                  ignore_indices,
                  argmax_overlaps_inds,
              ) = compute_gt_annotations(
                  anchors,
                  annotations["bboxes"],
                  negative_overlap,
                  positive_overlap,
              )
  
              labels_batch[index, ignore_indices, -1] = -1
              labels_batch[index, positive_indices, -1] = 1
  
              regression_batch[index, ignore_indices, -1] = -1
              regression_batch[index, positive_indices, -1] = 1
  
              # compute target class labels
              labels_batch[
                  index,
                  positive_indices,
                  annotations["labels"][
                      argmax_overlaps_inds[positive_indices]
                  ].astype(int),
              ] = 1
  
              regression_batch[index, :, :-1] = bbox_transform(
                  anchors,
                  annotations["bboxes"][argmax_overlaps_inds, :],
              )
  
          # ignore annotations outside of image
          if image.shape:
              anchors_centers = np.vstack(
                  [
                      (anchors[:, 0] + anchors[:, 2]) / 2,
                      (anchors[:, 1] + anchors[:, 3]) / 2,
                  ]
              ).T
              indices = np.logical_or(
                  anchors_centers[:, 0] >= image.shape[1],
                  anchors_centers[:, 1] >= image.shape[0],
              )
  
              labels_batch[index, indices, -1] = -1
              regression_batch[index, indices, -1] = -1
  
      return regression_batch, labels_batch
  
file_path: ImageAI/imageai_tf_deprecated/Detection/keras_retinanet/utils/anchors.py
imports: import numpy
  from tensorflow import keras
label_desc: Use image node_id 1 for calling a global function with example usage: anchor_targets_bbox(anchors, image_group, annotations_group, num_classes, negative_overlap, positive_overlap) and returns: regression_batch,  labels_batch