car-detection-bayes/models.py

370 lines
17 KiB
Python
Raw Normal View History

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 *
2019-07-29 21:37:12 +00:00
from pathlib import Path
2018-08-26 08:51:39 +00:00
2019-07-29 10:06:29 +00:00
import torch.nn.functional as F
2019-05-14 16:43:14 +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-07-03 12:42:11 +00:00
yolo_index = -1
2018-08-26 08:51:39 +00:00
for i, module_def in enumerate(module_defs):
modules = nn.Sequential()
if module_def['type'] == 'convolutional':
2019-06-27 22:38:52 +00:00
bn = int(module_def['batch_normalize'])
2018-08-26 08:51:39 +00:00
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':
2019-07-19 23:28:29 +00:00
# modules.add_module('leaky_%d' % i, nn.PReLU(num_parameters=filters, init=0.10))
2019-04-12 12:00:16 +00:00
modules.add_module('leaky_%d' % i, nn.LeakyReLU(0.1, inplace=True))
2018-08-26 08:51:39 +00:00
2018-12-22 11:36:33 +00:00
elif module_def['type'] == 'maxpool':
kernel_size = int(module_def['size'])
stride = int(module_def['stride'])
2019-08-03 12:22:25 +00:00
maxpool = nn.MaxPool2d(kernel_size=kernel_size, stride=stride, padding=int((kernel_size - 1) // 2))
2019-08-03 12:14:10 +00:00
if kernel_size == 2 and stride == 1: # yolov3-tiny
2018-12-22 11:36:33 +00:00
modules.add_module('_debug_padding_%d' % i, nn.ZeroPad2d((0, 1, 0, 1)))
modules.add_module('maxpool_%d' % i, maxpool)
2018-08-26 08:51:39 +00:00
elif module_def['type'] == 'upsample':
2019-08-03 12:14:10 +00:00
modules = nn.Upsample(scale_factor=int(module_def['stride']), mode='nearest')
2018-08-26 08:51:39 +00:00
2019-08-03 12:14:10 +00:00
elif module_def['type'] == 'route': # nn.Sequential() placeholder for 'route' layer
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])
2019-07-29 10:06:29 +00:00
# if module_defs[i+1]['type'] == 'reorg3d':
2019-08-03 12:14:10 +00:00
# modules = nn.Upsample(scale_factor=1/float(module_defs[i+1]['stride']), mode='nearest') # reorg3d
elif module_def['type'] == 'shortcut': # nn.Sequential() placeholder for 'shortcut' layer
2018-08-26 08:51:39 +00:00
filters = output_filters[int(module_def['from'])]
2019-08-03 12:14:10 +00:00
elif module_def['type'] == 'reorg3d': # yolov3-spp-pan-scale
2019-07-29 10:06:29 +00:00
# torch.Size([16, 128, 104, 104])
# torch.Size([16, 64, 208, 208]) <-- # stride 2 interpolate dimensions 2 and 3 to cat with prior layer
pass
2018-09-02 10:59:39 +00:00
elif module_def['type'] == 'yolo':
2019-07-03 12:42:11 +00:00
yolo_index += 1
2019-08-03 12:14:10 +00:00
mask = [int(x) for x in module_def['mask'].split(',')] # anchor mask
a = [float(x) for x in module_def['anchors'].split(',')] # anchors
a = [(a[i], a[i + 1]) for i in range(0, len(a), 2)]
modules = YOLOLayer(anchors=[a[i] for i in mask], # anchor list
nc=int(module_def['classes']), # number of classes
img_size=hyperparams['height'], # 416
yolo_index=yolo_index) # 0, 1 or 2
2019-07-28 22:42:03 +00:00
else:
print('Warning: Unrecognized Layer Type: ' + module_def['type'])
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 YOLOLayer(nn.Module):
2019-07-03 12:42:11 +00:00
def __init__(self, anchors, nc, img_size, yolo_index):
2018-08-26 08:51:39 +00:00
super(YOLOLayer, self).__init__()
2019-04-21 19:07:01 +00:00
self.anchors = torch.Tensor(anchors)
2019-04-19 18:41:18 +00:00
self.na = len(anchors) # number of anchors (3)
self.nc = nc # number of classes (80)
2019-04-22 10:51:20 +00:00
self.nx = 0 # initialize number of x gridpoints
self.ny = 0 # initialize number of y gridpoints
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__
2019-07-03 12:42:11 +00:00
stride = [32, 16, 8][yolo_index] # stride of this layer
2019-04-25 18:50:37 +00:00
nx = int(img_size[1] / stride) # number x grid points
ny = int(img_size[0] / stride) # number y grid points
create_grids(self, max(img_size), (nx, ny))
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:
2019-04-21 19:07:01 +00:00
bs = 1 # batch size
2019-02-19 18:00:44 +00:00
else:
2019-04-22 12:31:23 +00:00
bs, ny, nx = p.shape[0], p.shape[-2], p.shape[-1]
2019-04-25 18:50:37 +00:00
if (self.nx, self.ny) != (nx, ny):
2019-07-31 22:33:17 +00:00
create_grids(self, img_size, (nx, ny), p.device, p.dtype)
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-04-22 12:31:23 +00:00
p = p.view(bs, self.na, self.nc + 5, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction
2018-08-26 08:51:39 +00:00
if self.training:
return p
elif ONNX_EXPORT:
2019-04-21 21:49:10 +00:00
# Constants CAN NOT BE BROADCAST, ensure correct shape!
ngu = self.ng.repeat((1, self.na * self.nx * self.ny, 1))
2019-04-19 18:41:18 +00:00
grid_xy = self.grid_xy.repeat((1, self.na, 1, 1, 1)).view((1, -1, 2))
2019-04-21 21:49:10 +00:00
anchor_wh = self.anchor_wh.repeat((1, 1, self.nx, self.ny, 1)).view((1, -1, 2)) / ngu
2019-04-19 18:41:18 +00:00
# p = p.view(-1, 5 + self.nc)
2019-04-03 15:24:13 +00:00
# xy = torch.sigmoid(p[..., 0:2]) + grid_xy[0] # x, y
# wh = torch.exp(p[..., 2:4]) * anchor_wh[0] # width, height
# p_conf = torch.sigmoid(p[:, 4:5]) # Conf
2019-04-03 15:24:13 +00:00
# p_cls = F.softmax(p[:, 5:85], 1) * p_conf # SSD-like conf
2019-04-21 21:49:10 +00:00
# return torch.cat((xy / ngu[0], wh, p_conf, p_cls), 1).t()
2019-04-19 18:41:18 +00:00
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
2019-05-29 23:40:35 +00:00
p_cls = p[..., 5:5 + self.nc]
# 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-04-21 21:49:10 +00:00
return torch.cat((xy / ngu, wh, p_conf, p_cls), 2).squeeze().t()
else: # inference
2019-07-25 16:18:40 +00:00
# s = 1.5 # scale_xy (pxy = pxy * s - (s - 1) / 2)
2019-04-05 13:34:42 +00:00
io = p.clone() # inference output
io[..., 0:2] = torch.sigmoid(io[..., 0:2]) + self.grid_xy # xy
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh # wh yolo method
io[..., :4] *= self.stride
2019-08-03 12:14:10 +00:00
2019-04-06 14:13:35 +00:00
# io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method
2019-04-05 13:34:42 +00:00
io[..., 4:] = torch.sigmoid(io[..., 4:]) # p_conf, p_cls
# io[..., 5:] = F.softmax(io[..., 5:], dim=4) # p_cls
2019-08-03 12:14:10 +00:00
2019-05-03 18:51:30 +00:00
if self.nc == 1:
io[..., 5] = 1 # single-class model https://github.com/ultralytics/yolov3/issues/235
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-04-19 18:41:18 +00:00
return io.view(bs, -1, 5 + self.nc), p
2018-08-26 08:51:39 +00:00
class Darknet(nn.Module):
"""YOLOv3 object detection model"""
2019-04-23 14:48:47 +00:00
def __init__(self, cfg, img_size=(416, 416)):
2018-08-26 08:51:39 +00:00
super(Darknet, self).__init__()
2018-12-15 20:06:39 +00:00
2019-04-23 14:48:47 +00:00
self.module_defs = parse_model_cfg(cfg)
self.module_defs[0]['cfg'] = cfg
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)
2019-04-11 10:41:07 +00:00
self.yolo_layers = get_yolo_layers(self)
2018-08-26 08:51:39 +00:00
2019-06-05 11:49:56 +00:00
# Darknet Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
self.version = np.array([0, 2, 5], dtype=np.int32) # (int32) version info: major, minor, revision
self.seen = np.array([0], dtype=np.int64) # (int64) number of images seen during training
2019-04-23 14:48:47 +00:00
def forward(self, x, var=None):
2019-04-22 12:59:39 +00:00
img_size = max(x.shape[-2:])
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:
2019-07-29 10:06:29 +00:00
try:
x = torch.cat([layer_outputs[i] for i in layer_i], 1)
except: # apply stride 2 for darknet reorg layer
layer_outputs[layer_i[1]] = F.interpolate(layer_outputs[layer_i[1]], scale_factor=[0.5, 0.5])
x = torch.cat([layer_outputs[i] for i in layer_i], 1)
# print(''), [print(layer_outputs[i].shape) for i in layer_i], print(x.shape)
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':
2019-08-03 12:14:10 +00:00
x = module(x, img_size)
2018-08-26 08:51:39 +00:00
output.append(x)
layer_outputs.append(x)
2019-04-05 13:34:42 +00:00
if self.training:
return output
elif ONNX_EXPORT:
2019-04-03 15:24:13 +00:00
output = torch.cat(output, 1) # cat 3 layers 85 x (507, 2028, 8112) to 85 x 10647
2019-05-29 23:40:35 +00:00
nc = self.module_list[self.yolo_layers[0]][0].nc # number of classes
return output[5:5 + nc].t(), output[:4].t() # ONNX scores, boxes
else:
2019-04-05 13:34:42 +00:00
io, p = list(zip(*output)) # inference output, training output
return torch.cat(io, 1), p
2018-08-26 08:51:39 +00:00
2019-04-20 20:46:23 +00:00
def fuse(self):
# Fuse Conv2d + BatchNorm2d layers throughout model
fused_list = nn.ModuleList()
for a in list(self.children())[0]:
for i, b in enumerate(a):
if isinstance(b, nn.modules.batchnorm.BatchNorm2d):
# fuse this bn layer with the previous conv2d layer
conv = a[i - 1]
fused = torch_utils.fuse_conv_and_bn(conv, b)
a = nn.Sequential(fused, *list(a.children())[i + 1:])
break
fused_list.append(a)
self.module_list = fused_list
# model_info(self) # yolov3-spp reduced from 225 to 152 layers
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-07-31 22:33:17 +00:00
def create_grids(self, img_size=416, ng=(13, 13), device='cpu', type=torch.float32):
2019-04-25 18:50:37 +00:00
nx, ny = ng # x and y grid size
2019-03-19 13:35:12 +00:00
self.img_size = img_size
2019-04-21 19:07:01 +00:00
self.stride = img_size / max(ng)
2019-02-19 18:00:44 +00:00
# build xy offsets
2019-04-22 12:31:23 +00:00
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
2019-07-31 22:33:17 +00:00
self.grid_xy = torch.stack((xv, yv), 2).to(device).type(type).view((1, 1, ny, nx, 2))
2019-02-19 18:00:44 +00:00
# build wh gains
self.anchor_vec = self.anchors.to(device) / self.stride
2019-07-31 22:33:17 +00:00
self.anchor_wh = self.anchor_vec.view(1, self.na, 1, 1, 2).to(device).type(type)
2019-04-21 19:07:01 +00:00
self.ng = torch.Tensor(ng).to(device)
2019-04-21 18:30:11 +00:00
self.nx = nx
self.ny = ny
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-07-29 21:37:12 +00:00
file = Path(weights).name
2019-02-08 15:50:48 +00:00
# Try to download weights if not available locally
2019-08-01 19:58:43 +00:00
msg = weights + ' missing, download from https://drive.google.com/drive/folders/1uxgUBemJVw9wZsdpboYbzUN4bcRhsuAI'
2019-02-08 21:43:05 +00:00
if not os.path.isfile(weights):
2019-02-08 15:50:48 +00:00
try:
2019-07-29 21:37:12 +00:00
url = 'https://pjreddie.com/media/files/' + file
2019-07-20 15:05:09 +00:00
print('Downloading ' + url)
2019-08-01 19:58:43 +00:00
os.system('curl -f ' + url + ' -o ' + weights)
2019-02-11 17:15:51 +00:00
except IOError:
2019-08-01 19:58:43 +00:00
print(msg)
assert os.path.exists(weights), msg # download missing weights from Google Drive
2019-02-08 15:50:48 +00:00
# Establish cutoffs
2019-07-29 21:37:12 +00:00
if file == 'darknet53.conv.74':
2018-10-30 13:58:26 +00:00
cutoff = 75
2019-07-29 21:37:12 +00:00
elif file == 'yolov3-tiny.conv.15':
2019-02-21 14:57:18 +00:00
cutoff = 15
2018-08-26 08:51:39 +00:00
2019-06-05 11:49:56 +00:00
# Read weights file
2019-04-23 14:48:47 +00:00
with open(weights, 'rb') as f:
2019-06-05 11:49:56 +00:00
# Read Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
self.version = np.fromfile(f, dtype=np.int32, count=3) # (int32) version info: major, minor, revision
self.seen = np.fromfile(f, dtype=np.int64, count=1) # (int64) number of images seen during training
2018-08-26 08:51:39 +00:00
2019-04-23 14:48:47 +00:00
weights = np.fromfile(f, dtype=np.float32) # The rest are weights
2018-08-26 08:51:39 +00:00
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
2019-04-23 14:48:47 +00:00
def save_weights(self, path='model.weights', cutoff=-1):
# Converts a PyTorch model to Darket format (*.pt to *.weights)
# Note: Does not work if model.fuse() is applied
with open(path, 'wb') as f:
2019-06-05 11:49:56 +00:00
# Write Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
self.version.tofile(f) # (int32) version info: major, minor, revision
self.seen.tofile(f) # (int64) number of images seen during training
2019-04-23 14:48:47 +00:00
# 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(f)
bn_layer.weight.data.cpu().numpy().tofile(f)
bn_layer.running_mean.data.cpu().numpy().tofile(f)
bn_layer.running_var.data.cpu().numpy().tofile(f)
# Load conv bias
else:
conv_layer.bias.data.cpu().numpy().tofile(f)
# Load conv weights
conv_layer.weight.data.cpu().numpy().tofile(f)
2018-08-26 08:51:39 +00:00
2019-04-23 14:48:47 +00:00
def convert(cfg='cfg/yolov3-spp.cfg', weights='weights/yolov3-spp.weights'):
# Converts between PyTorch and Darknet format per extension (i.e. *.weights convert to *.pt and vice versa)
# from models import *; convert('cfg/yolov3-spp.cfg', 'weights/yolov3-spp.weights')
# Initialize model
model = Darknet(cfg)
# Load weights and save
if weights.endswith('.pt'): # if PyTorch format
model.load_state_dict(torch.load(weights, map_location='cpu')['model'])
save_weights(model, path='converted.weights', cutoff=-1)
print("Success: converted '%s' to 'converted.weights'" % weights)
elif weights.endswith('.weights'): # darknet format
_ = load_darknet_weights(model, weights)
2019-07-08 17:26:46 +00:00
chkpt = {'epoch': -1,
'best_fitness': None,
'training_results': None,
'model': model.state_dict(),
'optimizer': None}
2019-04-23 14:48:47 +00:00
torch.save(chkpt, 'converted.pt')
print("Success: converted '%s' to 'converted.pt'" % weights)
else:
print('Error: extension not supported.')