U
    (d(                     @   s  d dl Z d dlmZmZmZmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZmZ ddlmZmZ ddlmZmZ dd	lmZmZ G d
d dejZeddd fdejddddeee edejf eeee  ee edddZd#ej eeee  ee eedejf  edddZ!e"ee eeedddZ#eddd fdejddddeee e"edejf eeee  ee ejdddZ$d$eej%ej&f e"eeee  ee eedejf  ejd d!d"Z'dS )%    N)CallableDictListOptionalUnion)nnTensor)misc)ExtraFPNBlockFeaturePyramidNetworkLastLevelMaxPool   )	mobilenetresnet)WeightsEnum_get_enum_from_fn)IntermediateLayerGetterhandle_legacy_interfacec                
       sl   e Zd ZdZd
ejeeef ee	 e	e
e e
edejf  dd fddZeeeef ddd	Z  ZS )BackboneWithFPNa  
    Adds a FPN on top of a model.
    Internally, it uses torchvision.models._utils.IntermediateLayerGetter to
    extract a submodel that returns the feature maps specified in return_layers.
    The same limitations of IntermediateLayerGetter apply here.
    Args:
        backbone (nn.Module)
        return_layers (Dict[name, new_name]): a dict containing the names
            of the modules for which the activations will be returned as
            the key of the dict, and the value of the dict is the name
            of the returned activation (which the user can specify).
        in_channels_list (List[int]): number of channels for each feature map
            that is returned, in the order they are present in the OrderedDict
        out_channels (int): number of channels in the FPN.
        norm_layer (callable, optional): Module specifying the normalization layer to use. Default: None
    Attributes:
        out_channels (int): the number of channels in the FPN
    N.)backbonereturn_layersin_channels_listout_channelsextra_blocks
norm_layerreturnc                    sB   t    |d krt }t||d| _t||||d| _|| _d S )N)r   )r   r   r   r   )super__init__r   r   bodyr   fpnr   )selfr   r   r   r   r   r   	__class__ O/tmp/pip-unpacked-wheel-vx7f76es/torchvision/models/detection/backbone_utils.pyr   !   s    	
zBackboneWithFPN.__init__)xr   c                 C   s   |  |}| |}|S )N)r   r   )r    r%   r#   r#   r$   forward8   s    

zBackboneWithFPN.forward)NN)__name__
__module____qualname____doc__r   Moduler   strr   intr   r
   r   r   r   r&   __classcell__r#   r#   r!   r$   r      s     
r   Z
pretrainedc                 C   s   t tj| d  dS Nbackbone_nameZIMAGENET1K_V1)r   r   __dict__from_strkwargsr#   r#   r$   <lambda>A       r5   )weights   )r   trainable_layersreturned_layersr   .)r0   r7   r   r9   r:   r   r   c                 C   s    t j|  ||d}t||||S )a  
    Constructs a specified ResNet backbone with FPN on top. Freezes the specified number of layers in the backbone.

    Examples::

        >>> from torchvision.models.detection.backbone_utils import resnet_fpn_backbone
        >>> backbone = resnet_fpn_backbone('resnet50', weights=ResNet50_Weights.DEFAULT, trainable_layers=3)
        >>> # get some dummy image
        >>> x = torch.rand(1,3,64,64)
        >>> # compute the output
        >>> output = backbone(x)
        >>> print([(k, v.shape) for k, v in output.items()])
        >>> # returns
        >>>   [('0', torch.Size([1, 256, 16, 16])),
        >>>    ('1', torch.Size([1, 256, 8, 8])),
        >>>    ('2', torch.Size([1, 256, 4, 4])),
        >>>    ('3', torch.Size([1, 256, 2, 2])),
        >>>    ('pool', torch.Size([1, 256, 1, 1]))]

    Args:
        backbone_name (string): resnet architecture. Possible values are 'resnet18', 'resnet34', 'resnet50',
             'resnet101', 'resnet152', 'resnext50_32x4d', 'resnext101_32x8d', 'wide_resnet50_2', 'wide_resnet101_2'
        weights (WeightsEnum, optional): The pretrained weights for the model
        norm_layer (callable): it is recommended to use the default value. For details visit:
            (https://github.com/facebookresearch/maskrcnn-benchmark/issues/267)
        trainable_layers (int): number of trainable (not frozen) layers starting from final block.
            Valid values are between 0 and 5, with 5 meaning all backbone layers are trainable.
        returned_layers (list of int): The layers of the network to return. Each entry must be in ``[1, 4]``.
            By default all layers are returned.
        extra_blocks (ExtraFPNBlock or None): if provided, extra operations will
            be performed. It is expected to take the fpn features, the original
            features and the names of the original features as input, and returns
            a new list of feature maps and their corresponding names. By
            default a ``LastLevelMaxPool`` is used.
    r7   r   )r   r1   _resnet_fpn_extractor)r0   r7   r   r9   r:   r   r   r#   r#   r$   resnet_fpn_backbone>   s    2r=   )r   r9   r:   r   r   r   c           
         s  |dk s|dkrt d| dddddgd | }|dkrF|d	 |  D ](\}tfd
d|D rN|d qN|d krt }|d krddddg}t|dkst|dkrt d| dd t|D }| j	d   fdd|D }d}	t
| |||	||dS )Nr      z3Trainable layers should be in the range [0,5], got Zlayer4Zlayer3Zlayer2Zlayer1Zconv1Zbn1c                    s   g | ]}  | qS r#   )
startswith).0layer)namer#   r$   
<listcomp>   s     z)_resnet_fpn_extractor.<locals>.<listcomp>F   r   r8      z6Each returned layer should be in the range [1,4]. Got c                 S   s    i | ]\}}d | t |qS )rA   r,   r@   vkr#   r#   r$   
<dictcomp>   s      z)_resnet_fpn_extractor.<locals>.<dictcomp>   c                    s   g | ]} d |d   qS )r   rD   r#   r@   i)in_channels_stage2r#   r$   rC      s        r   r   )
ValueErrorappendZnamed_parametersallrequires_grad_r   minmax	enumerateZinplanesr   )
r   r9   r:   r   r   Zlayers_to_train	parameterr   r   r   r#   )rN   rB   r$   r<   t   s4    	

     r<   )
is_trainedtrainable_backbone_layers	max_valuedefault_valuer   c                 C   sX   | s"|d k	rt d| d |}|d kr.|}|dk s>||krTtd| d| d|S )NzChanging trainable_backbone_layers has not effect if neither pretrained nor pretrained_backbone have been set to True, falling back to trainable_backbone_layers=z! so that all layers are trainabler   z4Trainable backbone layers should be in the range [0,], got  )warningswarnrQ   )rY   rZ   r[   r\   r#   r#   r$   _validate_trainable_layers   s    
ra   c                 C   s   t tj| d  dS r/   )r   r   r1   r2   r3   r#   r#   r$   r5      r6   )r0   r7   r   r   r9   r:   r   r   c                 C   s"   t j|  ||d}t|||||S )Nr;   )r   r1   _mobilenet_extractor)r0   r7   r   r   r9   r:   r   r   r#   r#   r$   mobilenet_backbone   s    rc   )r   r   r9   r:   r   r   r   c                    sl   j  dgdd t D  t d g t}|dk sD||krZtd| d| d|dkrjt n
||  } d | D ]}| D ]}	|	d qqd	}
|rB|d krt }|d kr|d
 |d g}t|dk st||krtd|d  d| dfddt|D } fdd|D }t	 |||
||dS t
 t
 d j|
d}|
|_|S d S )Nr   c                 S   s    g | ]\}}t |d dr|qS )Z_is_cnF)getattr)r@   rM   br#   r#   r$   rC      s      z(_mobilenet_extractor.<locals>.<listcomp>rD   z+Trainable layers should be in the range [0,r]   r^   FrO   r   z.Each returned layer should be in the range [0,c                    s    i | ]\}} |  t |qS r#   rF   rG   )stage_indicesr#   r$   rJ      s      z(_mobilenet_extractor.<locals>.<dictcomp>c                    s   g | ]} |  j qS r#   )r   rL   r   rf   r#   r$   rC      s     rP   )featuresrW   lenrQ   
parametersrT   r   rU   rV   r   r   Z
SequentialZConv2dr   )r   r   r9   r:   r   r   Z
num_stagesZfreeze_beforere   rX   r   r   r   mr#   rg   r$   rb      sB    &     rb   )NNN)NNN)(r_   typingr   r   r   r   r   Ztorchr   r   Ztorchvision.opsr	   Zmisc_nn_opsZ'torchvision.ops.feature_pyramid_networkr
   r   r    r   r   Z_apir   r   _utilsr   r   r+   r   ZFrozenBatchNorm2dr,   r-   r=   ZResNetr<   boolra   rc   ZMobileNetV2ZMobileNetV3rb   r#   r#   r#   r$   <module>   s   1

3   
$
   
