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 ]}||_#qd
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__ Q/home/ubuntu/vllm_env/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(     !