-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathrevert_tfrecords.py
55 lines (44 loc) · 2.09 KB
/
revert_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 argparse
import tensorflow as tf
parser = argparse.ArgumentParser()
parser.add_argument('--records_name', type=str, required=True,
help='Name of tfrecords')
parser.add_argument('--output_path', type=str, required=True,
help="Folder to store the data")
parser.add_argument('--list_file', type=str, required=False, default='filelist.txt',
help='File to record the list of the data')
parser.add_argument('--count', type=int, required=False, default=0,
help='Specify the count of the data to extract')
parser.add_argument('--file_type', type=str, required=False, default='data',
help='File type')
def read_data_from_tfrecords(records_name, output_path, list_file, file_type, count):
records_iterator = tf.python_io.tf_record_iterator(records_name)
count = count if count != 0 else float('Inf')
with open(os.path.join(output_path, list_file), "w") as f:
num = 0
for string_record in records_iterator:
if num >= count: break
example = tf.train.Example()
example.ParseFromString(string_record)
label = int(example.features.feature['label'].int64_list.value[0])
# index = int(example.features.feature['index'].int64_list.value[0])
octree = example.features.feature[file_type].bytes_list.value[0]
if 'filename' in example.features.feature:
filename = example.features.feature['filename'].bytes_list.value[0] \
.decode('utf8').replace('/', '_').replace('\\', '_')
else:
filename = '%06d.%s' % (num, file_type)
num += 1
with open(os.path.join(output_path, filename), 'wb') as fo:
fo.write(octree)
f.write("{} {}\n".format(filename, label))
if __name__ == '__main__':
args = parser.parse_args()
if not os.path.exists(args.output_path):
os.makedirs(args.output_path)
read_data_from_tfrecords(args.records_name,
args.output_path,
args.list_file,
args.file_type,
args.count)