-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
99 lines (83 loc) · 2.58 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
#!/usr/bin/env python3
import argparse
import json
import os
import copy
import random
from collections import defaultdict
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from IPython import embed
from io import BytesIO
import os
import errno
import models
import datasets_multiclass as datasets
def manual_seed(seed):
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = defaultdict(int)
self.avg = defaultdict(float)
self.sum = defaultdict(int)
self.count = defaultdict(int)
def update(self, n=1, **val):
for k in val:
self.val[k] = val[k]
self.sum[k] += val[k] * n
self.count[k] += n
self.avg[k] = self.sum[k] / self.count[k]
def log_metrics(split, metrics, epoch, **kwargs):
print(f'[{epoch}] {split} metrics:' + json.dumps(metrics.avg))
def get_error(output, target):
if output.shape[1]>1:
pred = output.argmax(dim=1, keepdim=True)
return 1. - pred.eq(target.view_as(pred)).float().mean().item()
else:
pred = output.clone()
pred[pred>0]=1
pred[pred<=0]=-1
return 1 - pred.eq(target.view_as(pred)).float().mean().item()
def set_batchnorm_mode(model, train=True):
if isinstance(model, torch.nn.BatchNorm1d) or isinstance(model, torch.nn.BatchNorm2d):
if train:
model.train()
else:
model.eval()
for l in model.children():
set_batchnorm_mode(l, train=train)
def mkdir(directory):
'''Make directory and all parents, if needed.
Does not raise and error if directory already exists.
'''
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def lighten_color(color, amount=0.5):
"""
Lightens the given color by multiplying (1-luminosity) by the given amount.
Input can be matplotlib color string, hex string, or RGB tuple.
Examples:
>> lighten_color('g', 0.3)
>> lighten_color('#F034A3', 0.6)
>> lighten_color((.3,.55,.1), 0.5)
"""
import matplotlib.colors as mc
import colorsys
try:
c = mc.cnames[color]
except:
c = color
c = colorsys.rgb_to_hls(*mc.to_rgb(c))
return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2])