(
    img1, img2, filter_size, filter_width, max_val=255.0)
return ssim, cs
Computes SSIM index between img1 and img2 per color channel.

  This function matches the standard SSIM implementation found at:
  https://ece.uwaterloo.ca/~z70wang/research/ssim/ssim_index.m

  Details:
    - To reproduce a 11x11 Gaussian filter of width 1.5 is used.
    - k1 = 0.01, k2 = 0.03 as in the original paper.

  Args:
    img1: First RGB image batch.
    img2: Second RGB image batch.
    filter_size: An integer, the filter size of the Gaussian kernel used.
    filter_width: A float, the filter width of the Gaussian kernel used.
    max_val: the dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).

  Returns:
    A pair of tensors containing batch-wise and channel-wise SSIM and
    contrast-structure measure. The shape is [..., channels].
Computes SSIM index between img1 and img2 per color channel.
Computes SSIM index between img1 and img2 per color channel .
def _ssim_index_per_channel(
    img1, img2, filter_size, filter_width, max_val=255.0):
  """Computes SSIM index between img1 and img2 per color channel.

  This function matches the standard SSIM implementation found at:
  https://ece.uwaterloo.ca/~z70wang/research/ssim/ssim_index.m

  Details:
    - To reproduce a 11x11 Gaussian filter of width 1.5 is used.
    - k1 = 0.01, k2 = 0.03 as in the original paper.

  Args:
    img1: First RGB image batch.
    img2: Second RGB image batch.
    filter_size: An integer, the filter size of the Gaussian kernel used.
    filter_width: A float, the filter width of the Gaussian kernel used.
    max_val: the dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).

  Returns:
    A pair of tensors containing batch-wise and channel-wise SSIM and
    contrast-structure measure. The shape is [..., channels].
  """
  filter_size = tf.constant(filter_size, dtype=tf.int32)
  filter_sigma = tf.constant(filter_width, dtype=img1.dtype)

  shape1, shape2 = tf.shape_n([img1, img2])

  filter_size = tf.reduce_min(
      tf.concat([tf.expand_dims(filter_size, axis=0),
                 shape1[-3:-1],
                 shape2[-3:-1]],
                axis=0))

  kernel = f_special_gauss(filter_size, filter_sigma)
  kernel = tf.tile(kernel, multiples=[1, 1, shape1[-1], 1])

  # The correct compensation factor is `1.0 - tf.reduce_sum(tf.square(kernel))`,
  # but to match MATLAB implementation of MS-SSIM, we use 1.0 instead.
  compensation = 1.0


  def reducer(x):  # pylint: disable=invalid-name
    shape = tf.shape(x)
    x = tf.reshape(x, shape=tf.concat([[-1], shape[-3:]], 0))
    y = tf.nn.depthwise_conv2d(x, kernel, strides=[1] * 4, padding='VALID')
    return tf.reshape(y, tf.concat([shape[:-3], tf.shape(y)[1:]], 0))

  luminance, cs = _ssim_helper(img1, img2, reducer, max_val, compensation)

  # Average over the second and the third from the last: height, width.
  axes = tf.constant([-3, -2], dtype=tf.int32)
  ssim = tf.reduce_mean(luminance * cs, axes)
  cs = tf.reduce_mean(cs, axes)
  return ssim, cs
def _ssim_index_per_channel ( img1 , img2 , filter_size , filter_width , max_val = 255.0 ) : filter_size = tf . constant ( filter_size , dtype = tf . int32 ) filter_sigma = tf . constant ( filter_width , dtype = img1 . dtype ) shape1 , shape2 = tf . shape_n ( [ img1 , img2 ] ) filter_size = tf . reduce_min ( tf . concat ( [ tf . expand_dims ( filter_size , axis = 0 ) , shape1 [ - 3 : - 1 ] , shape2 [ - 3 : - 1 ] ] , axis = 0 ) ) kernel = f_special_gauss ( filter_size , filter_sigma ) kernel = tf . tile ( kernel , multiples = [ 1 , 1 , shape1 [ - 1 ] , 1 ] ) # The correct compensation factor is `1.0 - tf.reduce_sum(tf.square(kernel))`, # but to match MATLAB implementation of MS-SSIM, we use 1.0 instead. compensation = 1.0 def reducer ( x ) : # pylint: disable=invalid-name shape = tf . shape ( x ) x = tf . reshape ( x , shape = tf . concat ( [ [ - 1 ] , shape [ - 3 : ] ] , 0 ) ) y = tf . nn . depthwise_conv2d ( x , kernel , strides = [ 1 ] * 4 , padding = 'VALID' ) return tf . reshape ( y , tf . concat ( [ shape [ : - 3 ] , tf . shape ( y ) [ 1 : ] ] , 0 ) ) luminance , cs = _ssim_helper ( img1 , img2 , reducer , max_val , compensation ) # Average over the second and the third from the last: height, width. axes = tf . constant ( [ - 3 , - 2 ] , dtype = tf . int32 ) ssim = tf . reduce_mean ( luminance * cs , axes ) cs = tf . reduce_mean ( cs , axes ) return ssim , cs