car-detection-bayes/models.py

484 lines
21 KiB
Python
Raw Normal View History

2019-08-15 16:15:27 +00:00
import torch.nn.functional as F
2019-09-16 21:15:07 +00:00
from utils.google_utils import *
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 10:06:29 +00:00
2019-08-11 13:22:53 +00:00
ONNX_EXPORT = False
2019-01-03 22:41:31 +00:00
2018-08-26 08:51:39 +00:00
2019-08-23 15:18:59 +00:00
def create_modules(module_defs, img_size, arc):
# Constructs module list of layer blocks from module configuration in module_defs
2018-08-26 08:51:39 +00:00
hyperparams = module_defs.pop(0)
output_filters = [int(hyperparams['channels'])]
module_list = nn.ModuleList()
2019-12-29 22:54:08 +00:00
routs = [] # list of layers which rout to deeper layers
2019-07-03 12:42:11 +00:00
yolo_index = -1
2019-08-12 11:37:11 +00:00
for i, mdef in enumerate(module_defs):
2018-08-26 08:51:39 +00:00
modules = nn.Sequential()
2020-02-02 16:55:34 +00:00
# if i == 0:
# modules.add_module('BatchNorm2d_0', nn.BatchNorm2d(output_filters[-1], momentum=0.1))
2018-08-26 08:51:39 +00:00
2019-08-03 12:49:38 +00:00
if mdef['type'] == 'convolutional':
2020-02-20 01:08:03 +00:00
bn = mdef['batch_normalize']
filters = mdef['filters']
size = mdef['size']
stride = mdef['stride'] if 'stride' in mdef else (mdef['stride_y'], mdef['stride_x'])
2019-08-03 12:38:06 +00:00
modules.add_module('Conv2d', nn.Conv2d(in_channels=output_filters[-1],
out_channels=filters,
2019-12-09 21:17:30 +00:00
kernel_size=size,
2019-11-16 21:12:56 +00:00
stride=stride,
2020-02-20 01:08:03 +00:00
padding=(size - 1) // 2 if mdef['pad'] else 0,
groups=mdef['groups'] if 'groups' in mdef else 1,
2019-08-03 12:38:06 +00:00
bias=not bn))
2018-08-26 08:51:39 +00:00
if bn:
2019-08-08 17:49:15 +00:00
modules.add_module('BatchNorm2d', nn.BatchNorm2d(filters, momentum=0.1))
2020-02-28 18:06:35 +00:00
if mdef['activation'] == 'leaky': # activation study https://github.com/ultralytics/yolov3/issues/441
2019-08-03 12:38:06 +00:00
modules.add_module('activation', nn.LeakyReLU(0.1, inplace=True))
2019-08-11 13:17:40 +00:00
# modules.add_module('activation', nn.PReLU(num_parameters=1, init=0.10))
2019-11-23 02:20:11 +00:00
elif mdef['activation'] == 'swish':
modules.add_module('activation', Swish())
2018-08-26 08:51:39 +00:00
2019-08-03 12:49:38 +00:00
elif mdef['type'] == 'maxpool':
2020-02-20 01:08:03 +00:00
size = mdef['size']
stride = mdef['stride']
maxpool = nn.MaxPool2d(kernel_size=size, stride=stride, padding=(size - 1) // 2)
2019-12-09 21:17:30 +00:00
if size == 2 and stride == 1: # yolov3-tiny
2019-08-03 12:38:06 +00:00
modules.add_module('ZeroPad2d', nn.ZeroPad2d((0, 1, 0, 1)))
modules.add_module('MaxPool2d', maxpool)
else:
modules = maxpool
2018-12-22 11:36:33 +00:00
2019-08-03 12:49:38 +00:00
elif mdef['type'] == 'upsample':
2020-01-30 20:39:54 +00:00
if ONNX_EXPORT: # explicitly state size, avoid scale_factor
2020-02-17 20:36:11 +00:00
g = (yolo_index + 1) * 2 / 32 # gain
modules = nn.Upsample(size=tuple(int(x * g) for x in img_size)) # img_size = (320, 192)
2020-01-30 20:39:54 +00:00
else:
2020-02-20 01:08:03 +00:00
modules = nn.Upsample(scale_factor=mdef['stride'])
2018-08-26 08:51:39 +00:00
2019-08-03 12:49:38 +00:00
elif mdef['type'] == 'route': # nn.Sequential() placeholder for 'route' layer
2020-02-20 01:08:03 +00:00
layers = mdef['layers']
2018-12-22 11:36:33 +00:00
filters = sum([output_filters[i + 1 if i > 0 else i] for i in layers])
2019-08-12 11:37:11 +00:00
routs.extend([l if l > 0 else l + i for l in layers])
2019-08-03 12:49:38 +00:00
# if mdef[i+1]['type'] == 'reorg3d':
# modules = nn.Upsample(scale_factor=1/float(mdef[i+1]['stride']), mode='nearest') # reorg3d
2019-08-03 12:14:10 +00:00
2019-08-03 12:49:38 +00:00
elif mdef['type'] == 'shortcut': # nn.Sequential() placeholder for 'shortcut' layer
2020-02-20 01:08:03 +00:00
layers = mdef['from']
2020-02-20 02:26:45 +00:00
filters = output_filters[-1]
2020-02-17 23:28:11 +00:00
routs.extend([i + l if l < 0 else l for l in layers])
2020-02-19 20:59:56 +00:00
modules = weightedFeatureFusion(layers=layers, weight='weights_type' in mdef)
2018-08-26 08:51:39 +00:00
2019-08-03 12:49:38 +00:00
elif mdef['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
2019-08-03 12:49:38 +00:00
elif mdef['type'] == 'yolo':
2019-07-03 12:42:11 +00:00
yolo_index += 1
2020-02-20 01:08:03 +00:00
mask = mdef['mask'] # anchor mask
2019-08-15 16:15:27 +00:00
modules = YOLOLayer(anchors=mdef['anchors'][mask], # anchor list
2020-02-20 01:08:03 +00:00
nc=mdef['classes'], # number of classes
2019-08-12 11:49:38 +00:00
img_size=img_size, # (416, 416)
2019-08-23 15:18:59 +00:00
yolo_index=yolo_index, # 0, 1 or 2
arc=arc) # yolo architecture
2019-08-18 23:27:41 +00:00
2019-08-19 15:07:16 +00:00
# Initialize preceding Conv2d() bias (https://arxiv.org/pdf/1708.02002.pdf section 3.3)
2019-08-22 21:41:51 +00:00
try:
2020-01-22 01:23:35 +00:00
if arc == 'default' or arc == 'Fdefault': # default
2020-03-04 21:30:27 +00:00
b = [-3.0, -6.0] # obj, cls
2019-08-22 21:41:51 +00:00
elif arc == 'uBCE': # unified BCE (80 classes)
2019-12-17 20:26:42 +00:00
b = [0, -9.0]
2019-08-25 18:19:53 +00:00
elif arc == 'uCE': # unified CE (1 background + 80 classes)
b = [10, -0.1]
2020-01-22 01:23:35 +00:00
elif arc == 'uFBCE': # unified FocalBCE (5120 obj, 80 classes)
b = [0, -6.5]
2019-08-25 18:19:53 +00:00
elif arc == 'uFCE': # unified FocalCE (64 cls, 1 background + 80 classes)
b = [7.7, -1.1]
2019-08-22 21:41:51 +00:00
bias = module_list[-1][0].bias.view(len(mask), -1) # 255 to 3x85
2019-08-29 15:59:24 +00:00
bias[:, 4] += b[0] - bias[:, 4].mean() # obj
bias[:, 5:] += b[1] - bias[:, 5:].mean() # cls
2019-08-23 10:57:26 +00:00
# bias = torch.load('weights/yolov3-spp.bias.pt')[yolo_index] # list of tensors [3x85, 3x85, 3x85]
2019-08-22 21:41:51 +00:00
module_list[-1][0].bias = torch.nn.Parameter(bias.view(-1))
# utils.print_model_biases(model)
except:
print('WARNING: smart bias initialization failure.')
2019-08-19 15:07:16 +00:00
2019-07-28 22:42:03 +00:00
else:
2019-08-03 12:49:38 +00:00
print('Warning: Unrecognized Layer Type: ' + mdef['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)
2019-08-12 11:37:11 +00:00
return module_list, routs
2018-08-26 08:51:39 +00:00
2020-02-19 04:13:18 +00:00
class weightedFeatureFusion(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
2020-02-19 20:59:56 +00:00
def __init__(self, layers, weight=False):
2020-02-18 01:02:37 +00:00
super(weightedFeatureFusion, self).__init__()
self.layers = layers # layer indices
2020-02-19 20:59:56 +00:00
self.weight = weight # apply weights boolean
2020-02-20 00:05:57 +00:00
self.n = len(layers) + 1 # number of layers
2020-02-19 20:59:56 +00:00
if weight:
self.w = torch.nn.Parameter(torch.zeros(self.n)) # layer weights
2020-02-18 01:02:37 +00:00
def forward(self, x, outputs):
2020-02-20 00:05:57 +00:00
# Weights
2020-02-19 20:59:56 +00:00
if self.weight:
w = torch.sigmoid(self.w) * (2 / self.n) # sigmoid weights (0-1)
2020-02-20 00:05:57 +00:00
x = x * w[0]
# Fusion
2020-02-22 20:48:24 +00:00
nc = x.shape[1] # input channels
2020-02-20 00:05:57 +00:00
for i in range(self.n - 1):
2020-02-22 20:54:09 +00:00
a = outputs[self.layers[i]] * w[i + 1] if self.weight else outputs[self.layers[i]] # feature to add
2020-02-22 20:48:24 +00:00
ac = a.shape[1] # feature channels
dc = nc - ac # delta channels
2020-02-20 00:05:57 +00:00
# Adjust channels
2020-02-22 20:48:24 +00:00
if dc > 0: # slice input
2020-02-22 20:56:20 +00:00
x[:, :ac] = x[:, :ac] + a # or a = nn.ZeroPad2d((0, 0, 0, 0, 0, dc))(a); x = x + a
2020-02-22 20:48:24 +00:00
elif dc < 0: # slice feature
2020-02-22 20:54:09 +00:00
x = x + a[:, :nc]
2020-02-22 20:48:24 +00:00
else: # same shape
2020-02-22 20:54:09 +00:00
x = x + a
2020-02-20 00:05:57 +00:00
return x
2020-02-18 01:02:37 +00:00
2019-11-26 03:13:10 +00:00
class SwishImplementation(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
ctx.save_for_backward(i)
2019-11-26 04:42:48 +00:00
return i * torch.sigmoid(i)
2019-11-26 03:13:10 +00:00
@staticmethod
def backward(ctx, grad_output):
2019-11-26 04:42:48 +00:00
sigmoid_i = torch.sigmoid(ctx.saved_variables[0])
return grad_output * (sigmoid_i * (1 + ctx.saved_variables[0] * (1 - sigmoid_i)))
2019-11-26 03:13:10 +00:00
class MemoryEfficientSwish(nn.Module):
def forward(self, x):
return SwishImplementation.apply(x)
2019-08-10 20:11:55 +00:00
2019-11-26 03:13:10 +00:00
class Swish(nn.Module):
2019-08-10 20:11:55 +00:00
def forward(self, x):
2019-11-17 20:21:59 +00:00
return x.mul_(torch.sigmoid(x))
2019-08-10 20:11:55 +00:00
2019-11-13 01:57:22 +00:00
class Mish(nn.Module): # https://github.com/digantamisra98/Mish
def forward(self, x):
2019-11-17 20:21:59 +00:00
return x.mul_(F.softplus(x).tanh())
2019-11-13 01:57:22 +00:00
2018-08-26 08:51:39 +00:00
class YOLOLayer(nn.Module):
2019-08-23 15:18:59 +00:00
def __init__(self, anchors, nc, img_size, yolo_index, arc):
2018-08-26 08:51:39 +00:00
super(YOLOLayer, self).__init__()
2019-08-17 12:15:27 +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-12-20 02:09:13 +00:00
self.no = nc + 5 # number of outputs
2019-04-22 10:51:20 +00:00
self.nx = 0 # initialize number of x gridpoints
self.ny = 0 # initialize number of y gridpoints
2019-08-23 15:18:59 +00:00
self.arc = arc
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
2019-08-11 13:17:40 +00:00
create_grids(self, 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-12-09 02:08:19 +00:00
bs, _, ny, nx = p.shape # bs, 255, 13, 13
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-12-20 02:09:13 +00:00
p = p.view(bs, self.na, self.no, 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:
2020-02-09 05:51:31 +00:00
# Avoid broadcasting for ANE operations
2019-12-04 01:22:58 +00:00
m = self.na * self.nx * self.ny
2020-02-09 19:17:31 +00:00
ng = 1 / self.ng.repeat((m, 1))
2020-01-11 21:11:30 +00:00
grid_xy = self.grid_xy.repeat((1, self.na, 1, 1, 1)).view(m, 2)
2020-02-09 19:17:31 +00:00
anchor_wh = self.anchor_wh.repeat((1, 1, self.nx, self.ny, 1)).view(m, 2) * ng
2019-12-20 02:09:13 +00:00
p = p.view(m, self.no)
2020-01-11 21:11:30 +00:00
xy = torch.sigmoid(p[:, 0:2]) + grid_xy # x, y
wh = torch.exp(p[:, 2:4]) * anchor_wh # width, height
2020-01-30 05:52:00 +00:00
p_cls = torch.sigmoid(p[:, 4:5]) if self.nc == 1 else \
torch.sigmoid(p[:, 5:self.no]) * torch.sigmoid(p[:, 4:5]) # conf
2020-02-09 19:17:31 +00:00
return p_cls, xy * ng, wh
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
2019-12-24 07:34:30 +00:00
io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy # xy
2019-04-05 13:34:42 +00:00
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh # wh yolo method
2019-08-11 13:17:40 +00:00
# io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh # wh power method
io[..., :4] *= self.stride
2019-08-03 12:14:10 +00:00
2019-08-25 18:19:53 +00:00
if 'default' in self.arc: # seperate obj and cls
2020-01-18 01:44:22 +00:00
torch.sigmoid_(io[..., 4:])
2019-08-25 18:19:53 +00:00
elif 'BCE' in self.arc: # unified BCE (80 classes)
2019-08-18 19:24:48 +00:00
torch.sigmoid_(io[..., 5:])
io[..., 4] = 1
2019-08-25 18:19:53 +00:00
elif 'CE' in self.arc: # unified CE (1 background + 80 classes)
io[..., 4:] = F.softmax(io[..., 4:], dim=4)
io[..., 4] = 1
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
2020-01-12 20:01:58 +00:00
# reshape from [1, 3, 13, 13, 85] to [1, 507, 85]
2019-12-23 18:10:24 +00:00
return io.view(bs, -1, self.no), p
2018-08-26 08:51:39 +00:00
class Darknet(nn.Module):
2019-08-23 15:18:59 +00:00
# YOLOv3 object detection model
2018-08-26 08:51:39 +00:00
2019-08-23 15:18:59 +00:00
def __init__(self, cfg, img_size=(416, 416), arc='default'):
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)
2019-08-23 15:18:59 +00:00
self.module_list, self.routs = create_modules(self.module_defs, img_size, arc)
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-08-11 13:17:40 +00:00
img_size = x.shape[-2:]
2020-02-19 23:16:00 +00:00
yolo_out, out = [], []
2020-01-23 21:52:17 +00:00
verbose = False
if verbose:
2020-02-20 02:26:45 +00:00
str = ''
2020-01-23 21:52:17 +00:00
print('0', x.shape)
2018-08-26 08:51:39 +00:00
2019-08-03 12:49:38 +00:00
for i, (mdef, module) in enumerate(zip(self.module_defs, self.module_list)):
mtype = mdef['type']
2019-02-19 18:00:44 +00:00
if mtype in ['convolutional', 'upsample', 'maxpool']:
2018-08-26 08:51:39 +00:00
x = module(x)
2020-02-19 20:59:56 +00:00
elif mtype == 'shortcut': # sum
if verbose:
2020-02-20 02:26:45 +00:00
l = [i - 1] + module.layers # layers
2020-02-20 02:06:53 +00:00
s = [list(x.shape)] + [list(out[i].shape) for i in module.layers] # shapes
2020-02-20 02:26:45 +00:00
str = ' >> ' + ' + '.join(['layer %g %s' % x for x in zip(l, s)])
2020-02-19 23:16:00 +00:00
x = module(x, out) # weightedFeatureFusion()
2020-02-17 23:28:11 +00:00
elif mtype == 'route': # concat
2020-02-20 01:08:03 +00:00
layers = mdef['layers']
2020-01-27 22:03:27 +00:00
if verbose:
2020-02-20 02:26:45 +00:00
l = [i - 1] + layers # layers
s = [list(x.shape)] + [list(out[i].shape) for i in layers] # shapes
str = ' >> ' + ' + '.join(['layer %g %s' % x for x in zip(l, s)])
2019-08-12 11:49:38 +00:00
if len(layers) == 1:
2020-02-19 23:16:00 +00:00
x = out[layers[0]]
2019-02-09 21:14:07 +00:00
else:
2019-07-29 10:06:29 +00:00
try:
2020-02-19 23:16:00 +00:00
x = torch.cat([out[i] for i in layers], 1)
2019-07-29 10:06:29 +00:00
except: # apply stride 2 for darknet reorg layer
2020-02-19 23:16:00 +00:00
out[layers[1]] = F.interpolate(out[layers[1]], scale_factor=[0.5, 0.5])
x = torch.cat([out[i] for i in layers], 1)
# print(''), [print(out[i].shape) for i in layers], print(x.shape)
2019-02-19 18:00:44 +00:00
elif mtype == 'yolo':
2020-02-19 23:16:00 +00:00
yolo_out.append(module(x, img_size))
out.append(x if i in self.routs else [])
2020-01-23 21:52:17 +00:00
if verbose:
2020-02-20 02:26:45 +00:00
print('%g/%g %s -' % (i, len(self.module_list), mtype), list(x.shape), str)
str = ''
2018-08-26 08:51:39 +00:00
2020-02-19 20:59:56 +00:00
if self.training: # train
2020-02-19 23:16:00 +00:00
return yolo_out
2020-02-19 20:59:56 +00:00
elif ONNX_EXPORT: # export
2020-02-19 23:16:00 +00:00
x = [torch.cat(x, 0) for x in zip(*yolo_out)]
2020-01-11 21:11:30 +00:00
return x[0], torch.cat(x[1:3], 1) # scores, boxes: 3780x80, 3780x4
2020-02-19 20:59:56 +00:00
else: # test
2020-02-19 23:16:00 +00:00
io, p = zip(*yolo_out) # inference output, training output
2019-04-05 13:34:42 +00:00
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]:
2019-08-09 10:44:47 +00:00
if isinstance(a, nn.Sequential):
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
2019-04-20 20:46:23 +00:00
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):
2019-08-03 12:49:38 +00:00
return [i for i, x in enumerate(model.module_defs) if x['type'] == 'yolo'] # [82, 94, 106] for yolov3
2019-03-05 17:43:51 +00:00
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-08-11 13:17:40 +00:00
self.img_size = max(img_size)
self.stride = self.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
2019-09-19 16:05:04 +00:00
# Establish cutoffs (load layers between 0 and cutoff. if cutoff = -1 all are loaded)
file = Path(weights).name
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-11-15 01:48:06 +00:00
weights = np.fromfile(f, dtype=np.float32) # the rest are weights
2018-08-26 08:51:39 +00:00
ptr = 0
2019-08-03 12:49:38 +00:00
for i, (mdef, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
if mdef['type'] == 'convolutional':
2020-02-19 22:57:58 +00:00
conv = module[0]
2019-08-03 12:49:38 +00:00
if mdef['batch_normalize']:
2018-08-26 08:51:39 +00:00
# Load BN bias, weights, running mean and running variance
2020-02-19 22:57:58 +00:00
bn = module[1]
nb = bn.bias.numel() # number of biases
2018-08-26 08:51:39 +00:00
# Bias
2020-02-19 22:57:58 +00:00
bn.bias.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.bias))
ptr += nb
2018-08-26 08:51:39 +00:00
# Weight
2020-02-19 22:57:58 +00:00
bn.weight.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.weight))
ptr += nb
2018-08-26 08:51:39 +00:00
# Running Mean
2020-02-19 22:57:58 +00:00
bn.running_mean.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.running_mean))
ptr += nb
2018-08-26 08:51:39 +00:00
# Running Var
2020-02-19 22:57:58 +00:00
bn.running_var.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.running_var))
ptr += nb
2018-08-26 08:51:39 +00:00
else:
# Load conv. bias
2020-02-19 22:57:58 +00:00
nb = conv.bias.numel()
conv_b = torch.from_numpy(weights[ptr:ptr + nb]).view_as(conv.bias)
conv.bias.data.copy_(conv_b)
ptr += nb
2018-08-26 08:51:39 +00:00
# Load conv. weights
2020-02-19 22:57:58 +00:00
nw = conv.weight.numel() # number of weights
conv.weight.data.copy_(torch.from_numpy(weights[ptr:ptr + nw]).view_as(conv.weight))
ptr += nw
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
2019-08-03 12:49:38 +00:00
for i, (mdef, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
if mdef['type'] == 'convolutional':
2019-04-23 14:48:47 +00:00
conv_layer = module[0]
# If batch norm, load bn first
2019-08-03 12:49:38 +00:00
if mdef['batch_normalize']:
2019-04-23 14:48:47 +00:00
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.')
2019-09-19 16:05:04 +00:00
def attempt_download(weights):
# Attempt to download pretrained weights if not found locally
2019-12-06 21:44:13 +00:00
msg = weights + ' missing, try downloading from https://drive.google.com/open?id=1LezFG5g3BCW6iYaV89B2i64cqEUZD7e0'
2019-09-19 16:05:04 +00:00
2019-09-20 11:21:57 +00:00
if weights and not os.path.isfile(weights):
2019-12-06 20:56:22 +00:00
d = {'yolov3-spp.weights': '16lYS4bcIdM2HdmyJBVDOvt3Trx6N3W2R',
'yolov3.weights': '1uTlyDWlnaqXcsKOktP5aH_zRDbfcDp-y',
'yolov3-tiny.weights': '1CCF-iNIIkYesIDzaPvdwlcf7H9zSsKZQ',
'yolov3-spp.pt': '1f6Ovy3BSq2wYq4UfvFUpxJFNDFfrIDcR',
'yolov3.pt': '1SHNFyoe5Ni8DajDNEqgB2oVKBb_NoEad',
'yolov3-tiny.pt': '10m_3MlpQwRtZetQxtksm9jqHrPTHZ6vo',
'darknet53.conv.74': '1WUVBid-XuoUBmvzBVUCBl_ELrzqwA8dJ',
'yolov3-tiny.conv.15': '1Bw0kCpplxUqyRYAJr9RY9SGnOJbo9nEj',
'ultralytics49.pt': '158g62Vs14E3aj7oPVPuEnNZMKFNgGyNq',
2020-02-17 07:12:07 +00:00
'ultralytics68.pt': '1Jm8kqnMdMGUUxGo8zMFZMJ0eaPwLkxSG',
'yolov3-spp-ultralytics.pt': '1UcR-zVoMs7DH5dj3N1bswkiQTA4dmKF4'}
2019-12-06 20:56:22 +00:00
2019-12-06 21:47:17 +00:00
file = Path(weights).name
2019-12-06 21:44:13 +00:00
if file in d:
r = gdrive_download(id=d[file], name=weights)
2019-12-06 20:56:22 +00:00
else: # download from pjreddie.com
2019-12-06 21:44:13 +00:00
url = 'https://pjreddie.com/media/files/' + file
print('Downloading ' + url)
r = os.system('curl -f ' + url + ' -o ' + weights)
2019-12-06 21:50:16 +00:00
# Error check
if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
os.system('rm ' + weights) # remove partial downloads
raise Exception(msg)