a
    dF                     @   s  d Z ddlZddlmZ ddlZddlmZ ddlm  mZ	 ddl
mZmZ ddlmZ ddlmZ ddlmZmZ d	gZd
d ZG dd dejZG dd dejZG dd deZG dd deZG dd deZG dd dejZG dd	 d	ejZd?ddZd@ddZeedd ed!d"d#ed$d ed%d ed&d ed'd ed(d"d#ed)d ed*d d+	Z edAed,d-d.Z!edBed,d/d0Z"edCed,d1d2Z#edDed,d3d4Z$edEed,d5d6Z%edFed,d7d8Z&edGed,d9d:Z'edHed,d;d<Z(edIed,d=d>Z)dS )Ja:  
SEResNet implementation from Cadene's pretrained models
https://github.com/Cadene/pretrained-models.pytorch/blob/master/pretrainedmodels/models/senet.py
Additional credit to https://github.com/creafz

Original model: https://github.com/hujie-frank/SENet

ResNet code gently borrowed from
https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py

FIXME I'm deprecating this model and moving them to ResNet as I don't want to maintain duplicate
support for extras like dilation, switchable BN/activations, feature extraction, etc that don't exist here.
    N)OrderedDictIMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STD)create_classifier   )build_model_with_cfg)register_modelgenerate_default_cfgsSENetc                 C   sR   t | tjr"tjj| jddd n,t | tjrNtj| jd tj| jd d S )NZfan_outrelu)modeZnonlinearityg      ?        )	
isinstancennConv2dinitZkaiming_normal_ZweightBatchNorm2dZ	constant_bias)m r   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/timm/models/senet.py_weight_init   s
    r   c                       s$   e Zd Z fddZdd Z  ZS )SEModulec                    sV   t t|   tj||| dd| _tjdd| _tj|| |dd| _t	 | _
d S )Nr   )kernel_sizeTZinplace)superr   __init__r   r   fc1ReLUr   fc2ZSigmoidsigmoid)selfZchannels	reduction	__class__r   r   r   '   s
    zSEModule.__init__c                 C   sB   |}|j ddd}| |}| |}| |}| |}|| S )N)      T)Zkeepdim)meanr   r   r    r!   )r"   xZmodule_inputr   r   r   forward.   s    



zSEModule.forward)__name__
__module____qualname__r   r*   __classcell__r   r   r$   r   r   %   s   r   c                   @   s   e Zd ZdZdd ZdS )
BottleneckzH
    Base class for bottlenecks that implements `forward()` method.
    c                 C   s   |}|  |}| |}| |}| |}| |}| |}| |}| |}| jd urh| |}| || }| |}|S N)	conv1bn1r   conv2bn2conv3bn3
downsample	se_moduler"   r)   Zshortcutoutr   r   r   r*   =   s    










zBottleneck.forwardN)r+   r,   r-   __doc__r*   r   r   r   r   r/   8   s   r/   c                       s&   e Zd ZdZdZd fdd	Z  ZS )SEBottleneckz"
    Bottleneck for SENet154.
       r   Nc              	      s   t t|   tj||d ddd| _t|d | _tj|d |d d|d|dd| _t|d | _	tj|d |d ddd| _
t|d | _tjdd	| _t|d |d
| _|| _|| _d S )Nr&   r   Fr   r   r=   r'   r   stridepaddinggroupsr   Tr   r#   )r   r<   r   r   r   r1   r   r2   r3   r4   r5   r6   r   r   r   r8   r7   r@   r"   inplanesplanesrB   r#   r@   r7   r$   r   r   r   Z   s    zSEBottleneck.__init__)r   Nr+   r,   r-   r;   	expansionr   r.   r   r   r$   r   r<   T   s   r<   c                       s&   e Zd ZdZdZd fdd	Z  ZS )SEResNetBottleneckz
    ResNet bottleneck with a Squeeze-and-Excitation module. It follows Caffe
    implementation and uses `stride=stride` in `conv1` and not in `conv2`
    (the latter is used in the torchvision implementation of ResNet).
    r=   r   Nc                    s   t t|   tj||dd|d| _t|| _tj||dd|dd| _t|| _	tj||d ddd| _
t|d | _tjdd	| _t|d |d
| _|| _|| _d S )Nr   Fr   r   r@   r'   r   rA   rB   r   r=   r>   Tr   rC   )r   rI   r   r   r   r1   r   r2   r3   r4   r5   r6   r   r   r   r8   r7   r@   rD   r$   r   r   r   r   s    zSEResNetBottleneck.__init__)r   NrG   r   r   r$   r   rI   j   s   rI   c                       s&   e Zd ZdZdZd fdd	Z  ZS )SEResNeXtBottleneckzI
    ResNeXt bottleneck type C with a Squeeze-and-Excitation module.
    r=   r   Nc           	   	      s   t t|   t||d  | }tj||dddd| _t|| _	tj||d|d|dd| _
t|| _tj||d ddd| _t|d | _tjd	d
| _t|d |d| _|| _|| _d S )N@   r   FrJ   r'   r?   r=   r>   Tr   rC   )r   rL   r   mathfloorr   r   r1   r   r2   r3   r4   r5   r6   r   r   r   r8   r7   r@   )	r"   rE   rF   rB   r#   r@   r7   Z
base_widthwidthr$   r   r   r      s    zSEResNeXtBottleneck.__init__)r   Nr=   rG   r   r   r$   r   rL      s   rL   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )SEResNetBlockr   Nc                    s   t t|   tj||dd|dd| _t|| _tj||dd|dd| _t|| _	tj
dd| _t||d| _|| _|| _d S )	Nr'   r   F)r   rA   r@   r   rK   Tr   rC   )r   rQ   r   r   r   r1   r   r2   r3   r4   r   r   r   r8   r7   r@   rD   r$   r   r   r      s    zSEResNetBlock.__init__c                 C   sp   |}|  |}| |}| |}| |}| |}| |}| jd urT| |}| || }| |}|S r0   )r1   r2   r   r3   r4   r7   r8   r9   r   r   r   r*      s    








zSEResNetBlock.forward)r   N)r+   r,   r-   rH   r   r*   r.   r   r   r$   r   rQ      s   rQ   c                       s   e Zd Zd fd	d
	Zd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   皙?r'   rM   Fr   r     avgc                    sj  t t|   || _|| _|| _|rdtj|ddddddfdtdfd	tj	d
dfdtjdddddddfdtdfdtj	d
dfdtjd|dddddfdt|fdtj	d
dfg	}n6dtj||dddddfdt|fd	tj	d
dfg}t
t|| _tjddd
d| _t|dddg| _| j|d|d ||ddd| _|  jtd|j dddg7  _| j|d|d d|||	|
d| _|  jtd|j dddg7  _| j|d|d d|||	|
d| _|  jtd|j d d!dg7  _| j|d"|d d|||	|
d| _|  jtd"|j d#d$dg7  _d"|j | _t| j| j|d%\| _| _|  D ]}t| qVd&S )'af  
        Parameters
        ----------
        block (nn.Module): Bottleneck class.
            - For SENet154: SEBottleneck
            - For SE-ResNet models: SEResNetBottleneck
            - For SE-ResNeXt models:  SEResNeXtBottleneck
        layers (list of ints): Number of residual blocks for 4 layers of the
            network (layer1...layer4).
        groups (int): Number of groups for the 3x3 convolution in each
            bottleneck block.
            - For SENet154: 64
            - For SE-ResNet models: 1
            - For SE-ResNeXt models:  32
        reduction (int): Reduction ratio for Squeeze-and-Excitation modules.
            - For all models: 16
        dropout_p (float or None): Drop probability for the Dropout layer.
            If `None` the Dropout layer is not used.
            - For SENet154: 0.2
            - For SE-ResNet models: None
            - For SE-ResNeXt models: None
        inplanes (int):  Number of input channels for layer1.
            - For SENet154: 128
            - For SE-ResNet models: 64
            - For SE-ResNeXt models: 64
        input_3x3 (bool): If `True`, use three 3x3 convolutions instead of
            a single 7x7 convolution in layer0.
            - For SENet154: True
            - For SE-ResNet models: False
            - For SE-ResNeXt models: False
        downsample_kernel_size (int): Kernel size for downsampling convolutions
            in layer2, layer3 and layer4.
            - For SENet154: 3
            - For SE-ResNet models: 1
            - For SE-ResNeXt models: 1
        downsample_padding (int): Padding for downsampling convolutions in
            layer2, layer3 and layer4.
            - For SENet154: 1
            - For SE-ResNet models: 0
            - For SE-ResNeXt models: 0
        num_classes (int): Number of outputs in `last_linear` layer.
            - For all models: 1000
        r1   rM   r'   r&   r   F)r@   rA   r   r2   Zrelu1Tr   r3   r4   Zrelu2r5   r6   Zrelu3   r   r@   rA   r   )r@   Z	ceil_modelayer0)Znum_chsr#   moduler   )rF   blocksrB   r#   downsample_kernel_sizedownsample_paddingr=   layer1   )rF   rY   r@   rB   r#   rZ   r[      layer2      layer3i       layer4Z	pool_typeN)r   r   r   rE   num_classes	drop_rater   r   r   r   
Sequentialr   rW   Z	MaxPool2dpool0dictZfeature_info_make_layerr\   rH   r_   rb   rd   num_featuresr   global_poollast_linearmodulesr   )r"   blocklayersrB   r#   rg   Zin_chansrE   	input_3x3rZ   r[   rf   rm   Zlayer0_modulesr   r$   r   r   r      s    /	 
 
 
 
zSENet.__init__c	              
   C   s   d }	|dks| j ||j krNttj| j ||j |||ddt||j }	|| j |||||	g}
||j | _ td|D ]}|
|| j ||| qztj|
 S )Nr   FrV   )rE   rH   r   rh   r   r   rangeappend)r"   rp   rF   rY   rB   r#   r@   rZ   r[   r7   rq   ir   r   r   rk   5  s    zSENet._make_layerc                 C   s   t d|rdndd}|S )Nz^layer0z^layer(\d+)z^layer(\d+)\.(\d+))stemrY   )rj   )r"   ZcoarseZmatcherr   r   r   group_matcherG  s    zSENet.group_matcherTc                 C   s   |rJ dd S )Nz$gradient checkpointing not supportedr   )r"   enabler   r   r   set_grad_checkpointingL  s    zSENet.set_grad_checkpointingc                 C   s   | j S r0   )rn   )r"   r   r   r   get_classifierP  s    zSENet.get_classifierc                 C   s$   || _ t| j| j |d\| _| _d S )Nre   )rf   r   rl   rm   rn   )r"   rf   rm   r   r   r   reset_classifierT  s    
zSENet.reset_classifierc                 C   s@   |  |}| |}| |}| |}| |}| |}|S r0   )rW   ri   r\   r_   rb   rd   r"   r)   r   r   r   forward_featuresY  s    





zSENet.forward_features)
pre_logitsc                 C   s:   |  |}| jdkr(tj|| j| jd}|r0|S | |S )Nr   )ptraining)rm   rg   FZdropoutr   rn   )r"   r)   r~   r   r   r   forward_headb  s    

zSENet.forward_headc                 C   s   |  |}| |}|S r0   )r}   r   r|   r   r   r   r*   h  s    

zSENet.forward)rR   r'   rM   Fr   r   rS   rT   )r   r   r   )F)T)rT   )F)r+   r,   r-   r   rk   torchZjitignorerw   ry   rz   r{   r}   boolr   r*   r.   r   r   r$   r   r      s"      |  


	Fc                 K   s   t t| |fi |S r0   )r   r   )variant
pretrainedkwargsr   r   r   _create_senetn  s    r    c                 K   s   | dddddt tddd
|S )	NrS   )r'      r   )rU   rU   g      ?Zbilinearzlayer0.conv1rn   )
urlrf   Z
input_sizeZ	pool_sizeZcrop_pctinterpolationr(   ZstdZ
first_conv
classifierr   )r   r   r   r   r   _cfgr  s    r   zmhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/legacy_senet154-e9eb9fe6.pth)r   zhhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/seresnet18-4bb0ce65.pthZbicubic)r   r   zhhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/seresnet34-a4004e63.pthzhhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-cadene/se_resnet50-ce0d4300.pthzihttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-cadene/se_resnet101-7e38fcc6.pthzihttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-cadene/se_resnet152-d17c99b7.pthzphttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/seresnext26_32x4d-65ebdb501.pthzwhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/legacy_se_resnext50_32x4d-f3651bad.pthzxhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/legacy_se_resnext101_32x4d-37725eac.pth)	zlegacy_senet154.in1kzlegacy_seresnet18.in1kzlegacy_seresnet34.in1kzlegacy_seresnet50.in1kzlegacy_seresnet101.in1kzlegacy_seresnet152.in1kzlegacy_seresnext26_32x4d.in1kzlegacy_seresnext50_32x4d.in1kzlegacy_seresnext101_32x4d.in1k)returnc                 K   s.   t f tg dddd|}td| fi |S )Nr&   r&   r&   r&   r   ra   rp   rq   rB   r#   legacy_seresnet18rj   rQ   r   r   r   Z
model_argsr   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr'   r=      r'   r   ra   r   legacy_seresnet34r   r   r   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr   r   ra   r   legacy_seresnet50rj   rI   r   r   r   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr'   r=      r'   r   ra   r   legacy_seresnet101r   r   r   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr'   r^   $   r'   r   ra   r   legacy_seresnet152r   r   r   r   r   r     s    r   c                 K   s6   t f tg dddddddd|}td	| fi |S )
Nr   rM   ra   r'   r   r]   T)rp   rq   rB   r#   rZ   r[   rE   rr   legacy_senet154)rj   r<   r   r   r   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr   rc   ra   r   legacy_seresnext26_32x4drj   rL   r   r   r   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr   rc   ra   r   legacy_seresnext50_32x4dr   r   r   r   r   r     s    r   c                 K   s.   t f tg dddd|}td| fi |S )Nr   rc   ra   r   legacy_seresnext101_32x4dr   r   r   r   r   r     s    r   )F)r   )F)F)F)F)F)F)F)F)F)*r;   rN   collectionsr   r   Ztorch.nnr   Ztorch.nn.functionalZ
functionalr   Z	timm.datar   r   Ztimm.layersr   Z_builderr   	_registryr	   r
   __all__r   Moduler   r/   r<   rI   rL   rQ   r   r   r   Zdefault_cfgsr   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   " 8


