-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodels.py
196 lines (154 loc) · 6.38 KB
/
models.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import config
import torch
import torch.nn as nn
from torchvision.models import resnet50, ResNet50_Weights
#################################
# Transfer Learning #
#################################
class YOLOv1ResNet(nn.Module):
def __init__(self):
super().__init__()
self.depth = config.B * 5 + config.C
# Load backbone ResNet
backbone = resnet50(weights=ResNet50_Weights.DEFAULT)
backbone.requires_grad_(False) # Freeze backbone weights
# Delete last two layers and attach detection layers
backbone.avgpool = nn.Identity()
backbone.fc = nn.Identity()
self.model = nn.Sequential(
backbone,
Reshape(2048, 14, 14),
DetectionNet(2048) # 4 conv, 2 linear
)
def forward(self, x):
return self.model.forward(x)
class DetectionNet(nn.Module):
"""The layers added on for detection as described in the paper."""
def __init__(self, in_channels):
super().__init__()
inner_channels = 1024
self.depth = 5 * config.B + config.C
self.model = nn.Sequential(
nn.Conv2d(in_channels, inner_channels, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(inner_channels, inner_channels, kernel_size=3, stride=2, padding=1), # (Ch, 14, 14) -> (Ch, 7, 7)
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(inner_channels, inner_channels, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(inner_channels, inner_channels, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
nn.Flatten(),
nn.Linear(7 * 7 * inner_channels, 4096),
# nn.Dropout(),
nn.LeakyReLU(negative_slope=0.1),
nn.Linear(4096, config.S * config.S * self.depth)
)
def forward(self, x):
return torch.reshape(
self.model.forward(x),
(-1, config.S, config.S, self.depth)
)
###########################
# From Scratch #
###########################
class YOLOv1(nn.Module):
def __init__(self):
super().__init__()
self.depth = config.B * 5 + config.C
layers = [
# Probe(0, forward=lambda x: print('#' * 5 + ' Start ' + '#' * 5)),
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3), # Conv 1
nn.LeakyReLU(negative_slope=0.1),
# Probe('conv1', forward=probe_dist),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 192, kernel_size=3, padding=1), # Conv 2
nn.LeakyReLU(negative_slope=0.1),
# Probe('conv2', forward=probe_dist),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(192, 128, kernel_size=1), # Conv 3
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(256, 256, kernel_size=1),
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
# Probe('conv3', forward=probe_dist),
nn.MaxPool2d(kernel_size=2, stride=2)
]
for i in range(4): # Conv 4
layers += [
nn.Conv2d(512, 256, kernel_size=1),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1)
]
layers += [
nn.Conv2d(512, 512, kernel_size=1),
nn.Conv2d(512, 1024, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
# Probe('conv4', forward=probe_dist),
nn.MaxPool2d(kernel_size=2, stride=2)
]
for i in range(2): # Conv 5
layers += [
nn.Conv2d(1024, 512, kernel_size=1),
nn.Conv2d(512, 1024, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1)
]
layers += [
nn.Conv2d(1024, 1024, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1),
nn.Conv2d(1024, 1024, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.1),
# Probe('conv5', forward=probe_dist),
]
for _ in range(2): # Conv 6
layers += [
nn.Conv2d(1024, 1024, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.1)
]
# layers.append(Probe('conv6', forward=probe_dist))
layers += [
nn.Flatten(),
nn.Linear(config.S * config.S * 1024, 4096), # Linear 1
nn.Dropout(),
nn.LeakyReLU(negative_slope=0.1),
# Probe('linear1', forward=probe_dist),
nn.Linear(4096, config.S * config.S * self.depth), # Linear 2
# Probe('linear2', forward=probe_dist),
]
self.model = nn.Sequential(*layers)
def forward(self, x):
return torch.reshape(
self.model.forward(x),
(x.size(dim=0), config.S, config.S, self.depth)
)
#############################
# Helper Modules #
#############################
class Reshape(nn.Module):
def __init__(self, *args):
super().__init__()
self.shape = tuple(args)
def forward(self, x):
return torch.reshape(x, (-1, *self.shape))
class Probe(nn.Module):
names = set()
def __init__(self, name, forward=None):
super().__init__()
assert name not in self.names, f"Probe named '{name}' already exists"
self.name = name
self.names.add(name)
self.forward = self.probe_func_factory(probe_size if forward is None else forward)
def probe_func_factory(self, func):
def f(x):
print(f"\nProbe '{self.name}':")
func(x)
return x
return f
def probe_size(x):
print(x.size())
def probe_mean(x):
print(torch.mean(x).item())
def probe_dist(x):
print(torch.min(x).item(), '|', torch.median(x).item(), '|', torch.max(x).item())