-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
329 lines (245 loc) · 11.6 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from math import exp
import numpy as np
import os
import re
import csv
import time
import pickle
import logging
import torch
from torch.nn.functional import conv2d
from torchvision import datasets, transforms
import torchvision.utils
from torch.utils import data
import torch.nn.functional as F
import math
from options import HiDDenConfiguration, TrainingOptions
from model.ARWGAN import ARWGAN
def image_to_tensor(image):
"""
Transforms a numpy-image into torch tensor
:param image: (batch_size x height x width x channels) uint8 array
:return: (batch_size x channels x height x width) torch tensor in range [-1.0, 1.0]
"""
image_tensor = torch.Tensor(image)
image_tensor.unsqueeze_(0)
image_tensor = image_tensor.permute(0, 3, 1, 2)
image_tensor = image_tensor / 127.5 - 1
return image_tensor
def tensor_to_image(tensor):
"""
Transforms a torch tensor into numpy uint8 array (image)
:param tensor: (batch_size x channels x height x width) torch tensor in range [-1.0, 1.0]
:return: (batch_size x height x width x channels) uint8 array
"""
image = tensor.permute(0, 2, 3, 1).cpu().numpy()
image = (image + 1) * 127.5
return np.clip(image, 0, 255).astype(np.uint8)
def save_images(original_images, watermarked_images, epoch, folder, resize_to=None):
images = original_images[:original_images.shape[0], :, :, :].cpu()
watermarked_images = watermarked_images[:watermarked_images.shape[0], :, :, :].cpu()
# scale values to range [0, 1] from original range of [-1, 1]
images = (images + 1) / 2
watermarked_images = (watermarked_images + 1) / 2
if resize_to is not None:
images = F.interpolate(images, size=resize_to)
watermarked_images = F.interpolate(watermarked_images, size=resize_to)
stacked_images = torch.cat([images, watermarked_images], dim=0)
filename = os.path.join(folder, 'epoch-{}.png'.format(epoch))
torchvision.utils.save_image(stacked_images, filename, original_images.shape[0], normalize=False)
def sorted_nicely(l):
""" Sort the given iterable in the way that humans expect."""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def last_checkpoint_from_folder(folder: str):
last_file = sorted_nicely(os.listdir(folder))[-1]
last_file = os.path.join(folder, last_file)
return last_file
def save_checkpoint(model: ARWGAN, experiment_name: str, epoch: int, checkpoint_folder: str):
""" Saves a checkpoint at the end of an epoch. """
if not os.path.exists(checkpoint_folder):
os.makedirs(checkpoint_folder)
checkpoint_filename = f'{experiment_name}--epoch-{epoch}.pyt'
checkpoint_filename = os.path.join(checkpoint_folder, checkpoint_filename)
logging.info('Saving checkpoint to {}'.format(checkpoint_filename))
checkpoint = {
'enc-dec-model': model.encoder_decoder.state_dict(),
'enc-dec-optim': model.optimizer_enc_dec.state_dict(),
'discrim-model': model.discriminator.state_dict(),
'discrim-optim': model.optimizer_discrim.state_dict(),
'epoch': epoch
}
torch.save(checkpoint, checkpoint_filename)
logging.info('Saving checkpoint done.')
def load_last_checkpoint(checkpoint_folder):
""" Load the last checkpoint from the given folder """
last_checkpoint_file = last_checkpoint_from_folder(checkpoint_folder)
checkpoint = torch.load(last_checkpoint_file)
return checkpoint, last_checkpoint_file
def model_from_checkpoint(ARWGAN, checkpoint):
""" Restores the net object from a checkpoint object """
ARWGAN.encoder_decoder.load_state_dict(checkpoint['enc-dec-model'])
ARWGAN.optimizer_enc_dec.load_state_dict(checkpoint['enc-dec-optim'])
ARWGAN.discriminator.load_state_dict(checkpoint['discrim-model'])
ARWGAN.optimizer_discrim.load_state_dict(checkpoint['discrim-optim'])
def load_options(options_file_name) -> (TrainingOptions, HiDDenConfiguration, dict):
""" Loads the training, model, and noise configurations from the given folder """
with open(os.path.join(options_file_name), 'rb') as f:
train_options = pickle.load(f)
noise_config = pickle.load(f)
net_config = pickle.load(f)
# for backward-capability. Some models were trained and saved before .enable_fp16 was added
if not hasattr(net_config, 'enable_fp16'):
setattr(net_config, 'enable_fp16', False)
return train_options, net_config, noise_config
def get_data_loaders(network_config: HiDDenConfiguration, train_options: TrainingOptions):
""" Get torch data loaders for training and validation. The data loaders take a crop of the image,
transform it into tensor, and normalize it."""
data_transforms = {
'train': transforms.Compose([
transforms.RandomCrop((network_config.H, network_config.W), pad_if_needed=True),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]),
'test': transforms.Compose([
transforms.CenterCrop((network_config.H, network_config.W)),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
}
train_images = datasets.ImageFolder(train_options.train_folder, data_transforms['train'])
train_loader = torch.utils.data.DataLoader(train_images, batch_size=train_options.batch_size, shuffle=True,
num_workers=4)
validation_images = datasets.ImageFolder(train_options.validation_folder, data_transforms['test'])
validation_loader = torch.utils.data.DataLoader(validation_images, batch_size=train_options.batch_size,
shuffle=False, num_workers=4)
return train_loader, validation_loader
def log_progress(losses_accu):
log_print_helper(losses_accu, logging.info)
def print_progress(losses_accu):
log_print_helper(losses_accu, print)
def log_print_helper(losses_accu, log_or_print_func):
max_len = max([len(loss_name) for loss_name in losses_accu])
for loss_name, loss_value in losses_accu.items():
log_or_print_func(loss_name.ljust(max_len + 4) + '{:.4f}'.format(loss_value.avg))
def create_folder_for_run(runs_folder, experiment_name):
if not os.path.exists(runs_folder):
os.makedirs(runs_folder)
this_run_folder = os.path.join(runs_folder, f'{experiment_name} {time.strftime("%Y.%m.%d--%H-%M-%S")}')
os.makedirs(this_run_folder)
os.makedirs(os.path.join(this_run_folder, 'checkpoints'))
os.makedirs(os.path.join(this_run_folder, 'images'))
return this_run_folder
def write_losses(file_name, losses_accu, epoch, duration):
with open(file_name, 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
if epoch == 1:
row_to_write = ['epoch'] + [loss_name.strip() for loss_name in losses_accu.keys()] + ['duration']
writer.writerow(row_to_write)
row_to_write = [epoch] + ['{:.4f}'.format(loss_avg.avg) for loss_avg in losses_accu.values()] + [
'{:.0f}'.format(duration)]
writer.writerow(row_to_write)
def gaussian(window_size, sigma):
"""Gaussian window.
https://en.wikipedia.org/wiki/Window_function#Gaussian_window
"""
_exp = [exp(-(x - window_size // 2) ** 2 / float(2 * sigma ** 2)) for x in range(window_size)]
gauss = torch.Tensor(_exp)
return gauss / gauss.sum()
def create_window(window_size, channel):
_1D_window = gaussian(window_size, 1.5).unsqueeze(1)
_2D_window = _1D_window.mm(_1D_window.t()).float().unsqueeze(0).unsqueeze(0)
window = _2D_window.expand(channel, 1, window_size, window_size).contiguous()
return window
def _ssim(img1, img2, window, window_size, channel, size_average=True):
padding_size = window_size // 2
mu1 = conv2d(img1, window, padding=padding_size, groups=channel)
mu2 = conv2d(img2, window, padding=padding_size, groups=channel)
mu1_sq = mu1.pow(2)
mu2_sq = mu2.pow(2)
mu1_mu2 = mu1 * mu2
sigma1_sq = conv2d(img1 * img1, window, padding=padding_size, groups=channel) - mu1_sq
sigma2_sq = conv2d(img2 * img2, window, padding=padding_size, groups=channel) - mu2_sq
sigma12 = conv2d(img1 * img2, window, padding=padding_size, groups=channel) - mu1_mu2
C1 = 0.01**2
C2 = 0.03**2
_ssim_quotient = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2))
_ssim_divident = ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
ssim_map = _ssim_quotient / _ssim_divident
if size_average:
return ssim_map.mean()
else:
return ssim_map.mean(1).mean(1).mean(1)
def ssim(img1, img2, window_size=11, size_average=True):
(_, channel, _, _) = img1.size()
window = create_window(window_size, channel)
if img1.is_cuda:
window = window.cuda(img1.get_device())
window = window.type_as(img1)
return _ssim(img1, img2, window, window_size, channel, size_average)
def gaussian_kernel(sigma,kernel):
if type(sigma) is str:
sigma=float(sigma)
if type(kernel) is str:
kernel=int(kernel)
sigma_value=sigma*sigma
result=torch.zeros((kernel,kernel))
radius=int(kernel/2)
for i in range(kernel):
for j in range(kernel):
result[i,j]=(1/(math.pi*sigma_value))*(math.exp(-((i-radius)**2+(j-radius)**2)/sigma_value))
all=result.sum()
result=result/all
return result
def psnr(img1, img2):
mse = torch.mean((img1 - img2) ** 2)
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
def mse(img1, img2):
mse = torch.mean((img1 - img2) ** 2)
return mse
def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:
r"""Convert an RGB image to YCbCr.
.. image:: _static/img/rgb_to_ycbcr.png
Args:
image: RGB Image to be converted to YCbCr with shape :math:`(*, 3, H, W)`.
Returns:
YCbCr version of the image with shape :math:`(*, 3, H, W)`.
"""
if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(type(image)))
if len(image.shape) < 3 or image.shape[-3] != 3:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}".format(image.shape))
r: torch.Tensor = image[..., 0, :, :]
g: torch.Tensor = image[..., 1, :, :]
b: torch.Tensor = image[..., 2, :, :]
delta: float = 0.5
y: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
cb: torch.Tensor = (b - y) * 0.564 + delta
cr: torch.Tensor = (r - y) * 0.713 + delta
return torch.stack([y, cb, cr], -3)
def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor:
r"""Convert an YCbCr image to RGB.
The image data is assumed to be in the range of (0, 1).
Args:
image: YCbCr Image to be converted to RGB with shape :math:`(*, 3, H, W)`.
Returns:
RGB version of the image with shape :math:`(*, 3, H, W)`.
"""
if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(type(image)))
if len(image.shape) < 3 or image.shape[-3] != 3:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}".format(image.shape))
y: torch.Tensor = image[..., 0, :, :]
cb: torch.Tensor = image[..., 1, :, :]
cr: torch.Tensor = image[..., 2, :, :]
delta: float = 0.5
cb_shifted: torch.Tensor = cb - delta
cr_shifted: torch.Tensor = cr - delta
r: torch.Tensor = y + 1.403 * cr_shifted
g: torch.Tensor = y - 0.714 * cr_shifted - 0.344 * cb_shifted
b: torch.Tensor = y + 1.773 * cb_shifted
return torch.stack([r, g, b], -3)