-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet101.py
40 lines (34 loc) · 1.1 KB
/
resnet101.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
import keras
from keras import models
class ResNet101(models.ResNet):
def __init__(self, num_classes=1000, pretrained=True, **kwargs):
# Start with the standard resnet101
super().__init__(
block=models.resnet.Bottleneck,
layers=[3, 4, 23, 3],
num_classes=num_classes,
**kwargs
)
if pretrained:
state_dict = load_state_dict_from_url(
models.resnet.model_urls['resnet101'],
progress=True
)
self.load_state_dict(state_dict)
# Reimplementing forward pass.
# Replacing the forward inference defined here
# http://tiny.cc/23pmmz
def _forward_impl(self, x):
# Standard forward for resnet
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)
# Notice there is no forward pass through the original classifier.
x = self.avgpool(x)
x = torch.flatten(x, 1)
return x