          new_shape = shape[:3] + [shape[3] // (self.scale ** 2)]
          if isinstance(self.initializer, dict):
              self.initializer = initializers.deserialize(self.initializer)
          var_x = self.initializer(new_shape, dtype)
          var_x = tf.transpose(var_x, perm=[2, 0, 1, 3])
          var_x = tf.image.resize_nearest_neighbor(
              var_x,
              size=(shape[0] * self.scale, shape[1] * self.scale),
              align_corners=True)
          var_x = tf.space_to_depth(var_x, block_size=self.scale, data_format='NHWC')
          var_x = tf.transpose(var_x, perm=[1, 2, 0, 3])
          return var_x
  
      def get_config(self):
          config = {'scale': self.scale,
                    'initializer': self.initializer
 
</s> remove     new_shape = shape[:3] + [int(shape[3] / (scale ** 2))]
    var_x = initializer(new_shape, dtype)
    var_x = tf.transpose(var_x, perm=[2, 0, 1, 3])
    var_x = tf.image.resize_nearest_neighbor(var_x, size=(shape[0] * scale, shape[1] * scale))
    var_x = tf.space_to_depth(var_x, block_size=scale)
    var_x = tf.transpose(var_x, perm=[1, 2, 0, 3])
    return var_x
 </s> add logger = logging.getLogger(__name__)  # pylint: disable=invalid-name </s> remove logger = logging.getLogger(__name__)  # pylint: disable=invalid-name


def icnr_keras(shape, dtype=None):
    """
    Custom initializer for subpix upscaling
    From https://github.com/kostyaev/ICNR
    Note: upscale factor is fixed to 2, and the base initializer is fixed to random normal.
    """
    # TODO Roll this into ICNR_init when porting GAN 2.2
    shape = list(shape)
    scale = 2
    initializer = tf.keras.initializers.RandomNormal(0, 0.02)
 </s> add from lib.utils import get_backend </s> remove     def __call__(self, shape, dtype='float32'):  # tf needs partition_info=None
 </s> add     def __call__(self, shape, dtype="float32"):
        """ Call function for the ICNR initializer.

        Parameters
        ----------
        shape: tuple or list
            The required resized shape for the output tensor
        dtype: str
            The data type for the tensor

        Returns
        -------
        tensor
            The modified kernel weights
        """ </s> remove     Parameters:
        initializer: initializer used for sub kernels (orthogonal, glorot uniform, etc.)
        scale: scale factor of sub pixel convolution (upsampling from 8x8 to 16x16 is scale 2)
    Return:
        The modified kernel weights
    Example:
        x = conv2d(... weights_initializer=ICNR(initializer=he_uniform(), scale=2))
    '''
 </s> add     References
    ----------
    Andrew Aitken et al. Checkerboard artifact free sub-pixel convolution
    https://arxiv.org/pdf/1707.02937.pdf,  https://distill.pub/2016/deconv-checkerboard/
    """ </s> remove         # Convolutional Aware Initialization takes a long time.
        # Keras model loading loads a model, performs initialization and then
        # loads weights, which is an unnecessary waste of time.
        # init defaults to False so that this is bypassed when loading a saved model
        # passing zeros
 </s> add 
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
          new_shape = shape[:3] + [shape[3] // (self.scale ** 2)]
          if isinstance(self.initializer, dict):
              self.initializer = initializers.deserialize(self.initializer)
          var_x = self.initializer(new_shape, dtype)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
        var_x = K.permute_dimensions(var_x, [2, 0, 1, 3])
        var_x = self._resize_nearest_neighbour(var_x,
                                               (shape[0] * self.scale, shape[1] * self.scale))
        var_x = self._space_to_depth(var_x)
        var_x = K.permute_dimensions(var_x, [1, 2, 0, 3])
        logger.debug("Output: %s", var_x)
          return var_x
  
      def get_config(self):
          config = {'scale': self.scale,
                    'initializer': self.initializer
keep keep keep keep replace replace replace replace replace replace replace keep keep keep keep keep
 AMD: Fix ICNR Initialization for PlaidML backend 
lib/model/initializers.py