o
    *jC.                     @   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
 zddlmZ W n ey8   ddlmZ Y nw dZe
 ZG dd	 d	ejZd
d ZG dd dejjZG dd dejjZG dd dejjZG dd dejjZdS )zx
Part of the implementation is borrowed and modified from LaMa, publicly available at
https://github.com/saic-mdal/lama
    N)models)
get_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gddddf fd	d
	Zdd Z  ZS )InceptionV3z5Pretrained InceptionV3 network returning feature maps   r         )@            TFc                    sR  t t|   || _|| _t|| _t|| _| jdks J dt	
 | _|r+t }ntjdd}|j|j|jt	jdddg}| jt	j|  | jdkrb|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 3T)
pretrainedr	   )kernel_sizestrider   )r   r   )Zoutput_sizeN)%superr   __init__resize_inputnormalize_inputsortedoutput_blocksmaxlast_needed_blocknnZ
ModuleListblocksfid_inception_v3r   inception_v3ZConv2d_1a_3x3ZConv2d_2a_3x3ZConv2d_2b_3x3Z	MaxPool2dappendZ
SequentialZConv2d_3b_1x1ZConv2d_4a_3x3Mixed_5bMixed_5cMixed_5dZMixed_6aMixed_6bMixed_6cMixed_6dMixed_6eZMixed_7aMixed_7bMixed_7cZAdaptiveAvgPool2d
parametersrequires_grad)selfr   r   r   r(   Zuse_fid_inception	inceptionZblock0Zblock1Zblock2Zblock3param	__class__ x/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/image_inpainting/modules/inception.pyr   (   sR   $







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
        )+  r0   ZbilinearF)sizemodeZalign_cornersr	   r   )	r   FZinterpolater   	enumerater   r   r   r   )r)   inpZoutpxidxblockr.   r.   r/   forward   s"   


zInceptionV3.forward)	__name__
__module____qualname____doc__ZDEFAULT_BLOCK_INDEXZBLOCK_INDEX_BY_DIMr   r9   __classcell__r.   r.   r,   r/   r      s    ^r   c                  C   s   t d tjdddd} t 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 d ttdd}t d | | t 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.
    zfid_inception_v3 calledi  F)Znum_classesZ
aux_logitsr   zmodels.inception_v3 doner       )pool_features   r
   i   r      )channels_7x7   i   r   zfid_inception_v3 patching doneT)progressz#fid_inception_v3 weights downloadedz*fid_inception_v3 weights loaded into model)LOGGERinfor   r   FIDInceptionAr   r   r    FIDInceptionCr!   r"   r#   r$   FIDInceptionE_1r%   FIDInceptionE_2r&   r   FID_WEIGHTS_URLZload_state_dict)r*   Z
state_dictr.   r.   r/   r      s(   
	






r   c                       (   e Zd ZdZ fddZdd Z  ZS )rH   z,InceptionA block patched for FID computationc                       t t| || d S N)r   rH   r   )r)   in_channelsr@   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   paddingZcount_include_pad)	branch1x1Zbranch5x5_1Zbranch5x5_2branch3x3dbl_1branch3x3dbl_2Zbranch3x3dbl_3r3   
avg_pool2dbranch_pooltorchcat)r)   r6   rU   Z	branch5x5branch3x3dblrY   outputsr.   r.   r/   r9      s   







zFIDInceptionA.forwardr:   r;   r<   r=   r   r9   r>   r.   r.   r,   r/   rH          rH   c                       rM   )rI   z,InceptionC block patched for FID computationc                    rN   rO   )r   rI   r   )r)   rP   rC   r,   r.   r/   r      rQ   zFIDInceptionC.__init__c                 C   s   |  |}| |}| |}| |}| |}| |}| |}| |}| |}t	j
|ddddd}| |}||||g}t|dS rR   )rU   Zbranch7x7_1Zbranch7x7_2Zbranch7x7_3Zbranch7x7dbl_1Zbranch7x7dbl_2Zbranch7x7dbl_3Zbranch7x7dbl_4Zbranch7x7dbl_5r3   rX   rY   rZ   r[   )r)   r6   rU   Z	branch7x7Zbranch7x7dblrY   r]   r.   r.   r/   r9      s   










zFIDInceptionC.forwardr^   r.   r.   r,   r/   rI      r_   rI   c                       rM   )rJ   z2First InceptionE block patched for FID computationc                       t t| | d S rO   )r   rJ   r   r)   rP   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   FrS   )rU   branch3x3_1branch3x3_2abranch3x3_2brZ   r[   rV   rW   branch3x3dbl_3abranch3x3dbl_3br3   rX   rY   r)   r6   rU   Z	branch3x3r\   rY   r]   r.   r.   r/   r9     s$   





zFIDInceptionE_1.forwardr^   r.   r.   r,   r/   rJ     r_   rJ   c                       rM   )rK   z3Second InceptionE block patched for FID computationc                    r`   rO   )r   rK   r   ra   r,   r.   r/   r   '  rb   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   rT   )rU   rc   rd   re   rZ   r[   rV   rW   rf   rg   r3   Z
max_pool2drY   rh   r.   r.   r/   r9   *  s    




zFIDInceptionE_2.forwardr^   r.   r.   r,   r/   rK   $  r_   rK   )r=   rZ   Ztorch.nnr   Ztorch.nn.functionalZ
functionalr3   Ztorchvisionr   Zmodelscope.utils.loggerr   Ztorchvision.models.utilsr   ImportErrorZtorch.utils.model_zoor   rL   rF   Moduler   r   r*   Z
InceptionArH   Z
InceptionCrI   Z
InceptionErJ   rK   r.   r.   r.   r/   <module>   s(     ""