CoolFace
Apppublic

ysheng/SSN-Soft-Shadow-Network-for-Image-Composition

sourceHugging Faceunknownupdated 3y agoView on Hugging Face
2likes
SSN_Model.cpython-39.pyc94 linesDownload Raw Back to __pycache__
1a

2�d�(�@s2ddlZddlmZddlmmZddlmZddlZd'dd�Z	dd�Z3dd	�Zd4d�Zdd
�Z
d(dd�Zd)dd�ZGdd�dej�ZGdd�dej�ZGdd�dej�ZGdd�dej�ZGdd�dej�ZGdd�dej�Zed k�r.e�d!d"d#d#�Ze�d!d"d$d%�Zed"d"�Zeee�Zed&ej�dS)*�N�gaussian�{�G�z�?cs��fdd�}|S)Ncs�|jj}|�d�dks$|�d�dkr�t|d�r�dkrJtj�|jd��n~�dkrltjj|jt	�5d�d	�n\�d6kr�tjj|jddd�n>�d
kr�tjj|jt	�7d�d	�n�dkr�nds�Jd�
����t|d�r�|jdur�tj�|jd�dS)N�Convr�Linear�weightr��xavier�)�gain�kaiming�fan_in)�a�mode�8orthogonal�defaultzUnsupported initialization: {}�bias)�	__class__�__name__�find�hasattr�nn�init�normal_r�xavier_normal_�math�sqrt�kaiming_normal_�orthogonal_�formatr�	constant_)�m�	classname��	init_type�std��E/home/ysheng/Documents/Research/PixHtLab-Src/Demo/models/SSN_Model.py�init_funs*���zweights_init.<locals>.init_funr%)r#r$r'r%r"r&�weights_initsr(cCs|��D]9}d|_qdS)NF��10parameters�
requires_grad��module�paramr%r%r&�freezesr/cCs|��D]11}d|_qdS)NTr)r,r%r%r&�unfreeze!sr0c12Cs�t|dd�}t|dd�}t|dd�}|dd}g}|��D]B\}}|jsXqH|dd�dkrz||d	d13�g7}qH|||d14�g7}qH|dkr�tj|||dfd
d�Sd�|�}	t�|	�t|	��dS)N�hyper_params�lr�model�beta1�weight_decay�	optimizer�����rr)�paramsr5�Adamg+�����?g�h㈵��>)r2�betas�epsz{} not implemented yet)	�float�named_parametersr+�optimr9r�logging�error�NotImplementedError)15�optr3r2r4r5�opt_name�optim_params�key�value�errr%r%r&�
get_optimizer%s,��1617rHcCsh|durt��St��t��t��t��t�d�t��d�}||��vr`t	�18d�|��ds`J�||S)Ng�������?)�relu�sigmoid�tanh�prelu�leaky�geluz$activation {} is not implemented yetF)r�Identity�ReLU�Sigmoid�Tanh�PReLU�	LeakyReLU�GELU�keysr?r@r)�19activation�act_funcr%r%r&�get_activationBs�rY�InstancecCs�gd�}||vr&d}t�|�t|��|dkr<tj|dd�S|dkrNt�|�S|dkrt|dkrdd}nd	}t�||�Std20�|���dS)N)rZ�Batch�Groupz-Normalization {} has not been implemented yetrZT)�affiner[r\� �z{} has not implemented yet)	r?r@�21ValueErrorr�InstanceNorm2d�BatchNorm2d�	GroupNormrAr)�out_channels�	norm_type�norm_setrG�groupsr%r%r&�get_normTs2223rhrIcCst|�}t|d�}||fS)Nr\)rYrh)rdZactivation_funcrW�24norm_layerr%r%r&�get_layer_infons25rjcs*eZdZdZd26�fdd�	Zdd	�Z�ZS)rz (convolution => [BN] => ReLU) �r_TrMc	27sXt���t||�\}	}28|r,||kr,d|_nd|_t�tj||||||d�|	|29�|_dS)NTF)�stride�kernel_size�paddingr)�super�__init__rj�resnetr�30Sequential�Conv2d�conv)�self�in_channelsrdrmrlrnrrWrqrirX�rr%r&rpvs	31�z
Conv.__init__cCs|�|�}|jr||}|S�N)rtrq)ru�x�resr%r%r&�forward�s32zConv.forward)rkr_r_TrMT�r�33__module__�__qualname__�__doc__rpr{�
__classcell__r%r%rwr&rts�rcs*eZdZdZd�fdd�	Zdd�Z�ZS)	�Upz Upscaling then conv rITcs2t���tjdddd�|_t||||d�|_dS)Nr	�bilinearT)�scale_factorr�
align_corners�rWrq)rorpr�Upsample�up_layerr�up�rurvrdrWrqrwr%r&rp�s34zUp.__init__cCs|�|�}|�|�Srx)r�r��ruryr%r%r&r{�s35z36Up.forward)rITr|r%r%rwr&r��sr�cs*eZdZdZd�fdd�	Zdd�Z�ZS)	�DConvz Double Conv Layer37    rITcs2t���t||||d�|_t||||d�|_dS)Nr�)rorpr�conv1�conv2r�rwr%r&rp�s38zDConv.__init__cCs|�|�|��Srx)r�r�r�r%r%r&r{�sz
DConv.forward)rITr|r%r%rwr&r��sr�cs&eZdZd�fdd�	Zdd�Z�ZS)	�EncoderrkrMTcs�tt|���t|d|d||d�|_tddd||d�|_tdd||d�|_tddd||d�|_tdd||d�|_tddd||d�|_	tdd||d�|_39tdd	d||d�|_td	d	||d�|_td	d	||d�|_
td	d	||d�|_dS)40Nr^r_)rlrWrq�@r	r����)ror�rpr�in_conv�41down_32_64�down_64_64_1�down_64_128�down_128_128_1�down_128_256�down_256_256_1�down_256_512�down_512_512_1�down_512_512_2�down_512_512_3)rurv�mid_actrqrwr%r&rp�szEncoder.__init__c
Cs�|�|�}tj||fdd�}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�	|�}	|�42|	�}43|�|44�}|�|�}|||45|	|||||||fS)Nr_��dim)
r��torch�catr�r�r�r�r�r�r�r�r�r�)
rury�x1�x2�x3�x4�x5�x6�x7�x8�x9�x10�x11r%r%r&r{�s4647484950515253545556zEncoder.forward)rkrMT)rr}r~rpr{r�r%r%rwr&r��sr�cs*eZdZdZd57�fdd�	Zdd	�Z�ZS)�Decoderz Up Stream Sequence rkrIrJTcs�tt|���d}d}t|d||d�|_tdd||d�|_tdd||d�|_tdd||d�|_tdd||d�|_	tdd||d�|_58tdd||d�|_tdd||d�|_tdd||d�|_
tdd	||d�|_td||d59�|_dS)Nr��dr�r�iir�r�r^)rW)ror�rpr�60up_16_16_1�61up_16_16_2�62up_16_16_3r��up_16_32�63up_32_32_1�up_32_64�64up_64_64_1�	up_64_128�up_128_128_1�65up_128_256�out_conv)rurdr��out_actrq�
input_channelZfea_dimrwr%r&rp�szDecoder.__init__cCsn|\}}}}}}}	}66}}}
|jdd�\}}|�dddd��dd||�}|�|�}tj||fdd�}|�|�}tj||fdd�}|�|�}tj||fdd�}|�|�}tj||fdd�}|�	|�}tj||fdd�}|�67|�}tj|	|fdd�}|�|�}tj|68|fdd�}|�|�}tj||fdd�}|�
|�}tj||fdd�}|�|�}tj|
|fdd�}|�|�}|S)Nr	�����r�r_r�)�shape�view�repeatr�r�r�r�r�r�r�r�r�r�r�r�r�)rury�iblr�r�r�r�r�r�r�r�r�r�r��h�w�yr%r%r&r{�s26970717273747576777879zDecoder.forward)rkrIrJTr|r%r%rwr&r��s�r�cs*eZdZdZd80�fdd�	Zdd	�Z�ZS)�	SSN_Modelz" Implementation of Relighting Net rkrMrJTcs^tt|���||_t|||d�|_t||||d�|_tddd�}|j�	|�|j�	|�dS)N)r�rq)r�r�rqrg����MbP?)r$)81ror�rpr�r��encoderr��decoderr(�apply)rurvrdr�r�rq�	init_funcrwr%r&rp%szSSN_Model.__init__cCs,|�|�}|�||�}|jdkr(|d}|S)z�82            Input is (source image, target light, source light, )83            Output is: predicted new image, predicted source light, self-supervision image84        rJg>@)r�r�r�)ruryr�Zlatent�predr%r%r&r{8s858687zSSN_Model.forward)rkrkrMrJTr|r%r%rwr&r�"s�r��__main__�r_r�r^�zOutput: )rr)rZ)rI)r��torch.nnr�torch.nn.functional�88functional�FZtorch.optimr>r?r(r/r0rHrYrhrj�Modulerr�r�r�r�r�r�randnryr�r3r��printr�r%r%r%r&�<module>s.899091#
%J$929394