car-detection-bayes/models.py

432 lines
18 KiB
Python
Raw Normal View History

2018-08-26 08:51:39 +00:00
from collections import defaultdict
2019-02-08 15:50:48 +00:00
import os
2018-08-26 08:51:39 +00:00
import torch.nn as nn
from utils.parse_config import *
2018-09-19 02:32:16 +00:00
from utils.utils import *
2018-08-26 08:51:39 +00:00
2019-01-10 09:32:39 +00:00
ONNX_EXPORT = False
2019-01-03 22:41:31 +00:00
2018-08-26 08:51:39 +00:00
def create_modules(module_defs):
"""
Constructs module list of layer blocks from module configuration in module_defs
"""
hyperparams = module_defs.pop(0)
output_filters = [int(hyperparams['channels'])]
module_list = nn.ModuleList()
for i, module_def in enumerate(module_defs):
modules = nn.Sequential()
if module_def['type'] == 'convolutional':
bn = int(module_def['batch_normalize'])
filters = int(module_def['filters'])
kernel_size = int(module_def['size'])
pad = (kernel_size - 1) // 2 if int(module_def['pad']) else 0
modules.add_module('conv_%d' % i, nn.Conv2d(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(module_def['stride']),
padding=pad,
bias=not bn))
if bn:
modules.add_module('batch_norm_%d' % i, nn.BatchNorm2d(filters))
if module_def['activation'] == 'leaky':
modules.add_module('leaky_%d' % i, nn.LeakyReLU(0.1))
2018-12-22 11:36:33 +00:00
elif module_def['type'] == 'maxpool':
kernel_size = int(module_def['size'])
stride = int(module_def['stride'])
if kernel_size == 2 and stride == 1:
modules.add_module('_debug_padding_%d' % i, nn.ZeroPad2d((0, 1, 0, 1)))
maxpool = nn.MaxPool2d(kernel_size=kernel_size, stride=stride, padding=int((kernel_size - 1) // 2))
modules.add_module('maxpool_%d' % i, maxpool)
2018-08-26 08:51:39 +00:00
elif module_def['type'] == 'upsample':
2018-12-24 12:11:21 +00:00
# upsample = nn.Upsample(scale_factor=int(module_def['stride']), mode='nearest') # WARNING: deprecated
upsample = Upsample(scale_factor=int(module_def['stride']), mode='nearest')
2018-08-26 08:51:39 +00:00
modules.add_module('upsample_%d' % i, upsample)
elif module_def['type'] == 'route':
2018-09-02 10:59:39 +00:00
layers = [int(x) for x in module_def['layers'].split(',')]
2018-12-22 11:36:33 +00:00
filters = sum([output_filters[i + 1 if i > 0 else i] for i in layers])
2018-08-26 08:51:39 +00:00
modules.add_module('route_%d' % i, EmptyLayer())
elif module_def['type'] == 'shortcut':
filters = output_filters[int(module_def['from'])]
2018-09-02 10:59:39 +00:00
modules.add_module('shortcut_%d' % i, EmptyLayer())
2018-08-26 08:51:39 +00:00
2018-09-02 10:59:39 +00:00
elif module_def['type'] == 'yolo':
anchor_idxs = [int(x) for x in module_def['mask'].split(',')]
2018-08-26 08:51:39 +00:00
# Extract anchors
2018-09-02 10:59:39 +00:00
anchors = [float(x) for x in module_def['anchors'].split(',')]
2018-08-26 08:51:39 +00:00
anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)]
anchors = [anchors[i] for i in anchor_idxs]
num_classes = int(module_def['classes'])
img_height = int(hyperparams['height'])
# Define detection layer
2018-12-22 11:36:33 +00:00
yolo_layer = YOLOLayer(anchors, num_classes, img_height, anchor_idxs, cfg=hyperparams['cfg'])
2018-08-26 08:51:39 +00:00
modules.add_module('yolo_%d' % i, yolo_layer)
# Register module list and number of output filters
module_list.append(modules)
output_filters.append(filters)
return hyperparams, module_list
class EmptyLayer(nn.Module):
"""Placeholder for 'route' and 'shortcut' layers"""
def __init__(self):
super(EmptyLayer, self).__init__()
2019-01-03 22:41:31 +00:00
class Upsample(nn.Module):
2018-12-24 12:11:21 +00:00
# Custom Upsample layer (nn.Upsample gives deprecated warning message)
def __init__(self, scale_factor=1, mode='nearest'):
super(Upsample, self).__init__()
self.scale_factor = scale_factor
self.mode = mode
def forward(self, x):
2019-01-02 15:32:38 +00:00
return F.interpolate(x, scale_factor=self.scale_factor, mode=self.mode)
2018-12-24 12:11:21 +00:00
2018-08-26 08:51:39 +00:00
class YOLOLayer(nn.Module):
2018-12-22 11:36:33 +00:00
def __init__(self, anchors, nC, img_dim, anchor_idxs, cfg):
2018-08-26 08:51:39 +00:00
super(YOLOLayer, self).__init__()
anchors = [(a_w, a_h) for a_w, a_h in anchors] # (pixels)
nA = len(anchors)
self.anchors = anchors
self.nA = nA # number of anchors (3)
2018-08-26 17:33:37 +00:00
self.nC = nC # number of classes (80)
2018-08-26 08:51:39 +00:00
self.bbox_attrs = 5 + nC
self.img_dim = img_dim # from hyperparams in cfg file, NOT from parser
if anchor_idxs[0] == (nA * 2): # 6
stride = 32
elif anchor_idxs[0] == nA: # 3
stride = 16
else:
stride = 8
2018-12-22 11:36:33 +00:00
if cfg.endswith('yolov3-tiny.cfg'):
stride *= 2
2018-08-26 08:51:39 +00:00
# Build anchor grids
2018-10-10 14:16:17 +00:00
nG = int(self.img_dim / stride) # number grid points
2018-08-26 08:51:39 +00:00
self.grid_x = torch.arange(nG).repeat(nG, 1).view([1, 1, nG, nG]).float()
self.grid_y = torch.arange(nG).repeat(nG, 1).t().view([1, 1, nG, nG]).float()
2019-01-02 15:32:38 +00:00
self.anchor_wh = torch.FloatTensor([(a_w / stride, a_h / stride) for a_w, a_h in anchors]) # scale anchors
2019-01-03 22:41:31 +00:00
self.anchor_w = self.anchor_wh[:, 0].view((1, nA, 1, 1))
self.anchor_h = self.anchor_wh[:, 1].view((1, nA, 1, 1))
2018-10-10 15:07:21 +00:00
self.weights = class_weights()
2018-11-22 16:13:47 +00:00
self.loss_means = torch.ones(6)
2018-12-25 12:24:21 +00:00
self.yolo_layer = anchor_idxs[0] / nA # 2, 1, 0
2019-01-03 22:41:31 +00:00
self.stride = stride
if ONNX_EXPORT: # use fully populated and reshaped tensors
self.anchor_w = self.anchor_w.repeat((1, 1, nG, nG)).view(1, -1, 1)
self.anchor_h = self.anchor_h.repeat((1, 1, nG, nG)).view(1, -1, 1)
self.grid_x = self.grid_x.repeat(1, nA, 1, 1).view(1, -1, 1)
self.grid_y = self.grid_y.repeat(1, nA, 1, 1).view(1, -1, 1)
self.grid_xy = torch.cat((self.grid_x, self.grid_y), 2)
self.anchor_wh = torch.cat((self.anchor_w, self.anchor_h), 2) / nG
2018-11-16 19:01:38 +00:00
2018-11-27 17:14:48 +00:00
def forward(self, p, targets=None, batch_report=False, var=None):
2018-08-26 08:51:39 +00:00
FT = torch.cuda.FloatTensor if p.is_cuda else torch.FloatTensor
2018-09-02 10:59:39 +00:00
bs = p.shape[0] # batch size
nG = p.shape[2] # number of grid points
2018-08-26 08:51:39 +00:00
2019-01-03 22:41:31 +00:00
if p.is_cuda and not self.weights.is_cuda:
2018-08-26 08:51:39 +00:00
self.grid_x, self.grid_y = self.grid_x.cuda(), self.grid_y.cuda()
self.anchor_w, self.anchor_h = self.anchor_w.cuda(), self.anchor_h.cuda()
2018-11-22 16:13:47 +00:00
self.weights, self.loss_means = self.weights.cuda(), self.loss_means.cuda()
2018-08-26 08:51:39 +00:00
2018-09-02 10:59:39 +00:00
# p.view(12, 255, 13, 13) -- > (12, 3, 13, 13, 80) # (bs, anchors, grid, grid, classes + xywh)
2018-08-26 08:51:39 +00:00
p = p.view(bs, self.nA, self.bbox_attrs, nG, nG).permute(0, 1, 3, 4, 2).contiguous() # prediction
# Training
if targets is not None:
2018-11-09 23:54:55 +00:00
MSELoss = nn.MSELoss()
BCEWithLogitsLoss = nn.BCEWithLogitsLoss()
2018-10-10 14:16:17 +00:00
CrossEntropyLoss = nn.CrossEntropyLoss()
2018-08-26 08:51:39 +00:00
2018-12-25 12:24:21 +00:00
# Get outputs
x = torch.sigmoid(p[..., 0]) # Center x
y = torch.sigmoid(p[..., 1]) # Center y
p_conf = p[..., 4] # Conf
p_cls = p[..., 5:] # Class
# Width and height (yolo method)
w = p[..., 2] # Width
h = p[..., 3] # Height
width = torch.exp(w.data) * self.anchor_w
height = torch.exp(h.data) * self.anchor_h
# Width and height (power method)
# w = torch.sigmoid(p[..., 2]) # Width
# h = torch.sigmoid(p[..., 3]) # Height
# width = ((w.data * 2) ** 2) * self.anchor_w
# height = ((h.data * 2) ** 2) * self.anchor_h
2018-12-23 11:51:02 +00:00
p_boxes = None
2018-11-22 13:54:52 +00:00
if batch_report:
2018-12-31 11:33:34 +00:00
# Predicted boxes: add offset and scale with anchors (in grid space, i.e. 0-13)
2018-12-31 11:31:38 +00:00
gx = x.data + self.grid_x[:, :, :nG, :nG]
gy = y.data + self.grid_y[:, :, :nG, :nG]
p_boxes = torch.stack((gx - width / 2,
gy - height / 2,
gx + width / 2,
gy + height / 2), 4) # x1y1x2y2
2018-08-26 08:51:39 +00:00
tx, ty, tw, th, mask, tcls, TP, FP, FN, TC = \
2019-01-02 15:32:38 +00:00
build_targets(p_boxes, p_conf, p_cls, targets, self.anchor_wh, self.nA, self.nC, nG, batch_report)
2018-12-23 11:51:02 +00:00
2018-08-26 08:51:39 +00:00
tcls = tcls[mask]
if x.is_cuda:
tx, ty, tw, th, mask, tcls = tx.cuda(), ty.cuda(), tw.cuda(), th.cuda(), mask.cuda(), tcls.cuda()
2018-10-10 14:16:17 +00:00
# Compute losses
2018-09-23 20:25:23 +00:00
nT = sum([len(x) for x in targets]) # number of targets
nM = mask.sum().float() # number of anchors (assigned to targets)
2018-09-24 23:30:51 +00:00
nB = len(targets) # batch size
k = nM / nB
2018-09-23 20:25:23 +00:00
if nM > 0:
lx = k * MSELoss(x[mask], tx[mask])
ly = k * MSELoss(y[mask], ty[mask])
2018-11-17 13:37:36 +00:00
lw = k * MSELoss(w[mask], tw[mask])
lh = k * MSELoss(h[mask], th[mask])
2018-09-24 23:30:51 +00:00
2018-12-23 11:51:02 +00:00
lcls = (k / 4) * CrossEntropyLoss(p_cls[mask], torch.argmax(tcls, 1))
# lcls = (k * 10) * BCEWithLogitsLoss(p_cls[mask], tcls.float())
2018-09-23 20:25:23 +00:00
else:
lx, ly, lw, lh, lcls, lconf = FT([0]), FT([0]), FT([0]), FT([0]), FT([0]), FT([0])
2018-12-23 11:51:02 +00:00
lconf = (k * 64) * BCEWithLogitsLoss(p_conf, mask.float())
2018-09-23 20:25:23 +00:00
# Sum loss components
2018-11-23 17:13:35 +00:00
balance_losses_flag = False
2018-11-16 19:01:38 +00:00
if balance_losses_flag:
k = 1 / self.loss_means.clone()
2018-11-22 16:13:47 +00:00
loss = (lx * k[0] + ly * k[1] + lw * k[2] + lh * k[3] + lconf * k[4] + lcls * k[5]) / k.mean()
self.loss_means = self.loss_means * 0.99 + \
FT([lx.data, ly.data, lw.data, lh.data, lconf.data, lcls.data]) * 0.01
2018-11-16 19:01:38 +00:00
else:
loss = lx + ly + lw + lh + lconf + lcls
2018-09-23 20:25:23 +00:00
# Sum False Positives from unassigned anchors
2018-11-22 13:29:50 +00:00
FPe = torch.zeros(self.nC)
2018-11-22 13:54:52 +00:00
if batch_report:
2018-12-23 11:51:02 +00:00
i = torch.sigmoid(p_conf[~mask]) > 0.5
2018-11-22 13:29:50 +00:00
if i.sum() > 0:
2018-12-23 11:51:02 +00:00
FP_classes = torch.argmax(p_cls[~mask][i], 1)
2018-11-22 13:29:50 +00:00
FPe = torch.bincount(FP_classes, minlength=self.nC).float().cpu() # extra FPs
2018-08-26 08:51:39 +00:00
return loss, loss.item(), lx.item(), ly.item(), lw.item(), lh.item(), lconf.item(), lcls.item(), \
2018-09-19 02:32:16 +00:00
nT, TP, FP, FPe, FN, TC
2018-08-26 08:51:39 +00:00
else:
2019-01-03 22:41:31 +00:00
if ONNX_EXPORT:
p = p.view(1, -1, 85)
xy = torch.sigmoid(p[..., 0:2]) + self.grid_xy # x, y
width_height = torch.exp(p[..., 2:4]) * self.anchor_wh # width, height
p_conf = torch.sigmoid(p[..., 4:5]) # Conf
2019-01-06 12:23:04 +00:00
p_cls = p[..., 5:85]
# Broadcasting only supported on first dimension in CoreML. See onnx-coreml/_operators.py
# p_cls = F.softmax(p_cls, 2) * p_conf # SSD-like conf
p_cls = torch.exp(p_cls).permute(2, 1, 0)
p_cls = p_cls / p_cls.sum(0).unsqueeze(0) * p_conf.permute(2, 1, 0) # F.softmax() equivalent
p_cls = p_cls.permute(2, 1, 0)
2019-01-03 22:41:31 +00:00
return torch.cat((xy / nG, width_height, p_conf, p_cls), 2).squeeze().t()
2018-12-25 12:24:21 +00:00
p[..., 0] = torch.sigmoid(p[..., 0]) + self.grid_x # x
p[..., 1] = torch.sigmoid(p[..., 1]) + self.grid_y # y
p[..., 2] = torch.exp(p[..., 2]) * self.anchor_w # width
p[..., 3] = torch.exp(p[..., 3]) * self.anchor_h # height
p[..., 4] = torch.sigmoid(p[..., 4]) # p_conf
2019-01-03 22:41:31 +00:00
p[..., :4] *= self.stride
2018-12-25 12:24:21 +00:00
2018-12-26 11:32:34 +00:00
# reshape from [1, 3, 13, 13, 85] to [1, 507, 85]
2019-01-03 22:41:31 +00:00
return p.view(bs, -1, 5 + self.nC)
2018-08-26 08:51:39 +00:00
class Darknet(nn.Module):
"""YOLOv3 object detection model"""
def __init__(self, cfg_path, img_size=416):
2018-08-26 08:51:39 +00:00
super(Darknet, self).__init__()
2018-12-15 20:06:39 +00:00
self.module_defs = parse_model_config(cfg_path)
2018-12-22 11:36:33 +00:00
self.module_defs[0]['cfg'] = cfg_path
2018-08-26 08:51:39 +00:00
self.module_defs[0]['height'] = img_size
self.hyperparams, self.module_list = create_modules(self.module_defs)
self.img_size = img_size
2018-09-19 02:32:16 +00:00
self.loss_names = ['loss', 'x', 'y', 'w', 'h', 'conf', 'cls', 'nT', 'TP', 'FP', 'FPe', 'FN', 'TC']
2018-08-26 08:51:39 +00:00
2018-11-27 17:14:48 +00:00
def forward(self, x, targets=None, batch_report=False, var=0):
2018-08-26 08:51:39 +00:00
self.losses = defaultdict(float)
2018-12-26 11:32:34 +00:00
is_training = targets is not None
2018-08-26 08:51:39 +00:00
layer_outputs = []
2018-12-26 11:32:34 +00:00
output = []
2018-08-26 08:51:39 +00:00
for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
2018-12-22 11:36:33 +00:00
if module_def['type'] in ['convolutional', 'upsample', 'maxpool']:
2018-08-26 08:51:39 +00:00
x = module(x)
elif module_def['type'] == 'route':
layer_i = [int(x) for x in module_def['layers'].split(',')]
x = torch.cat([layer_outputs[i] for i in layer_i], 1)
elif module_def['type'] == 'shortcut':
layer_i = int(module_def['from'])
x = layer_outputs[-1] + layer_outputs[layer_i]
elif module_def['type'] == 'yolo':
# Train phase: get loss
if is_training:
2018-11-27 17:14:48 +00:00
x, *losses = module[0](x, targets, batch_report, var)
2018-08-26 08:51:39 +00:00
for name, loss in zip(self.loss_names, losses):
self.losses[name] += loss
# Test phase: Get detections
else:
x = module(x)
output.append(x)
layer_outputs.append(x)
2018-11-22 14:04:02 +00:00
if is_training:
if batch_report:
self.losses['TC'] /= 3 # target category
metrics = torch.zeros(3, len(self.losses['FPe'])) # TP, FP, FN
ui = np.unique(self.losses['TC'])[1:]
for i in ui:
j = self.losses['TC'] == float(i)
metrics[0, i] = (self.losses['TP'][j] > 0).sum().float() # TP
metrics[1, i] = (self.losses['FP'][j] > 0).sum().float() # FP
metrics[2, i] = (self.losses['FN'][j] == 3).sum().float() # FN
metrics[1] += self.losses['FPe']
self.losses['TP'] = metrics[0].sum()
self.losses['FP'] = metrics[1].sum()
self.losses['FN'] = metrics[2].sum()
self.losses['metrics'] = metrics
else:
self.losses['TP'] = 0
self.losses['FP'] = 0
self.losses['FN'] = 0
self.losses['nT'] /= 3
self.losses['TC'] = 0
2019-01-03 22:41:31 +00:00
if ONNX_EXPORT:
2018-12-28 19:15:26 +00:00
# Produce a single-layer *.onnx model (upsample ops not working in PyTorch 1.0 export yet)
2019-01-03 22:41:31 +00:00
output = output[0] # first layer reshaped to 85 x 507
return output[5:85].t(), output[:4].t() # ONNX scores, boxes
2018-12-26 11:32:34 +00:00
2018-08-26 08:51:39 +00:00
return sum(output) if is_training else torch.cat(output, 1)
2019-02-08 15:50:48 +00:00
def load_darknet_weights(self, weights_path, cutoff=-1):
2018-10-30 13:58:26 +00:00
# Parses and loads the weights stored in 'weights_path'
2019-02-08 15:50:48 +00:00
# cutoff: save layers between 0 and cutoff (if cutoff = -1 all are saved)
weights_file = weights_path.split(os.sep)[-1]
# Try to download weights if not available locally
if not os.path.isfile(weights_path):
try:
os.system('wget https://pjreddie.com/media/files/' + weights_file + ' -P ' + weights_path)
except:
assert os.path.isfile(weights_path)
# Establish cutoffs
if weights_file == 'darknet53.conv.74':
2018-10-30 13:58:26 +00:00
cutoff = 75
2019-02-08 15:50:48 +00:00
elif weights_file == 'yolov3-tiny.conv.15':
2019-01-06 22:57:59 +00:00
cutoff = 16
2018-08-26 08:51:39 +00:00
# Open the weights file
2018-09-02 10:59:39 +00:00
fp = open(weights_path, 'rb')
2018-08-26 08:51:39 +00:00
header = np.fromfile(fp, dtype=np.int32, count=5) # First five are header values
# Needed to write header when saving weights
self.header_info = header
self.seen = header[3]
weights = np.fromfile(fp, dtype=np.float32) # The rest are weights
fp.close()
ptr = 0
2018-10-30 13:58:26 +00:00
for i, (module_def, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
2018-08-26 08:51:39 +00:00
if module_def['type'] == 'convolutional':
conv_layer = module[0]
if module_def['batch_normalize']:
# Load BN bias, weights, running mean and running variance
bn_layer = module[1]
num_b = bn_layer.bias.numel() # Number of biases
# Bias
bn_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.bias)
bn_layer.bias.data.copy_(bn_b)
ptr += num_b
# Weight
bn_w = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.weight)
bn_layer.weight.data.copy_(bn_w)
ptr += num_b
# Running Mean
bn_rm = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_mean)
bn_layer.running_mean.data.copy_(bn_rm)
ptr += num_b
# Running Var
bn_rv = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_var)
bn_layer.running_var.data.copy_(bn_rv)
ptr += num_b
else:
# Load conv. bias
num_b = conv_layer.bias.numel()
conv_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.bias)
conv_layer.bias.data.copy_(conv_b)
ptr += num_b
# Load conv. weights
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
"""
@:param path - path of the new weights file
@:param cutoff - save layers between 0 and cutoff (cutoff = -1 -> all are saved)
"""
def save_weights(self, path, cutoff=-1):
fp = open(path, 'wb')
self.header_info[3] = self.seen
self.header_info.tofile(fp)
# Iterate through layers
for i, (module_def, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
if module_def['type'] == 'convolutional':
conv_layer = module[0]
# If batch norm, load bn first
if module_def['batch_normalize']:
bn_layer = module[1]
bn_layer.bias.data.cpu().numpy().tofile(fp)
bn_layer.weight.data.cpu().numpy().tofile(fp)
bn_layer.running_mean.data.cpu().numpy().tofile(fp)
bn_layer.running_var.data.cpu().numpy().tofile(fp)
# Load conv bias
else:
conv_layer.bias.data.cpu().numpy().tofile(fp)
# Load conv weights
conv_layer.weight.data.cpu().numpy().tofile(fp)
fp.close()