o
    پi5                     @   sp  d Z ddlmZ ddlmZmZmZmZ ddlZddl	m
Z
 ddlmZ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G dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZG dd de
jZ G dd de
jZ!G dd de
jZ"d-de"fdd Z#ed!d"d#d$d%d&d'eed(d)d*
iZ$ed-d+d,Z%dS ).z Pytorch Inception-V4 implementation
Sourced from https://github.com/Cadene/tensorflow-model-zoo.torch (MIT License) which is
based upon Google's Tensorflow implementation and pretrained weights (Apache 2.0 License)
    )partial)ListOptionalTupleUnionN)IMAGENET_INCEPTION_MEANIMAGENET_INCEPTION_STD)create_classifierConvNormAct   )build_model_with_cfg)feature_take_indices)register_modelgenerate_default_cfgsInceptionV4c                       (   e Zd Zef fdd	Zdd Z  ZS )Mixed3ac                    s4   t t|   tjddd| _|ddddd| _d S )N      stride@   `   kernel_sizer   )superr   __init__nn	MaxPool2dmaxpoolconvself
conv_block	__class__ L/home/ubuntu/.local/lib/python3.10/site-packages/timm/models/inception_v4.pyr      s   zMixed3a.__init__c                 C   (   |  |}| |}t||fd}|S Nr   )r   r    torchcatr"   xx0x1outr&   r&   r'   forward      

zMixed3a.forward__name__
__module____qualname__r
   r   r1   __classcell__r&   r&   r$   r'   r          r   c                       r   )Mixed4ac                    s~   t t|   t|ddddd|ddddd| _t|ddddd|dddddd	|ddd
ddd	|ddddd| _d S )N   r   r   r   r   r   r      r   r   r   r   paddingr<   r   r   r   )r   r   )r   r9   r   r   
Sequentialbranch0branch1r!   r$   r&   r'   r   "   s   
zMixed4a.__init__c                 C   r(   r)   )rC   rD   r*   r+   r,   r&   r&   r'   r1   1   r2   zMixed4a.forwardr3   r&   r&   r$   r'   r9   !   s    r9   c                       r   )Mixed5ac                    s4   t t|   |ddddd| _tjddd| _d S )N   r   r   r   r   )r   rE   r   r    r   r   r   r!   r$   r&   r'   r   9   s   zMixed5a.__init__c                 C   r(   r)   )r    r   r*   r+   r,   r&   r&   r'   r1   >   r2   zMixed5a.forwardr3   r&   r&   r$   r'   rE   8   r8   rE   c                       r   )
InceptionAc                    s   t t|   |ddddd| _t|ddddd|dddddd| _t|ddddd|dddddd|dddddd| _ttjddddd	|ddddd| _	d S )
N  r   r   r   r   r   r>   Fr   r?   count_include_pad)
r   rG   r   rC   r   rB   rD   branch2	AvgPool2dbranch3r!   r$   r&   r'   r   F   s   
zInceptionA.__init__c                 C   @   |  |}| |}| |}| |}t||||fd}|S r)   rC   rD   rK   rM   r*   r+   r"   r-   r.   r/   x2x3r0   r&   r&   r'   r1   Z      



zInceptionA.forwardr3   r&   r&   r$   r'   rG   E       rG   c                       r   )
ReductionAc              
      sj   t t|   |ddddd| _t|ddddd|dddddd|dd	ddd| _tjddd
| _d S )NrH   r   r   r   rF   r      r>      r   )	r   rU   r   rC   r   rB   rD   r   rK   r!   r$   r&   r'   r   d   s   zReductionA.__init__c                 C   4   |  |}| |}| |}t|||fd}|S r)   rC   rD   rK   r*   r+   r"   r-   r.   r/   rQ   r0   r&   r&   r'   r1   p   
   


zReductionA.forwardr3   r&   r&   r$   r'   rU   c   s    rU   c                       r   )
InceptionBc                    s   t t|   |ddddd| _t|ddddd|dddddd	|dd
dddd	| _t|ddddd|dddddd	|dddddd	|dddddd	|dd
dddd	| _ttjddddd|ddddd| _	d S )N   rH   r   r   rF   rV   r;   r=   r>   rW   r@   rA   r   FrI      )
r   r\   r   rC   r   rB   rD   rK   rL   rM   r!   r$   r&   r'   r   y   s$   
zInceptionB.__init__c                 C   rN   r)   rO   rP   r&   r&   r'   r1      rS   zInceptionB.forwardr3   r&   r&   r$   r'   r\   x   s    r\   c                       r   )
ReductionBc                    s   t t|   t|ddddd|ddddd| _t|ddddd|ddddd	d
|dddddd
|ddddd| _tjddd| _d S )Nr]   rF   r   r   r   r   rW   r;   r=   r>   i@  r@   rA   r   )	r   r_   r   r   rB   rC   rD   r   rK   r!   r$   r&   r'   r      s   zReductionB.__init__c                 C   rX   r)   rY   rZ   r&   r&   r'   r1      r[   zReductionB.forwardr3   r&   r&   r$   r'   r_      s    r_   c                       r   )
InceptionCc              	      s   t t|   |ddddd| _|ddddd| _|dddddd| _|ddd	dd
d| _|ddddd| _|ddd	dd
d| _|dddddd| _	|dddddd| _
|ddd	dd
d| _ttjddddd|ddddd| _d S )N   rW   r   r   rH   )r   r   )r   r   r>   )r   r   )r   r   i  i   r   FrI   )r   r`   r   rC   	branch1_0
branch1_1a
branch1_1b	branch2_0	branch2_1	branch2_2
branch2_3a
branch2_3br   rB   rL   rM   r!   r$   r&   r'   r      s   
zInceptionC.__init__c                 C   s   |  |}| |}| |}| |}t||fd}| |}| |}| |}	| 	|	}
| 
|	}t|
|fd}| |}t||||fd}|S r)   )rC   rb   rc   rd   r*   r+   re   rf   rg   rh   ri   rM   )r"   r-   r.   x1_0x1_1ax1_1br/   x2_0x2_1x2_2x2_3ax2_3brQ   rR   r0   r&   r&   r'   r1      s   









zInceptionC.forwardr3   r&   r&   r$   r'   r`      rT   r`   c                       s*  e Zd Z								d. fd	d
	Zejjd/ddZejjd0ddZejjde	j
fddZd1dedefddZ					d2dejdeeeee f  dededededeeej eejeej f f fd d!Z	"		d3deeee f d#ed$efd%d&Zd'd( Zd/d)efd*d+Zd,d- Z  ZS )4r     r               avgbatchnorm2dMbP?reluc	           
   	      sj  t t|   |dksJ || _d | _| _ttd||t|dtddd  |ddd	d
 ddddd
 ddddddt	 t
 t g}	|	 fddtdD 7 }	|	t g7 }	|	 fddtdD 7 }	|	t g7 }	|	 fddtdD 7 }	tj|	 | _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 S ) Nrs   ra   r   )epsT)inplace)r?   
norm_layer	act_layernorm_kwargs
act_kwargsr   r   r   r   r   r>   c                       g | ]}t  qS r&   )rG   .0_r#   r&   r'   
<listcomp>       z(InceptionV4.__init__.<locals>.<listcomp>   c                    r   r&   )r\   r   r   r&   r'   r      r   r<   c                    r   r&   )r`   r   r   r&   r'   r     r   z
features.2)num_chs	reductionmoduler:   z
features.3rH      z
features.9r]      zfeatures.17zfeatures.21)	pool_type	drop_rate)r   r   r   num_classesnum_featureshead_hidden_sizer   r
   dictr   r9   rE   rangerU   r_   r   rB   featuresfeature_infor	   global_pool	head_droplast_linear)
r"   r   in_chansoutput_strider   r   r{   norm_epsr|   r   r$   r   r'   r      sD   
zInceptionV4.__init__Fc                 C   s   t dddS )Nz^features\.[012]\.z^features\.(\d+))stemblocks)r   )r"   coarser&   r&   r'   group_matcher  s   zInceptionV4.group_matcherTc                 C   s   |rJ dd S )Nz$gradient checkpointing not supportedr&   )r"   enabler&   r&   r'   set_grad_checkpointing  s   z"InceptionV4.set_grad_checkpointingreturnc                 C   s   | j S N)r   )r"   r&   r&   r'   get_classifier  s   zInceptionV4.get_classifierr   r   c                 C   s$   || _ t| j| j |d\| _| _d S )N)r   )r   r	   r   r   r   )r"   r   r   r&   r&   r'   reset_classifier  s   
zInceptionV4.reset_classifierNNCHWr-   indicesnorm
stop_early
output_fmtintermediates_onlyc                    s   |dv sJ dg }dd | j D  tt |\}}	 fdd|D } |	 }	tj s/|s3| j}
n	| jd|	d  }
t|
D ]\}}||}||v rQ|| q@|rV|S ||fS )a   Forward features that returns intermediates.

        Args:
            x: Input image tensor
            indices: Take last n blocks if int, all if None, select matching indices if sequence
            norm: Apply norm layer to compatible intermediates
            stop_early: Stop iterating over blocks when last desired intermediate hit
            output_fmt: Shape of intermediate feature outputs
            intermediates_only: Only return intermediate features
        Returns:

        )r   zOutput shape must be NCHW.c                 S   "   g | ]}t |d  dd qS r   .intsplitr   infor&   r&   r'   r   9     " z5InceptionV4.forward_intermediates.<locals>.<listcomp>c                    s   g | ]} | qS r&   r&   )r   i
stage_endsr&   r'   r   ;  r   Nr   )	r   r   lenr*   jitis_scriptingr   	enumerateappend)r"   r-   r   r   r   r   r   intermediatestake_indices	max_indexstagesfeat_idxstager&   r   r'   forward_intermediates"  s"   
z!InceptionV4.forward_intermediatesr   
prune_norm
prune_headc                 C   sR   dd | j D }tt||\}}|| }| jd|d  | _|r'| dd |S )z@ Prune layers not required for specified intermediates.
        c                 S   r   r   r   r   r&   r&   r'   r   V  r   z9InceptionV4.prune_intermediate_layers.<locals>.<listcomp>Nr   r    )r   r   r   r   r   )r"   r   r   r   r   r   r   r&   r&   r'   prune_intermediate_layersN  s   z%InceptionV4.prune_intermediate_layersc                 C   s
   |  |S r   )r   r"   r-   r&   r&   r'   forward_features^  s   
zInceptionV4.forward_features
pre_logitsc                 C   s&   |  |}| |}|r|S | |S r   )r   r   r   )r"   r-   r   r&   r&   r'   forward_heada  s   

zInceptionV4.forward_headc                 C   s   |  |}| |}|S r   )r   r   r   r&   r&   r'   r1   f  s   

zInceptionV4.forward)rr   r   rs   rt   ru   rv   rw   rx   F)T)ru   )NFFr   F)r   FT)r4   r5   r6   r   r*   r   ignorer   r   r   Moduler   r   strr   Tensorr   r   r   boolr   r   r   r   r   r1   r7   r&   r&   r$   r'   r      sd    0 
.
Fr   c                 K   s   t t| |fdtddi|S )Nfeature_cfgT)flatten_sequential)r   r   r   )variant
pretrainedkwargsr&   r&   r'   _create_inception_v4l  s   r   zinception_v4.tf_in1kztimm/rr   )r   +  r   )r   r   g      ?bicubiczfeatures.0.convr   )
	hf_hub_idr   
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierc                 K   s   t d| fi |S )Ninception_v4)r   )r   r   r&   r&   r'   r     s   r   r   )&__doc__	functoolsr   typingr   r   r   r   r*   torch.nnr   	timm.datar   r   timm.layersr	   r
   _builderr   	_featuresr   	_registryr   r   __all__r   r   r9   rE   rG   rU   r\   r_   r`   r   r   default_cfgsr   r&   r&   r&   r'   <module>   sB    !* 
