car-detection-bayes/models.py

322 lines
13 KiB
Python
Raw Normal View History

2019-02-12 17:05:58 +00:00
import os
2018-08-26 08:51:39 +00:00
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-02-19 18:01:31 +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()
2019-02-19 18:00:44 +00:00
yolo_layer_count = 0
2018-08-26 08:51:39 +00:00
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
2019-02-09 21:14:07 +00:00
upsample = Upsample(scale_factor=int(module_def['stride']))
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]
2019-02-19 18:00:44 +00:00
nC = int(module_def['classes']) # number of classes
img_size = int(hyperparams['height'])
2018-08-26 08:51:39 +00:00
# Define detection layer
2019-02-19 18:00:44 +00:00
yolo_layer = YOLOLayer(anchors, nC, img_size, yolo_layer_count, cfg=hyperparams['cfg'])
2018-08-26 08:51:39 +00:00
modules.add_module('yolo_%d' % i, yolo_layer)
2019-02-19 18:00:44 +00:00
yolo_layer_count += 1
2018-08-26 08:51:39 +00:00
# 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-02-11 17:15:51 +00:00
def forward(self, x):
return x
2018-08-26 08:51:39 +00:00
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):
2019-02-19 18:00:44 +00:00
def __init__(self, anchors, nC, img_size, yolo_layer, cfg):
2018-08-26 08:51:39 +00:00
super(YOLOLayer, self).__init__()
2019-02-19 18:00:44 +00:00
self.anchors = torch.FloatTensor(anchors)
self.nA = len(anchors) # number of anchors (3)
2018-08-26 17:33:37 +00:00
self.nC = nC # number of classes (80)
2019-02-19 18:00:44 +00:00
self.img_size = 0
2019-03-19 13:44:36 +00:00
self.nG, self.stride, self.grid_xy, self.anchor_vec, self.anchor_wh = \
[], [], [], [], []
2018-08-26 08:51:39 +00:00
2019-03-19 13:35:12 +00:00
if ONNX_EXPORT: # grids must be computed in __init__
stride = [32, 16, 8][yolo_layer] # stride of this layer
if cfg.endswith('yolov3-tiny.cfg'):
stride *= 2
2019-03-19 13:35:12 +00:00
nG = int(img_size / stride) # number grid points
create_grids(self, img_size, nG)
2019-02-19 15:11:18 +00:00
def forward(self, p, img_size, var=None):
2019-02-19 18:00:44 +00:00
if ONNX_EXPORT:
bs, nG = 1, self.nG # batch size, grid size
else:
bs, nG = p.shape[0], p.shape[-1]
if self.img_size != img_size:
create_grids(self, img_size, nG, p.device)
2018-08-26 08:51:39 +00:00
# p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh)
2019-02-19 18:00:44 +00:00
p = p.view(bs, self.nA, self.nC + 5, nG, nG).permute(0, 1, 3, 4, 2).contiguous() # prediction
2018-08-26 08:51:39 +00:00
if self.training:
return p
elif ONNX_EXPORT:
grid_xy = self.grid_xy.repeat((1, self.nA, 1, 1, 1)).view((1, -1, 2))
anchor_wh = self.anchor_wh.repeat((1, 1, nG, nG, 1)).view((1, -1, 2)) / nG
# p = p.view(-1, 5 + self.nC)
# xy = xy + self.grid_xy[0] # x, y
# wh = torch.exp(wh) * self.anchor_wh[0] # width, height
# p_conf = torch.sigmoid(p[:, 4:5]) # Conf
# p_cls = F.softmax(p[:, 5:], 1) * p_conf # SSD-like conf
# return torch.cat((xy / nG, wh, p_conf, p_cls), 1).t()
p = p.view(1, -1, 5 + self.nC)
xy = torch.sigmoid(p[..., 0:2]) + grid_xy # x, y
wh = torch.exp(p[..., 2:4]) * anchor_wh # width, height
p_conf = torch.sigmoid(p[..., 4:5]) # Conf
p_cls = p[..., 5:]
# 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)
return torch.cat((xy / nG, wh, p_conf, p_cls), 2).squeeze().t()
else: # inference
p[..., 0:2] = torch.sigmoid(p[..., 0:2]) + self.grid_xy # xy
p[..., 2:4] = torch.exp(p[..., 2:4]) * self.anchor_wh # wh yolo method
# p[..., 2:4] = ((torch.sigmoid(p[..., 2:4]) * 2) ** 2) * self.anchor_wh # wh power method
2018-12-25 12:24:21 +00:00
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
2019-02-12 15:58:07 +00:00
self.module_defs = parse_model_cfg(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
2019-02-19 18:55:33 +00:00
self.loss_names = ['loss', 'xy', 'wh', 'conf', 'cls', 'nT']
2019-02-11 17:15:51 +00:00
self.losses = []
2018-08-26 08:51:39 +00:00
def forward(self, x, var=None):
2019-02-19 18:00:44 +00:00
img_size = x.shape[-1]
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)):
2019-02-19 18:00:44 +00:00
mtype = module_def['type']
if mtype in ['convolutional', 'upsample', 'maxpool']:
2018-08-26 08:51:39 +00:00
x = module(x)
2019-02-19 18:00:44 +00:00
elif mtype == 'route':
2018-08-26 08:51:39 +00:00
layer_i = [int(x) for x in module_def['layers'].split(',')]
2019-02-09 21:14:07 +00:00
if len(layer_i) == 1:
x = layer_outputs[layer_i[0]]
else:
x = torch.cat([layer_outputs[i] for i in layer_i], 1)
2019-02-19 18:00:44 +00:00
elif mtype == 'shortcut':
2018-08-26 08:51:39 +00:00
layer_i = int(module_def['from'])
x = layer_outputs[-1] + layer_outputs[layer_i]
2019-02-19 18:00:44 +00:00
elif mtype == 'yolo':
x = module[0](x, img_size)
2018-08-26 08:51:39 +00:00
output.append(x)
layer_outputs.append(x)
2019-01-03 22:41:31 +00:00
if ONNX_EXPORT:
2019-02-18 15:25:57 +00:00
output = torch.cat(output, 1) # merge the 3 layers 85 x (507, 2028, 8112) to 85 x 10647
2019-03-13 01:51:05 +00:00
return output[5:].t(), output[:4].t() # ONNX scores, boxes
else:
return output if self.training else torch.cat(output, 1)
2018-08-26 08:51:39 +00:00
2019-03-05 17:43:51 +00:00
def get_yolo_layers(model):
a = [module_def['type'] == 'yolo' for module_def in model.module_defs]
return [i for i, x in enumerate(a) if x] # [82, 94, 106] for yolov3
2019-03-19 13:35:12 +00:00
def create_grids(self, img_size, nG, device='cpu'):
self.img_size = img_size
2019-02-19 18:00:44 +00:00
self.stride = img_size / nG
# build xy offsets
grid_x = torch.arange(nG).repeat((nG, 1)).view((1, 1, nG, nG)).float()
grid_y = grid_x.permute(0, 1, 3, 2)
self.grid_xy = torch.stack((grid_x, grid_y), 4).to(device)
2019-02-19 18:00:44 +00:00
# build wh gains
self.anchor_vec = self.anchors.to(device) / self.stride
self.anchor_wh = self.anchor_vec.view(1, self.nA, 1, 1, 2).to(device)
2019-03-19 13:38:53 +00:00
self.nG = torch.FloatTensor([nG]).to(device)
2019-02-19 18:00:44 +00:00
2019-03-19 13:43:10 +00:00
2019-02-08 21:43:05 +00:00
def load_darknet_weights(self, weights, cutoff=-1):
# Parses and loads the weights stored in 'weights'
2019-02-08 15:50:48 +00:00
# cutoff: save layers between 0 and cutoff (if cutoff = -1 all are saved)
2019-02-08 21:43:05 +00:00
weights_file = weights.split(os.sep)[-1]
2019-02-08 15:50:48 +00:00
# Try to download weights if not available locally
2019-02-08 21:43:05 +00:00
if not os.path.isfile(weights):
2019-02-08 15:50:48 +00:00
try:
2019-02-17 17:02:56 +00:00
os.system('wget https://pjreddie.com/media/files/' + weights_file + ' -O ' + weights)
2019-02-11 17:15:51 +00:00
except IOError:
print(weights + ' not found')
2019-02-08 15:50:48 +00:00
# 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-02-21 14:57:18 +00:00
cutoff = 15
2018-08-26 08:51:39 +00:00
# Open the weights file
2019-02-08 21:43:05 +00:00
fp = open(weights, '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
2019-02-15 12:07:55 +00:00
self.seen = header[3] # number of images seen during training
2018-08-26 08:51:39 +00:00
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
2019-03-19 08:38:32 +00:00
return cutoff
2018-08-26 08:51:39 +00:00
def save_weights(self, path, cutoff=-1):
fp = open(path, 'wb')
2019-02-15 12:07:55 +00:00
self.header_info[3] = self.seen # number of images seen during training
2018-08-26 08:51:39 +00:00
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()