code: def connect_data_and_network(
      self, outputs_collector=None, gradients_collector=None
  ):
      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.is_training:
          self.patience = self.action_param.patience
          if self.action_param.validation_every_n > 0:
              data_dict = tf.cond(
                  tf.logical_not(self.is_validation),
                  lambda: switch_sampler(True),
                  lambda: switch_sampler(False),
              )
          else:
              data_dict = switch_sampler(for_training=True)
  
          image = tf.cast(data_dict["image"], tf.float32)
          net_output = self.net(image, is_training=self.is_training)
  
          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
              )
  
          loss_func = LossFunction(
              loss_type=self.action_param.loss_type
          )
          data_loss = loss_func(net_output)
          loss = data_loss
          if self.net_param.decay > 0.0:
              reg_losses = tf.get_collection(
                  tf.GraphKeys.REGULARIZATION_LOSSES
              )
              if reg_losses:
                  reg_loss = tf.reduce_mean(
                      [
                          tf.reduce_mean(reg_loss)
                          for reg_loss in reg_losses
                      ]
                  )
                  loss = loss + reg_loss
  
          self.total_loss = loss
          grads = self.optimiser.compute_gradients(
              loss, colocate_gradients_with_ops=True
          )
          # collecting gradients variables
          gradients_collector.add_to_collection([grads])
  
          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,
          )
  
          outputs_collector.add_to_collection(
              var=data_loss,
              name="variational_lower_bound",
              average_over_devices=True,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=data_loss,
              name="variational_lower_bound",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
  
          outputs_collector.add_to_collection(
              var=net_output[4],
              name="Originals",
              average_over_devices=False,
              summary_type="image3_coronal",
              collection=TF_SUMMARIES,
          )
          outputs_collector.add_to_collection(
              var=net_output[2],
              name="Means",
              average_over_devices=False,
              summary_type="image3_coronal",
              collection=TF_SUMMARIES,
          )
          outputs_collector.add_to_collection(
              var=net_output[5],
              name="Variances",
              average_over_devices=False,
              summary_type="image3_coronal",
              collection=TF_SUMMARIES,
          )
      else:
          if self._infer_type in ("encode", "encode-decode"):
              data_dict = self.get_sampler()[0][0].pop_batch_op()
              image = tf.cast(data_dict["image"], dtype=tf.float32)
              net_output = self.net(image, is_training=False)
  
              outputs_collector.add_to_collection(
                  var=data_dict["image_location"],
                  name="location",
                  average_over_devices=True,
                  collection=NETWORK_OUTPUT,
              )
  
              if self._infer_type == "encode-decode":
                  outputs_collector.add_to_collection(
                      var=net_output[2],
                      name="generated_image",
                      average_over_devices=True,
                      collection=NETWORK_OUTPUT,
                  )
              if self._infer_type == "encode":
                  outputs_collector.add_to_collection(
                      var=net_output[7],
                      name="embedded",
                      average_over_devices=True,
                      collection=NETWORK_OUTPUT,
                  )
  
              self.output_decoder = WindowAsImageAggregator(
                  image_reader=self.readers[0],
                  output_path=self.action_param.save_seg_dir,
              )
              return
          elif self._infer_type == "sample":
              image_size = (
                  (self.net_param.batch_size,)
                  + self.action_param.spatial_window_size
                  + (1,)
              )
              dummy_image = tf.zeros(image_size)
              net_output = self.net(dummy_image, is_training=False)
              noise_shape = net_output[-1].shape.as_list()
              stddev = self.autoencoder_param.noise_stddev
              noise = tf.random_normal(
                  shape=noise_shape,
                  mean=0.0,
                  stddev=stddev,
                  dtype=tf.float32,
              )
              partially_decoded_sample = self.net.shared_decoder(
                  noise, is_training=False
              )
              decoder_output = self.net.decoder_means(
                  partially_decoded_sample, is_training=False
              )
  
              outputs_collector.add_to_collection(
                  var=decoder_output,
                  name="generated_image",
                  average_over_devices=True,
                  collection=NETWORK_OUTPUT,
              )
              self.output_decoder = WindowAsImageAggregator(
                  image_reader=None,
                  output_path=self.action_param.save_seg_dir,
              )
              return
          elif self._infer_type == "linear_interpolation":
              # construct the entire network
              image_size = (
                  (self.net_param.batch_size,)
                  + self.action_param.spatial_window_size
                  + (1,)
              )
              dummy_image = tf.zeros(image_size)
              net_output = self.net(dummy_image, is_training=False)
              data_dict = self.get_sampler()[0][0].pop_batch_op()
              real_code = data_dict["feature"]
              real_code = tf.reshape(
                  real_code, net_output[-1].get_shape()
              )
              partially_decoded_sample = self.net.shared_decoder(
                  real_code, is_training=False
              )
              decoder_output = self.net.decoder_means(
                  partially_decoded_sample, is_training=False
              )
  
              outputs_collector.add_to_collection(
                  var=decoder_output,
                  name="generated_image",
                  average_over_devices=True,
                  collection=NETWORK_OUTPUT,
              )
              outputs_collector.add_to_collection(
                  var=data_dict["feature_location"],
                  name="location",
                  average_over_devices=True,
                  collection=NETWORK_OUTPUT,
              )
              self.output_decoder = WindowAsImageAggregator(
                  image_reader=self.readers[0],
                  output_path=self.action_param.save_seg_dir,
              )
          else:
              raise NotImplementedError
  
imports: 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
  from niftynet.engine.application_variables import NETWORK_OUTPUT
  from niftynet.engine.application_variables import TF_SUMMARIES
  from niftynet.engine.sampler_linear_interpolate_v2 import LinearInterpolateSampler
  from niftynet.contrib.csv_reader.sampler_linear_interpolate_v2_csv import LinearInterpolateSamplerCSV
  from niftynet.engine.sampler_resize_v2 import ResizeSampler
  from niftynet.contrib.csv_reader.sampler_resize_v2_csv import ResizeSamplerCSV
  from niftynet.contrib.csv_reader.csv_reader import CSVReader
  from niftynet.engine.windows_aggregator_identity import WindowAsImageAggregator
  from niftynet.io.image_reader import ImageReader
  from niftynet.layer.loss_autoencoder import LossFunction
  from niftynet.utilities.util_common import look_up_operations
label_desc: Use image node_id 5 for calling the AutoencoderApplication obj's underlying member method code with example usage: obj.connect_data_and_network(outputs_collector, gradients_collector) and returns: sampler