(x, name, use_sn)
Self-attention (non-local) block.

  This method is used to exactly reproduce SAGAN and ignores Gin settings on
  weight initialization and spectral normalization.


  Args:
    x: Input tensor of shape [batch, h, w, c].
    name: Name of the variable scope.
    use_sn: Apply spectral norm to the weights.

  Returns:
    A tensor of the same shape after self-attention was applied.
Self-attention (non-local) block.
Self - attention ( non - local ) block .
def non_local_block(x, name, use_sn):
  """Self-attention (non-local) block.

  This method is used to exactly reproduce SAGAN and ignores Gin settings on
  weight initialization and spectral normalization.


  Args:
    x: Input tensor of shape [batch, h, w, c].
    name: Name of the variable scope.
    use_sn: Apply spectral norm to the weights.

  Returns:
    A tensor of the same shape after self-attention was applied.
  """
  def _spatial_flatten(inputs):
    shape = inputs.shape
    return tf.reshape(inputs, (-1, shape[1] * shape[2], shape[3]))

  with tf.variable_scope(name):
    h, w, num_channels = x.get_shape().as_list()[1:]
    num_channels_attn = num_channels // 8
    num_channels_g = num_channels // 2

    # Theta path
    theta = conv1x1(x, num_channels_attn, name="conv2d_theta", use_sn=use_sn,
                    use_bias=False)
    theta = _spatial_flatten(theta)

    # Phi path
    phi = conv1x1(x, num_channels_attn, name="conv2d_phi", use_sn=use_sn,
                  use_bias=False)
    phi = tf.layers.max_pooling2d(inputs=phi, pool_size=[2, 2], strides=2)
    phi = _spatial_flatten(phi)

    attn = tf.matmul(theta, phi, transpose_b=True)
    attn = tf.nn.softmax(attn)

    # G path
    g = conv1x1(x, num_channels_g, name="conv2d_g", use_sn=use_sn,
                use_bias=False)
    g = tf.layers.max_pooling2d(inputs=g, pool_size=[2, 2], strides=2)
    g = _spatial_flatten(g)

    attn_g = tf.matmul(attn, g)
    attn_g = tf.reshape(attn_g, [-1, h, w, num_channels_g])
    sigma = tf.get_variable("sigma", [], initializer=tf.zeros_initializer())
    attn_g = conv1x1(attn_g, num_channels, name="conv2d_attn_g", use_sn=use_sn,
                     use_bias=False)
    return x + sigma * attn_g
def non_local_block ( x , name , use_sn ) : def _spatial_flatten ( inputs ) : shape = inputs . shape return tf . reshape ( inputs , ( - 1 , shape [ 1 ] * shape [ 2 ] , shape [ 3 ] ) ) with tf . variable_scope ( name ) : h , w , num_channels = x . get_shape ( ) . as_list ( ) [ 1 : ] num_channels_attn = num_channels // 8 num_channels_g = num_channels // 2 # Theta path theta = conv1x1 ( x , num_channels_attn , name = "conv2d_theta" , use_sn = use_sn , use_bias = False ) theta = _spatial_flatten ( theta ) # Phi path phi = conv1x1 ( x , num_channels_attn , name = "conv2d_phi" , use_sn = use_sn , use_bias = False ) phi = tf . layers . max_pooling2d ( inputs = phi , pool_size = [ 2 , 2 ] , strides = 2 ) phi = _spatial_flatten ( phi ) attn = tf . matmul ( theta , phi , transpose_b = True ) attn = tf . nn . softmax ( attn ) # G path g = conv1x1 ( x , num_channels_g , name = "conv2d_g" , use_sn = use_sn , use_bias = False ) g = tf . layers . max_pooling2d ( inputs = g , pool_size = [ 2 , 2 ] , strides = 2 ) g = _spatial_flatten ( g ) attn_g = tf . matmul ( attn , g ) attn_g = tf . reshape ( attn_g , [ - 1 , h , w , num_channels_g ] ) sigma = tf . get_variable ( "sigma" , [ ] , initializer = tf . zeros_initializer ( ) ) attn_g = conv1x1 ( attn_g , num_channels , name = "conv2d_attn_g" , use_sn = use_sn , use_bias = False ) return x + sigma * attn_g