code: def test_badcase(
      data_set,
      mx_model,
      batch_size,
      name="",
      data_extra=None,
      label_shape=None,
  ):
      print("testing verification badcase..")
      data_list = data_set[0]
      issame_list = data_set[1]
      model = mx_model
      embeddings_list = []
      if data_extra is not None:
          _data_extra = nd.array(data_extra)
      time_consumed = 0.0
      if label_shape is None:
          _label = nd.ones((batch_size,))
      else:
          _label = nd.ones(label_shape)
      for i in range(len(data_list)):
          data = data_list[i]
          embeddings = None
          ba = 0
          while ba < data.shape[0]:
              bb = min(ba + batch_size, data.shape[0])
              count = bb - ba
              _data = nd.slice_axis(
                  data, axis=0, begin=bb - batch_size, end=bb
              )
              # print(_data.shape, _label.shape)
              time0 = datetime.datetime.now()
              if data_extra is None:
                  db = mx.io.DataBatch(data=(_data,), label=(_label,))
              else:
                  db = mx.io.DataBatch(
                      data=(_data, _data_extra), label=(_label,)
                  )
              model.forward(db, is_train=False)
              net_out = model.get_outputs()
              _embeddings = net_out[0].asnumpy()
              time_now = datetime.datetime.now()
              diff = time_now - time0
              time_consumed += diff.total_seconds()
              if embeddings is None:
                  embeddings = np.zeros(
                      (data.shape[0], _embeddings.shape[1])
                  )
              embeddings[ba:bb, :] = _embeddings[
                  (batch_size - count) :, :
              ]
              ba = bb
          embeddings_list.append(embeddings)
      embeddings = embeddings_list[0] + embeddings_list[1]
      embeddings = sklearn.preprocessing.normalize(embeddings)
      thresholds = np.arange(0, 4, 0.01)
      actual_issame = np.asarray(issame_list)
      nrof_folds = 10
      embeddings1 = embeddings[0::2]
      embeddings2 = embeddings[1::2]
      assert embeddings1.shape[0] == embeddings2.shape[0]
      assert embeddings1.shape[1] == embeddings2.shape[1]
      nrof_pairs = min(len(actual_issame), embeddings1.shape[0])
      nrof_thresholds = len(thresholds)
      k_fold = LFold(n_splits=nrof_folds, shuffle=False)
  
      tprs = np.zeros((nrof_folds, nrof_thresholds))
      fprs = np.zeros((nrof_folds, nrof_thresholds))
      accuracy = np.zeros((nrof_folds))
      indices = np.arange(nrof_pairs)
  
      diff = np.subtract(embeddings1, embeddings2)
      dist = np.sum(np.square(diff), 1)
      data = data_list[0]
  
      pouts = []
      nouts = []
  
      for fold_idx, (train_set, test_set) in enumerate(
          k_fold.split(indices)
      ):
          # Find the best threshold for the fold
          acc_train = np.zeros((nrof_thresholds))
          # print(train_set)
          # print(train_set.__class__)
          for threshold_idx, threshold in enumerate(thresholds):
              p2 = dist[train_set]
              p3 = actual_issame[train_set]
              _, _, acc_train[threshold_idx] = calculate_accuracy(
                  threshold, p2, p3
              )
          best_threshold_index = np.argmax(acc_train)
          for threshold_idx, threshold in enumerate(thresholds):
              (
                  tprs[fold_idx, threshold_idx],
                  fprs[fold_idx, threshold_idx],
                  _,
              ) = calculate_accuracy(
                  threshold, dist[test_set], actual_issame[test_set]
              )
          _, _, accuracy[fold_idx] = calculate_accuracy(
              thresholds[best_threshold_index],
              dist[test_set],
              actual_issame[test_set],
          )
          best_threshold = thresholds[best_threshold_index]
          for iid in test_set:
              ida = iid * 2
              idb = ida + 1
              asame = actual_issame[iid]
              _dist = dist[iid]
              violate = _dist - best_threshold
              if not asame:
                  violate *= -1.0
              if violate > 0.0:
                  imga = (
                      data[ida]
                      .asnumpy()
                      .transpose((1, 2, 0))[..., ::-1]
                  )  # to bgr
                  imgb = (
                      data[idb]
                      .asnumpy()
                      .transpose((1, 2, 0))[..., ::-1]
                  )
                  # print(imga.shape, imgb.shape, violate, asame, _dist)
                  if asame:
                      pouts.append(
                          (imga, imgb, _dist, best_threshold, ida)
                      )
                  else:
                      nouts.append(
                          (imga, imgb, _dist, best_threshold, ida)
                      )
  
      tpr = np.mean(tprs, 0)
      fpr = np.mean(fprs, 0)
      acc = np.mean(accuracy)
      pouts = sorted(pouts, key=lambda x: x[2], reverse=True)
      nouts = sorted(nouts, key=lambda x: x[2], reverse=False)
      print(len(pouts), len(nouts))
      print("acc", acc)
      gap = 10
      image_shape = (112, 224, 3)
      out_dir = "./badcases"
      if not os.path.exists(out_dir):
          os.makedirs(out_dir)
      if len(nouts) > 0:
          threshold = nouts[0][3]
      else:
          threshold = pouts[-1][3]
  
      for item in [
          (pouts, "positive(false_negative).png"),
          (nouts, "negative(false_positive).png"),
      ]:
          cols = 4
          rows = 8000
          outs = item[0]
          if len(outs) == 0:
              continue
          # if len(outs)==9:
          #  cols = 3
          #  rows = 3
  
          _rows = int(math.ceil(len(outs) / cols))
          rows = min(rows, _rows)
          hack = {}
  
          if name.startswith("cfp") and item[1].startswith("pos"):
              hack = {
                  0: "manual/238_13.jpg.jpg",
                  6: "manual/088_14.jpg.jpg",
                  10: "manual/470_14.jpg.jpg",
                  25: "manual/238_13.jpg.jpg",
                  28: "manual/143_11.jpg.jpg",
              }
  
          filename = item[1]
          if len(name) > 0:
              filename = name + "_" + filename
          filename = os.path.join(out_dir, filename)
          img = np.zeros(
              (
                  image_shape[0] * rows + 20,
                  image_shape[1] * cols + (cols - 1) * gap,
                  3,
              ),
              dtype=np.uint8,
          )
          img[:, :, :] = 255
          text_color = (0, 0, 153)
          text_color = (255, 178, 102)
          text_color = (153, 255, 51)
          for outi, out in enumerate(outs):
              row = outi // cols
              col = outi % cols
              if row == rows:
                  break
              imga = out[0].copy()
              imgb = out[1].copy()
              if outi in hack:
                  idx = out[4]
                  print("noise idx", idx)
                  aa = hack[outi]
                  imgb = cv2.imread(aa)
                  # if aa==1:
                  #  imgb = cv2.transpose(imgb)
                  #  imgb = cv2.flip(imgb, 1)
                  # elif aa==3:
                  #  imgb = cv2.transpose(imgb)
                  #  imgb = cv2.flip(imgb, 0)
                  # else:
                  #  for ii in range(2):
                  #    imgb = cv2.transpose(imgb)
                  #    imgb = cv2.flip(imgb, 1)
              dist = out[2]
              _img = np.concatenate((imga, imgb), axis=1)
              k = "%.3f" % dist
              # print(k)
              font = cv2.FONT_HERSHEY_SIMPLEX
              cv2.putText(
                  _img,
                  k,
                  (80, image_shape[0] // 2 + 7),
                  font,
                  0.6,
                  text_color,
                  2,
              )
              # _filename = filename+"_%d.png"%outi
              # cv2.imwrite(_filename, _img)
              img[
                  row * image_shape[0] : (row + 1) * image_shape[0],
                  (col * image_shape[1] + gap * col) : (
                      (col + 1) * image_shape[1] + gap * col
                  ),
                  :,
              ] = _img
          # threshold = outs[0][3]
          font = cv2.FONT_HERSHEY_SIMPLEX
          k = "threshold: %.3f" % threshold
          cv2.putText(
              img,
              k,
              (img.shape[1] // 2 - 70, img.shape[0] - 5),
              font,
              0.6,
              text_color,
              2,
          )
          cv2.imwrite(filename, img)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import os
  import argparse
  import sys
  import numpy
  from scipy import misc
  from sklearn.model_selection import KFold
  from scipy import interpolate
  import sklearn
  import cv2
  import math
  import datetime
  import pickle
  from sklearn.decomposition import PCA
  import mxnet
  from mxnet import ndarray
label_desc: Use image node_id 8 for calling a global function with example usage: test_badcase(data_set, mx_model, batch_size, name, data_extra, label_shape) without return types