forked from NextAkatsuki/hairGAN_pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_lib.py
192 lines (159 loc) · 7.02 KB
/
tf_lib.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
import tensorflow as tf
import multiprocessing
def batch_dataset(dataset,
batch_size,
drop_remainder=True,
n_prefetch_batch=1,
filter_fn=None,
map_fn=None,
n_map_threads=None,
filter_after_map=False,
shuffle=True,
shuffle_buffer_size=None,
repeat=None):
# set defaults
if n_map_threads is None:
n_map_threads = multiprocessing.cpu_count()
if shuffle and shuffle_buffer_size is None:
shuffle_buffer_size = max(batch_size * 128, 2048) # set the minimum buffer size as 2048
# [*] it is efficient to conduct `shuffle` before `map`/`filter` because `map`/`filter` is sometimes costly
if shuffle:
dataset = dataset.shuffle(shuffle_buffer_size)
if not filter_after_map:
if filter_fn:
dataset = dataset.filter(filter_fn)
if map_fn:
dataset = dataset.map(map_fn, num_parallel_calls=n_map_threads)
else: # [*] this is slower
if map_fn:
dataset = dataset.map(map_fn, num_parallel_calls=n_map_threads)
if filter_fn:
dataset = dataset.filter(filter_fn)
dataset = dataset.batch(batch_size, drop_remainder=drop_remainder)
dataset = dataset.repeat(repeat).prefetch(n_prefetch_batch)
return dataset
def memory_data_batch_dataset(memory_data,
batch_size,
drop_remainder=True,
n_prefetch_batch=1,
filter_fn=None,
map_fn=None,
n_map_threads=None,
filter_after_map=False,
shuffle=True,
shuffle_buffer_size=None,
repeat=None):
"""Batch dataset of memory data.
Parameters
----------
memory_data : nested structure of tensors/ndarrays/lists
"""
dataset = tf.data.Dataset.from_tensor_slices(memory_data)
dataset = batch_dataset(dataset,
batch_size,
drop_remainder=drop_remainder,
n_prefetch_batch=n_prefetch_batch,
filter_fn=filter_fn,
map_fn=map_fn,
n_map_threads=n_map_threads,
filter_after_map=filter_after_map,
shuffle=shuffle,
shuffle_buffer_size=shuffle_buffer_size,
repeat=repeat)
return dataset
def disk_image_batch_dataset(img_paths,
batch_size,
labels=None,
drop_remainder=True,
n_prefetch_batch=1,
filter_fn=None,
map_fn=None,
n_map_threads=None,
filter_after_map=False,
shuffle=True,
shuffle_buffer_size=None,
repeat=None):
"""Batch dataset of disk image for PNG and JPEG.
Parameters
----------
img_paths : 1d-tensor/ndarray/list of str
labels : nested structure of tensors/ndarrays/lists
"""
if labels is None:
memory_data = img_paths
else:
memory_data = (img_paths, labels)
def parse_fn(path, *label):
img = tf.io.read_file(path)
img = tf.image.decode_png(img, 3) # fix channels to 3
return (img,) + label
if map_fn: # fuse `map_fn` and `parse_fn`
def map_fn_(*args):
return map_fn(*parse_fn(*args))
else:
map_fn_ = parse_fn
dataset = memory_data_batch_dataset(memory_data,
batch_size,
drop_remainder=drop_remainder,
n_prefetch_batch=n_prefetch_batch,
filter_fn=filter_fn,
map_fn=map_fn_,
n_map_threads=n_map_threads,
filter_after_map=filter_after_map,
shuffle=shuffle,
shuffle_buffer_size=shuffle_buffer_size,
repeat=repeat)
return dataset
class Checkpoint:
"""Enhanced "tf.train.Checkpoint"."""
def __init__(self,
checkpoint_kwargs, # for "tf.train.Checkpoint"
directory, # for "tf.train.CheckpointManager"
max_to_keep=5,
keep_checkpoint_every_n_hours=None):
self.checkpoint = tf.train.Checkpoint(**checkpoint_kwargs)
self.manager = tf.train.CheckpointManager(self.checkpoint, directory, max_to_keep, keep_checkpoint_every_n_hours)
def restore(self, save_path=None):
save_path = self.manager.latest_checkpoint if save_path is None else save_path
return self.checkpoint.restore(save_path)
def save(self, file_prefix_or_checkpoint_number=None, session=None):
if isinstance(file_prefix_or_checkpoint_number, str):
return self.checkpoint.save(file_prefix_or_checkpoint_number, session=session)
else:
return self.manager.save(checkpoint_number=file_prefix_or_checkpoint_number)
def __getattr__(self, attr):
if hasattr(self.checkpoint, attr):
return getattr(self.checkpoint, attr)
elif hasattr(self.manager, attr):
return getattr(self.manager, attr)
else:
self.__getattribute__(attr) # this will raise an exception
def summary(name_data_dict,
step=None,
types=['mean', 'std', 'max', 'min', 'sparsity', 'histogram'],
historgram_buckets=None,
name='summary'):
"""Summary.
Examples
--------
>>> summary({'a': data_a, 'b': data_b})
"""
def _summary(name, data):
if data.shape == ():
tf.summary.scalar(name, data, step=step)
else:
if 'mean' in types:
tf.summary.scalar(name + '-mean', tf.math.reduce_mean(data), step=step)
if 'std' in types:
tf.summary.scalar(name + '-std', tf.math.reduce_std(data), step=step)
if 'max' in types:
tf.summary.scalar(name + '-max', tf.math.reduce_max(data), step=step)
if 'min' in types:
tf.summary.scalar(name + '-min', tf.math.reduce_min(data), step=step)
if 'sparsity' in types:
tf.summary.scalar(name + '-sparsity', tf.math.zero_fraction(data), step=step)
if 'histogram' in types:
tf.summary.histogram(name, data, step=step, buckets=historgram_buckets)
with tf.name_scope(name):
for name, data in name_data_dict.items():
_summary(name, data)