code: def model_fn(features, labels, mode, params):
      """Model function to construct a tf.estimator.EstimatorSpec. It creates a
          network given input features (e.g. from a dltk.io.abstract_reader) and
          training targets (labels). Further, loss, optimiser, evaluation ops and
          custom tensorboard summary ops can be added. For additional information,
          please refer to https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator#model_fn.
  
      Args:
          features (tf.Tensor): Tensor of input features to train from. Required
              rank and dimensions are determined by the subsequent ops
              (i.e. the network).
          labels (tf.Tensor): Tensor of training targets or labels. Required rank
              and dimensions are determined by the network output.
          mode (str): One of the tf.estimator.ModeKeys: TRAIN, EVAL or PREDICT
          params (dict, optional): A dictionary to parameterise the model_fn
              (e.g. learning_rate)
  
      Returns:
          tf.estimator.EstimatorSpec: A custom EstimatorSpec for this experiment
      """
  
      assert len(params["upsampling_factor"]) == 3
  
      # During training downsample the original input to create a lower resolution
      #  image
      if (
          mode == tf.estimator.ModeKeys.TRAIN
          or mode == tf.estimator.ModeKeys.EVAL
      ):
          lo_res = tf.layers.average_pooling3d(
              inputs=features["x"],
              pool_size=[
                  2 * s if s > 1 else 1
                  for s in params["upsampling_factor"]
              ],
              strides=params["upsampling_factor"],
              padding="same",
              name="training_downsample",
          )
  
          # Compute some linear upsampling for comparison:
          lin_up = linear_upsample_3d(
              inputs=lo_res, strides=params["upsampling_factor"]
          )
      else:
          lo_res = features["x"]
  
      # 1. create a model and its outputs
      net_output_ops = simple_super_resolution_3d(
          inputs=lo_res,
          num_convolutions=2,
          upsampling_factor=params["upsampling_factor"],
          filters=(16, 32, 64, 128),
          mode=mode,
      )
  
      # 1.1 Generate predictions only (for `ModeKeys.PREDICT`)
      if mode == tf.estimator.ModeKeys.PREDICT:
          return tf.estimator.EstimatorSpec(
              mode=mode,
              predictions=net_output_ops,
              export_outputs={
                  "out": tf.estimator.export.PredictOutput(
                      net_output_ops
                  )
              },
          )
  
      # 2. set up a loss function
      loss = tf.losses.mean_squared_error(
          labels=features["x"], predictions=net_output_ops["x_"]
      )
  
      # 3. define a training op and ops for updating moving averages
      # (i.e. for batch normalisation)
      global_step = tf.train.get_global_step()
      optimiser = tf.train.AdamOptimizer(
          learning_rate=params["learning_rate"], epsilon=1e-5
      )
  
      update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
      with tf.control_dependencies(update_ops):
          train_op = optimiser.minimize(loss, global_step=global_step)
  
      # 4.1 (optional) create custom image summaries for tensorboard
      tf.summary.image(
          "feat_lo_res",
          tf.reshape(lo_res[0, 4, :, :, 0:1], [1, 32, 32, 1]),
      )
  
      my_image_summaries = {}
      my_image_summaries["feat_hi_res"] = features["x"][
          0, 16, :, :, 0:1
      ]
      my_image_summaries["linear_up_hi_res"] = tf.cast(
          lin_up, tf.float32
      )[0, 16, :, :, 0:1]
      my_image_summaries["pred_hi_res"] = tf.cast(
          net_output_ops["x_"], tf.float32
      )[0, 16, :, :, 0:1]
  
      expected_output_size = [1, 128, 128, 1]  # [B, W, H, C]
      [
          tf.summary.image(
              name, tf.reshape(image, expected_output_size)
          )
          for name, image in my_image_summaries.items()
      ]
  
      # 5. Return EstimatorSpec object
      return tf.estimator.EstimatorSpec(
          mode=mode,
          predictions=net_output_ops,
          loss=loss,
          train_op=train_op,
          eval_metric_ops=None,
      )
  
file_path: DLTK/examples/applications/IXI_HH_superresolution/train.py
imports: from __future__ import unicode_literals
  from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import argparse
  import os
  import pandas
  import tensorflow
  import numpy
  from dltk.core.upsample import linear_upsample_3d
  from dltk.networks.super_resolution.simple_super_resolution import simple_super_resolution_3d
  from dltk.io.abstract_reader import Reader
  from reader import read_fn
label_desc: Use image node_id 1 for calling a global function with example usage: model_fn(features, labels, mode, params) and returns: tf, tf