-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5.convert_train_binary_tfrecord.py
163 lines (121 loc) · 5.58 KB
/
5.convert_train_binary_tfrecord.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
# from absl import app, flags, logging
# from absl.flags import FLAGS
# import os
# import tqdm
# import glob
# import random
# import tensorflow as tf
# from tensorflow.python.client import device_lib
# print(device_lib.list_local_devices())
# os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"]="0"
# #flags.DEFINE_string('dataset_path', './train/ms1m_align_112/imgs',
# # 'path to dataset')
# #flags.DEFINE_string('output_path', './train/ms1m_kface_bin.tfrecord',
# # 'path to ouput tfrecord')
# def _bytes_feature(value):
# """Returns a bytes_list from a string / byte."""
# if isinstance(value, type(tf.constant(0))):
# value = value.numpy()
# return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
# def _float_feature(value):
# """Returns a float_list from a float / double."""
# return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
# def _int64_feature(value):
# """Returns an int64_list from a bool / enum / int / uint."""
# return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
# def make_example(img_str, source_id, filename):
# # Create a dictionary with features that may be relevant.
# feature = {'image/source_id': _int64_feature(source_id),
# 'image/filename': _bytes_feature(filename),
# 'image/encoded': _bytes_feature(img_str)}
# return tf.train.Example(features=tf.train.Features(feature=feature))
# def main():
# #dataset_path = FLAGS.dataset_path
# if not os.path.isdir(dataset_path):
# print('Please define valid dataset path.')
# else:
# print('Loading {}'.format(dataset_path))
# samples = []
# print('Reading data list...')
# for id_name in tqdm.tqdm(os.listdir(dataset_path)):
# img_paths = glob.glob(os.path.join(dataset_path, id_name, '*.jpg'))
# for img_path in img_paths:
# filename = os.path.join(id_name, os.path.basename(img_path))
# samples.append((img_path, id_name, filename))
# random.shuffle(samples)
# print('Writing tfrecord file...')
# with tf.io.TFRecordWriter(output_path) as writer:
# for img_path, id_name, filename in tqdm.tqdm(samples):
# #print(img_path)
# tf_example = make_example(img_str=open(img_path, 'rb').read(),
# source_id=int(id_name),
# filename=str.encode(filename))
# writer.write(tf_example.SerializeToString())
# if __name__ == '__main__':
# dataset_path = 'preprocessing/data/k-face/new_path/rename_train_align_112'
# output_path = 'preprocessing/data/k-face/tfrecord_output'
# main()
# #try:
# # app.run(main)
# #except SystemExit:
# # pass
import os
import tqdm
import glob
import random
import tensorflow as tf
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
if isinstance(value, type(tf.constant(0))):
value = value.numpy()
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
"""Returns a float_list from a float / double."""
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):
"""Returns an int64_list from a bool / enum / int / uint."""
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def make_example(img_str, source_id, filename):
# Create a dictionary with features that may be relevant.
feature = {'image/source_id': _int64_feature(source_id),
'image/filename': _bytes_feature(filename),
'image/encoded': _bytes_feature(img_str)}
return tf.train.Example(features=tf.train.Features(feature=feature))
def main():
dataset_path = 'preprocessing/data/k-face/new_path/rename_train_align_112'
output_path = 'preprocessing/data/k-face/tfrecord_output/ms1m_kface_bin.tfrecord'
if not os.path.isdir(dataset_path):
print('Please define valid dataset path.')
return
else:
print('Loading {}'.format(dataset_path))
# Ensure the output directory exists
output_dir = os.path.dirname(output_path)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
samples = []
print('Reading data list...')
for id_name in tqdm.tqdm(os.listdir(dataset_path)):
img_paths = glob.glob(os.path.join(dataset_path, id_name, '*.jpg'))
for img_path in img_paths:
filename = os.path.join(id_name, os.path.basename(img_path))
samples.append((img_path, id_name, filename))
random.shuffle(samples)
print('Writing tfrecord file...')
with tf.io.TFRecordWriter(output_path) as writer:
for img_path, id_name, filename in tqdm.tqdm(samples):
try:
img_str = open(img_path, 'rb').read()
tf_example = make_example(img_str=img_str,
source_id=int(id_name),
filename=str.encode(filename))
writer.write(tf_example.SerializeToString())
except Exception as e:
print(f"Error processing {img_path}: {e}")
if __name__ == '__main__':
main()