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
      """
  
      # 1. create a model and its outputs
      net_output_ops = resnet_3d(
          features["x"],
          num_res_units=2,
          num_classes=NUM_CLASSES,
          filters=(16, 32, 64, 128, 256),
          strides=(
              (1, 1, 1),
              (2, 2, 2),
              (2, 2, 2),
              (2, 2, 2),
              (2, 2, 2),
          ),
          mode=mode,
          kernel_regularizer=tf.contrib.layers.l2_regularizer(1e-3),
      )
  
      # 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
      one_hot_labels = tf.reshape(
          tf.one_hot(labels["y"], depth=NUM_CLASSES), [-1, NUM_CLASSES]
      )
  
      loss = tf.losses.softmax_cross_entropy(
          onehot_labels=one_hot_labels, logits=net_output_ops["logits"]
      )
  
      # 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
      my_image_summaries = {}
      my_image_summaries["feat_t1"] = features["x"][0, 32, :, :, 0]
  
      expected_output_size = [1, 96, 96, 1]  # [B, W, H, C]
      [
          tf.summary.image(
              name, tf.reshape(image, expected_output_size)
          )
          for name, image in my_image_summaries.items()
      ]
  
      # 4.2 (optional) track the rmse (scaled back by 100, see reader.py)
      acc = tf.metrics.accuracy
      prec = tf.metrics.precision
      eval_metric_ops = {
          "accuracy": acc(labels["y"], net_output_ops["y_"]),
          "precision": prec(labels["y"], net_output_ops["y_"]),
      }
  
      # 5. Return EstimatorSpec object
      return tf.estimator.EstimatorSpec(
          mode=mode,
          predictions=net_output_ops,
          loss=loss,
          train_op=train_op,
          eval_metric_ops=eval_metric_ops,
      )
  
file_path: DLTK/examples/applications/IXI_HH_sex_classification_resnet/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.networks.regression_classification.resnet import resnet_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