-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
233 lines (204 loc) · 7.25 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import cv2
import glob
import torch
import imageio
import numpy as np
import pandas as pd
import seaborn as sn
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from scipy.io import wavfile
from PIL import Image
from losses.losses import *
def set_lr(optimizer, lrs):
if(len(lrs) == 1):
for param in optimizer.param_groups:
param['lr'] = lrs[0]
else:
for i, param in enumerate(optimizer.param_groups):
param['lr'] = lrs[i]
def get_lr(optimizer):
optim_param_groups = optimizer.param_groups
if(len(optim_param_groups) == 1):
return optim_param_groups[0]['lr']
else:
lrs = []
for param in optim_param_groups:
lrs.append(param['lr'])
return lrs
def histogram_sizes(img_dir, h_lim = None, w_lim = None):
hs, ws = [], []
for file in glob.iglob(os.path.join(img_dir, '**/*.*')):
try:
with Image.open(file) as im:
h, w = im.size
hs.append(h)
ws.append(w)
except:
print('Not an Image file')
if(h_lim is not None and w_lim is not None):
hs = [h for h in hs if h<h_lim]
ws = [w for w in ws if w<w_lim]
plt.figure('Height')
plt.hist(hs)
plt.figure('Width')
plt.hist(ws)
plt.show()
return hs, ws
def generate_noise(bs, nz, device):
noise = torch.randn(bs, nz, 1, 1, device = device)
return noise
def plot_multiple_images(images, h, w):
fig = plt.figure(figsize=(8, 8))
for i in range(1, h*w+1):
img = images[i-1]
fig.add_subplot(h, w, i)
if(img.shape[2] == 1):
img = img.reshape(img.shape[0], img.shape[1])
plt.imshow(img, cmap = 'gray')
plt.show()
return fig
def get_display_samples(samples, num_samples_x, num_samples_y):
sz = samples[0].shape[0]
nc = samples[0].shape[2]
display = np.zeros((sz*num_samples_x, sz*num_samples_y, nc))
for i in range(num_samples_y):
for j in range(num_samples_x):
display[i*sz:(i+1)*sz, j*sz:(j+1)*sz, :] = cv2.cvtColor(samples[i*num_samples_x+j]*255.0, cv2.COLOR_BGR2RGB)
return display.astype(np.uint8)
def save(filename, netD, netG, optD, optG):
state = {
'netD' : netD.state_dict(),
'netG' : netG.state_dict(),
'optD' : optD.state_dict(),
'optG' : optG.state_dict()
}
torch.save(state, filename)
def load(filename, netD, netG, optD, optG):
state = torch.load(filename)
netD.load_state_dict(state['netD'])
netG.load_state_dict(state['netG'])
optD.load_state_dict(state['optD'])
optG.load_state_dict(state['optG'])
def save_fig(filename, fig):
fig.savefig(filename)
def rgb_to_ab(img):
ab_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2LAB)[:, :, 1:]
ab_img = (ab_img - 128.0) / 127.0
ab_img = torch.from_numpy(ab_img.transpose(2, 0, 1)).float()
return ab_img
def rgb_to_l(img):
l_img = np.expand_dims(cv2.cvtColor(np.array(img), cv2.COLOR_RGB2LAB)[:, :, 0], axis = 2)
l_img = l_img * 2.0 / 100.0 - 1.0
l_img = torch.from_numpy(l_img.transpose(2, 0, 1)).float()
return l_img
def lab_to_rgb(img):
l, a, b = (img[:, :, 0] + 1.0) * 100.0 / 2.0, img[:, :, 1] * 127.0 + 128.0, img[:, :, 2] * 127.0 + 128.0
lab = np.dstack([l, a, b]).astype(np.uint8)
rgb = cv2.cvtColor(lab, cv2.COLOR_LAB2RGB)
return rgb
def get_sample_images_list(mode, inputs):
if(mode == 'Pix2pix_Normal'):
val_data, netG, device = inputs[0], inputs[1], inputs[2]
netG.eval()
with torch.no_grad():
val_x = val_data[0].to(device)
val_y = val_data[1].to(device)
sample_input_images = val_x.detach().cpu().numpy() # l (C, H, W)
sample_input_images_list = []
sample_output_images = val_y.detach().cpu().numpy()# real ab
sample_output_images_list = []
sample_fake_images = netG(val_x).detach().cpu().numpy() # fake ab
sample_fake_images_list = []
sample_images_list = []
for j in range(3):
cur_img = (sample_fake_images[j] + 1) / 2.0
sample_fake_images_list.append(cur_img.transpose(1, 2, 0))
for j in range(3):
cur_img = (sample_input_images[j] + 1) / 2.0
sample_input_images_list.append(cur_img.transpose(1, 2, 0))
for j in range(3):
cur_img = (sample_output_images[j] + 1) / 2.0
sample_output_images_list.append(cur_img.transpose(1, 2, 0))
netG.train()
sample_images_list.extend(sample_input_images_list)
sample_images_list.extend(sample_fake_images_list)
sample_images_list.extend(sample_output_images_list)
elif(mode == 'Pix2pix_Colorization'):
val_data, netG, device = inputs[0], inputs[1], inputs[2]
netG.eval()
with torch.no_grad():
val_x = val_data[0].to(device)
val_y = val_data[1].to(device)
sample_input_images = val_x.detach().cpu().numpy() # l (C, H, W)
sample_input_images_list = []
sample_output_images = val_y.detach().cpu().numpy()# real ab
sample_output_images_list = []
sample_fake_images = netG(val_x).detach().cpu().numpy() # fake ab
sample_fake_images_list = []
sample_images_list = []
for j in range(3):
cur_img_1 = sample_input_images[j].transpose(1, 2, 0)
cur_img_2 = sample_output_images[j].transpose(1, 2, 0)
cur_img = lab_to_rgb(np.concatenate([cur_img_1, cur_img_2], axis = 2))
sample_output_images_list.append(cur_img)
for j in range(3):
cur_img_1 = sample_input_images[j].transpose(1, 2, 0)
cur_img_2 = sample_fake_images[j].transpose(1, 2, 0)
cur_img = lab_to_rgb(np.concatenate([cur_img_1, cur_img_2], axis = 2))
sample_fake_images_list.append(cur_img)
netG.train()
sample_images_list.extend(sample_fake_images_list)
sample_images_list.extend(sample_output_images_list)
elif(mode == 'Pix2pixHD_Normal'):
val_data, netG, stage, device = inputs[0], inputs[1], inputs[2], inputs[3]
netG.eval()
with torch.no_grad():
val_x = val_data[0].to(device)
val_y = val_data[1].to(device)
sample_input_images = val_x.detach().cpu().numpy() # l (C, H, W)
sample_input_images_list = []
sample_output_images = val_y.detach().cpu().numpy()# real ab
sample_output_images_list = []
sample_fake_images = netG(val_x, stage).detach().cpu().numpy() # fake ab
sample_fake_images_list = []
sample_images_list = []
for j in range(3):
cur_img = (sample_fake_images[j] + 1) / 2.0
sample_fake_images_list.append(cur_img.transpose(1, 2, 0))
for j in range(3):
cur_img = (sample_input_images[j] + 1) / 2.0
sample_input_images_list.append(cur_img.transpose(1, 2, 0))
for j in range(3):
cur_img = (sample_output_images[j] + 1) / 2.0
sample_output_images_list.append(cur_img.transpose(1, 2, 0))
netG.train()
sample_images_list.extend(sample_input_images_list)
sample_images_list.extend(sample_fake_images_list)
sample_images_list.extend(sample_output_images_list)
return sample_images_list
def get_require_type(loss_type):
if(loss_type == 'SGAN' or loss_type == 'LSGAN' or loss_type == 'HINGEGAN' or loss_type == 'WGAN'):
require_type = 0
elif(loss_type == 'RASGAN' or loss_type == 'RALSGAN' or loss_type == 'RAHINGEGAN'):
require_type = 1
elif(loss_type == 'QPGAN'):
require_type = 2
else:
require_type = -1
return require_type
def get_gan_loss(device, loss_type):
loss_dict = {'SGAN':SGAN, 'LSGAN':LSGAN, 'HINGEGAN':HINGEGAN, 'WGAN':WGAN, 'RASGAN':RASGAN, 'RALSGAN':RALSGAN, 'RAHINGEGAN':RAHINGEGAN, 'QPGAN':QPGAN}
require_type = get_require_type(loss_type)
if(require_type == 0):
loss = loss_dict[loss_type](device)
elif(require_type == 1):
loss = loss_dict[loss_type](device)
elif(require_type == 2):
loss = loss_dict[loss_type](device, 'L1')
else:
loss = None
return loss