-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
163 lines (125 loc) · 4.94 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import math
import pdb
import absl
import datetime
import logging
import torch
import torch.nn as nn
import numpy as np
import shutil
from PIL import Image
from absl import flags
from absl import app
FLAGS = flags.FLAGS
def rand_bbox(size, lam):
W = size[2]
H = size[3]
cut_rat = np.sqrt(1. - lam)
cut_w = np.int(W * cut_rat)
cut_h = np.int(H * cut_rat)
# uniform
cx = np.random.randint(W)
cy = np.random.randint(H)
bbx1 = np.clip(cx - cut_w // 2, 0, W)
bby1 = np.clip(cy - cut_h // 2, 0, H)
bbx2 = np.clip(cx + cut_w // 2, 0, W)
bby2 = np.clip(cy + cut_h // 2, 0, H)
return bbx1, bby1, bbx2, bby2
def cutmix(ori_img, near_img, alpha=2.):
lam = np.random.beta(alpha, alpha)
bbx1, bby1, bbx2, bby2 = rand_bbox(ori_img.size(), lam)
lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (ori_img.size()[-1] * ori_img.size()[-2]))
ori_img[:, :, bbx1:bbx2, bby1:bby2] = near_img[:, :, bbx1:bbx2, bby1:bby2]
mixed_img = ori_img
return mixed_img, lam
class Log():
def __init__(self, exp_path):
self.logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s - %(name)-8s - %(levelname)-6s - %(message)s')
if FLAGS.rank == 0: # only rank 0 output is visible
# str handler
strHandler = logging.StreamHandler()
strHandler.setFormatter(formatter)
self.logger.addHandler(strHandler)
self.logger.setLevel(logging.INFO)
# file handler
self.log_path = os.path.join(exp_path, 'logs')
if (not os.path.exists(self.log_path)):
os.makedirs(self.log_path, exist_ok=True) # handle FileExistsError in multiprocessing mode
now_str = datetime.datetime.now().__str__().replace(' ','_')
self.file_name = 'LOG_INFO_'+now_str+'_rank'+str(FLAGS.rank)+'.txt'
self.log_file = os.path.join(self.log_path, self.file_name)
self.log_fileHandler = logging.FileHandler(self.log_file)
self.log_fileHandler.setFormatter(formatter)
self.logger.addHandler(self.log_fileHandler)
# remove root hanlder intro by absl
logging.root.removeHandler(absl.logging._absl_handler)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def display(self, batch, log):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
log.logger.info('\t'.join(entries))
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def save_ckpt(state, epoch, save_freq, is_best=None):
if FLAGS.rank == 0:
filename = os.path.join(FLAGS.train_url, 'ckpt.pth.tar')
torch.save(state, filename)
if epoch % save_freq == 0:
filename = os.path.join(FLAGS.train_url, 'ckpt_%s.pth.tar'%(epoch))
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, os.path.join(FLAGS.train_url, 'ckpt_best.pth.tar'))
else:
pass
def adjust_learning_rate(optimizer, epoch, log):
"""Decay the learning rate based on schedule"""
lr = FLAGS.init_lr
if FLAGS.decay_method == 'cos': # cosine lr schedule
lr *= 0.5 * (1. + math.cos(math.pi * epoch / FLAGS.end_epoch))
else: # stepwise lr schedule
for milestone in FLAGS.schedule:
# lr *= 0.1 if epoch >= milestone else 1.
lr *= FLAGS.lr_decay if epoch >= milestone else 1.
for param_group in optimizer.param_groups:
param_group['lr'] = lr
log.logger.info('==> Setting model optimizer lr = %.6f'%(lr))