Team Ai
Apppublic

xdecoder/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
163likes
resnet.cpython-38.pyc273 linesDownload Raw Back to __pycache__
1U

2E$�c$a�@s4ddlZddlZddlmZmZddlmmZddl	Z	ddl3mmZddl	mZddl
mZddlmZddlmZmZmZmZmZmZddlmZd	d4ddd
dddgZGdd5�d6e�ZGdd�de�ZGdd�de�ZGdd
�d
e�ZGdd�de�ZeZ dd�Z!ee"efdd�dd�Z#edd��Z$dS)�N)�Any�Dict)�nn�)�Backbone)�register_backbone)�CNNBlockBase�Conv2d�7DeformConv�ModulatedDeformConv�	ShapeSpec�get_norm)�PathManager�ResNetBlockBase�8BasicBlock�BottleneckBlock�DeformBottleneckBlock�	BasicStem�ResNet�9make_stage�get_resnet_backbonecs0eZdZdZddd��fdd�10Zdd�Z�ZS)	rz�11    The basic residual block for ResNet-18 and ResNet-34 defined in :paper:`ResNet`,12    with two 3x3 conv layers and a projection shortcut if needed.13    r�BN)�stride�normc14s�t��|||�||kr6t||d|dt||�d�|_nd|_t||d|ddt||�d�|_t||ddddt||�d�|_|j|j|jfD]}|dk	r�t�|�q�dS)aI15        Args:16            in_channels (int): Number of input channels.17            out_channels (int): Number of output channels.18            stride (int): Stride for the first conv.19            norm (str or callable): normalization for all conv layers.20                See :func:`layers.get_norm` for supported format.21        rF��kernel_sizer�biasrN��rr�paddingrr)	�super�__init__r	r
�shortcut�conv1�conv2�weight_init�c2_msra_fill)�self�in_channels�out_channelsrr�layer��	__class__��1/data/arXiv/demo/Demo/xdecoder/backbone/resnet.pyr!*s@	�22	�23�24zBasicBlock.__init__cCsN|�|�}t�|�}|�|�}|jdk	r4|�|�}n|}||7}t�|�}|S�N)r#�F�relu_r$r"�r'�x�outr"r-r-r.�forwardYs2526272829zBasicBlock.forward��__name__�30__module__�__qualname__�__doc__r!r5�
__classcell__r-r-r+r.r$s/cs6eZdZdZdddddd��fdd�31Zdd	�Z�ZS)32rz�33    The standard bottleneck residual block used by ResNet-50, 101 and 15234    defined in :paper:`ResNet`.  It contains 3 conv layers with kernels35    1x1, 3x3, 1x1, and a projection shortcut if needed.36    rrF)r�37num_groupsr�
stride_in_1x1�dilationcs�t��|||�||kr6t||d|dt||�d�|_nd|_|rH|dfnd|f\}	}38t||d|	dt||�d�|_t||d|39d|d||t||�d�	|_t||ddt||�d�|_|j|j|j|jfD]}|dk	r�t�	|�q�dS)a:40        Args:41            bottleneck_channels (int): number of output channels for the 3x342                "bottleneck" conv layers.43            num_groups (int): number of groups for the 3x3 conv layer.44            norm (str or callable): normalization for all conv layers.45                See :func:`layers.get_norm` for supported format.46            stride_in_1x1 (bool): when stride>1, whether to put stride in the47                first 1x1 convolution or the bottleneck 3x3 convolution.48            dilation (int): the dilation rate of the 3x3 conv layer.49        rFrNr)rrrr�groupsr>r�rrr)50r r!r	r
r"r#r$�conv3r%r&)r'r(r)�bottleneck_channelsrr<rr=r>�51stride_1x1�52stride_3x3r*r+r-r.r!osR�53	�	��zBottleneckBlock.__init__cCsb|�|�}t�|�}|�|�}t�|�}|�|�}|jdk	rH|�|�}n|}||7}t�|�}|Sr/)r#r0r1r$rAr"r2r-r-r.r5�s54555657585960zBottleneckBlock.forwardr6r-r-r+r.rhs�Wcs:eZdZdZdddddddd��fdd�61Zdd	�Z�ZS)62rzy63    Similar to :class:`BottleneckBlock`, but with :paper:`deformable conv <deformconv>`64    in the 3x3 convolution.65    rrF)rr<rr=r>�deform_modulated�deform_num_groupsc
sDt��|||�|	|_||kr<t||d|dt||�d�|_nd|_|rN|dfnd|f\}}t||d|dt||�d�|_|	r�t}
d}nt}
d}t|||66d|d||d�|_	|
||d|d|d|||67t||�d�68|_69t||ddt||�d	�|_|j|j70|j|jfD]}|dk	�rt�
|��qtj�|j	jd71�tj�|j	jd72�dS)NrFr��r)rrrr>)rrrrr?r>�deformable_groupsrr@r)r r!rEr	r
r"r#rr73�conv2_offsetr$rAr%r&r�init�	constant_�weightr)r'r(r)rBrr<rr=r>rErFrCrD�deform_conv_op�offset_channelsr*r+r-r.r!�st�74	�	��
�75zDeformBottleneckBlock.__init__c	Cs�|�|�}t�|�}|jrd|�|�}tj|ddd�\}}}tj||fdd�}|��}|�	|||�}n|�|�}|�	||�}t�|�}|�76|�}|jdk	r�|�|�}n|}||7}t�|�}|S)Nrr)�dim)r#r0r1rErJ�torch�chunk�cat�sigmoidr$rAr")	r'r3r4�offset_mask�offset_x�offset_y�mask�offsetr"r-r-r.r53s$7778798081828384zDeformBottleneckBlock.forwardr6r-r-r+r.r�s�Tcs*eZdZdZd	�fdd�	Zdd�Z�ZS)85rzp86    The standard ResNet stem (layers before the first residual block),87    with a conv, relu and max_pool.88    r�@rc89sDt��||d�||_t||ddddt||�d�|_t�|j�dS)z�90        Args:91            norm (str or callable): norm after the first conv layer.92                See :func:`layers.get_norm` for supported format.93        ���rFrN)r r!r(r	r
r#r%r&)r'r(r)rr+r-r.r!Ts�	zBasicStem.__init__cCs*|�|�}t�|�}tj|dddd�}|S)Nrr]r)rrr)r#r0r1�94max_pool2d)r'r3r-r-r.r5gs9596zBasicStem.forward)rrZrr6r-r-r+r.rNscsVeZdZdZd�fdd�	Zdd�Zdd	�Zdd97d�Zedd
��Z	eddd��Z98�ZS)rz$99    Implement :paper:`ResNet`.100    Nrcs�t���||_||_|jj}d|i|_d|jji|_gg|_|_	|dk	rjt101dd�|D��}|d|�}t|�D]�\}}	t|	�dks�t
t|	���|	D]}102t|103t�s�t
|104��q�dt|d�}tj|	�}|�||�|j�|�|j	�|�t|t�dd�|	D���|j|<}|	d	j|j|<}
qrt|j�|_|dk	�rpt�d105�|_t�|
|�|_tjj|jjdd�d
}|dk�r�|g}||_ t|j ��s�t
�dd�|�!�D�}|j D]$}||k�s�t
d�"d�#|�����q�|�$|�dS)a�106        Args:107            stem (nn.Module): a stem module108            stages (list[list[CNNBlockBase]]): several (typically 4) stages,109                each contains multiple :class:`CNNBlockBase`.110            num_classes (None or int): if None, will not perform classification.111                Otherwise, will create a linear layer.112            out_features (list[str]): name of the layers whose outputs should113                be returned in forward. Can be anything in "stem", "linear", or "res2" ...114                If None, will return the output of the last layer.115            freeze_at (int): The number of stages at the beginning to freeze.116                see :meth:`freeze` for detailed explanation.117        �stemNcSs"g|]}ddddd��|d��qS)rr]rr[)�res2�res3�res4�res5r)�get)�.0�fr-r-r.�118<listcomp>�sz#ResNet.__init__.<locals>.<listcomp>r�resr]cSsg|]119}|j�qSr-)r)re�kr-r-r.rg�s�����)rrg{�G�z�?)�std�linearcSsg|]}|d�qS)rr-)rer3r-r-r.rg�szAvailable children: {}z, )%r r!r_�num_classesr�_out_feature_stridesr)�_out_feature_channels�stage_names�stages�max�	enumerate�len�AssertionError�120isinstancer�strr�121Sequential�122add_module�append�int�np�prod�tuple�AdaptiveAvgPool2d�avgpool�LinearrlrK�normal_rM�
_out_features�named_children�format�join�freeze)r'r_rqrm�out_features�	freeze_at�current_stride�123num_stages�i�blocks�block�name�stage�
curr_channels�children�out_featurer+r-r.r!ssN124125�126�127128129"zResNet.__init__cCs�|��dkstd|j�d���i}|�|�}d|jkr>||d<t|j|j�D]"\}}||�}||jkrL|||<qL|jdk	r�|�	|�}t130�|d�}|�|�}d|jkr�||d<|S)z�131        Args:132            x: Tensor of shape (N,C,H,W). H, W must be a multiple of ``self.size_divisibility``.133 134        Returns:135            dict[str->Tensor]: names and the corresponding features136        r[z1ResNet takes an input of shape (N, C, H, W). Got z	 instead!r_Nrrl)
rPru�shaper_r��ziprprqrmr�rQ�flattenrl)r'r3�outputsr�r�r-r-r.r5�s 137138139140141142143144zResNet.forwardcs�fdd��jD�S)Ncs&i|]}|t�j|�j|d��qS))�channelsr)rrorn)rer��r'r-r.�145<dictcomp>�s��z'ResNet.output_shape.<locals>.<dictcomp>)r�r�r-r�r.�output_shape�s146�zResNet.output_shapecCsL|dkr|j��t|jdd�D]&\}}||kr |��D]}|��q8q |S)a�147        Freeze the first several stages of the ResNet. Commonly used in148        fine-tuning.149 150        Layers that produce the same feature map spatial size are defined as one151        "stage" by :paper:`FPN`.152 153        Args:154            freeze_at (int): number of stages to freeze.155                `1` means freezing the stem. `2` means freezing the stem and156                one residual stage, etc.157 158        Returns:159            nn.Module: this ResNet itself160        rr])�start)r_r�rsrqr�)r'r��idxr�r�r-r-r.r��s161z
ResNet.freezecKs�g}t|�D]�}i}|��D]z\}}	|�d�r�t|	�|ksPtd|�d|�d���|dtd��}162|163|ks�td|�d|164�d���|	|||165<q|	||<q|�|f||d	�|���|}q|S)166a�167        Create a list of blocks of the same type that forms one ResNet stage.168 169        Args:170            block_class (type): a subclass of CNNBlockBase that's used to create all blocks in this171                stage. A module of this type must not change spatial resolution of inputs unless its172                stride != 1.173            num_blocks (int): number of blocks in this stage174            in_channels (int): input channels of the entire stage.175            out_channels (int): output channels of **every block** in the stage.176            kwargs: other arguments passed to the constructor of177                `block_class`. If the argument name is "xx_per_block", the178                argument is a list of values to be passed to each block in the179                stage. Otherwise, the same argument is passed to every block180                in the stage.181 182        Returns:183            list[CNNBlockBase]: a list of block module.184 185        Examples:186        ::187            stage = ResNet.make_stage(188                BottleneckBlock, 3, in_channels=16, out_channels=64,189                bottleneck_channels=16, num_groups=1,190                stride_per_block=[2, 1, 1],191                dilations_per_block=[1, 1, 2]192            )193 194        Usually, layers that produce the same feature map spatial size are defined as one195        "stage" (in :paper:`FPN`). Under such definition, ``stride_per_block[1:]`` should196        all be 1.197        �198_per_blockz199Argument 'z:' of make_stage should have the same length as num_blocks=�.Nz!Cannot call make_stage with both z and �!)r(r))�range�items�endswithrtrurz)�block_class�200num_blocksr(r)�kwargsr�r��curr_kwargsri�v�newkr-r-r.r�s""201�202�zResNet.make_stagecKsddddgddddgddddgddddgddddgd�|}|d	krT|d203krPtnt}|d204krvdddd
g}ddd
dg}ndd
ddg}d
dddg}g}t|ddddg||�D]R\}}}	}205|d206kr�|207d|d<|�tjf|||gdg|d|	|208d�|���q�|S)aA209        Created list of ResNet stages from pre-defined depth (one of 18, 34, 50, 101, 152).210        If it doesn't create the ResNet variant you need, please use :meth:`make_stage`211        instead for fine-grained customization.212 213        Args:214            depth (int): depth of ResNet215            block_class (type): the CNN block class. Has to accept216                `bottleneck_channels` argument for depth > 50.217                By default it is BasicBlock or BottleneckBlock, based on the218                depth.219            kwargs:220                other arguments to pass to `make_stage`. Should not contain221                stride and channels, as they are predefined for each depth.222 223        Returns:224            list[list[CNNBlockBase]]: modules in all stages; see arguments of225                :class:`ResNet.__init__`.226        r]rr[����$�rH�"�2�e�Nr�rZ��iiirrB)r�r��stride_per_blockr(r))rrr�rzrr)�depthr�r��num_blocks_per_stager(r)�ret�n�sr��or-r-r.�make_default_stages's>227228229230231��"���232zResNet.make_default_stages)NNr)r)N)r7r8r9r:r!r5r�r��staticmethodrr�r;r-r-r+r.rnsD2332346cOstj||�S)z6235    Deprecated alias for backward compatibiltiy.236    )rr)�argsr�r-r-r.rbs)�237state_dict�returncCsdt|���D]R}||}t|tj�sDt|tj�sDtd�|t	|����t|tj�st�238|�||<qdS)z�239    In-place convert all numpy arrays in the state_dict to torch tensor.240    Args:241        state_dict (dict): a state-dict to be loaded to the model.242            Will be modified.243    z,Unsupported type found in checkpoint! {}: {}N)�list�keysrvr|�ndarrayrQ�Tensor�244ValueErrorr��type�245from_numpy)r�rir�r-r-r.�_convert_ndarray_to_tensoris246�r�c	Cs�|ddd}|d}t|d|d|d�}|d}|d	}|d247}|d}|d}||}	|d}248|d
}|d}|d}
|d}|d}|d}|
dks�td�|
���ddddgddddgddddgddddgddddgd�|}|dk�r:|dk�std��t|��rtd ��|
d!k�s(td"��|d!k�s:td#��g}ttdd��D]�\}}|d$k�rb|
nd!}|d%k�s�|d$k�r�|dk�r�d!nd}|||gd!g||d!|249||d&�}|dk�r�t|d'<nL|	|d(<||d)<||d*<||d+<||�rt|d'<||d,<||d-<nt|d'<t	j250f|�}|}251|d9}|	d9}	|�|��qLt	||||d.�}|ddd/d0k�r�|ddd1}t�
|d2��}tj|d3d4�d5}W5QRXt|�|�d6�|�d7�|�|�|S)8zi252    Create a ResNet instance from config.253 254    Returns:255        ResNet: a :class:`ResNet` instance.256    �MODEL�BACKBONE�RESNETS�NORMZSTEM_IN_CHANNELS�STEM_OUT_CHANNELS)r(r)r�	FREEZE_AT�OUT_FEATURES�DEPTH�257NUM_GROUPS�WIDTH_PER_GROUP�RES2_OUT_CHANNELS�
STRIDE_IN_1X1�
RES5_DILATION�DEFORM_ON_PER_STAGE�DEFORM_MODULATED�DEFORM_NUM_GROUPS>rr]zres5_dilation cannot be {}.r]rr[r�r�r�r�r�)rHr�rZz9Must set MODEL.RESNETS.RES2_OUT_CHANNELS = 64 for R18/R34z9MODEL.RESNETS.DEFORM_ON_PER_STAGE unsupported for R18/R34rz4Must set MODEL.RESNETS.RES5_DILATION = 1 for R18/R34z1Must set MODEL.RESNETS.NUM_GROUPS = 1 for R18/R34�r)r�r�r(r)rr�rBr=r>r<rErF)r�r�ZLOAD_PRETRAINEDTZ258PRETRAINED�rb�latin1)�encoding�modelzstem.fc.weightzstem.fc.bias)rrur��anyrsr�rrrrrrzr�open�pickle�loadr��pop�load_state_dict)�cfgZres_cfgrr_r�r�r�r<�width_per_grouprBr(r)r=�
res5_dilation�deform_on_per_stagerErFr�rqr��	stage_idxr>�first_stride�stage_kargsr��backbone�filenamerfZckptr-r-r.r}s��259260261262263��264��&�265266267268269270271)%r��numpyr|�typingrrZfvcore.nn.weight_initrr%rQZtorch.nn.functional�272functionalr0r�r�registryr�detectron2.layersrr	r273rrr
�detectron2.utils.file_ior�__all__rrrrrrrrwr�rr-r-r-r.�<module>s< �Dqu o