-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.py
178 lines (147 loc) · 5.69 KB
/
util.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
import itertools, imageio, torch, random
import matplotlib.pyplot as plt
import numpy as np
from torchvision import datasets
from scipy.misc import imresize
from torch.autograd import Variable
def show_result(G, x_, y_, num_epoch, show = False, save = False, path = 'result.png'):
test_images = G(x_)
size_figure_grid = 3
fig, ax = plt.subplots(x_.size()[0], size_figure_grid, figsize=(5, 5))
for i, j in itertools.product(range(x_.size()[0]), range(size_figure_grid)):
ax[i, j].get_xaxis().set_visible(False)
ax[i, j].get_yaxis().set_visible(False)
for i in range(x_.size()[0]):
ax[i, 0].cla()
ax[i, 0].imshow((x_[i].cpu().data.numpy().transpose(1, 2, 0) + 1) / 2)
ax[i, 1].cla()
ax[i, 1].imshow((test_images[i].cpu().data.numpy().transpose(1, 2, 0) + 1) / 2)
ax[i, 2].cla()
ax[i, 2].imshow((y_[i].numpy().transpose(1, 2, 0) + 1) / 2)
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def show_train_hist(hist, show = False, save = False, path = 'Train_hist.png'):
x = range(len(hist['D_A_losses']))
y1 = hist['D_A_losses']
y2 = hist['D_B_losses']
y3 = hist['G_A_losses']
y4 = hist['G_B_losses']
y5 = hist['A_cycle_losses']
y6 = hist['B_cycle_losses']
plt.plot(x, y1, label='D_A_loss')
plt.plot(x, y2, label='D_B_loss')
plt.plot(x, y3, label='G_A_loss')
plt.plot(x, y4, label='G_B_loss')
plt.plot(x, y5, label='A_cycle_loss')
plt.plot(x, y6, label='B_cycle_loss')
plt.xlabel('Iter')
plt.ylabel('Loss')
plt.legend(loc=4)
plt.grid(True)
plt.tight_layout()
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def generate_animation(root, model, opt):
images = []
for e in range(opt.train_epoch):
img_name = root + 'Fixed_results/' + model + str(e + 1) + '.png'
images.append(imageio.imread(img_name))
imageio.mimsave(root + model + 'generate_animation.gif', images, fps=5)
def data_load(path, subfolder, transform, batch_size, shuffle=False):
dset = datasets.ImageFolder(path, transform)
ind = dset.class_to_idx[subfolder]
n = 0
for i in range(dset.__len__()):
if ind != dset.imgs[n][1]:
del dset.imgs[n]
n -= 1
n += 1
return torch.utils.data.DataLoader(dset, batch_size=batch_size, shuffle=shuffle)
def imgs_resize(imgs, resize_scale = 286):
outputs = torch.FloatTensor(imgs.size()[0], imgs.size()[1], resize_scale, resize_scale)
for i in range(imgs.size()[0]):
img = imresize(imgs[i].numpy(), [resize_scale, resize_scale])
outputs[i] = torch.FloatTensor((img.transpose(2, 0, 1).astype(np.float32).reshape(-1, imgs.size()[1], resize_scale, resize_scale) - 127.5) / 127.5)
return outputs
def random_crop(imgs, crop_size = 256):
outputs = torch.FloatTensor(imgs.size()[0], imgs.size()[1], crop_size, crop_size)
for i in range(imgs.size()[0]):
img = imgs[i]
rand1 = np.random.randint(0, imgs.size()[2] - crop_size)
rand2 = np.random.randint(0, imgs.size()[2] - crop_size)
outputs[i] = img[:, rand1: crop_size + rand1, rand2: crop_size + rand2]
return outputs
def random_fliplr(imgs):
outputs = torch.FloatTensor(imgs.size())
for i in range(imgs.size()[0]):
if torch.rand(1)[0] < 0.5:
img = torch.FloatTensor(
(np.fliplr(imgs[i].numpy().transpose(1, 2, 0)).transpose(2, 0, 1).reshape(-1, imgs.size()[1], imgs.size()[2], imgs.size()[3]) + 1) / 2)
outputs[i] = (img - 0.5) / 0.5
else:
outputs[i] = imgs[i]
return outputs
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)
class image_store():
def __init__(self, store_size=50):
self.store_size = store_size
self.num_img = 0
self.images = []
def query(self, image):
select_imgs = []
for i in range(image.size()[0]):
if self.num_img < self.store_size:
self.images.append(image)
select_imgs.append(image)
self.num_img += 1
else:
prob = np.random.uniform(0, 1)
if prob > 0.5:
ind = np.random.randint(0, self.store_size - 1)
select_imgs.append(self.images[ind])
self.images[ind] = image
else:
select_imgs.append(image)
return Variable(torch.cat(select_imgs, 0))
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