-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
34 lines (26 loc) · 1015 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
def poly_lr_scheduler(optimizer, init_lr, iter, lr_decay_iter=1,
max_iter=300, power=0.9):
"""Polynomial decay of learning rate
:param init_lr is base learning rate
:param iter is a current iteration
:param lr_decay_iter how frequently decay occurs, default is 1
:param max_iter is number of maximum iterations
:param power is a polymomial power
"""
# if iter % lr_decay_iter or iter > max_iter:
# return optimizer
lr = init_lr*(1 - iter/max_iter)**power
optimizer.param_groups[0]['lr'] = lr
return lr
# return lr
def fast_hist(a, b, n):
'''
a and b are label and prediction respectively
n is the number of classes
'''
k = (a >= 0) & (a < n)
return np.bincount(n * a[k].astype(int) + b[k], minlength=n ** 2).reshape(n, n)
def per_class_iou(hist):
epsilon = 1e-5
return (np.diag(hist)) / (hist.sum(1) + hist.sum(0) - np.diag(hist) + epsilon)