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(True),
                  lambda: switch_sampler(False),
              )
          else:
              data_dict = switch_sampler(for_training=True)
  
          image = tf.cast(data_dict["image"], tf.float32)
          pct1_out = self.net(image, self.is_training)
          res2_out = self.net2(
              tf.concat([image, pct1_out], 4), self.is_training
          )
          pct2_out = tf.add(pct1_out, res2_out)
          res3_out = self.net2(
              tf.concat([image, pct2_out], 4), self.is_training
          )
          pct3_out = tf.add(pct2_out, res3_out)
          # res4_out = self.net2(tf.concat([image, pct3_out],4), self.is_training)
          # pct4_out = tf.add(pct3_out,res4_out)
          # net_out = 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
          )
  
          crop_layer = CropLayer(
              border=self.regression_param.loss_border, name="crop-88"
          )
  
          data_loss1 = loss_func(
              prediction=crop_layer(pct1_out),
              ground_truth=crop_layer(data_dict.get("output", None)),
              weight_map=None
              if data_dict.get("weight", None) is None
              else crop_layer(data_dict.get("weight", None)),
          )
          data_loss2 = loss_func(
              prediction=crop_layer(pct2_out),
              ground_truth=crop_layer(data_dict.get("output", None)),
              weight_map=None
              if data_dict.get("weight", None) is None
              else crop_layer(data_dict.get("weight", None)),
          )
          data_loss3 = loss_func(
              prediction=crop_layer(pct3_out),
              ground_truth=crop_layer(data_dict.get("output", None)),
              weight_map=None
              if data_dict.get("weight", None) is None
              else crop_layer(data_dict.get("weight", None)),
          )
  
          # prediction = crop_layer(net_out)
          # ground_truth = crop_layer(data_dict.get('output', None))
          # weight_map = None if data_dict.get('weight', None) is None \
          # else crop_layer(data_dict.get('weight', None))
          # data_loss = loss_func(prediction=prediction,
          # ground_truth=ground_truth,
          # weight_map=weight_map)
  
          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 = reg_loss + data_loss1 + data_loss2 + data_loss3
          else:
              loss = data_loss1 + data_loss2 + data_loss3
          grads = self.optimiser.compute_gradients(loss)
          # collecting gradients variables
          gradients_collector.add_to_collection([grads])
          # collecting output variables
          outputs_collector.add_to_collection(
              var=loss,
              name="Loss",
              average_over_devices=False,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=data_loss1,
              name="data_loss1",
              average_over_devices=True,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=data_loss2,
              name="data_loss2",
              average_over_devices=True,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=data_loss3,
              name="data_loss3",
              average_over_devices=True,
              collection=CONSOLE,
          )
          outputs_collector.add_to_collection(
              var=data_loss1,
              name="data_loss1",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
          outputs_collector.add_to_collection(
              var=data_loss2,
              name="data_loss2",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
          outputs_collector.add_to_collection(
              var=data_loss3,
              name="data_loss3",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
          outputs_collector.add_to_collection(
              var=loss,
              name="LossSum",
              average_over_devices=True,
              summary_type="scalar",
              collection=TF_SUMMARIES,
          )
      #            outputs_collector.add_to_collection(
      #                var=pct3_out, name="pct3_out",
      #                average_over_devices=True, summary_type="image3_axial",
      #                collection=TF_SUMMARIES)
  
      else:
          data_dict = switch_sampler(for_training=False)
          image = tf.cast(data_dict["image"], tf.float32)
          # net_out = self.net(image, is_training=self.is_training)
          pct1_out = self.net(image, self.is_training)
          res2_out = self.net2(
              tf.concat([image, pct1_out], 4), self.is_training
          )
          pct2_out = tf.add(pct1_out, res2_out)
          res3_out = self.net2(
              tf.concat([image, pct2_out], 4), self.is_training
          )
          pct3_out = tf.add(pct2_out, res3_out)
          res4_out = self.net2(
              tf.concat([image, pct3_out], 4), self.is_training
          )
          pct4_out = tf.add(pct3_out, res4_out)
          crop_layer = CropLayer(border=0, name="crop-88")
          post_process_layer = PostProcessingLayer("IDENTITY")
          net_out = post_process_layer(crop_layer(pct4_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,
          )
          init_aggregator = self.SUPPORTED_SAMPLING[
              self.net_param.window_sampling
          ][2]
          init_aggregator()
  
imports: import tensorflow
  import copy
  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_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.windows_aggregator_grid import GridSamplesAggregator
  from niftynet.engine.windows_aggregator_resize import ResizeSamplesAggregator
  from niftynet.io.image_reader import ImageReader
  from niftynet.layer.crop import CropLayer
  from niftynet.layer.histogram_normalisation import HistogramNormalisationLayer
  from niftynet.layer.loss_regression 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
label_desc: Use image node_id 12 for calling the RegressionRecApplication obj's underlying member method code with example usage: obj.connect_data_and_network(outputs_collector, gradients_collector) and returns: sampler