-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
129 lines (106 loc) · 4.36 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
import os
import time
import numpy as np
import torchvision.utils as vutils
from collections import OrderedDict
import visdom
import torch
def plot_current_errors(epoch, counter_ratio, errors,vis):
"""Plot current errros.
Args:
epoch (int): Current epoch
counter_ratio (float): Ratio to plot the range between two epoch.
errors (OrderedDict): Error for the current epoch.
"""
# plot_data = None
# plot_res = None
# if not hasattr('plot_data') or plot_data is None:
plot_data = {}
plot_data = {'X': [], 'Y': [], 'legend': list(errors.keys())}
plot_data['X'].append(epoch + counter_ratio)
plot_data['Y'].append([errors[k] for k in plot_data['legend']])
vis.line(win='wire train loss', update='append',
X=np.stack([np.array(plot_data['X'])] * len(plot_data['legend']), 1),
Y=np.array(plot_data['Y']),
opts={
'title': 'CSA-CDGAN' + ' loss over time',
'legend': plot_data['legend'],
'xlabel': 'Epoch',
'ylabel': 'Loss'
})
def normalize(inp):
"""Normalize the tensor
Args:
inp ([FloatTensor]): Input tensor
Returns:
[FloatTensor]: Normalized tensor.
"""
return (inp - inp.min()) / (inp.max() - inp.min() + 1e-5)
def display_current_images(reals, fakes, vis):
""" Display current images.
Args:
epoch (int): Current epoch
counter_ratio (float): Ratio to plot the range between two epoch.
reals ([FloatTensor]): Real Image
fakes ([FloatTensor]): Fake Image
fixed ([FloatTensor]): Fixed Fake Image
"""
reals = normalize(reals.cpu().numpy())
fakes = normalize(fakes.cpu().numpy())
# fixed = normalize(fixed.cpu().numpy())
vis.images(reals, win=1, opts={'title': 'Reals'})
vis.images(fakes, win=2, opts={'title': 'Fakes'})
# vis.images(fixed, win=3, opts={'title': 'fixed'})
def get_errors(err_d, err_g):
""" Get netD and netG errors.
Returns:
[OrderedDict]: Dictionary containing errors.
"""
errors = OrderedDict([
('err_d', err_d.item()),
('err_g', err_g.item())])
return errors
def save_current_images(epoch, reals, fakes,save_dir,name):
""" Save images for epoch i.
Args:
epoch ([int]) : Current epoch
reals ([FloatTensor]): Real Image
fakes ([FloatTensor]): Fake Image
fixed ([FloatTensor]): Fixed Fake Image
"""
save_path = os.path.join(save_dir,name)
if not os.path.exists(save_path):
os.makedirs(save_path)
vutils.save_image(reals, '%s/reals.png' % save_path, normalize=True)
vutils.save_image(fakes, '%s/fakes_%03d.png' % (save_path, epoch+1), normalize=True)
def save_weights(epoch,net,optimizer,save_path, model_name):
checkpoint = {
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'learning_rate': optimizer.state_dict()['param_groups'][0]['lr'],
}
torch.save(checkpoint,os.path.join(save_path,'current_%s.pth'%(model_name)))
if epoch % 20 == 0:
torch.save(checkpoint,os.path.join(save_path,'%d_%s.pth'%(epoch,model_name)))
def plot_performance( epoch, performance, vis):
""" Plot performance
Args:
epoch (int): Current epoch
counter_ratio (float): Ratio to plot the range between two epoch.
performance (OrderedDict): Performance for the current epoch.
"""
plot_res = []
plot_res = {'X': [], 'Y': [], 'legend': list(performance.keys())}
plot_res['X'].append(epoch)
plot_res['Y'].append([performance[k] for k in plot_res['legend']])
vis.line(win='AUC', update='append',
X=np.stack([np.array(plot_res['X'])] * len(plot_res['legend']), 1),
Y=np.array(plot_res['Y']),
opts={
'title': 'Testing ' + 'Performance Metrics',
'legend': plot_res['legend'],
'xlabel': 'Epoch',
'ylabel': 'Stats'
},
)