code: def f1_score(output, label, rel_num=42, na_num=13):
      correct_by_relation = Counter()
      guess_by_relation = Counter()
      gold_by_relation = Counter()
      output = np.argmax(output, axis=-1)
  
      for i in range(len(output)):
          guess = output[i]
          gold = label[i]
  
          if guess == na_num:
              guess = 0
          elif guess < na_num:
              guess += 1
  
          if gold == na_num:
              gold = 0
          elif gold < na_num:
              gold += 1
  
          if gold == 0 and guess == 0:
              continue
          if gold == 0 and guess != 0:
              guess_by_relation[guess] += 1
          if gold != 0 and guess == 0:
              gold_by_relation[gold] += 1
          if gold != 0 and guess != 0:
              guess_by_relation[guess] += 1
              gold_by_relation[gold] += 1
              if gold == guess:
                  correct_by_relation[gold] += 1
  
      f1_by_relation = Counter()
      recall_by_relation = Counter()
      prec_by_relation = Counter()
      for i in range(1, rel_num):
          recall = 0
          if gold_by_relation[i] > 0:
              recall = correct_by_relation[i] / gold_by_relation[i]
          precision = 0
          if guess_by_relation[i] > 0:
              precision = correct_by_relation[i] / guess_by_relation[i]
          if recall + precision > 0:
              f1_by_relation[i] = (
                  2 * recall * precision / (recall + precision)
              )
          recall_by_relation[i] = recall
          prec_by_relation[i] = precision
  
      micro_f1 = 0
      if (
          sum(guess_by_relation.values()) != 0
          and sum(correct_by_relation.values()) != 0
      ):
          recall = sum(correct_by_relation.values()) / sum(
              gold_by_relation.values()
          )
          prec = sum(correct_by_relation.values()) / sum(
              guess_by_relation.values()
          )
          micro_f1 = 2 * recall * prec / (recall + prec)
  
      return dict(f1=micro_f1)
  
imports: import numpy
  from collections import Counter
label_desc: Use image node_id 5 for calling a global function with example usage: f1_score(output, label, rel_num, na_num) and returns: dict