Team Ai
Apppublic

David310/Detect_AI-generated_Image

sourceHugging Faceupdated 2y agoView on Hugging Face
4likes
trainer.cpython-38.pyc24 linesDownload Raw Back to __pycache__
1U

2��^f��@sTddlZddlZddlmZddlmZmZddlZddlm	Z	Gdd�de�Z3dS)�N)�	BaseModel�init_weights)�	get_modelcsNeZdZdd�Z�fdd�Zddd�Zdd	�Zd4d�Zdd
�Zdd�Z	�Z5S)�TrainercCsdS)Nr���selfrr�8/root/autodl-tmp/UniversalFakeDetect/networks/trainer.py�name	szTrainer.namecstt|��|�||_t|j�|_tjj	�6|jjjj
d|j�|jr�g}|j��D]*\}}|dksj|dkrv|�|�qRd|_qRn$td�ddl}|�d�|j��}|jdkr�tjj||j|jd	f|jd7�|_n.|jdkr�tjj||jd|jd�|_ntd
��t��|_ |j�!|j"d�dS)Ngz	fc.weightzfc.biasFz�Your backbone is not fixed. Are you sure you want to proceed? If this is a mistake, enable the --fix_backbone command during training and rerunr��adamg+�����?)�lr�betas�weight_decay�sgd)r
�momentumrzoptim should be [adam, sgd])#�superr�__init__�optr�arch�model�torch�nn�init�normal_�fc�weight�dataZ	init_gainZfix_backbone�named_parameters�append�
requires_grad�print�time�sleep�8parameters�optim�AdamWr
�beta1r�	optimizer�SGD�9ValueError�BCEWithLogitsLoss�loss_fn�toZgpu_ids)rr�paramsr10�pr"��	__class__rr	rs*11121314"1516zTrainer.__init__�����ư>cCs4|jjD]&}|dd<|d|krdSqdS)Nr
g$@FT)r(�param_groups)r�min_lr�param_grouprrr	�adjust_learning_rate-s17zTrainer.adjust_learning_ratecCs,|d�|j�|_|d�|j���|_dS)Nr�)r-�device�input�float�label)rr9rrr	�	set_input5szTrainer.set_inputcCs&|�|j�|_|j�d��d�|_dS)N�����r7)rr9�output�view�	unsqueezerrrr	�forward:szTrainer.forwardcCs|�|j�d�|j�S�Nr7)r,r>�squeezer;rrrr	�get_loss?szTrainer.get_losscCsB|��|�|j�d�|j�|_|j��|j��|j�	�dSrB)18rAr,r>rCr;�lossr(�	zero_grad�backward�steprrrr	�optimize_parametersBs192021zTrainer.optimize_parameters)r2)�__name__�22__module__�__qualname__r23rr6r<rArDrI�
__classcell__rrr0r	rs!24r)�	functoolsr�torch.nnrZnetworks.base_modelrr�sys�modelsrrrrrr	�<module>s