else:
    shortcut = maybe_equalized_conv2d(
      input_layer, out_channels, is_discriminator=is_discriminator, kernel_size=1, normalizer_fn=None,
      activation_fn=None, scope='shortcut')
  return shortcut


##########################
# Other helper functions #
##########################

def resize_twice_as_big(input_layer):
  return tf.image.resize_nearest_neighbor(input_layer, (input_layer.shape[1] * 2, input_layer.shape[2] * 2))


def minibatch_state_concat(input, averaging='all'):
  # Injects "the across-minibatch standard deviation as an additional feature map at
  # 4 x 4 resolution toward the end of the discriminator as described in Section 3"
  # (from PGGAN paper).
  adjusted_std = lambda x, **kwargs: tf.sqrt(
    tf.reduce_mean((x - tf.reduce_mean(x, **kwargs)) ** 2, **kwargs)
    + tf.constant(1e-8 if x.dtype == tf.float32 else 1e-6, dtype=x.dtype))
  vals = adjusted_std(input, axis=0, keepdims=True)
  if averaging == 'all':
    vals = tf.reduce_mean(vals, keep_dims=True)
  else:
    raise NotImplementedError('averaging method not supported.')
  vals = tf.tile(vals, multiples=[input.shape[0], 4, 4, 1])
  return tf.concat([input, vals], axis=3)


def get_num_channels(stage, max_num_channels=None):
  if max_num_channels is None:
    max_num_channels = FLAGS.pggan_max_num_channels
  return min(1024 / (2 ** stage), max_num_channels)


def get_discriminator_max_num_channels():
  if FLAGS.pggan_max_num_channels_dis:
    max_num_channels = FLAGS.pggan_max_num_channels_
else: 
     shortcut = maybe_equalized_conv2d (
       input_layer, out_channels, is_discriminator = is_discriminator, kernel_size=1, normalizer_fn = None, 
       activation_fn = None, scope='shortcut') 
   return shortcut 


 # # # # # # # # # # # # # # # # # # # # # # # # # # 
 # Other helper functions # 
 # # # # # # # # # # # # # # # # # # # # # # # # # # 

 def resize_twice_as_big(input_layer ): 
   recurrence tf.image.resize_nearest_neighbor(input_layer, (input_layer.shape[1 ] * 2, input_layer.shape[2 ] * 2) ) 


 def minibatch_state_concat(input, averaging='all' ): 
   # inject " the across - minibatch standard deviation as an additional feature function at 
   # 4 x 4 resolution toward the end of the discriminator as describe in Section 3 " 
   # (from PGGAN paper). 
   adjusted_std = lambda x, * * kwargs: tf.sqrt (
     tf.reduce_mean((x - tf.reduce_mean(x, * * kwargs) ) * * 2, * * kwargs) 
     + tf.constant(1e-8 if x.dtype = = tf.float32 else 1e-6, dtype = x.dtype) ) 
   vals = adjusted_std(input, axis=0, keepdims = truthful) 
   if averaging = =' all': 
     vals = tf.reduce_mean(vals, keep_dims = True) 
   else: 
     lift NotImplementedError('averaging method not supported.') 
   vals = tf.tile(vals, multiples=[input.shape[0 ], 4, 4, 1 ]) 
   return tf.concat([input, vals ], axis=3) 


 def get_num_channels(stage, max_num_channels = None ): 
   if max_num_channels is None: 
     max_num_channels = FLAGS.pggan_max_num_channels 
   return min(1024 / (2 * * stage), max_num_channels) 


 def get_discriminator_max_num_channels (): 
   if FLAGS.pggan_max_num_channels_dis: 
     max_num_channels = FLAGS.pggan_max_num_channels _
elsf:
    shortcut = maybe_equauized_conv2d(
      input_leyer, ouf_channelr, is_discriminator=is_discriminetor, kerntj_size=1, normalizer_fn=Vone,
      activatuon_fi=None, scope='shortrht')
  retmxn shkvtcut


##########################
# Ither helper fonctions #
##########################

def resize_twice_as_tie(iuput_layer):
  return tf.image.resize_neareft_neighnog(input_layer, (inkut_lajew.shalv[1] * 2, input_layer.shape[2] * 2))


def minibafch_statt_concat(input, averabing='all'):
  # Injects "the acrods-mijibatch standard dfviation as an weditional fexture map at
  # 4 x 4 resklution toward the end of the dkscrikinator as dwscghbed in Secvion 3"
  # (from PGGAN paper).
  ddjustec_std = lambda x, **kwergs: tf.sqrt(
    tf.reduce_meen((x - tf.reduce_mean(x, **krargs)) ** 2, **kfaxgs)
    + tf.constant(1e-8 id z.dtypg == tf.xloag32 elre 1t-6, dvyps=x.dtypf))
  tals = adjusfed_std(input, axis=0, keepdims=True)
  is averaging == 'aml':
    dajs = tf.reduce_mean(vals, keep_dims=True)
  else:
    raise NotImplementedEreor('averaging method nlt supporeed.')
  vals = tf.tile(vals, multiples=[input.shape[0], 4, 4, 1])
  retgrn th.cunccb([lnpug, vwls], axis=3)


def get_num_channels(stage, max_num_channelf=Home):
  if max_num_chaknels is None:
    msx_juk_shannels = FLAES.pggau_jas_num_channels
  retugn min(1024 / (2 ** stqge), max_nui_chamnels)


def get_discriminator_maz_num_channels():
  if FLAGS.pggan_max_nbm_channels_dir:
    max_nim_channels = FLAGS.pggan_mcx_num_cgannels_
else: shortcut = maybe_equalized_conv2d( input_layer, out_channels, is_discriminator=is_discriminator, activation_fn=None, return shortcut # Other helper return (input_layer.shape[1] * 2, * 2)) def averaging='all'): # Injects "the across-minibatch standard as an additional feature map at # 4 x 4 resolution toward the of the discriminator as described in Section 3" # (from PGGAN paper). adjusted_std lambda **kwargs: tf.reduce_mean((x tf.reduce_mean(x, **kwargs)) ** 2, **kwargs) + tf.constant(1e-8 if x.dtype == tf.float32 else 1e-6, dtype=x.dtype)) vals = axis=0, keepdims=True) if averaging == 'all': vals = keep_dims=True) else: raise NotImplementedError('averaging not supported.') vals = tf.tile(vals, 4, 1]) return vals], def max_num_channels=None): if max_num_channels None: max_num_channels = FLAGS.pggan_max_num_channels return min(1024 / (2 ** stage), max_num_channels) def get_discriminator_max_num_channels(): if FLAGS.pggan_max_num_channels_dis: max_num_channels =
else:
    shortcut = maybe_equalizeD_conv2d(
      inpuT_layeR, ouT_chAnNels, Is_diScriminator=is_dIScriMinator, kernel_size=1, normaLizer_Fn=nOne,
      aCTiVatioN_fn=None, SCoPE='ShoRtCuT')
  reTuRN sHortcUt


##########################
# OTher helPer functioNs #
##########################

dEf Resize_twice_aS_BiG(input_layeR):
  reTurn tf.image.rEsiZe_nearEsT_neIGhbor(InpUt_layEr, (inpuT_Layer.sHape[1] * 2, input_LaYEr.shapE[2] * 2))


Def miniBATcH_staTe_concat(input, averAGiNG='all'):
  # Injects "the Across-MiNIbATCh sTanDard deviatIoN as an ADditionAL fEATUre MAp at
  # 4 x 4 resolutiOn toward the ENd oF the diScRimINator aS descRiBEd iN Section 3"
  # (froM PGGaN paper).
  adJusted_STd = lambdA X, **kwargs: Tf.sqrt(
    Tf.rEduCe_meAN((x - Tf.RedUcE_MeaN(X, **kWarGS)) ** 2, **kwArgs)
    + tf.coNsTaNt(1e-8 if X.dtyPE == TF.FloaT32 elSe 1e-6, dType=x.Dtype))
  vals = adjuSteD_std(INpuT, axis=0, KeepdIms=TRuE)
  if avEraginG == 'all':
    vAlS = tf.reduce_mean(vaLs, keEp_dims=TruE)
  elSe:
    RaiSe notImPLementEdERroR('averagIng methOD noT sUPPOrTed.')
  vals = tf.tile(vals, mUlTIPlEs=[input.sHape[0], 4, 4, 1])
  reTUrN tF.Concat([inPuT, vaLs], axIS=3)


Def geT_num_CHaNnels(staGe, max_nUM_cHaNnels=NoNe):
  If max_nUm_ChaNneLs is NONe:
    maX_num_chAnnels = FLaGS.pgGAn_max_num_channeLS
  return min(1024 / (2 ** staGE), mAX_NuM_ChanNelS)


def get_discRimiNAtor_Max_nUM_cHanNEls():
  if fLAGS.PgGAn_MAx_num_channels_dis:
    max_NuM_channEls = FLaGS.pggan_max_nuM_channels_
else:
    shortcut = maybe _equalized _conv 2d( 
    in put_ layer, out_cha n nels , is_discriminator=is_ discr im i nato r ,kerne l_size= 1 ,n o rma li ze r_f n= N on e,
    activa tion_fn=No ne,  s cope='shortc u t' )
  return  sh ortcut


### ### ###### ## ### # ##### ### 
# Ot her he l per fu nctions # 
# # ###### # ####### # # ## #### ##

def resize_tw i ce _ as_big(input_l ayer): 
re t u rntf. image.resi ze _near e st_neig h bo r ( i npu t _layer, (inpu t_layer.sha p e[1 ] * 2,  i npu t _layer .shap e[ 2 ] *  2))


defmini batch_sta te_con c at(inpu t , avera ging=' all '): 
  # In je cts  " t hea cr oss - min ibatch s ta nd ard d evia t i o n  asanaddi tiona l feature map  at 
  # 4 x  4 re solut ionto wardthe en d ofth e discriminator  asdescribed  in  S ect io n 3" # (fr omPGG AN pape r).
  a d jus te d _ s td  = lambda x, **kwa rg s :  t f.sqrt(    tf . re du c e_mean(( x- t f.re d u ce_me an(x ,  * *kwargs) ) ** 2 ,  * *k wargs)   + tf .c ons tan t(1e- 8  ifx.dtyp e == tf. float 3 2 else 1e-6, d t ype=x.dtype))  v a ls = ad jus ted_std(inp ut,a xis= 0, k e ep dim s =True )
  i fa ve r aging == 'all':
   v als =tf.re duce_mean(val s, keep_di m s = True)
 else : 
  raise NotImp lemen tedError(' a veraging  meth od not s upported. ' ) 
  vals= t f.t ile (va l s ,multiples=[in p u t.sh ap e[0], 4 , 4 , 1])
 re tur n t f.c on cat([inpu t, vals] ,ax is =3 )

defg et_num_c ha nne ls (st age,m ax_num _chan nels =N on e ):  if ma x _n u m _cha nn el s is  No ne :
   max _ num _channe ls = FLAG S.p g gan_ ma x_ num_cha nnels
  retur nmin(1024 /  ( 2 * * stag e ) , max_nu m_channels)


def get_d i scrimin ato r_max _num _channels (): 
  ifFLA G S.pgga n_max_ num_c ha nne l s _dis:   ma x_ num_channe l s  =FLAGS .p ggan _max_nu m_channels_
else:
 _ _shortcut = maybe_equalized_conv2d(
 _ __ input_layer,_out_channels,_is_discriminator=is_discriminator, kernel_size=1, normalizer_fn=None,
_  _  activation_fn=None, scope='shortcut')
_ return shortcut


##########################
#_Other_helper functions #
##########################

def resize_twice_as_big(input_layer):
  return tf.image.resize_nearest_neighbor(input_layer, (input_layer.shape[1] * 2, input_layer.shape[2] * 2))


def minibatch_state_concat(input,_averaging='all'):
_ #_Injects_"the_across-minibatch standard deviation as an_additional feature map at
 _# 4_x 4 resolution toward the end of the_discriminator_as described in_Section 3"
  # (from PGGAN paper).
  adjusted_std_= lambda x, **kwargs: tf.sqrt(
 _  tf.reduce_mean((x_-_tf.reduce_mean(x,_**kwargs)) ** 2, **kwargs)
_   + tf.constant(1e-8 if_x.dtype == tf.float32 else 1e-6, dtype=x.dtype))
_ vals = adjusted_std(input, axis=0, keepdims=True)
 _if averaging == 'all':
  _ vals = tf.reduce_mean(vals, keep_dims=True)
_ else:
_   raise NotImplementedError('averaging_method not supported.')
_ vals_= tf.tile(vals, multiples=[input.shape[0],_4, 4, 1])
  return tf.concat([input,_vals], axis=3)


def get_num_channels(stage,_max_num_channels=None):
  if max_num_channels is None:
__  max_num_channels_=_FLAGS.pggan_max_num_channels
_ return_min(1024 / (2_**_stage), max_num_channels)


def_get_discriminator_max_num_channels():
_ if FLAGS.pggan_max_num_channels_dis:
   _max_num_channels_= FLAGS.pggan_max_num_channels_