o
    ^{jEH  ã                   @   sú   d dl Z d dlmZ d dlm  mZ d'dd„Zd(dd„ZG dd„ dejƒZ	G d	d
„ d
ejƒZ
G dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdd„ Zd)dd„Zd)dd„Zd)dd„Zd)dd„Zd)dd„Zd)dd „Zd)d!d"„Zd)d#d$„Zd)d%d&„ZdS )*é    Né   c              
   C   s   t j| |d|||d|d�S )z3x3 convolution with paddingé   F)Úkernel_sizeÚstrideÚpaddingÚgroupsÚbiasÚdilation©ÚnnÚConv2d)Ú	in_planesÚ
out_planesr   r   r	   © r   ú,/root/disk_3tb/HAC/VGGSound/models/resnet.pyÚconv3x3   s   ÿr   c                 C   s   t j| |d|dd�S )z1x1 convolutionr   F)r   r   r   r
   )r   r   r   r   r   r   Úconv1x1   s   r   c                       s.   e Zd ZdZ		d‡ fdd„	Zdd„ Z‡  ZS )	Ú
BasicBlockr   Né@   c	           	         s�   t t| ƒ ¡  |d u rtj}|dks|dkrtdƒ‚|dkr"tdƒ‚t|||ƒ| _||ƒ| _	tj
dd�| _t||ƒ| _||ƒ| _|| _|| _d S )Nr   r   z3BasicBlock only supports groups=1 and base_width=64z(Dilation > 1 not supported in BasicBlockT©Úinplace)Úsuperr   Ú__init__r   ÚBatchNorm2dÚ
ValueErrorÚNotImplementedErrorr   Úconv1Úbn1ÚReLUÚreluÚconv2Úbn2Ú
downsampler   )	ÚselfÚinplanesÚplanesr   r"   r   Ú
base_widthr	   Ú
norm_layer©Ú	__class__r   r   r      s   


zBasicBlock.__init__c                 C   s`   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}| jd ur%|  |¡}||7 }|  |¡}|S ©N)r   r   r   r    r!   r"   ©r#   ÚxÚidentityÚoutr   r   r   Úforward'   s   







zBasicBlock.forward©r   Nr   r   r   N©Ú__name__Ú
__module__Ú__qualname__Ú	expansionr   r/   Ú__classcell__r   r   r(   r   r      s    ÿr   c                	       sF   e Zd Zeg d¢dddddddf	‡ fdd	„	Zdd
d„Zdd„ Z‡  ZS )ÚAudioAttGenModule©é   r9   r9   r9   éè  ÚavgpoolFr   r   Nc
                    s˜  t t| ƒ ¡  || _|	d u rtj}	|	| _d| _d| _|d u r"g d¢}t	|ƒdkr/t
d |¡ƒ‚|| _|| _| j|d|d d|d d�| _| jd	kr\t d
¡| _t d|j d |¡| _n| jdkrotƒ | _t d|j |¡| _|  ¡ D ].}
t|
tjƒr†tjj|
jddd� qst|
tjtjfƒr¡tjj|
jddd� tj |
j d¡ qs|rÈ|  ¡ D ]!}
t|
t!ƒr¹tj |
j"jd¡ q¨t|
t#ƒrÇtj |
j$jd¡ q¨d S d S )Né   r   ©FFFr   úHreplace_stride_with_dilation should be None or a 3-element tuple, got {}é   r9   ©r   Údilater;   ©r   r   é€   Úvladé    Úfan_outr   ©ÚmodeÚnonlinearityç{®Gáz”?©ÚmeanÚstdr   )%r   r7   r   Úpoolr   r   Ú_norm_layerr$   r	   Úlenr   Úformatr   r&   Ú_make_layerÚlayer4ÚAdaptiveAvgPool2dr;   ÚLinearr5   ÚfcÚNetVLADÚfc_ÚmodulesÚ
isinstancer   ÚinitÚkaiming_normal_ÚweightÚ	GroupNormÚnormal_Ú	constant_r   Ú
BottleneckÚbn3r   r!   ©r#   ÚblockÚlayersÚnum_classesrN   Úzero_init_residualr   Úwidth_per_groupÚreplace_stride_with_dilationr'   Úmr(   r   r   r   <   sP   ÿÿ

	€

€ûzAudioAttGenModule.__init__c                 C   óÒ   | j }d }| j}|r|  j|9  _d}|dks| j||j kr2t t| j||j |ƒ|||j ƒ¡}g }	|	 || j|||| j| j	||ƒ¡ ||j | _t
d|ƒD ]}
|	 || j|| j| j	| j|d�¡ qPtj|	Ž S ©Nr   )r   r&   r	   r'   ©rO   r	   r$   r5   r   Ú
Sequentialr   Úappendr   r&   Úrange©r#   rd   r%   Úblocksr   rA   r'   r"   Úprevious_dilationre   Ú_r   r   r   rR   s   s,   þÿ
þ
zAudioAttGenModule._make_layerc                 C   sB   |   |¡}|  |¡}| | d¡d¡}| jdkr|  |¡}||fS )Nr   éÿÿÿÿr;   )rS   r;   ÚreshapeÚsizerN   rV   ©r#   r,   Úx_r   r   r   r/   ‹   s   



zAudioAttGenModule.forward©r   F)r2   r3   r4   r   r   rR   r/   r6   r   r   r(   r   r7   :   s    þ
7r7   c                       s>   e Zd Z			d‡ fdd„	Zdd	d
„Zdd„ Zdd„ Z‡  ZS )ÚResNetr:   r;   Fr   r   Nc
                    s0  t t| ƒ ¡  || _|	d u rtj}	|	| _d| _d| _|d u r"g d¢}t	|ƒdkr/t
d |¡ƒ‚|| _|| _tjd| jddddd	�| _|	| jƒ| _tjd
d�| _tjdddd�| _|  |d|d ¡| _| j|d|d d|d d�| _| j|d|d d|d d�| _| j|d|d d|d d�| _| jdkr¥t d¡| _t d|j |¡| _n| jdkr¸tƒ | _t d|j |¡| _ |  !¡ D ].}
t"|
tjƒrÏtj#j$|
j%ddd� q¼t"|
tjtj&fƒrêtj#j'|
j%ddd� tj# (|
j)d¡ q¼|�r|  !¡ D ]#}
t"|
t*ƒ�rtj# (|
j+j%d¡ qòt"|
t,ƒ�rtj# (|
j-j%d¡ qòd S d S )Nr   r   r=   r   r>   é   r9   F)r   r   r   r   Tr   )r   r   r   r   rC   r@   r<   r?   r;   rB   rD   rE   rF   r   rG   rJ   rK   ).r   r{   r   rN   r   r   rO   r$   r	   rP   r   rQ   r   r&   r   r   r   r   r   Ú	MaxPool2dÚmaxpoolrR   Úlayer1Úlayer2Úlayer3rS   rT   r;   rU   r5   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   r   ra   rb   r   r!   rc   r(   r   r   r   ™   sj   ÿÿÿÿÿ

€€ûzResNet.__init__c                 C   rk   rl   rm   rq   r   r   r   rR   Ó   s,   þÿ
þ
zResNet._make_layerc                 C   s    |   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}| 	| 
d¡d¡}| jdkrA|  |¡}n
| jdkrK|  |¡}|||fS )Nr   ru   r;   rD   )r   r   r   r~   r   r€   r�   rS   r;   rv   rw   rN   rV   rX   )r#   r,   ry   Úx1r   r   r   r/   ì   s   












zResNet.forwardc                 C   s8   |   |¡}|  |¡}| | d¡d¡}|  |¡}||fS )Nr   ru   )rS   r;   rv   rw   rV   rx   r   r   r   Úget_predictÿ   s
   


zResNet.get_predict)r:   r;   Fr   r   NNrz   )r2   r3   r4   r   rR   r/   rƒ   r6   r   r   r(   r   r{   —   s    þ
:r{   c                       s6   e Zd ZdZ		d‡ fdd„	Zdd	„ Zd
d„ Z‡  ZS )rW   zNetVLAD layer implementationé   r?   ç      Y@Tc                    sZ   t t| ƒ ¡  || _|| _|| _|| _tj||ddd�| _	t 
t ||¡¡| _|  ¡  dS )ax  
        Args:
            num_clusters : int
                The number of clusters
            dim : int
                Dimension of descriptors
            alpha : float
                Parameter of initialization. Larger value is harder assignment.
            normalize_input : bool
                If true, descriptor-wise L2 normalization is applied to input.
        rB   T)r   r   N)r   rW   r   Únum_clustersÚdimÚalphaÚnormalize_inputr   r   ÚconvÚ	ParameterÚtorchÚrandÚ	centroidsÚ_init_params)r#   r†   r‡   rˆ   r‰   r(   r   r   r   	  s   zNetVLAD.__init__c                 C   sJ   t  d| j | j  d¡ d¡¡| j_t  | j | jjdd� ¡| j_d S )Ng       @ru   r   ©r‡   )	r   r‹   rˆ   rŽ   Ú	unsqueezerŠ   r]   Únormr   )r#   r   r   r   r�     s   ÿÿzNetVLAD._init_paramsc                 C   sî   |j d d… \}}| jrtj|ddd�}|  |¡ || jd¡}tj|dd�}| ||d¡}| | jddd¡ 	dddd¡| j
 | d¡dd¡ 	ddd¡ d¡ }|| d¡9 }|jdd�}tj|ddd�}| | d¡d¡}tj|ddd�}|S )Nr9   r   )Úpr‡   ru   r�   r   r   )Úshaper‰   ÚFÚ	normalizerŠ   Úviewr†   ÚsoftmaxÚexpandÚpermuterŽ   rw   r‘   Úsum)r#   r,   ÚNÚCZsoft_assignZ	x_flattenÚresidualrD   r   r   r   r/   '  s   $ÿzNetVLAD.forward)r„   r?   r…   T)r2   r3   r4   Ú__doc__r   r�   r/   r6   r   r   r(   r   rW     s    ÿrW   c                       s.   e Zd ZdZ		d	‡ fdd„	Zdd„ Z‡  ZS )
ra   é   r   Nr   c	           
         s¢   t t| ƒ ¡  |d u rtj}t||d  ƒ| }	t||	ƒ| _||	ƒ| _t	|	|	|||ƒ| _
||	ƒ| _t|	|| j ƒ| _||| j ƒ| _tjdd�| _|| _|| _d S )Ng      P@Tr   )r   ra   r   r   r   Úintr   r   r   r   r    r!   r5   Úconv3rb   r   r   r"   r   )
r#   r$   r%   r   r"   r   r&   r	   r'   Úwidthr(   r   r   r   B  s   


zBottleneck.__init__c                 C   s~   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}| jd ur4|  |¡}||7 }|  |¡}|S r*   )r   r   r   r    r!   r¢   rb   r"   r+   r   r   r   r/   S  s   










zBottleneck.forwardr0   r1   r   r   r(   r   ra   ?  s    ÿra   c                 K   s4   t ||fi |¤Ž}|rtt|  |d�}| |¡ |S )N)Úprogress)r{   Úload_state_dict_from_urlÚ
model_urlsÚload_state_dict)Úarchrd   re   Ú
pretrainedr¤   ÚkwargsÚmodelÚ
state_dictr   r   r   Ú_resnetl  s   ÿ
r­   FTc                 K   ó   t dtg d¢| |fi |¤ŽS )zÍConstructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    Úresnet18r8   ©r­   r   ©r©   r¤   rª   r   r   r   r¯   u  ó   ÿr¯   c                 K   r®   )zÍConstructs a ResNet-34 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    Úresnet34©r   r    é   r   r°   r±   r   r   r   r³   €  r²   r³   c                 K   r®   )zÍConstructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    Úresnet50r´   ©r­   ra   r±   r   r   r   r¶   ‹  r²   r¶   c                 K   r®   )zÎConstructs a ResNet-101 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    Ú	resnet101©r   r    é   r   r·   r±   r   r   r   r¸   –  r²   r¸   c                 K   r®   )zÎConstructs a ResNet-152 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    Ú	resnet152)r   é   é$   r   r·   r±   r   r   r   r»   ¡  r²   r»   c                 K   ó,   d|d< d|d< t dtg d¢| |fi |¤ŽS )zÔConstructs a ResNeXt-50 32x4d model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    é    r   r    rh   Úresnext50_32x4dr´   r·   r±   r   r   r   rÀ   ¬  ó   ÿÿrÀ   c                 K   r¾   )zÕConstructs a ResNeXt-101 32x8d model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    r¿   r   r¼   rh   Úresnext101_32x8dr¹   r·   r±   r   r   r   rÂ   ¹  rÁ   rÂ   c                 K   ó$   d|d< t dtg d¢| |fi |¤ŽS )aû  Constructs a Wide ResNet-50-2 model.

    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    rC   rh   Úwide_resnet50_2r´   r·   r±   r   r   r   rÄ   Æ  ó   ÿÿrÄ   c                 K   rÃ   )aü  Constructs a Wide ResNet-101-2 model.

    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    rC   rh   Úwide_resnet101_2r¹   r·   r±   r   r   r   rÆ   ×  rÅ   rÆ   )r   r   r   )r   )FT)rŒ   Útorch.nnr   Útorch.nn.functionalÚ
functionalr•   r   r   ÚModuler   r7   r{   rW   ra   r­   r¯   r³   r¶   r¸   r»   rÀ   rÂ   rÄ   rÆ   r   r   r   r   Ú<module>   s(    

(]o9-
	






