-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tfrecords.py
executable file
·55 lines (37 loc) · 1.46 KB
/
create_tfrecords.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
import os
import shutil
from glob import glob
import numpy as np
import tensorflow as tf
from tqdm import tqdm
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_list_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
def _string_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value.tostring()]))
def dict_to_tf_example(npy_path):
filename = npy_path.split('/')[-1]
feature_array = np.load(npy_path).astype(np.float32)
example = tf.train.Example(features=tf.train.Features(feature={
'filename': _bytes_feature(filename.encode()),
'feature': _string_feature(feature_array)
}))
return example
def main():
npy_dir = './datasets/ffhq-npy'
output_dir = './datasets/ffhq-npy-tfrecords'
print('Reading npys from:', npy_dir)
examples_list = glob(os.path.join(npy_dir, '*.npy'))
num_examples = len(examples_list)
print('Number of npys:', num_examples)
shutil.rmtree(output_dir, ignore_errors=True)
os.makedirs(output_dir)
for idx, example in tqdm(enumerate(examples_list)):
shard_path = os.path.join(output_dir, 'shard-%04d.tfrecords' % idx)
writer = tf.python_io.TFRecordWriter(shard_path)
tf_example = dict_to_tf_example(example)
writer.write(tf_example.SerializeToString())
writer.close()
print('Result is here:', output_dir)
main()