forked from kohpangwei/group_DRO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
122 lines (99 loc) · 3.34 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
import sys
import os
import torch
import numpy as np
import csv
class Logger(object):
def __init__(self, fpath=None, mode='w'):
self.console = sys.stdout
self.file = None
if fpath is not None:
self.file = open(fpath, mode)
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
class CSVBatchLogger:
def __init__(self, csv_path, n_groups, mode='w'):
columns = ['epoch', 'batch']
for idx in range(n_groups):
columns.append(f'avg_loss_group:{idx}')
columns.append(f'exp_avg_loss_group:{idx}')
columns.append(f'avg_acc_group:{idx}')
columns.append(f'processed_data_count_group:{idx}')
columns.append(f'update_data_count_group:{idx}')
columns.append(f'update_batch_count_group:{idx}')
columns.append('avg_actual_loss')
columns.append('avg_per_sample_loss')
columns.append('avg_acc')
columns.append('model_norm_sq')
columns.append('reg_loss')
self.path = csv_path
self.file = open(csv_path, mode)
self.columns = columns
self.writer = csv.DictWriter(self.file, fieldnames=columns)
if mode=='w':
self.writer.writeheader()
def log(self, epoch, batch, stats_dict):
stats_dict['epoch'] = epoch
stats_dict['batch'] = batch
self.writer.writerow(stats_dict)
def flush(self):
self.file.flush()
def close(self):
self.file.close()
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
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 accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
temp = target.view(1, -1).expand_as(pred)
temp = temp.cuda()
correct = pred.eq(temp)
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def set_seed(seed):
"""Sets seed"""
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.manual_seed(seed)
np.random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def log_args(args, logger):
for argname, argval in vars(args).items():
logger.write(f'{argname.replace("_"," ").capitalize()}: {argval}\n')
logger.write('\n')