o
    ߥi4                     @   sp   d dl Zd dlm  mZ dddZdddZdd	d
Z				dddZ					dddZ	e
dkr6	 dS dS )    N    resblockc                 C   sr   t |* tj| |ddgd dd}t j|}tj||ddgd dd}||  W  d    S 1 s2w   Y  d S )N   conv1)activation_fnscopeconv2)tfvariable_scopeslimconvolution2dnn
leaky_relu)inputsout_channelnamex r   X/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/cartoon/network.pyr      s   $   c                 C   s  | j  }t| d|d g} tjdd|d gt dd}|}d }t|D ]}	 t|t| }tj	
|}t|| }tj	
|}q&t|}t|}tt|| t|}	t||g | |	 }
t|
|}
W d    |
S 1 s{w   Y  |
S )Nur   F)initializer	trainable)shapeas_listr	   reshapeget_variablerandom_normal_initializerrangematmul	transposer   l2_normalizestop_gradientcontrol_dependenciesassign)w	iterationw_shaper   u_hatv_hativ_u_sigmaw_normr   r   r   spectral_norm   s2   



r0   
conv_snormc                 C   s   t |; t jd|d |d |  d |gd}t jd|gt dd}t jj| t|d||dgd	d
| } | W  d    S 1 sCw   Y  d S )Nkernelr   r   r   )r   biasg        )r   SAME)inputfilterstridespadding)r	   r
   r   	get_shapeconstant_initializerr   conv2dr0   )r   channelk_sizestrider   r&   br   r   r   conv_spectral_norm3   s&   
$r@      	generatorFc                 C   s8  t j||d	 tj| |ddgd d}t j|}tj||ddgdd d}t j|}tj||d ddgd d}t j|}tj||d ddgdd d}t j|}tj||d ddgd d}t j|}t|D ]}t||d d|d	}qjtj||d ddgd d}t j|}t 	|d
 t 	|d }	}
t j
||	d |
d f}tj|| |d ddgd d}t j|}tj||ddgd d}t j|}t 	|d
 t 	|d }}t j
||d |d f}tj|| |ddgd d}t j|}tj|dddgd d}|W  d    S 1 sw   Y  d S )Nreuse   r   r      )r>   r   rA   zblock_{})r   r   r   )r	   r
   r   r   r   r   r   r   formatr   imageresize_bilinear)r   r<   
num_blocksr   rD   x0x1x2idxh1w1x3h2w2x4r   r   r   unet_generatorD   sH   &rV   Tdiscriminatorc              
   C   s   t j||d` tdD ]1}t| |d|  ddgdd|d} t j| } t| |d|  ddgd|d} t j| } q|du rMt| d	d	d	gd
d} nt j| d	dgd} tj	| d	d d} | W  d    S 1 sjw   Y  d S )NrC   r   rG   zconv{}_1)r>   r   zconv{}_2)r   Tr   conv_out)axisrF   )
r	   r
   r   r@   rH   r   r   reduce_meanr   fully_connected)r   scaler<   patchr   rD   rO   r   r   r   disc_sns   s&   $r^   __main__)r   r   )r   )r   r1   )r   rA   rB   F)r   r   TrW   F)
tensorflowr	   tensorflow.contrib.slimcontribr   r   r0   r@   rV   r^   __name__r   r   r   r   <module>   s&    


"
0
