code: def f1_eval(logits, labels):
      def getpred(result, T1=0.5, T2=0.4):
          # 使用阈值得到preds, result = logits
          # T2 表示如果都低于T2 那么就是 no relation, 否则选取一个最大的
          ret = []
          for i in range(len(result)):
              r = []
              maxl, maxj = -1, -1
              for j in range(len(result[i])):
                  if result[i][j] > T1:
                      r += [j]
                  if result[i][j] > maxl:
                      maxl = result[i][j]
                      maxj = j
              if len(r) == 0:
                  if maxl <= T2:
                      r = [36]
                  else:
                      r += [maxj]
              ret.append(r)
          return ret
  
      def geteval(devp, data):
          correct_sys, all_sys = 0, 0
          correct_gt = 0
  
          for i in range(len(data)):
              # 每一个样本 都是[1,4,...,20] 表示有1,4,20 是1， 如果没有就是[36]
              for id in data[i]:
                  if id != 36:
                      # 标签中 1 的个数
                      correct_gt += 1
                      if id in devp[i]:
                          # 预测正确
                          correct_sys += 1
  
              for id in devp[i]:
                  if id != 36:
                      all_sys += 1
  
          precision = 1 if all_sys == 0 else correct_sys / all_sys
          recall = 0 if correct_gt == 0 else correct_sys / correct_gt
          f_1 = (
              2 * precision * recall / (precision + recall)
              if precision + recall != 0
              else 0
          )
          return f_1
  
      logits = np.asarray(logits)
      logits = list(1 / (1 + np.exp(-logits)))
  
      temp_labels = []
      for l in labels:
          t = []
          for i in range(36):
              if l[i] == 1:
                  t += [i]
          if len(t) == 0:
              t = [36]
          temp_labels.append(t)
      assert len(labels) == len(logits)
      labels = temp_labels
  
      bestT2 = bestf_1 = 0
      for T2 in range(51):
          devp = getpred(logits, T2=T2 / 100.0)
          f_1 = geteval(devp, labels)
          if f_1 > bestf_1:
              bestf_1 = f_1
              bestT2 = T2 / 100.0
  
      return bestf_1, bestT2
  
imports: import numpy
  from collections import Counter
label_desc: Use image node_id 2 for calling a global function with example usage: f1_eval(logits, labels) and returns: bestf_1,  bestT2, ret, f_1