code: def test_net(
      net,
      imdb,
      output_dir,
      feature_scale,
      classifier="svm",
      nmsThreshold=0.3,
      boUsePythonImpl=False,
      boThresholdDetections=True,
      boApplyNms=True,
  ):
      """Test a Fast R-CNN network on an image database."""
      num_images = len(imdb.image_index)
      # heuristic: keep an average of 40 detections per class per images prior
      # to NMS
      max_per_set = 40 * num_images
      # heuristic: keep at most 100 detection per class per image prior to NMS
      max_per_image = 100
      # detection thresold for each class (this is adaptively set based on the
      # max_per_set constraint)
      thresh = -np.inf * np.ones(imdb.num_classes)
      # top_scores will hold one minheap of scores per class (used to enforce
      # the max_per_set constraint)
      top_scores = [[] for _ in range(imdb.num_classes)]
      # all detections are collected into:
      #    all_boxes[cls][image] = N x 5 array of detections in
      #    (x1, y1, x2, y2, score)
      all_boxes = [
          [[] for _ in range(num_images)]
          for _ in range(imdb.num_classes)
      ]
  
      # output_dir = get_output_dir(imdb, net)
  
      # timers
      _t = {"im_detect": Timer(), "misc": Timer()}
      roidb = imdb.roidb
  
      if not boThresholdDetections:
          for i in range(num_images):
              if i % 1000 == 0:
                  print(
                      "   Processing image {} of {}..".format(
                          i, num_images
                      )
                  )
              scores, _, _ = im_detect(
                  net,
                  i,
                  roidb[i]["boxes"],
                  feature_scale=feature_scale,
                  classifier=classifier,
              )
  
              for j in range(1, imdb.num_classes):
                  inds = np.where(roidb[i]["gt_classes"] == 0)[0]
                  cls_scores = scores[inds, j]
                  cls_boxes = roidb[i]["boxes"][inds]
                  all_boxes[j][i] = np.hstack(
                      (cls_boxes, cls_scores[:, np.newaxis])
                  ).astype(np.float32, copy=False)
  
      else:
          for i in range(num_images):
              if i % 1000 == 0:
                  print(
                      "   Processing image {} of {}..".format(
                          i, num_images
                      )
                  )
              # im = cv2.imread(imdb.image_path_at(i))
              # _t['im_detect'].tic()
              scores, _, _ = im_detect(
                  net,
                  i,
                  roidb[i]["boxes"],
                  feature_scale=feature_scale,
                  classifier=classifier,
              )
              # _t['im_detect'].toc()
  
              _t["misc"].tic()
              for j in range(1, imdb.num_classes):
                  inds = np.where(
                      (scores[:, j] > thresh[j])
                      & (roidb[i]["gt_classes"] == 0)
                  )[0]
                  cls_scores = scores[inds, j]
  
                  # cls_boxes = boxes[inds, j * 4:(j + 1) * 4]
                  boxes = roidb[i]["boxes"]
                  cls_boxes = boxes[inds]
  
                  top_inds = np.argsort(-cls_scores)[:max_per_image]
                  cls_scores = cls_scores[top_inds]
                  cls_boxes = cls_boxes[top_inds, :]
                  # push new scores onto the minheap
                  for val in cls_scores:
                      heapq.heappush(top_scores[j], val)
                  # if we've collected more than the max number of detection,
                  # then pop items off the minheap and update the class threshold
                  if len(top_scores[j]) > max_per_set:
                      while len(top_scores[j]) > max_per_set:
                          heapq.heappop(top_scores[j])
                      thresh[j] = top_scores[j][0]
  
                  all_boxes[j][i] = np.hstack(
                      (cls_boxes, cls_scores[:, np.newaxis])
                  ).astype(np.float32, copy=False)
  
                  # visualize rois
                  if False:
                      im = cv2.imread(imdb.image_path_at(i))
                      if boUsePythonImpl:
                          keep = nmsPython.nms(all_boxes[j][i], 0.3)
                      else:
                          keep = nms(all_boxes[j][i], 0.3)
                      vis_detections(
                          im, imdb.classes[j], all_boxes[j][i][keep, :]
                      )
              _t["misc"].toc()
  
              # print ('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
              #       .format(i + 1, num_images, _t['im_detect'].average_time,
              #               _t['misc'].average_time))
  
          # keep only the boxes with highest score for each class
          #   shape of all_boxes: e.g. 21 classes x 4952 images x 58 rois x 5 coord+score
          for j in range(1, imdb.num_classes):
              for i in range(num_images):
                  inds = np.where(all_boxes[j][i][:, -1] > thresh[j])[0]
                  if len(inds) == 0:
                      all_boxes[j][i] = []
                  else:
                      all_boxes[j][i] = all_boxes[j][i][inds, :]
  
      if output_dir:
          det_file = os.path.join(output_dir, "detections.pkl")
          with open(det_file, "wb") as f:
              cp.dump(all_boxes, f, cp.HIGHEST_PROTOCOL)
  
      if boApplyNms:
          print(
              "Number of rois before non-maxima surpression: %d"
              % sum(
                  [
                      len(all_boxes[i][j])
                      for i in range(imdb.num_classes)
                      for j in range(imdb.num_images)
                  ]
              )
          )
          nms_dets, _ = apply_nms(
              all_boxes, nmsThreshold, boUsePythonImpl
          )
          print(
              "Number of rois  after non-maxima surpression: %d"
              % sum(
                  [
                      len(nms_dets[i][j])
                      for i in range(imdb.num_classes)
                      for j in range(imdb.num_images)
                  ]
              )
          )
      else:
          print("Skipping non-maxima surpression")
          nms_dets = all_boxes
  
      print("Evaluating detections")
      imdb.evaluate_detections(nms_dets, output_dir)
  
file_path: CNTK/Examples/Image/Detection/FastRCNN/BrainScript/fastRCNN/test.py
imports: from __future__ import print_function
  import os, sys, cv2, numpy, pickle, heapq
  from .nms import nms
  from .utils.cython_nms import nms
  from .timer import Timer
  from cntk_helpers import im_detect, apply_nms
  from builtins import range
label_desc: Use image node_id 8 for calling a global function with example usage: test_net(net, imdb, output_dir, feature_scale, classifier, nmsThreshold, boUsePythonImpl, boThresholdDetections, boApplyNms) without return types