2018-12-05 10:55:27 +00:00
|
|
|
import torch
|
2019-08-17 23:55:45 +00:00
|
|
|
import torch.nn as nn
|
2018-12-05 10:55:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_seeds(seed=0):
|
2019-07-14 09:29:07 +00:00
|
|
|
torch.cuda.empty_cache()
|
2018-12-05 10:55:27 +00:00
|
|
|
torch.manual_seed(seed)
|
2019-02-25 12:47:51 +00:00
|
|
|
torch.cuda.manual_seed(seed)
|
|
|
|
torch.cuda.manual_seed_all(seed)
|
2019-06-24 11:43:17 +00:00
|
|
|
# torch.backends.cudnn.deterministic = True # https://pytorch.org/docs/stable/notes/randomness.html
|
2018-12-05 10:55:27 +00:00
|
|
|
|
|
|
|
|
2019-07-24 16:28:11 +00:00
|
|
|
def select_device(force_cpu=False, apex=False):
|
|
|
|
# apex if mixed precision training https://github.com/NVIDIA/apex
|
2019-04-08 13:41:14 +00:00
|
|
|
cuda = False if force_cpu else torch.cuda.is_available()
|
|
|
|
device = torch.device('cuda:0' if cuda else 'cpu')
|
2019-02-16 13:33:52 +00:00
|
|
|
|
2019-04-08 13:41:14 +00:00
|
|
|
if not cuda:
|
|
|
|
print('Using CPU')
|
|
|
|
if cuda:
|
2019-06-24 11:51:54 +00:00
|
|
|
torch.backends.cudnn.benchmark = True # set False for reproducible results
|
2019-04-08 13:41:14 +00:00
|
|
|
c = 1024 ** 2 # bytes to MB
|
|
|
|
ng = torch.cuda.device_count()
|
|
|
|
x = [torch.cuda.get_device_properties(i) for i in range(ng)]
|
2019-07-24 16:30:35 +00:00
|
|
|
cuda_str = 'Using CUDA ' + ('Apex ' if apex else '')
|
2019-07-16 17:09:40 +00:00
|
|
|
for i in range(0, ng):
|
2019-07-16 17:10:33 +00:00
|
|
|
if i == 1:
|
2019-07-16 17:09:40 +00:00
|
|
|
# torch.cuda.set_device(0) # OPTIONAL: Set GPU ID
|
|
|
|
cuda_str = ' ' * len(cuda_str)
|
|
|
|
print("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" %
|
|
|
|
(cuda_str, i, x[i].name, x[i].total_memory / c))
|
2019-02-16 13:33:52 +00:00
|
|
|
|
2019-05-03 16:14:16 +00:00
|
|
|
print('') # skip a line
|
2018-12-05 10:55:27 +00:00
|
|
|
return device
|
2019-04-19 18:41:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fuse_conv_and_bn(conv, bn):
|
|
|
|
# https://tehnokv.com/posts/fusing-batchnorm-and-conv/
|
|
|
|
with torch.no_grad():
|
|
|
|
# init
|
2019-07-24 17:02:24 +00:00
|
|
|
fusedconv = torch.nn.Conv2d(conv.in_channels,
|
|
|
|
conv.out_channels,
|
|
|
|
kernel_size=conv.kernel_size,
|
|
|
|
stride=conv.stride,
|
|
|
|
padding=conv.padding,
|
|
|
|
bias=True)
|
2019-04-19 18:41:18 +00:00
|
|
|
|
|
|
|
# prepare filters
|
|
|
|
w_conv = conv.weight.clone().view(conv.out_channels, -1)
|
|
|
|
w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var)))
|
|
|
|
fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size()))
|
|
|
|
|
|
|
|
# prepare spatial bias
|
|
|
|
if conv.bias is not None:
|
|
|
|
b_conv = conv.bias
|
|
|
|
else:
|
|
|
|
b_conv = torch.zeros(conv.weight.size(0))
|
|
|
|
b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps))
|
|
|
|
fusedconv.bias.copy_(b_conv + b_bn)
|
|
|
|
|
|
|
|
return fusedconv
|
2019-08-17 23:55:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FocalLoss(nn.Module):
|
|
|
|
# Wraps focal loss around existing loss_fcn() https://arxiv.org/pdf/1708.02002.pdf
|
|
|
|
# i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=2.5)
|
|
|
|
def __init__(self, loss_fcn, alpha=1, gamma=2, reduction='mean'):
|
|
|
|
super(FocalLoss, self).__init__()
|
|
|
|
self.loss_fcn = loss_fcn
|
|
|
|
self.alpha = alpha
|
|
|
|
self.gamma = gamma
|
|
|
|
self.reduction = reduction
|
|
|
|
|
|
|
|
def forward(self, input, target):
|
|
|
|
loss = self.loss_fcn(input, target, reduction='none')
|
|
|
|
pt = torch.exp(-loss)
|
|
|
|
loss *= self.alpha * (1 - pt) ** self.gamma
|
|
|
|
|
|
|
|
if self.reduction == 'mean':
|
|
|
|
return loss.mean()
|
|
|
|
elif self.reduction == 'sum':
|
|
|
|
return loss.sum()
|
|
|
|
else: # 'none'
|
|
|
|
return loss
|