-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnext.py
82 lines (67 loc) · 2.23 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
import math
from functools import partial
import torch
import torch.nn as nn
import torch.nn.functional as F
from resnet import conv1x1x1, Bottleneck, ResNet
from utils import partialclass
def get_inplanes():
return [128, 256, 512, 1024]
class ResNeXtBottleneck(Bottleneck):
expansion = 2
def __init__(self, in_planes, planes, cardinality, stride=1, downsample=None):
super().__init__(in_planes, planes, stride, downsample)
mid_planes = cardinality * planes // 32
self.conv1 = conv1x1x1(in_planes, mid_planes)
self.bn1 = nn.BatchNorm3d(mid_planes)
self.conv2 = nn.Conv3d(
mid_planes,
mid_planes,
kernel_size=3,
stride=stride,
padding=1,
groups=cardinality,
bias=False,
)
self.bn2 = nn.BatchNorm3d(mid_planes)
self.conv3 = conv1x1x1(mid_planes, planes * self.expansion)
class ResNeXt(ResNet):
def __init__(
self,
block,
layers,
block_inplanes,
n_input_channels=3,
conv1_t_size=7,
conv1_t_stride=1,
no_max_pool=False,
shortcut_type="B",
cardinality=32,
n_classes=400,
):
block = partialclass(block, cardinality=cardinality)
# print("will initialize resnext...")
super().__init__(
block,
layers,
block_inplanes,
n_input_channels,
conv1_t_size,
conv1_t_stride,
no_max_pool,
shortcut_type,
n_classes,
)
self.fc = nn.Linear(cardinality * 32 * block.expansion, n_classes)
# print("resnext is initialized!")
def generate_model(model_depth, **kwargs):
assert model_depth in [50, 101, 152, 200]
if model_depth == 50:
model = ResNeXt(ResNeXtBottleneck, [3, 4, 6, 3], get_inplanes(), **kwargs)
elif model_depth == 101:
model = ResNeXt(ResNeXtBottleneck, [3, 4, 23, 3], get_inplanes(), **kwargs)
elif model_depth == 152:
model = ResNeXt(ResNeXtBottleneck, [3, 8, 36, 3], get_inplanes(), **kwargs)
elif model_depth == 200:
model = ResNeXt(ResNeXtBottleneck, [3, 24, 36, 3], get_inplanes(), **kwargs)
return model