-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
206 lines (151 loc) · 7.11 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import torch
import numpy as np
import os
import matplotlib.pyplot as plt
def make_dir(dir_name: str):
"""
creates directory "dir_name" if it doesn't exists
"""
if not os.path.exists(dir_name):
os.makedirs(dir_name)
def plot_recons_samples(recons, images, path_to_save: str, filename: str, title: str):
assert recons.shape[0] == 64, 'plotting 8x8 canvas, give 64 recons'
assert images.shape[0] == 64, 'plotting 8x8 canvas, give 64 images'
rat = 1 / 16
rat_white = 0.04
width_ratios = (np.ones((17)) * rat) - (rat_white / 16)
width_ratios[8] = rat_white
w = 33.28 # 16.64 x 32
h = 16 # 8 x 32
fig, axs = plt.subplots(nrows=8, ncols=17, figsize=(w, h),
gridspec_kw={'width_ratios': width_ratios.tolist()},
)
for row in range(8):
for col in range(17):
if col < 8:
i = row
j = col
axs[row, col].imshow(recons[i*8 + j])
# elif col == 8:
# axs[row, col].imshow(X1[0 + 0])
elif col > 8:
i = row
j = col - 9
axs[row, col].imshow(images[i*8 + j])
axs[row, col].axis('off')
# axs[row, col].set_xticklabels([])
# axs[row, col].set_yticklabels([])
# axs[row, col].set_aspect('equal')
plt.subplots_adjust(wspace=0, hspace=0) # , left=0, right=1, bottom=0, top=1
fig.suptitle(f'{title}', fontsize=32)
# plt.tight_layout()
# fig.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
# fig.subplots_adjust(hspace=0)
# plt.show()
make_dir(path_to_save)
plt.savefig(f'{path_to_save}/{filename}.jpg') # , bbox_inches="tight"
def custom_plot_loss(loss_hist, phase_list, title: str, dir: str):
fig, ax1 = plt.subplots(nrows = 1, ncols = 1, figsize=[7, 6], dpi=100)
for phase in phase_list:
lowest_loss_x = np.argmin(np.array(loss_hist[phase]))
lowest_loss_y = loss_hist[phase][lowest_loss_x]
ax1.annotate("{:.4f}".format(lowest_loss_y), [lowest_loss_x, lowest_loss_y])
ax1.plot(loss_hist[phase], '-x', label=f'{phase} loss', markevery = [lowest_loss_x])
ax1.set_xlabel(xlabel='epochs')
ax1.set_ylabel(ylabel='loss')
ax1.grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
ax1.legend()
ax1.label_outer()
fig.suptitle(f'{title}')
make_dir(dir)
plt.savefig(f'{dir}/losses.jpg')
plt.clf()
def custpm_plot_clf_acc(acc_hist, AE_epoch, phase_list, title: str, dir: str):
fig, ax1 = plt.subplots(nrows = 1, ncols = 1, figsize=[7, 6], dpi=100)
for phase in phase_list:
highest_acc_x = np.argmax(np.array(acc_hist[phase]))
highest_acc_y = acc_hist[phase][highest_acc_x]
ax1.annotate("{:.4f}".format(highest_acc_y), [highest_acc_x, highest_acc_y])
ax1.plot(acc_hist[phase], '-x', label=f'{phase} loss', markevery = [highest_acc_x])
ax1.set_xlabel(xlabel='epochs')
ax1.set_ylabel(ylabel='loss')
ax1.grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
ax1.legend()
ax1.label_outer()
fig.suptitle(f'{title}')
make_dir(dir)
plt.savefig(f'{dir}/acc-AE_epoch_{AE_epoch}.jpg')
plt.clf()
def custom_plot_training_stats(acc_hist, loss_hist, phase_list, title: str, dir: str):
fig, (ax1, ax2) = plt.subplots(nrows = 1, ncols = 2, figsize=[14, 6], dpi=100)
for phase in phase_list:
lowest_loss_x = np.argmin(np.array(loss_hist[phase]))
lowest_loss_y = loss_hist[phase][lowest_loss_x]
ax1.annotate("{:.4f}".format(lowest_loss_y), [lowest_loss_x, lowest_loss_y])
ax1.plot(loss_hist[phase], '-x', label=f'{phase} loss', markevery = [lowest_loss_x])
ax1.set_xlabel(xlabel='epochs')
ax1.set_ylabel(ylabel='loss')
ax1.grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
ax1.legend()
ax1.label_outer()
# acc:
for phase in phase_list:
highest_acc_x = np.argmax(np.array(acc_hist[phase]))
highest_acc_y = acc_hist[phase][highest_acc_x]
ax2.annotate("{:.4f}".format(highest_acc_y), [highest_acc_x, highest_acc_y])
ax2.plot(acc_hist[phase], '-x', label=f'{phase} loss', markevery = [highest_acc_x])
ax2.set_xlabel(xlabel='epochs')
ax2.set_ylabel(ylabel='acc')
ax2.grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
ax2.legend()
#ax2.label_outer()
fig.suptitle(f'{title}')
make_dir(dir)
plt.savefig(f'{dir}/AE acc-loss.jpg')
plt.clf()
def cutsom_plot_GAN(Loss_G_hist, D_Gz_hist, Loss_D_hist, Dx_hist, title: str, dir: str, filename: str = 'GAN stats'):
# Loss_G_hist -> decrease
# Loss_D_hist -> decrease?
# Dx_hist -> This should start close to 1 then theoretically converge to 0.5 when G gets better - decrease
# D_Gz_hist -> These numbers should start near 0 and converge to 0.5 as G gets better - increase
fig, axs = plt.subplots(nrows = 2, ncols = 2, figsize=[14, 12], dpi=100)
# plot losses
plt_indx = 0
loss_hist = {'Loss G': Loss_G_hist, 'Loss D': Loss_D_hist}
for phase in ['Loss G', 'Loss D']:
lowest_loss_x = np.argmin(np.array(loss_hist[phase]))
lowest_loss_y = loss_hist[phase][lowest_loss_x]
axs[0][plt_indx].annotate("{:.4f}".format(lowest_loss_y), [lowest_loss_x, lowest_loss_y])
axs[0][plt_indx].plot(loss_hist[phase], '-x', label=f'{phase}', markevery = [lowest_loss_x])
axs[0][plt_indx].set_xlabel(xlabel='epochs')
axs[0][plt_indx].set_ylabel(ylabel=f'{phase}')
axs[0][plt_indx].grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
axs[0][plt_indx].legend()
# axs[0][plt_indx].label_outer()
plt_indx += 1
# plot Dx
plt_indx = 0
lowest_Dx_x = np.argmin(np.array(Dx_hist))
lowest_Dx_y = Dx_hist[lowest_Dx_x]
axs[1][plt_indx].annotate("{:.4f}".format(lowest_Dx_y), [lowest_Dx_x, lowest_Dx_y])
axs[1][plt_indx].plot(Dx_hist, '-x', label=f'Dx', markevery = [lowest_Dx_x])
axs[1][plt_indx].set_xlabel(xlabel='epochs')
axs[1][plt_indx].set_ylabel(ylabel='Dx')
axs[1][plt_indx].grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
axs[1][plt_indx].legend()
# axs[1][plt_indx].label_outer()
plt_indx += 1
# plot D_Gz
highest_D_Gz_x = np.argmax(np.array(D_Gz_hist))
highest_D_Gz_y = D_Gz_hist[highest_D_Gz_x]
axs[1][plt_indx].annotate("{:.4f}".format(highest_D_Gz_y), [highest_D_Gz_x, highest_D_Gz_y])
axs[1][plt_indx].plot(D_Gz_hist, '-x', label=f'D Gz', markevery = [highest_D_Gz_x])
axs[1][plt_indx].set_xlabel(xlabel='epochs')
axs[1][plt_indx].set_ylabel(ylabel='D Gz')
axs[1][plt_indx].grid(color = 'green', linestyle = '--', linewidth = 0.5, alpha=0.75)
axs[1][plt_indx].legend()
# axs[1][plt_indx].label_outer()
fig.suptitle(f'{title}')
make_dir(dir)
plt.savefig(f'{dir}/{filename}.jpg')
plt.clf()