code: def connect_data_and_network(
      self, outputs_collector=None, gradients_collector=None
  ):
      if self.is_training:
          self.patience = self.action_param.patience
          self.mode = self.action_param.early_stopping_mode
  
          def switch_sampler(for_training):
              with tf.name_scope(
                  "train" if for_training else "validation"
              ):
                  sampler = self.get_sampler()[0][
                      0 if for_training else -1
                  ]
                  return sampler.pop_batch_op()
  
          if self.action_param.validation_every_n > 0:
              data_dict = tf.cond(
                  tf.logical_not(self.is_validation),
                  lambda: switch_sampler(for_training=True),
                  lambda: switch_sampler(for_training=False),
              )
          else:
              data_dict = switch_sampler(for_training=True)
  
          images = tf.cast(data_dict["image"], tf.float32)
          noise_shape = [
              self.net_param.batch_size,
              self.gan_param.noise_size,
          ]
          noise = tf.random_normal(
              shape=noise_shape, mean=0.0, stddev=1.0, dtype=tf.float32
          )
          conditioning = data_dict["conditioning"]
          net_output = self.net(
              noise, images, conditioning, self.is_training
          )
  
          loss_func = LossFunction(
              loss_type=self.action_param.loss_type
          )
          real_logits = net_output[1]
          fake_logits = net_output[2]
          lossG, lossD = loss_func(real_logits, fake_logits)
          if self.net_param.decay > 0:
              reg_losses = tf.get_collection(
                  tf.GraphKeys.REGULARIZATION_LOSSES
              )
              if reg_losses:
                  reg_loss = tf.reduce_mean(
                      [tf.reduce_mean(l_reg) for l_reg in reg_losses]
                  )
                  lossD = lossD + reg_loss
                  lossG = lossG + reg_loss
  
          self.total_loss = lossD + lossG
  
          outputs_collector.add_to_collection(
              var=self.total_loss,
              name="total_loss",
              average_over_devices=True,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=self.total_loss,
              name="total_loss",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
  
          # variables to display in STDOUT
          outputs_collector.add_to_collection(
              var=lossD,
              name="lossD",
              average_over_devices=True,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=lossG,
              name="lossG",
              average_over_devices=False,
              collection=CONSOLE,
          )
          # variables to display in tensorboard
          outputs_collector.add_to_collection(
              var=lossD,
              name="lossD",
              average_over_devices=True,
              collection=TF_SUMMARIES,
          )
          outputs_collector.add_to_collection(
              var=lossG,
              name="lossG",
              average_over_devices=False,
              collection=TF_SUMMARIES,
          )
  
          with tf.name_scope("Optimiser"):
              optimiser_class = OptimiserFactory.create(
                  name=self.action_param.optimiser
              )
              self.optimiser = optimiser_class.get_instance(
                  learning_rate=self.action_param.lr
              )
  
          with tf.name_scope("ComputeGradients"):
              # gradients of generator
              generator_variables = tf.get_collection(
                  tf.GraphKeys.TRAINABLE_VARIABLES, scope="generator"
              )
              generator_grads = self.optimiser.compute_gradients(
                  lossG,
                  var_list=generator_variables,
                  colocate_gradients_with_ops=True,
              )
  
              # gradients of discriminator
              discriminator_variables = tf.get_collection(
                  tf.GraphKeys.TRAINABLE_VARIABLES,
                  scope="discriminator",
              )
              discriminator_grads = self.optimiser.compute_gradients(
                  lossD,
                  var_list=discriminator_variables,
                  colocate_gradients_with_ops=True,
              )
              grads = [generator_grads, discriminator_grads]
  
              # add the grads back to application_driver's training_grads
              gradients_collector.add_to_collection(grads)
      else:
          data_dict = self.get_sampler()[0][0].pop_batch_op()
          conditioning_dict = self.get_sampler()[1][0].pop_batch_op()
          conditioning = conditioning_dict["conditioning"]
          image_size = conditioning.shape.as_list()[:-1]
          dummy_image = tf.zeros(image_size + [1])
          net_output = self.net(
              data_dict["vector"],
              dummy_image,
              conditioning,
              self.is_training,
          )
          outputs_collector.add_to_collection(
              var=net_output[0],
              name="image",
              average_over_devices=False,
              collection=NETWORK_OUTPUT,
          )
          outputs_collector.add_to_collection(
              var=conditioning_dict["conditioning_location"],
              name="location",
              average_over_devices=False,
              collection=NETWORK_OUTPUT,
          )
  
          self.output_decoder = WindowAsImageAggregator(
              image_reader=self.readers[0],
              output_path=self.action_param.save_seg_dir,
          )
  
imports: from __future__ import absolute_import, print_function
  import tensorflow
  from niftynet.application.base_application import BaseApplication
  from niftynet.engine.application_factory import ApplicationNetFactory
  from niftynet.engine.application_factory import OptimiserFactory
  from niftynet.engine.application_variables import CONSOLE, NETWORK_OUTPUT, TF_SUMMARIES
  from niftynet.engine.sampler_random_vector_v2 import RandomVectorSampler
  from niftynet.engine.sampler_resize_v2 import ResizeSampler
  from niftynet.engine.windows_aggregator_identity import WindowAsImageAggregator
  from niftynet.io.image_reader import ImageReader
  from niftynet.layer.binary_masking import BinaryMaskingLayer
  from niftynet.layer.histogram_normalisation import HistogramNormalisationLayer
  from niftynet.layer.loss_gan import LossFunction
  from niftynet.layer.mean_variance_normalisation import MeanVarNormalisationLayer
  from niftynet.layer.rand_flip import RandomFlipLayer
  from niftynet.layer.rand_rotation import RandomRotationLayer
  from niftynet.layer.rand_spatial_scaling import RandomSpatialScalingLayer
label_desc: Use image node_id 5 for calling the GANApplication obj's underlying member method code with example usage: obj.connect_data_and_network(outputs_collector, gradients_collector) and returns: sampler