code: def build_graph_train(
      g_slice,
      gt_bbox,
      img,
      ids,
      scores,
      bbox,
      feat_ind,
      spatial_feat,
      iou_thresh=0.5,
      bbox_improvement=True,
      scores_top_k=50,
      overlap=False,
  ):
      """given ground truth and predicted bboxes, assign the label to the predicted w.r.t iou_thresh"""
      # match and re-factor the graph
      img_size = img.shape[2:4]
      gt_bbox[:, :, 0] /= img_size[1]
      gt_bbox[:, :, 1] /= img_size[0]
      gt_bbox[:, :, 2] /= img_size[1]
      gt_bbox[:, :, 3] /= img_size[0]
      bbox[:, :, 0] /= img_size[1]
      bbox[:, :, 1] /= img_size[0]
      bbox[:, :, 2] /= img_size[1]
      bbox[:, :, 3] /= img_size[0]
  
      n_graph = len(g_slice)
      g_pred_batch = []
      for gi in range(n_graph):
          g = g_slice[gi]
          ctx = g.ndata["bbox"].context
          inds = np.where(scores[gi, :, 0].asnumpy() > 0)[0].tolist()
          if len(inds) == 0:
              return None
          if len(inds) > scores_top_k:
              top_score_inds = (
                  scores[gi, inds, 0]
                  .asnumpy()
                  .argsort()[::-1][0:scores_top_k]
              )
              inds = np.array(inds)[top_score_inds].tolist()
  
          n_nodes = len(inds)
          roi_ind = feat_ind[gi, inds].squeeze(axis=1)
          g_pred = dgl.DGLGraph()
          g_pred.add_nodes(
              n_nodes,
              {
                  "pred_bbox": bbox[gi, inds],
                  "node_feat": spatial_feat[gi, roi_ind],
                  "node_class_pred": ids[gi, inds, 0],
                  "node_class_logit": nd.log(
                      scores[gi, inds, 0] + 1e-7
                  ),
              },
          )
  
          # iou matching
          ious = nd.contrib.box_iou(
              gt_bbox[gi], g_pred.ndata["pred_bbox"]
          ).asnumpy()
          H, W = ious.shape
          h = H
          w = W
          pred_to_gt_ind = np.array([-1 for i in range(W)])
          pred_to_gt_class_match = [0 for i in range(W)]
          pred_to_gt_class_match_id = [0 for i in range(W)]
          while h > 0 and w > 0:
              ind = int(ious.argmax())
              row_ind = ind // W
              col_ind = ind % W
              if ious[row_ind, col_ind] < iou_thresh:
                  break
              pred_to_gt_ind[col_ind] = row_ind
              gt_node_class = g.ndata["node_class"][row_ind]
              pred_node_class = g_pred.ndata["node_class_pred"][col_ind]
              if gt_node_class == pred_node_class:
                  pred_to_gt_class_match[col_ind] = 1
                  pred_to_gt_class_match_id[col_ind] = row_ind
              ious[row_ind, :] = -1
              ious[:, col_ind] = -1
              h -= 1
              w -= 1
  
          n_nodes = g_pred.number_of_nodes()
          triplet = []
          adjmat = np.zeros((n_nodes, n_nodes))
  
          src, dst = g.all_edges(order="eid")
          eid_keys = np.column_stack([src.asnumpy(), dst.asnumpy()])
          eid_dict = {}
          for i, key in enumerate(eid_keys):
              k = tuple(key)
              if k not in eid_dict:
                  eid_dict[k] = [i]
              else:
                  eid_dict[k].append(i)
          ori_rel_class = g.edata["rel_class"].asnumpy()
          for i in range(n_nodes):
              for j in range(n_nodes):
                  if i != j:
                      if (
                          pred_to_gt_class_match[i]
                          and pred_to_gt_class_match[j]
                      ):
                          sub_gt_id = pred_to_gt_class_match_id[i]
                          ob_gt_id = pred_to_gt_class_match_id[j]
                          eids = eid_dict[(sub_gt_id, ob_gt_id)]
                          rel_cls = ori_rel_class[eids]
                          n_edges_between = len(rel_cls)
                          for ii in range(n_edges_between):
                              triplet.append((i, j, rel_cls[ii]))
                          adjmat[i, j] = 1
                      else:
                          triplet.append((i, j, 0))
          src, dst, rel_class = tuple(zip(*triplet))
          rel_class = nd.array(rel_class, ctx=ctx).expand_dims(1)
          g_pred.add_edges(src, dst, data={"rel_class": rel_class})
  
          # other operations
          n_nodes = g_pred.number_of_nodes()
          n_edges = g_pred.number_of_edges()
          if bbox_improvement:
              g_pred.ndata["pred_bbox"] = bbox_improve(
                  g_pred.ndata["pred_bbox"]
              )
          g_pred.edata["rel_bbox"] = extract_edge_bbox(g_pred)
          g_pred.edata["batch_id"] = (
              nd.zeros((n_edges, 1), ctx=ctx) + gi
          )
  
          # remove non-overlapping edges
          if overlap:
              overlap_ious = nd.contrib.box_iou(
                  g_pred.ndata["pred_bbox"][:, 0:4],
                  g_pred.ndata["pred_bbox"][:, 0:4],
              ).asnumpy()
              cols, rows = np.where(overlap_ious <= 1e-7)
              if cols.shape[0] > 0:
                  eids = (
                      g_pred.edge_ids(cols, rows)[2].asnumpy().tolist()
                  )
                  if len(eids):
                      g_pred.remove_edges(eids)
                      if g_pred.number_of_edges() == 0:
                          g_pred = None
          g_pred_batch.append(g_pred)
  
      if n_graph > 1:
          return dgl.batch(g_pred_batch)
      else:
          return g_pred_batch[0]
  
file_path: dgl/examples/mxnet/scenegraph/utils/build_graph.py
imports: import dgl
  import numpy
  from mxnet import nd
label_desc: Use image node_id 3 for calling a global function with example usage: build_graph_train(g_slice, gt_bbox, img, ids, scores, bbox, feat_ind, spatial_feat, iou_thresh, bbox_improvement, scores_top_k, overlap) and returns: dgl, g_pred_batch, None