code: def calculate_metrics(
      predicted_batch,
      real_batch,
      threshold=0.5,
      is_training=False,
      ema_decay=0.9,
  ):
      with tf.variable_scope("metric"):
          threshold_graph = tf.constant(threshold, name="threshold")
          zero_point_five = tf.constant(0.5)
          predicted_bool = tf.greater_equal(
              predicted_batch, threshold_graph
          )
          real_bool = tf.greater_equal(real_batch, zero_point_five)
          predicted_bool_neg = tf.logical_not(predicted_bool)
          real_bool_neg = tf.logical_not(real_bool)
          differences_bool = tf.logical_xor(predicted_bool, real_bool)
          tp = tf.logical_and(predicted_bool, real_bool)
          tn = tf.logical_and(predicted_bool_neg, real_bool_neg)
          fn = tf.logical_and(differences_bool, real_bool)
          fp = tf.logical_and(differences_bool, predicted_bool)
          tp = tf.reduce_sum(tf.cast(tp, tf.float32))
          tn = tf.reduce_sum(tf.cast(tn, tf.float32))
          fn = tf.reduce_sum(tf.cast(fn, tf.float32))
          fp = tf.reduce_sum(tf.cast(fp, tf.float32))
  
          average_ops = None
          init_op = None
          if is_training:
              ema = tf.train.ExponentialMovingAverage(decay=ema_decay)
              average_ops = ema.apply([tp, tn, fp, fn])
              tp = ema.average(tp)
              tn = ema.average(tn)
              fp = ema.average(fp)
              fn = ema.average(fn)
          else:
              tp_v = tf.Variable(
                  0,
                  dtype=tf.float32,
                  name="true_positive",
                  trainable=False,
              )
              tn_v = tf.Variable(
                  0,
                  dtype=tf.float32,
                  name="true_negative",
                  trainable=False,
              )
              fp_v = tf.Variable(
                  0,
                  dtype=tf.float32,
                  name="false_positive",
                  trainable=False,
              )
              fn_v = tf.Variable(
                  0,
                  dtype=tf.float32,
                  name="false_negative",
                  trainable=False,
              )
              init_op = [
                  tf.assign(tp_v, 0),
                  tf.assign(tn_v, 0),
                  tf.assign(fp_v, 0),
                  tf.assign(fn_v, 0),
              ]
              tp = tf.assign_add(tp_v, tp)
              tn = tf.assign_add(tn_v, tn)
              fp = tf.assign_add(fp_v, fp)
              fn = tf.assign_add(fn_v, fn)
  
          # calculate metrics
          precision = tp / (tp + fp)
          recall = tp / (tp + fn)
          accuracy = (tp + tn) / (tp + tn + fp + fn)
          fall_out = fp / (tn + fp)
          f1_score = tp * 2 / (tp * 2 + fp + fn)
  
          # remove NaNs and set them to 0
          zero = tf.constant(0, dtype=tf.float32)
          precision = tf.cond(
              tf.equal(tp, 0.0), lambda: zero, lambda: precision
          )
          recall = tf.cond(
              tf.equal(tp, 0.0), lambda: zero, lambda: recall
          )
          accuracy = tf.cond(
              tf.equal(tp + tn, 0.0), lambda: zero, lambda: accuracy
          )
          fall_out = tf.cond(
              tf.equal(fp, 0.0), lambda: zero, lambda: fall_out
          )
          f1_score = tf.cond(
              tf.equal(tp, 0.0), lambda: zero, lambda: f1_score
          )
  
          # add to tensorboard
          # tf.summary.scalar('accuracy', accuracy)
          tf.summary.scalar("precision", precision)
          tf.summary.scalar("recall", recall)
          tf.summary.scalar("fall-out", fall_out)
          tf.summary.scalar("f1-score", f1_score)
          tf.summary.scalar("true_positive", tp)
          tf.summary.scalar("true_negative", tn)
          tf.summary.scalar("false_positive", fp)
          tf.summary.scalar("false_negative", fn)
  
      metrics_ops = {
          # 'accuracy' : accuracy,
          "precision": precision,
          "recall": recall,
          "fall-out": fall_out,
          "f1-score": f1_score,
          "true positive": tp,
          "true negative": tn,
          "false positive": fp,
          "false negative": fn,
      }
      return init_op, average_ops, metrics_ops
  
imports: import argparse
  import logging
  import multiprocessing
  import os
  import random
  import sys
  import time
  from xml.etree import ElementTree
  import numpy
  import tensorflow
  from tensorflow.contrib import slim
  from tensorflow.contrib.slim.python.slim.nets.inception_v3 import inception_v3, inception_v3_arg_scope
  from tensorflow.python.framework.errors_impl import OutOfRangeError
  from tensorflow.python.training import session_run_hook
  from tensorflow.python.training.basic_session_run_hooks import StopAtStepHook
  from tensorflow.python.training.monitored_session import SingularMonitoredSession
  import tensorlayer
label_desc: Use image node_id 7 for calling a global function with example usage: calculate_metrics(predicted_batch, real_batch, threshold, is_training, ema_decay) and returns: init_op,  average_ops,  metrics_ops