o
    àÙ·i_1  ã                   @   sô   d dl Z d dlZd dlmZ d dlm  mZ d dlmZ ee	ƒZ
zd dlmZ W n ey8   d dlmZ Y nw dZG dd„ dejƒZdd	„ Zd
d„ ZG dd„ dejjjƒZG dd„ dejjjƒZG dd„ dejjjƒZG dd„ dejjjƒZdS )é    N)Úinit_logger)Úload_state_dict_from_url)Úload_urlzjhttps://github.com/mseitzer/pytorch-fid/releases/download/fid_weights/pt_inception-2015-12-05-6726825d.pthc                       sH   e Zd ZdZdZdddddœZefddddf‡ fd	d
„	Zdd„ Z‡  ZS )ÚInceptionV3z5Pretrained InceptionV3 network returning feature mapsé   r   é   é   )é@   éÀ   é   é   TFc                    sP  t t| ƒ ¡  || _|| _t|ƒ| _t|ƒ| _| jdks J dƒ‚t	 
¡ | _|r+tƒ }ntdd}|j|j|jt	jdddg}| j t	j|Ž ¡ | jdkra|j|jt	jdddg}| j t	j|Ž ¡ | jdkr|j|j|j|j|j|j|j|jg}	| j t	j|	Ž ¡ | jdkrœ|j|j|j t	j!dd	g}
| j t	j|
Ž ¡ |  "¡ D ]}||_#q d
S )aM  Build pretrained InceptionV3

        Parameters
        ----------
        output_blocks : list of int
            Indices of blocks to return features of. Possible values are:
                - 0: corresponds to output of first max pooling
                - 1: corresponds to output of second max pooling
                - 2: corresponds to output which is fed to aux classifier
                - 3: corresponds to output of final average pooling
        resize_input : bool
            If true, bilinearly resizes input to width and height 299 before
            feeding input to model. As the network without fully connected
            layers is fully convolutional, it should be able to handle inputs
            of arbitrary size, so resizing might not be strictly needed
        normalize_input : bool
            If true, scales the input from range (0, 1) to the range the
            pretrained Inception network expects, namely (-1, 1)
        requires_grad : bool
            If true, parameters of the model require gradients. Possibly useful
            for finetuning the network
        use_fid_inception : bool
            If true, uses the pretrained Inception model used in Tensorflow's
            FID implementation. If false, uses the pretrained Inception model
            available in torchvision. The FID Inception model has different
            weights and a slightly different structure from torchvision's
            Inception model. If you want to compute FID scores, you are
            strongly advised to set this parameter to true to get comparable
            results.
        r   z%Last possible output block index is 3ÚDEFAULT)Úweightsr   )Úkernel_sizeÚstrider   )r   r   )Úoutput_sizeN)$Úsuperr   Ú__init__Úresize_inputÚnormalize_inputÚsortedÚoutput_blocksÚmaxÚlast_needed_blockÚnnÚ
ModuleListÚblocksÚfid_inception_v3Ú_inception_v3ÚConv2d_1a_3x3ÚConv2d_2a_3x3ÚConv2d_2b_3x3Ú	MaxPool2dÚappendÚ
SequentialÚConv2d_3b_1x1ÚConv2d_4a_3x3ÚMixed_5bÚMixed_5cÚMixed_5dÚMixed_6aÚMixed_6bÚMixed_6cÚMixed_6dÚMixed_6eÚMixed_7aÚMixed_7bÚMixed_7cÚAdaptiveAvgPool2dÚ
parametersÚrequires_grad)Úselfr   r   r   r4   Úuse_fid_inceptionÚ	inceptionÚblock0Úblock1Úblock2Úblock3Úparam©Ú	__class__© úO/home/ubuntu/.local/lib/python3.10/site-packages/cache_dit/metrics/inception.pyr   $   sV   &



ü
ý
ø


üÿzInceptionV3.__init__c                 C   sv   g }|}| j rtj|dddd}| jrd| d }t| jƒD ]\}}||ƒ}|| jv r0| |¡ || jkr8 |S q|S )ai  Get Inception feature maps

        Parameters
        ----------
        inp : torch.autograd.Variable
            Input tensor of shape Bx3xHxW. Values are expected to be in
            range (0, 1)

        Returns
        -------
        List of torch.autograd.Variable, corresponding to the selected output
        block, sorted ascending by index
        )é+  rA   ÚbilinearF)ÚsizeÚmodeÚalign_cornersr   r   )	r   ÚFÚinterpolater   Ú	enumerater   r   r#   r   )r5   ÚinpÚoutpÚxÚidxÚblockr?   r?   r@   Úforward‡   s   


ýzInceptionV3.forward)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_BLOCK_INDEXÚBLOCK_INDEX_BY_DIMr   rN   Ú__classcell__r?   r?   r=   r@   r      s    ü	úcr   c                  O   s´   zt tttj d¡dd… ƒƒ}W n ty   d}Y nw |dkr%d|d< |dk rQd	|v rQ|d	 d
kr8d|d< n|d	 du rCd|d< ntd |d	 tj¡ƒ‚|d	= tjj	| i |¤ŽS )z'Wraps `torchvision.models.inception_v3`Ú.Nr   )r   )r   é   FÚinit_weights)r   é   r   r   TÚ
pretrainedz+weights=={} not supported in torchvision {})
ÚtupleÚmapÚintÚtorchvisionÚ__version__ÚsplitÚ
ValueErrorÚformatÚmodelsÚinception_v3)ÚargsÚkwargsÚversionr?   r?   r@   r   ©   s&   "þ


ÿÿr   c                  C   sž   t dddd} tddd| _tdd	d| _td
d	d| _tddd| _tddd| _tddd| _tddd| _	t
dƒ| _tdƒ| _ttdd}|  |¡ | S )aa  Build pretrained Inception model for FID computation

    The Inception model for FID computation uses a different set of weights
    and has a slightly different structure than torchvision's Inception.

    This method first constructs torchvision's Inception and then patches the
    necessary parts that are different in the FID Inception model.
    ið  FN)Únum_classesÚ
aux_logitsr   r
   é    )Úpool_featuresé   r	   i   r   é€   )Úchannels_7x7é    i   r   )Úprogress)r   ÚFIDInceptionAr'   r(   r)   ÚFIDInceptionCr+   r,   r-   r.   ÚFIDInceptionE_1r0   ÚFIDInceptionE_2r1   r   ÚFID_WEIGHTS_URLÚload_state_dict)r7   Ú
state_dictr?   r?   r@   r   È   s   	


r   c                       ó(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rq   z,InceptionA block patched for FID computationc                    ó   t t| ƒ ||¡ d S ©N)r   rq   r   )r5   Úin_channelsrk   r=   r?   r@   r   ä   ó   zFIDInceptionA.__init__c                 C   sr   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}tj|ddddd}|  |¡}||||g}t	 
|d¡S ©Nr   r   F©r   r   ÚpaddingÚcount_include_pad)Ú	branch1x1Úbranch5x5_1Úbranch5x5_2Úbranch3x3dbl_1Úbranch3x3dbl_2Úbranch3x3dbl_3rF   Ú
avg_pool2dÚbranch_poolÚtorchÚcat)r5   rK   r   Ú	branch5x5Úbranch3x3dblrˆ   Úoutputsr?   r?   r@   rN   ç   s   






zFIDInceptionA.forward©rO   rP   rQ   rR   r   rN   rU   r?   r?   r=   r@   rq   á   ó    rq   c                       rx   )rr   z,InceptionC block patched for FID computationc                    ry   rz   )r   rr   r   )r5   r{   rn   r=   r?   r@   r   ý   r|   zFIDInceptionC.__init__c                 C   s   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}t	j
|ddddd}|  |¡}||||g}t |d¡S r}   )r   Úbranch7x7_1Úbranch7x7_2Úbranch7x7_3Úbranch7x7dbl_1Úbranch7x7dbl_2Úbranch7x7dbl_3Úbranch7x7dbl_4Úbranch7x7dbl_5rF   r‡   rˆ   r‰   rŠ   )r5   rK   r   Ú	branch7x7Úbranch7x7dblrˆ   r   r?   r?   r@   rN      s   









zFIDInceptionC.forwardrŽ   r?   r?   r=   r@   rr   ú   r   rr   c                       rx   )rs   z2First InceptionE block patched for FID computationc                    ó   t t| ƒ |¡ d S rz   )r   rs   r   ©r5   r{   r=   r?   r@   r     ó   zFIDInceptionE_1.__init__c                 C   sž   |   |¡}|  |¡}|  |¡|  |¡g}t |d¡}|  |¡}|  |¡}|  |¡|  	|¡g}t |d¡}t
j|ddddd}|  |¡}||||g}t |d¡S )Nr   r   Fr~   )r   Úbranch3x3_1Úbranch3x3_2aÚbranch3x3_2br‰   rŠ   r„   r…   Úbranch3x3dbl_3aÚbranch3x3dbl_3brF   r‡   rˆ   ©r5   rK   r   Ú	branch3x3rŒ   rˆ   r   r?   r?   r@   rN     s    

þ

þ
zFIDInceptionE_1.forwardrŽ   r?   r?   r=   r@   rs     r   rs   c                       rx   )rt   z3Second InceptionE block patched for FID computationc                    rš   rz   )r   rt   r   r›   r=   r?   r@   r   :  rœ   zFIDInceptionE_2.__init__c                 C   sœ   |   |¡}|  |¡}|  |¡|  |¡g}t |d¡}|  |¡}|  |¡}|  |¡|  	|¡g}t |d¡}t
j|dddd}|  |¡}||||g}t |d¡S )Nr   r   )r   r   r   )r   r   rž   rŸ   r‰   rŠ   r„   r…   r    r¡   rF   Ú
max_pool2drˆ   r¢   r?   r?   r@   rN   =  s    

þ

þ
zFIDInceptionE_2.forwardrŽ   r?   r?   r=   r@   rt   7  r   rt   )r‰   r^   Útorch.nnr   Útorch.nn.functionalÚ
functionalrF   Úcache_dit.loggerr   rO   ÚloggerÚtorchvision.models.utilsr   ÚImportErrorÚtorch.utils.model_zoor   ru   ÚModuler   r   r   rc   r7   Ú
InceptionArq   Ú
InceptionCrr   Ú
InceptionErs   rt   r?   r?   r?   r@   Ú<module>   s(    ÿ !