-
Notifications
You must be signed in to change notification settings - Fork 22
/
ResNeXt.py
executable file
·160 lines (136 loc) · 5.31 KB
/
ResNeXt.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
import os
import torch
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
from resnext_features.resnext101_32x4d_features import resnext101_32x4d_features,resnext101_32x4d_features_blob
from resnext_features import resnext101_64x4d_features
__all__ = ['ResNeXt101_32x4d', 'resnext101_32x4d',
'ResNeXt101_64x4d', 'resnext101_64x4d']
pretrained_settings = {
'resnext101_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/resnext101_32x4d-29e315fa.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'resnext101_64x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/resnext101_64x4d-e77a0586.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
}
}
class ResNeXt101_32x4d_blob(nn.Module):
def __init__(self, num_classes=1000):
super(ResNeXt101_32x4d_blob, self).__init__()
self.num_classes = num_classes
resnext = resnext101_32x4d_features_blob()
self.features = resnext.resnext101_32x4d_features
self.avg_pool = nn.AvgPool2d((7, 7), (1, 1))
self.last_linear = nn.Linear(2048, num_classes)
def logits(self, input):
x = self.avg_pool(input)
x = x.view(x.size(0), -1)
x = self.last_linear(x)
return x
def forward(self, input):
x = self.features(input)
x = self.logits(x)
return x
class ResNeXt101_32x4d(nn.Module):
def __init__(self, num_classes=1000):
super(ResNeXt101_32x4d, self).__init__()
self.num_classes = num_classes
resnext = resnext101_32x4d_features()
self.stem = resnext.resnext101_32x4d_stem
self.layer1 = resnext.resnext101_32x4d_layer1
self.layer2 = resnext.resnext101_32x4d_layer2
self.layer3 = resnext.resnext101_32x4d_layer3
self.layer4 = resnext.resnext101_32x4d_layer4
self.avg_pool = nn.AvgPool2d((7, 7), (1, 1))
self.last_linear = nn.Linear(2048, num_classes)
def logits(self, input):
x = self.avg_pool(input)
x = x.view(x.size(0), -1)
x = self.last_linear(x)
return x
def forward(self, input):
x = self.stem(input)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.logits(x)
return x
class ResNeXt101_64x4d(nn.Module):
def __init__(self, num_classes=1000):
super(ResNeXt101_64x4d, self).__init__()
self.num_classes = num_classes
self.features = resnext101_64x4d_features
self.avg_pool = nn.AvgPool2d((7, 7), (1, 1))
self.last_linear = nn.Linear(2048, num_classes)
def logits(self, input):
x = self.avg_pool(input)
x = x.view(x.size(0), -1)
x = self.last_linear(x)
return x
def forward(self, input):
x = self.features(input)
x = self.logits(x)
return x
def resnext101_32x4d(num_classes=1000, pretrained='imagenet'):
model = ResNeXt101_32x4d(num_classes=num_classes)
model_blob = ResNeXt101_32x4d_blob(num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['resnext101_32x4d'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model_blob.load_state_dict(model_zoo.load_url(settings['url']))
model.stem = nn.Sequential(
model_blob.features[0],
model_blob.features[1],
model_blob.features[2],
model_blob.features[3],
)
model.layer1 = nn.Sequential(
model_blob.features[4],
)
model.layer2 = nn.Sequential(
model_blob.features[5],
)
model.layer3 = nn.Sequential(
model_blob.features[6],
)
model.layer4 = nn.Sequential(
model_blob.features[7],
)
# finish here
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
def resnext101_64x4d(num_classes=1000, pretrained='imagenet'):
model = ResNeXt101_64x4d(num_classes=num_classes)
if pretrained is not None:
settings = pretrained_settings['resnext101_64x4d'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model