CoolFace
Modelpublic

TTXian/RemoteSensingChangeDetection-RSCD.HA2F

sourceHugging Faceupdated 7d agoView on Hugging Face
0likes
resnet.cpython-39.pyc84 linesDownload Raw Back to __pycache__
1a

28��g��@sJddlmZddlZddlZddlmmZddlmmZ	ddl3mZgd�Zdddddd	�Z
d$dd�ZGd
d�dej�ZGdd�dej�ZGdd�dej�Zd%dd�Zd&dd�Zd'dd�Zd(dd�Zd)dd�Zedk�rFedd d!�Ze�d4d"d#d#�Ze�d5d#d �e�d6d#d �e�d7d#d �gZeee�\ZZZe ej!ej!ej!�dS)*�N)�	rearrange)�ResNet�resnet18�resnet34�resnet50�	resnet101�	resnet152z9https://download.pytorch.org/models/resnet18-5c106cde.pthz9https://download.pytorch.org/models/resnet34-333f7ec4.pthz9https://download.pytorch.org/models/resnet50-19c8e357.pthz:https://download.pytorch.org/models/resnet101-5d3b4d8f.pthz:https://download.pytorch.org/models/resnet152-b121ed2d.pth)rrrrr�cCstj||d|ddd�S)z3x3 convolution with padding�r	F��kernel_size�stride�padding�bias)�nn�Conv2d)Z	in_planesZ8out_planesr
�r�<C:\Users\82607\Desktop\code\ChangeViT-master\model\resnet.py�conv3x3s�rcs*eZdZdZd�fdd�	Zdd�Z�ZS)�9BasicBlockr	Ncs^tt|���t|||�|_t�|�|_tjdd�|_	t||�|_10t�|�|_||_||_
dS)NT��inplace)�superr�__init__r�conv1r�BatchNorm2d�bn1�ReLU�relu�conv2�bn2�11downsampler
��self�inplanes�planesr
r!��	__class__rrr!szBasicBlock.__init__cCs`|}|�|�}|�|�}|�|�}|�|�}|�|�}|jdurJ|�|�}||7}|�|�}|S�N)rrrrr r!�r#�x�residual�outrrr�forward+s1213141516171819zBasicBlock.forward)r	N��__name__�20__module__�__qualname__�	expansionrr-�
__classcell__rrr&rrs21rcs*eZdZdZd�fdd�	Zdd�Z�ZS)	�22Bottleneck�r	Ncs�tt|���tj||ddd�|_t�|�|_tj||d|ddd�|_t�|�|_	tj||dddd�|_23t�|d�|_tjdd�|_
||_||_dS)	Nr	F)rrr24rr5Tr)rr4rrrrrrrr �conv3�bn3rrr!r
r"r&rrrAs�zBottleneck.__init__cCs~|}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|jdurh|�|�}||7}|�|�}|Sr()rrrrr r6r7r!r)rrrr-Ns2526272829303132333435zBottleneck.forward)r	Nr.rrr&rr4>s
r4cs0eZdZd	�fdd�	Zd36dd�Zdd�Z�ZS)r��cs�d|_tt|���tjddddddd�|_t�d�|_tj	dd�|_37tjddd	d38�|_|�
|d|d�|_|j
|d|d	dd
�|_|j
|d|ddd
�|_|j
|d|ddd
�|_tjdd	d
�|_t�d|j|�|_dS)N�@r39��FrTrr	)rr
rr�)r
�i)r$rrrrrrrrrr�	MaxPool2d�maxpool�_make_layer�layer1�layer2�layer3�layer4�	AvgPool2d�avgpool�Linearr2�fc)r#�block�layers�num_classesr&rrrgs�zResNet.__init__r	c	Cs�d}|dks|j||jkrLt�tj|j||jd|dd�t�||j��}g}|�||j|||��||j|_td|�D]}|�||j|��q|tj|�S)Nr	F)rr
r)r$r2r�40Sequentialrr�append�range)r#rIr%�blocksr
r!rJ�irrrr@vs��zResNet._make_layercCsz|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�|�}|�	|�41d�d�}|�|�}|S)Nr�����)rrrr?rArBrCrDrF�view�sizerH)r#r*rrrr-�s42434445464748495051zResNet.forward)r8)r	)r/r0r1rr@r-r3rrr&rres52rFcKs6ttgd�fi|��}|r2|jt�td�dd�|S)zxConstructs a ResNet-18 model.53    Args:54        pretrained (bool): If True, returns a model pre-trained on ImageNet55    )r;r;r;r;rF)�strict�rr�load_state_dict�	model_zoo�load_url�56model_urls��57pretrained�kwargs�modelrrrr�srcKs2ttgd�fi|��}|r.|�t�td��|S)zxConstructs a ResNet-34 model.58    Args:59        pretrained (bool): If True, returns a model pre-trained on ImageNet60    �r61r5�r62rrUrZrrrr�srcKs2ttgd�fi|��}|r.|�t�td��|S)zxConstructs a ResNet-50 model.63    Args:64        pretrained (bool): If True, returns a model pre-trained on ImageNet65    r^r�rr4rVrWrXrYrZrrrr�srcKs2ttgd�fi|��}|r.|�t�td��|S)zyConstructs a ResNet-101 model.66    Args:67        pretrained (bool): If True, returns a model pre-trained on ImageNet68    )r69r5�r70rr`rZrrrr�srcKs2ttgd�fi|��}|r.|�t�td��|S)zyConstructs a ResNet-152 model.71    Args:72        pretrained (bool): If True, returns a model pre-trained on ImageNet73    )r74��$r75rr`rZrrrr�sr�__main__Ti)r[Zvit_dimr76r=)r	)F)F)F)F)F)"�torch.nnr�math�torchZtorch.utils.model_zoo�utilsrW�torch.nn.functional�77functional�F�einopsr�__all__rYr�Modulerr4rrrrrrr/�m�randr*�vit�x2�x3�x4�print�shaperrrr�<module>s6�	78 '4798081828384(