-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyolo.py
147 lines (122 loc) · 6.19 KB
/
yolo.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
import utils
import models
import constants
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
class Yolo(nn.Module):
def __init__(self):
super(Yolo, self).__init__()
self.config_list = utils.parse_config()
self.module_list = models.create_modules()
self.meta = self.config_list[0]
def transform_output(self, output, input_dim, anchors, num_classes, CUDA=False):
batch_size = output.size(0)
stride = input_dim // output.size(2)
# print(input_dim, stride)
grid_size = output.size(2)
bbox_attrs = 5 + num_classes
num_anchors = len(anchors)
anchors = [(a[0] / stride, a[1] / stride) for a in anchors]
# Transform output from 3D [x, y, (5+C)*B] => 2D [x*y*B, 5+C]
# for easier processing of different scale grids
# print(output.shape)
# print(batch_size, bbox_attrs, num_anchors, grid_size, grid_size)
# print(batch_size * bbox_attrs * num_anchors * grid_size * grid_size)
output = output.view(batch_size, bbox_attrs * num_anchors, grid_size * grid_size)
output = output.transpose(1, 2).contiguous()
output = output.view(batch_size, grid_size * grid_size * num_anchors, bbox_attrs)
# Sigmoid of centre coordinates and object confidence
output[:, :, 0] = torch.sigmoid(output[:, :, 0])
output[:, :, 1] = torch.sigmoid(output[:, :, 1])
output[:, :, 4] = torch.sigmoid(output[:, :, 4])
# Add the center offsets
grid = np.arange(grid_size)
a, b = np.meshgrid(grid, grid)
x_offset = torch.FloatTensor(a).view(-1, 1)
y_offset = torch.FloatTensor(b).view(-1, 1)
if CUDA:
x_offset = x_offset.cuda()
y_offset = y_offset.cuda()
x_y_offset = torch.cat((x_offset, y_offset), 1).repeat(1, num_anchors).view(-1, 2).unsqueeze(0)
output[:, :, :2] += x_y_offset
# log space transform for height and width
anchors = torch.FloatTensor(anchors)
if CUDA:
anchors = anchors.cuda()
anchors = anchors.repeat(grid_size * grid_size, 1).unsqueeze(0)
output[:, :, 2:4] = torch.exp(output[:, :, 2:4]) * anchors
# Sigmoid of class scores
output[:, :, 5: 5 + num_classes] = torch.sigmoid((output[:, :, 5: 5 + num_classes]))
# Resize bounding box properties w.r.t input image dimensions using stride
output[:, :, :4] *= stride
return output
def forward(self, input, CUDA):
output_cache = {}
net_properties = self.config_list[0]
detections_list = []
for (index, (module_config, module)) in enumerate(zip(self.config_list[1:], self.module_list)):
module_type = module_config["type"]
# print(module_type)
if module_type in [constants.CONVOLUTIONAL_LAYER, constants.UPSAMPLE_LAYER]:
output = module(input)
elif module_type == constants.SHORTCUT_LAYER:
shortcut_from = int(module_config["from"])
if shortcut_from > 0:
shortcut_from -= index
output = output_cache[index - 1] + output_cache[index + shortcut_from]
elif module_type == constants.ROUTE_LAYER:
layers = module_config["layers"].split(',')
layer_list = []
for layer in layers:
layer_index = int(layer)
if layer_index > 0:
layer_index -= index
layer_list.append(output_cache[index + layer_index])
output = torch.cat(layer_list, dim=1)
elif module_type == constants.YOLO_LAYER:
anchors = module[0].anchors
input_dim = int(net_properties["height"])
num_classes = int(module_config["classes"])
output = self.transform_output(input, input_dim, anchors, num_classes, CUDA)
detections_list.append(output)
print(output.shape)
output_cache[index] = output
input = output
detections = torch.cat(detections_list, 1)
return detections
def load_weights(self):
f = open(constants.WEIGHTS_FILE, "rb")
header = np.fromfile(f, dtype=np.int32, count=5)
weights = np.fromfile(f, dtype=np.float32)
f.close()
ptr = 0
for (index, (module_config, module)) in enumerate(zip(self.config_list[1:], self.module_list)):
if module_config["type"] == "convolutional":
conv_layer = module[0]
if "batch_normalize" in module_config:
batch_norm_layer = module[1]
num_bias = batch_norm_layer.bias.numel()
batch_norm_bias = torch.from_numpy(weights[ptr: ptr + num_bias]).view_as(batch_norm_layer.bias)
batch_norm_layer.bias.data.copy_(batch_norm_bias)
ptr += num_bias
batch_norm_weights = torch.from_numpy(weights[ptr: ptr + num_bias]).view_as(batch_norm_layer.weight)
batch_norm_layer.weight.data.copy_(batch_norm_weights)
ptr += num_bias
batch_norm_running_mean = torch.from_numpy(weights[ptr: ptr + num_bias]).view_as(batch_norm_layer.running_mean)
batch_norm_layer.running_mean.data.copy_(batch_norm_running_mean)
ptr += num_bias
batch_norm_running_variance = torch.from_numpy(weights[ptr: ptr + num_bias]).view_as(batch_norm_layer.running_var)
batch_norm_layer.running_var.data.copy_(batch_norm_running_variance)
ptr += num_bias
else:
num_bias = conv_layer.bias.numel()
conv_bias = torch.from_numpy(weights[ptr: ptr + num_bias]).view_as(conv_layer.bias)
conv_layer.bias.data.copy_(conv_bias)
ptr += num_bias
num_w = conv_layer.weight.numel()
conv_w = torch.from_numpy(weights[ptr: ptr + num_w]).view_as(conv_layer.weight)
conv_layer.weight.data.copy_(conv_w)
ptr += num_w