-
Notifications
You must be signed in to change notification settings - Fork 140
/
utils.py
75 lines (62 loc) · 2.52 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
# Copyright (C) 2018 Artsiom Sanakoyeu and Dmytro Kotovenko
#
# This file is part of Adaptive Style Transfer
#
# Adaptive Style Transfer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Adaptive Style Transfer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import division
import math
import scipy.misc
from scipy.ndimage.filters import gaussian_filter
import numpy as np
from ops import *
import random
import copy
def save_batch(input_painting_batch, input_photo_batch, output_painting_batch, output_photo_batch, filepath):
"""
Concatenates, processes and stores batches as image 'filepath'.
Args:
input_painting_batch: numpy array of size [B x H x W x C]
input_photo_batch: numpy array of size [B x H x W x C]
output_painting_batch: numpy array of size [B x H x W x C]
output_photo_batch: numpy array of size [B x H x W x C]
filepath: full name with path of file that we save
Returns:
"""
def batch_to_img(batch):
return np.reshape(batch,
newshape=(batch.shape[0]*batch.shape[1], batch.shape[2], batch.shape[3]))
inputs = np.concatenate([batch_to_img(input_painting_batch), batch_to_img(input_photo_batch)],
axis=0)
outputs = np.concatenate([batch_to_img(output_painting_batch), batch_to_img(output_photo_batch)],
axis=0)
to_save = np.concatenate([inputs,outputs], axis=1)
to_save = np.clip(to_save, a_min=0., a_max=255.).astype(np.uint8)
scipy.misc.imsave(filepath, arr=to_save)
def normalize_arr_of_imgs(arr):
"""
Normalizes an array so that the result lies in [-1; 1].
Args:
arr: numpy array of arbitrary shape and dimensions.
Returns:
"""
return arr/127.5 - 1.
# return (arr - np.mean(arr)) / np.std(arr)
def denormalize_arr_of_imgs(arr):
"""
Inverse of the normalize_arr_of_imgs function.
Args:
arr: numpy array of arbitrary shape and dimensions.
Returns:
"""
return (arr + 1.) * 127.5