U
    ¯Ê¹j¢  ã                   @   sä   d Z ddlZddlmZ ddlm  mZ ddlm	Z	 ddl
mZ ddlmZmZmZ dgZG dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZddd„Zeddddddddddddœ
iƒZed edœdd„ƒZeeddiƒ dS )!a×  
Ported to pytorch thanks to [tstandley](https://github.com/tstandley/Xception-PyTorch)

@author: tstandley
Adapted by cadene

Creates an Xception Model as defined in:

Francois Chollet
Xception: Deep Learning with Depthwise Separable Convolutions
https://arxiv.org/pdf/1610.02357.pdf

This weights ported from the Keras implementation. Achieves the following performance on the validation set:

Loss:0.9173 Prec@1:78.892 Prec@5:94.292

REMEMBER to set your image size to 3x299x299 for both test and validation

normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5],
                                  std=[0.5, 0.5, 0.5])

The resize parameter of the validation transform should be 333, and make sure to center crop at 299x299
é    N)Úcreate_classifieré   )Úbuild_model_with_cfg)Úregister_modelÚgenerate_default_cfgsÚregister_model_deprecationsÚXceptionc                       s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )ÚSeparableConv2dr   r   c              
      sJ   t t| ƒ ¡  tj|||||||dd�| _tj||ddddddd�| _d S )NF)ÚgroupsÚbiasr   r   ©r   )Úsuperr	   Ú__init__ÚnnÚConv2dÚconv1Ú	pointwise)ÚselfÚin_channelsÚout_channelsZkernel_sizeÚstrideÚpaddingZdilation©Ú	__class__© úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/models/xception.pyr   $   s           ÿzSeparableConv2d.__init__c                 C   s   |   |¡}|  |¡}|S ©N)r   r   ©r   Úxr   r   r   Úforward+   s    

zSeparableConv2d.forward)r   r   r   r   ©Ú__name__Ú
__module__Ú__qualname__r   r   Ú__classcell__r   r   r   r   r	   #   s   r	   c                       s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )ÚBlockr   Tc              
      s  t t| ƒ ¡  ||ks|dkrBtj||d|dd�| _t |¡| _nd | _g }t|ƒD ]p}|rr|dkrh|n|}	|}
n|}	||d k r†|n|}
| 	tj
dd�¡ | 	t|	|
dddd�¡ | 	t |
¡¡ qT|sØ|dd … }ntj
dd�|d< |dk�r| 	t d|d¡¡ tj|Ž | _d S )	Nr   F)r   r   r   T©Zinplaceé   )r   r   )r   r%   r   r   r   ÚskipÚBatchNorm2dÚskipbnÚrangeÚappendÚReLUr	   Z	MaxPool2dZ
SequentialÚrep)r   r   r   ZrepsÚstridesÚstart_with_reluÚ
grow_firstr.   ÚiÚincZoutcr   r   r   r   2   s*    
zBlock.__init__c                 C   s:   |   |¡}| jd k	r*|  |¡}|  |¡}n|}||7 }|S r   )r.   r(   r*   )r   Zinpr   r(   r   r   r   r   P   s    


zBlock.forward)r   TTr    r   r   r   r   r%   1   s   r%   c                       s€   e Zd ZdZd‡ fdd„	Zejjdd	d
„ƒZejjddd„ƒZ	ejjdd„ ƒZ
ddd„Zdd„ Zdedœdd„Zdd„ Z‡  ZS )r   zo
    Xception optimized for the ImageNet dataset, as specified in
    https://arxiv.org/pdf/1610.02357.pdf
    éè  r'   ç        Úavgc              	      sf  t t| ƒ ¡  || _|| _|| _d| _tj|dddddd�| _	t 
d¡| _tjdd	�| _tjdd
ddd�| _t 
d
¡| _tjdd	�| _td
ddddd�| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tddddƒ| _tdddddd�| _tdddddƒ| _t 
d¡| _ tjdd	�| _!td| jdddƒ| _"t 
| j¡| _#tjdd	�| _$t%d
ddd�t%dddd�t%dddd�t%dddd�t%dddd�g| _&t'| j| j|d�\| _| _(|  )¡ D ]R}t*|tjƒ�r6tj+j,|j-ddd� n(t*|tj
ƒ�r|j-j. /d¡ |j0j. 1¡  �qd S )!zN Constructor
        Args:
            num_classes: number of classes
        i   é    r'   é   r   Fr   Tr&   é@   é€   )r0   é   iØ  r   i   )r1   i   Úact2)Znum_chsZ	reductionÚmoduleé   zblock2.rep.0é   zblock3.rep.0é   zblock12.rep.0Úact4©Z	pool_typeZfan_outZrelu)ÚmodeZnonlinearityN)2r   r   r   Ú	drop_rateÚglobal_poolÚnum_classesÚnum_featuresr   r   r   r)   Úbn1r-   Úact1Úconv2Úbn2r<   r%   Úblock1Úblock2Úblock3Úblock4Úblock5Úblock6Úblock7Úblock8Úblock9Úblock10Úblock11Úblock12r	   Úconv3Úbn3Úact3Úconv4Úbn4rA   ÚdictZfeature_infor   ÚfcÚmodulesÚ
isinstanceÚinitZkaiming_normal_ÚweightÚdataZfill_r   Zzero_)r   rF   Zin_chansrD   rE   Úmr   r   r   r   c   sT    ûzXception.__init__Fc                 C   s   t dddgd�S )Nz^conv[12]|bn[12])z^block(\d+)N)z^conv[34]|bn[34])éc   )ÚstemÚblocks)r]   )r   Zcoarser   r   r   Úgroup_matcherŸ   s    þþzXception.group_matcherTc                 C   s   |rt dƒ‚d S )Nz$gradient checkpointing not supported)ÚAssertionError)r   Úenabler   r   r   Úset_grad_checkpointing©   s    zXception.set_grad_checkpointingc                 C   s   | j S r   )r^   )r   r   r   r   Úget_classifier­   s    zXception.get_classifierc                 C   s$   || _ t| j| j |d�\| _| _d S )NrB   )rF   r   rG   rE   r^   )r   rF   rE   r   r   r   Úreset_classifier±   s    zXception.reset_classifierc                 C   sô   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  	|¡}|  
|¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|S r   )r   rH   rI   rJ   rK   r<   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   rA   r   r   r   r   Úforward_featuresµ   s2    























zXception.forward_features)Ú
pre_logitsc                 C   s6   |   |¡}| jr$tj|| j| jd� |r,|S |  |¡S )N)Útraining)rE   rD   ÚFZdropoutrp   r^   )r   r   ro   r   r   r   Úforward_headÔ   s    
zXception.forward_headc                 C   s   |   |¡}|  |¡}|S r   )rn   rr   r   r   r   r   r   Ú   s    

zXception.forward)r4   r'   r5   r6   )F)T)r6   )F)r!   r"   r#   Ú__doc__r   ÚtorchZjitÚignorerh   rk   rl   rm   rn   Úboolrr   r   r$   r   r   r   r   r   ]   s   <	

Fc                 K   s   t t| |fdtdd�i|—ŽS )NZfeature_cfgÚhook)Zfeature_cls)r   r   r]   )ÚvariantÚ
pretrainedÚkwargsr   r   r   Ú	_xceptionà   s      ÿþýr{   zlegacy_xception.tf_in1kzfhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-cadene/xception-43020ad28.pth)r'   é+  r|   )é
   r}   g¸…ëQ¸ì?Zbicubic)ç      à?r~   r~   r4   r   r^   )
ÚurlZ
input_sizeZ	pool_sizeZcrop_pctÚinterpolationZmeanZstdrF   Z
first_convÚ
classifier)Úreturnc                 K   s   t dd| i|—ŽS )NÚlegacy_xceptionry   )rƒ   )r{   )ry   rz   r   r   r   rƒ   ø   s    rƒ   Zxception)F)F)rs   Z	torch.jitrt   Ztorch.nnr   Ztorch.nn.functionalZ
functionalrq   Ztimm.layersr   Z_builderr   Ú	_registryr   r   r   Ú__all__ÚModuler	   r%   r   r{   Zdefault_cfgsrƒ   r!   r   r   r   r   Ú<module>   s@   , 
öÿ ÿ