code: def patches2im(
      patches,
      patch_size,
      strides=[1, 1],
      padding="SAME",
      img_size=None,
      consolidation="SUM",
      data_format="NCHW",
  ):
      """
      Reconstructed image fromt extracted patches.
      See: https://stackoverflow.com/a/56891806
  
      :param patches: a 4-D Tensor in 'NCHW' or "NHWC" format where the depth
          dimension ('C') represents a patch
      :param img_size: a tuple (height, width) representing output image size
          (optional). If `None`, image size will be inferred from inputs.
      """
      with tf.name_scope("patches2im"):
          # To NHWC.
          if data_format == "NCHW":
              patches = patches.transpose([0, 2, 3, 1])
  
          _h = patches.shape[1]
          _w = patches.shape[2]
  
          bs = patches.shape[0]  # batch size
          np = _h * _w  # number of patches
          ps_h = patch_size[0]  # patch height
          ps_w = patch_size[1]  # patch width
          col_ch = patches.shape[3] // (
              ps_h * ps_w
          )  # Colour channel count
          assert (
              patches.shape[3] % (ps_h * ps_w) == 0
          ), "Unexpected patch size"
  
          patches = patches.reshape((bs, -1, ps_h, ps_w, col_ch))
  
          # Recalculate output shape of "extract_image_patches" including padded pixels
          wout = (_w - 1) * strides[0] + ps_w
          # Recalculate output shape of "extract_image_patches" including padded pixels
          hout = (_h - 1) * strides[1] + ps_h
  
          x, y = tf.meshgrid(tf.range(ps_w), tf.range(ps_h))
          x = tf.reshape(x, (1, 1, ps_h, ps_w, 1, 1))
          y = tf.reshape(y, (1, 1, ps_h, ps_w, 1, 1))
          xstart, ystart = tf.meshgrid(
              tf.range(0, (wout - ps_w) + 1, strides[0]),
              tf.range(0, (hout - ps_h) + 1, strides[1]),
          )
  
          bb = tf.zeros(
              (1, np, ps_h, ps_w, col_ch, 1), dtype=tf.int32
          ) + tf.reshape(
              tf.range(bs), (-1, 1, 1, 1, 1, 1)
          )  # batch indices
          yy = (
              tf.zeros((bs, 1, 1, 1, col_ch, 1), dtype=tf.int32)
              + y
              + tf.reshape(ystart, (1, -1, 1, 1, 1, 1))
          )  # y indices
          xx = (
              tf.zeros((bs, 1, 1, 1, col_ch, 1), dtype=tf.int32)
              + x
              + tf.reshape(xstart, (1, -1, 1, 1, 1, 1))
          )  # x indices
          cc = tf.zeros(
              (bs, np, ps_h, ps_w, 1, 1), dtype=tf.int32
          ) + tf.reshape(
              tf.range(col_ch), (1, 1, 1, 1, -1, 1)
          )  # color indices
          dd = tf.zeros(
              (bs, 1, ps_h, ps_w, col_ch, 1), dtype=tf.int32
          ) + tf.reshape(
              tf.range(np), (1, -1, 1, 1, 1, 1)
          )  # shift indices
  
          idx = tf.concat([bb, yy, xx, cc, dd], -1)
  
          stratified_img = patches.scatter_nd(
              idx, (bs, hout, wout, col_ch, np)
          )
          stratified_img = stratified_img.transpose((0, 4, 1, 2, 3))
  
          stratified_img_count = patches.factory.ones_like(
              patches
          ).scatter_nd(idx, (bs, hout, wout, col_ch, np))
          stratified_img_count = stratified_img_count.transpose(
              (0, 4, 1, 2, 3)
          )
  
          with tf.name_scope("consolidate"):
              sum_stratified_img = stratified_img.reduce_sum(axis=1)
              if consolidation == "SUM":
                  reconstructed_img = sum_stratified_img
              elif consolidation == "AVG":
                  stratified_img_count = (
                      stratified_img_count.reduce_sum(axis=1)
                  )
                  reconstructed_img = (
                      sum_stratified_img / stratified_img_count
                  )
              else:
                  raise NotImplementedError(
                      "Unknown consolidation method: {}".format(
                          consolidation
                      )
                  )
  
          if img_size is not None:
              img_h, img_w = img_size
              if img_h > hout:
                  # This happens when padding is 'VALID' and the image has been cropped,
                  # hence we will just pad 0 at the end
                  reconstructed_img = reconstructed_img.factory.pad(
                      reconstructed_img,
                      [[0, 0], [0, img_h - hout], [0, 0], [0, 0]],
                  )
              elif img_h < hout:
                  pad_top = (hout - img_h) // 2
                  pad_bottom = hout - img_h - pad_top
                  reconstructed_img = reconstructed_img[
                      :, pad_top : (hout - pad_bottom), :, :
                  ]
  
              if img_w > wout:
                  reconstructed_img = reconstructed_img.factory.pad(
                      reconstructed_img,
                      [[0, 0], [0, 0], [0, img_w - hout], [0, 0]],
                  )
              elif img_w < wout:
                  pad_left = (wout - img_w) // 2
                  pad_right = wout - img_w - pad_left
                  reconstructed_img = reconstructed_img[
                      :, :, pad_left : (wout - pad_right), :
                  ]
  
          # To NCHW.
          if data_format == "NCHW":
              reconstructed_img = reconstructed_img.transpose(
                  [0, 3, 1, 2]
              )
  
          return reconstructed_img
  
file_path: tf-encrypted/tf_encrypted/tensor/shared.py
imports: import math
  from typing import Optional
  import tensorflow
label_desc: Use image node_id 4 for calling a global function with example usage: patches2im(patches, patch_size, strides, padding, img_size, consolidation, data_format) and returns: reconstructed_img