-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathresnet.py
166 lines (134 loc) · 5.68 KB
/
resnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import torch
import torchvision
from torch import Tensor
import torch.nn as nn
import math
import numpy as np
import torch.nn.functional as F
from torch.hub import load_state_dict_from_url
class ResNet10(torchvision.models.resnet.ResNet):
def __init__(self, track_bn=True):
def norm_layer(*args, **kwargs):
return nn.BatchNorm2d(*args, **kwargs, track_running_stats=track_bn)
super().__init__(torchvision.models.resnet.BasicBlock, [1, 1, 1, 1], norm_layer=norm_layer)
del self.fc
self.final_feat_dim = 512
def load_sl_official_weights(self, progress=True):
raise NotImplemented
def load_ssl_official_weights(self, progress=True):
raise NotImplemented
def _forward_impl(self, x: Tensor) -> Tensor:
# See note [TorchScript super()]
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
# x = self.fc(x)
return x
class ResNet18(torchvision.models.resnet.ResNet):
def __init__(self, track_bn=True):
def norm_layer(*args, **kwargs):
return nn.BatchNorm2d(*args, **kwargs, track_running_stats=track_bn)
super().__init__(torchvision.models.resnet.BasicBlock, [2, 2, 2, 2], norm_layer=norm_layer)
del self.fc
self.final_feat_dim = 512
def load_sl_official_weights(self, progress=True):
state_dict = load_state_dict_from_url(torchvision.models.resnet.model_urls['resnet18'],
progress=progress)
missing, unexpected = self.load_state_dict(state_dict, strict=False)
if len(missing) > 0:
raise AssertionError('Model code may be incorrect')
def load_ssl_official_weights(self, progress=True):
raise NotImplemented
def _forward_impl(self, x: Tensor) -> Tensor:
# See note [TorchScript super()]
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
# x = self.fc(x)
return x
class ResNet50(torchvision.models.resnet.ResNet):
def __init__(self, track_bn=True):
def norm_layer(*args, **kwargs):
return nn.BatchNorm2d(*args, **kwargs, track_running_stats=track_bn)
super().__init__(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], norm_layer=norm_layer)
del self.fc
self.final_feat_dim = 2048
def load_sl_official_weights(self, progress=True):
state_dict = load_state_dict_from_url(torchvision.models.resnet.model_urls['resnet50'],
progress=progress)
missing, unexpected = self.load_state_dict(state_dict, strict=False)
if len(missing) > 0:
raise AssertionError('Model code may be incorrect')
def load_ssl_official_weights(self, progress=True):
# only SimCLR is available
from pl_bolts.models.self_supervised import SimCLR
weight_path = 'https://pl-bolts-weights.s3.us-east-2.amazonaws.com/simclr/bolts_simclr_imagenet/simclr_imagenet.ckpt'
simclr = SimCLR.load_from_checkpoint(weight_path, strict=False)
state_dict = {}
for k, v in simclr.state_dict().items():
if 'encoder.' in k:
k = k.replace('encoder.', '')
if 'fc' not in k or 'project' not in k:
state_dict[k] = v
missing, unexpected = self.load_state_dict(state_dict, strict=False)
# non_linear_evaluator.block_forward is a pretrained MLP classifier for SimCLR
# refer to https://github.com/Lightning-AI/lightning-bolts/blob/bcbbf6ab6c36430946dd8a416ddc7e697e8507fc/pl_bolts/models/self_supervised/evaluator.py#L7
if len(missing) > 0:
raise AssertionError('Model code may be incorrect')
def _forward_impl(self, x: Tensor) -> Tensor:
# See note [TorchScript super()]
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
# x = self.fc(x)
return x
class ResNet101(torchvision.models.resnet.ResNet):
def __init__(self, track_bn=True):
def norm_layer(*args, **kwargs):
return nn.BatchNorm2d(*args, **kwargs, track_running_stats=track_bn)
super().__init__(torchvision.models.resnet.Bottleneck, [3, 4, 23, 3], norm_layer=norm_layer)
del self.fc
self.final_feat_dim = 2048
def load_sl_official_weights(self, progress=True):
state_dict = load_state_dict_from_url(torchvision.models.resnet.model_urls['resnet101'],
progress=progress)
missing, unexpected = self.load_state_dict(state_dict, strict=False)
if len(missing) > 0:
raise AssertionError('Model code may be incorrect')
def load_ssl_official_weights(self, progress=True):
raise NotImplemented
def _forward_impl(self, x: Tensor) -> Tensor:
# See note [TorchScript super()]
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
# x = self.fc(x)
return x