-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
69 lines (59 loc) · 2.3 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
from torchvision import models
from torch import nn
class ResnetYolo(nn.Module):
def __init__(self, feature_size=7, num_bboxes=2, num_classes=20) -> None:
super().__init__()
resnet = models.resnet18(weights= models.ResNet18_Weights.IMAGENET1K_V1)
self.feature_size = feature_size
self.num_bboxes = num_bboxes
self.num_classes = num_classes
self.features = nn.Sequential(*list(resnet.children())[:-2])
self.convs = self._make_conv_layers(True)
self.fcs = self._make_fc_layers()
self.net = nn.Sequential(
self.features,
self.convs,
self.fcs
)
def forward(self, x):
return self.net(x)
def _make_conv_layers(self, bn):
if bn:
net = nn.Sequential(
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(512, 1024, 3, padding=1),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(1024, 1024, 3, stride=2, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(1024, 1024, 3, padding=1),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(1024, 1024, 3, padding=1),
nn.LeakyReLU(0.1, inplace=True)
)
else:
net = nn.Sequential(
nn.Conv2d(1024, 1024, 3, padding=1),
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(1024, 1024, 3, stride=2, padding=1),
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.1),
nn.Conv2d(1024, 1024, 3, padding=1),
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(1024, 1024, 3, padding=1),
nn.BatchNorm2d(1024),
nn.LeakyReLU(0.1, inplace=True)
)
return net
def _make_fc_layers(self):
S, B, C = self.feature_size, self.num_bboxes, self.num_classes
net = nn.Sequential(
nn.Flatten(),
nn.Linear(7 * 7 * 1024, 4096),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(0.5, inplace=False), # is it okay to use Dropout with BatchNorm?
nn.Linear(4096, S * S * (5 * B + C)),
nn.Sigmoid()
)
return net