-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.py
160 lines (130 loc) · 4.75 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
import torch
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
import os
import imageio
import random
# For logger
def to_np(x):
return x.data.cpu().numpy()
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
# De-normalization
def denorm(x):
out = (x + 1) / 2
return out.clamp(0, 1)
# Plot losses
def plot_loss(avg_losses, num_epochs, save=False, save_dir='results/', show=False):
fig, ax = plt.subplots()
ax.set_xlim(0, num_epochs)
temp = 0.0
for i in range(len(avg_losses)):
temp = max(np.max(avg_losses[i]), temp)
ax.set_ylim(0, temp*1.1)
plt.xlabel('# of Epochs')
plt.ylabel('Loss values')
plt.plot(avg_losses[0], label='D_A')
plt.plot(avg_losses[1], label='D_B')
plt.plot(avg_losses[2], label='G_A')
plt.plot(avg_losses[3], label='G_B')
plt.plot(avg_losses[4], label='cycle_A')
plt.plot(avg_losses[5], label='cycle_B')
plt.legend()
# save figure
if save:
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_fn = save_dir + 'Loss_values_epoch_{:d}'.format(num_epochs) + '.png'
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
def plot_train_result(real_image, gen_image, recon_image, epoch, save=False, save_dir='results/', show=False, fig_size=(5, 5)):
fig, axes = plt.subplots(2, 3, figsize=fig_size)
imgs = [to_np(real_image[0]), to_np(gen_image[0]), to_np(recon_image[0]),
to_np(real_image[1]), to_np(gen_image[1]), to_np(recon_image[1])]
for ax, img in zip(axes.flatten(), imgs):
ax.axis('off')
ax.set_adjustable('box-forced')
# Scale to 0-255
img = img.squeeze()
img = (((img - img.min()) * 255) / (img.max() - img.min())).transpose(1, 2, 0).astype(np.uint8)
ax.imshow(img, cmap=None, aspect='equal')
plt.subplots_adjust(wspace=0, hspace=0)
title = 'Epoch {0}'.format(epoch + 1)
fig.text(0.5, 0.04, title, ha='center')
# save figure
if save:
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_fn = save_dir + 'Result_epoch_{:d}'.format(epoch+1) + '.png'
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
def plot_test_result(real_image, gen_image, recon_image, index, save=False, save_dir='results/', show=False):
fig_size = (real_image.size(2) * 3 / 100, real_image.size(3) / 100)
fig, axes = plt.subplots(1, 3, figsize=fig_size)
imgs = [to_np(real_image), to_np(gen_image), to_np(recon_image)]
for ax, img in zip(axes.flatten(), imgs):
ax.axis('off')
ax.set_adjustable('box-forced')
# Scale to 0-255
img = img.squeeze()
img = (((img - img.min()) * 255) / (img.max() - img.min())).transpose(1, 2, 0).astype(np.uint8)
ax.imshow(img, cmap=None, aspect='equal')
plt.subplots_adjust(wspace=0, hspace=0)
# save figure
if save:
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_fn = save_dir + 'Test_result_{:d}'.format(index + 1) + '.png'
fig.subplots_adjust(bottom=0)
fig.subplots_adjust(top=1)
fig.subplots_adjust(right=1)
fig.subplots_adjust(left=0)
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
# Make gif
def make_gif(dataset, num_epochs, save_dir='results/'):
gen_image_plots = []
for epoch in range(num_epochs):
# plot for generating gif
save_fn = save_dir + 'Result_epoch_{:d}'.format(epoch + 1) + '.png'
gen_image_plots.append(imageio.imread(save_fn))
imageio.mimsave(save_dir + dataset + '_CycleGAN_epochs_{:d}'.format(num_epochs) + '.gif', gen_image_plots, fps=5)
class ImagePool():
def __init__(self, pool_size):
self.pool_size = pool_size
if self.pool_size > 0:
self.num_imgs = 0
self.images = []
def query(self, images):
if self.pool_size == 0:
return images
return_images = []
for image in images.data:
image = torch.unsqueeze(image, 0)
if self.num_imgs < self.pool_size:
self.num_imgs = self.num_imgs + 1
self.images.append(image)
return_images.append(image)
else:
p = random.uniform(0, 1)
if p > 0.5:
random_id = random.randint(0, self.pool_size-1)
tmp = self.images[random_id].clone()
self.images[random_id] = image
return_images.append(tmp)
else:
return_images.append(image)
return_images = Variable(torch.cat(return_images, 0))
return return_images