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:
          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)
  
          image = tf.cast(data_dict["image"], tf.float32)
          net_args = {
              "is_training": self.is_training,
              "keep_prob": self.net_param.keep_prob,
          }
          net_out = self.net(image, **net_args)
  
          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(
              n_class=self.segmentation_param.num_classes,
              loss_type=self.action_param.loss_type,
              softmax=self.segmentation_param.softmax,
          )
          data_loss = loss_func(
              prediction=net_out,
              ground_truth=data_dict.get("label", None),
              weight_map=data_dict.get("weight", None),
          )
          reg_losses = tf.get_collection(
              tf.GraphKeys.REGULARIZATION_LOSSES
          )
          if self.net_param.decay > 0.0 and reg_losses:
              reg_loss = tf.reduce_mean(
                  [tf.reduce_mean(reg_loss) for reg_loss in reg_losses]
              )
              loss = data_loss + reg_loss
          else:
              loss = data_loss
          grads = self.optimiser.compute_gradients(
              loss, colocate_gradients_with_ops=True
          )
          # collecting gradients variables
          gradients_collector.add_to_collection([grads])
          # collecting output variables
          outputs_collector.add_to_collection(
              var=data_loss,
              name="loss",
              average_over_devices=False,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=data_loss,
              name="loss",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
  
          # outputs_collector.add_to_collection(
          #    var=image*180.0, name='image',
          #    average_over_devices=False, summary_type='image3_sagittal',
          #    collection=TF_SUMMARIES)
  
          # outputs_collector.add_to_collection(
          #    var=image, name='image',
          #    average_over_devices=False,
          #    collection=NETWORK_OUTPUT)
  
          # outputs_collector.add_to_collection(
          #    var=tf.reduce_mean(image), name='mean_image',
          #    average_over_devices=False, summary_type='scalar',
          #    collection=CONSOLE)
      elif self.is_inference:
          # converting logits into final output for
          # classification probabilities or argmax classification labels
          data_dict = switch_sampler(for_training=False)
          image = tf.cast(data_dict["image"], tf.float32)
          net_args = {
              "is_training": self.is_training,
              "keep_prob": self.net_param.keep_prob,
          }
          net_out = self.net(image, **net_args)
  
          output_prob = self.segmentation_param.output_prob
          num_classes = self.segmentation_param.num_classes
          if output_prob and num_classes > 1:
              post_process_layer = PostProcessingLayer(
                  "SOFTMAX", num_classes=num_classes
              )
          elif not output_prob and num_classes > 1:
              post_process_layer = PostProcessingLayer(
                  "ARGMAX", num_classes=num_classes
              )
          else:
              post_process_layer = PostProcessingLayer(
                  "IDENTITY", num_classes=num_classes
              )
          net_out = post_process_layer(net_out)
  
          outputs_collector.add_to_collection(
              var=net_out,
              name="window",
              average_over_devices=False,
              collection=NETWORK_OUTPUT,
          )
          outputs_collector.add_to_collection(
              var=data_dict["image_location"],
              name="location",
              average_over_devices=False,
              collection=NETWORK_OUTPUT,
          )
          self.initialise_aggregator()
  
imports: import tensorflow
  from niftynet.application.base_application import BaseApplication
  from niftynet.engine.application_factory import ApplicationNetFactory, InitializerFactory, OptimiserFactory
  from niftynet.engine.application_variables import CONSOLE, NETWORK_OUTPUT, TF_SUMMARIES
  from niftynet.engine.sampler_grid_v2 import GridSampler
  from niftynet.engine.sampler_resize_v2 import ResizeSampler
  from niftynet.engine.sampler_uniform_v2 import UniformSampler
  from niftynet.engine.sampler_weighted_v2 import WeightedSampler
  from niftynet.engine.sampler_balanced_v2 import BalancedSampler
  from niftynet.engine.windows_aggregator_grid import GridSamplesAggregator
  from niftynet.engine.windows_aggregator_resize import ResizeSamplesAggregator
  from niftynet.io.image_reader import ImageReader
  from niftynet.layer.binary_masking import BinaryMaskingLayer
  from niftynet.layer.discrete_label_normalisation import DiscreteLabelNormalisationLayer
  from niftynet.layer.histogram_normalisation import HistogramNormalisationLayer
  from niftynet.layer.loss_segmentation import LossFunction
  from niftynet.layer.mean_variance_normalisation import MeanVarNormalisationLayer
  from niftynet.layer.pad import PadLayer
  from niftynet.layer.post_processing import PostProcessingLayer
  from niftynet.layer.rand_flip import RandomFlipLayer
  from niftynet.layer.rand_rotation import RandomRotationLayer
  from niftynet.layer.rand_spatial_scaling import RandomSpatialScalingLayer
  from niftynet.evaluation.segmentation_evaluator import SegmentationEvaluator
  from niftynet.layer.rand_elastic_deform import RandomElasticDeformationLayer
  from niftynet.contrib.csv_reader.csv_reader import CSVReader
  from niftynet.contrib.csv_reader.sampler_csvpatch import CSVPatchSampler
label_desc: Use image node_id 14 for calling the SegmentationApplicationPatchSampler obj's underlying member method code with example usage: obj.connect_data_and_network(outputs_collector, gradients_collector) and returns: sampler