-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
97 lines (82 loc) · 3.37 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
# MIT License
#
# Copyright (c) 2019 Drew Szurko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import shutil
import numpy as np
import tensorflow as tf
from absl import flags
from tqdm.autonotebook import tqdm
FLAGS = flags.FLAGS
def img_merge(images, n_rows=None, n_cols=None, padding=0, pad_value=0):
images = (tf.cast(images, tf.float32) + 1.0) * 127.5
images = np.array(images)
n = images.shape[0]
if n_rows:
n_rows = max(min(n_rows, n), 1)
n_cols = int(n - 0.5) // n_rows + 1
elif n_cols:
n_cols = max(min(n_cols, n), 1)
n_rows = int(n - 0.5) // n_cols + 1
else:
n_rows = int(n**0.5)
n_cols = int(n - 0.5) // n_rows + 1
h, w = images.shape[1], images.shape[2]
shape = (h * n_rows + padding * (n_rows - 1), w * n_cols + padding * (n_cols - 1))
if images.ndim == 4:
shape += (images.shape[3], )
img = np.full(shape, pad_value, dtype=images.dtype)
for idx, image in enumerate(images):
i = idx % n_cols
j = idx // n_cols
img[j * (h + padding):j * (h + padding) + h, i * (w + padding):i *
(w + padding) + w, ...] = image
return img
def save_image_grid(img_grid, epoch):
"""Saves image grid to user output dir."""
file_name = FLAGS.dataset + f'_{epoch}.png'
output_dir = os.path.join(FLAGS.output_dir, file_name)
tf.io.write_file(output_dir, tf.image.encode_png(tf.cast(img_grid, tf.uint8)))
def get_terminal_width():
width = shutil.get_terminal_size(fallback=(200, 24))[0]
if width == 0:
width = 120
return width
def pbar(total_images, batch_size, epoch, epochs):
bar = tqdm(total=(total_images // batch_size) * batch_size,
ncols=int(get_terminal_width() * .9),
desc=tqdm.write(f'Epoch {epoch + 1}/{epochs}'),
postfix={
'g_loss': f'{0:6.3f}',
'd_loss': f'{0:6.3f}',
1: 1
},
bar_format='{n_fmt}/{total_fmt} |{bar}| {rate_fmt} '
'ETA: {remaining} Elapsed Time: {elapsed} '
'G Loss: {postfix[g_loss]} D Loss: {postfix['
'd_loss]}',
unit=' images',
miniters=10)
return bar