forked from motokimura/yolo_v1_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolo_v1.py
97 lines (74 loc) · 2.75 KB
/
yolo_v1.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from darknet import DarkNet
from util_layers import Flatten
class YOLOv1(nn.Module):
def __init__(self, features, num_bboxes=2, num_classes=20, bn=True):
super(YOLOv1, self).__init__()
self.feature_size = 7
self.num_bboxes = num_bboxes
self.num_classes = num_classes
self.features = features
self.conv_layers = self._make_conv_layers(bn)
self.fc_layers = self._make_fc_layers()
def _make_conv_layers(self, bn):
if bn:
net = nn.Sequential(
nn.Conv2d(1024, 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(
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
def forward(self, x):
S, B, C = self.feature_size, self.num_bboxes, self.num_classes
x = self.features(x)
x = self.conv_layers(x)
x = self.fc_layers(x)
x = x.view(-1, S, S, 5 * B + C)
return x
def test():
from torch.autograd import Variable
# Build model with randomly initialized weights
darknet = DarkNet(conv_only=True, bn=True, init_weight=True)
yolo = YOLOv1(darknet.features)
# Prepare a dummy image to input
image = torch.rand(10, 3, 448, 448)
image = Variable(image)
# Forward
output = yolo(image)
# Check ouput tensor size, which should be [10, 7, 7, 30]
print(output.size())
if __name__ == '__main__':
test()