-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert_Dataset_to_TFRecord.py
362 lines (310 loc) · 12.6 KB
/
convert_Dataset_to_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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import sys
import os
import cv2
import numpy as np
from scipy.io import loadmat
import tensorflow as tf
from pycocotools.coco import COCO
def warning(msg):
orange = '\033[33m'
end = '\033[0m'
print(orange + msg + end)
return
def error(msg):
red = '\033[31m'
end = '\033[0m'
print(red + msg + end)
sys.exit(-1)
return
def check_filename(fname):
# TFRecord output filename
if os.path.isfile(fname):
choice = input('file {} already exist, overwrite ? [y/n] '.format(fname))
if not choice in ['y', 'Y']:
sys.exit(0)
return
def get_help():
gbold = '\033[1;32m'
green = '\033[0;32m'
dpath_1 = '../home/amusaal/DATA/Coco'
dpath_2 = '../home/amusaal/DATA/Cornell'
dtype_1 = 'val2014'
dtype_2 = 'kitchen'
mhelp = gbold + "This script is used to convert Coco and Cornell datasets to TFRecords file\n"
mhelp += "COCO:\t\tdataDir is the directory containing the folder dataType which contains all images\n"
mhelp += "\t\tCOCO Python API is required (pip install pycocotools)\n"
mhelp += "Cornell:\tdataDir is the directory containing the folder dataType (kitchen/office)\n"
mhelp += "\t\tIt also contains split files, and classnames files.\n\n"
mhelp += "--help [-h]\t\t\t" + green
mhelp += "Show help\n"
mhelp += gbold + "--dataDir [-d]\tPATH\t\t" + green
mhelp += "Path to the directory of the dataset\n"
mhelp += gbold + "--dataType [-t]\tNAME\t\t" + green
mhelp += "Name of the folder containing data. It will be used as the output prefix."
mhelp += gbold + "--overwrite [-o]\t\t\t" + green
mhelp += "Overwrite output files if they already exist.\n\n"
mhelp += gbold + "Example:\n" + green
mhelp += "python3 convert_Dataset_to_TFRecord.py --dataDir {} --dataType {}\n".format(dpath_1, dtype_1)
mhelp += "python3 convert_Dataset_to_TFRecord.py -d {} -t {}\n\n".format(dpath_2, dtype_2)
mhelp += '\033[0m'
return mhelp
def parser():
global dataDir, dataType, overwrite
argv = sys.argv
argc = len(argv)
help_ = get_help()
for a, arg in enumerate(argv):
if arg in ['--overwrite', '-o']:
overwrite = True
elif arg in ['--help', '-h']:
print(help_)
sys.exit()
elif a == argc-1:
break
elif arg in ['--help', '-h']:
print(help_)
sys.exit()
elif arg in ["--dataDir", "-d"]:
if os.path.isdir(argv[a + 1]):
dataDir = argv[a + 1]
else:
error('Error: Invalid directory {}'.format(argv[a + 1]))
elif arg in ["--dataType", "-t"]:
dataType = argv[a + 1]
else:
continue
if not os.path.isdir('{}/{}'.format(dataDir, dataType)):
error('Error: could not find {} in {}'. format(dataType,dataDir))
print("dataDir =\t" + dataDir)
print("dataType =\t" + dataType)
return
def load_image(filename):
img = cv2.imread(filename)
s = img.shape
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# img = img.astype(np.float32)
# img = cv2.imencode('.jpg', img)[1]
f = open(filename, "rb")
img = f.read()
f.close()
return s, img
def load_joints(f, b, bmat):
jts = []
tstates = []
for j in range(25):
tstates.append(bmat['body'][f, b][0, 0][1][0, j][0][0][0][0][0])
jts.append(bmat['body'][f, b][0, 0][1][0, j][0][0][5][0].tolist())
return tstates, jts
def load_classes(ddir, dataset, fname):
mgnd = loadmat("{}/{}_class/{}/gnd.mat".format(ddir, dataset, fname))
gnd = [gn[0] for gn in mgnd['gnd']]
return gnd
def int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def int64_feature_list(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def float_feature_list(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
def bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def gen_feature1(ft_image, height, width, nb_ob):
ft = {'image': ft_image,
'height': int64_feature(height),
'width': int64_feature(width),
'objects_number': int64_feature(nb_ob)}
return ft
def gen_feature2(ft_image, joints, tstates, aclass, bodies_id):
ft = {'image': ft_image,
'class': int64_feature(aclass),
'bodies': int64_feature_list(bodies_id),
'joints': float_feature_list(joints),
'trackingStates': int64_feature_list(tstates)}
return ft
# Generate Example for Coco Dataset
def gen_coco_example(dataDir, dataType):
global overwrite
# Open TFRecords file
filename = '{}/{}_coco.tfrecords'.format(dataDir, dataType)
if not overwrite:
check_filename(filename)
writer = tf.python_io.TFRecordWriter(filename)
# Annotations
annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataType)
if not os.path.isfile(annFile):
print("Error incorrect path: " + annFile)
sys.exit()
coco = COCO(annFile)
imgIds = coco.getImgIds()
nb_imgs = len(imgIds)
for i in range(nb_imgs):
# Print progression
if not i % 1000:
print("[{}/{}]".format(str(i), str(nb_imgs)))
sys.stdout.flush()
# Load image
im_id = imgIds[i]
im = coco.loadImgs([im_id])[0]
im_filename = im['file_name']
imshape, image = load_image("{}/{}/{}".format(dataDir, dataType, im_filename))
height, width, channels = imshape
# Labels
bbox = []
nb_ob = 0
annIds = coco.getAnnIds([im_id])
for a in coco.loadAnns(annIds):
label = a['category_id']
bb = np.array(a['bbox']) / np.array((width,height,width,height))
bbox.append([label] + bb.tolist())
nb_ob += 1
if not len(bbox) > 0:
continue
bbox = np.array(bbox, dtype=np.float32)#.flatten()
# Features
# bytes_image = tf.compat.as_bytes(image.tostring())
bytes_image = tf.compat.as_bytes(image)
feature_image = bytes_feature(bytes_image)
feature = gen_feature1(feature_image, height, width, nb_ob)
# Feature list
context = tf.train.Features(feature=feature)
fl_bboxes = tf.train.FeatureList(feature=[float_feature_list(bbo) for bbo in bbox])
feature_list = tf.train.FeatureLists(feature_list={'bboxes': fl_bboxes})
# Example
# example = tf.train.Example(features=tf.train.Features(feature=feature))
example = tf.train.SequenceExample(context=context, feature_lists=feature_list)
# Serialize
writer.write(example.SerializeToString())
# Close writer
writer.close()
return
# Generate Example for Cornell Dataset
def gen_cornell_example(dataDir, dataType, split=None):
global overwrite
# Open TFRecords file
filename = '{}/{}_{}.tfrecords'.format(dataDir, dataType, split[:split.index('_')])
if not overwrite:
check_filename(filename)
writer = tf.python_io.TFRecordWriter(filename)
path = "{}/{}/".format(dataDir, dataType)
folders = os.listdir(path)
# Get list of files if we split into train/test datasets
if split != None:
msplit = loadmat("{}/{}_split.mat".format(dataDir, dataType))
folders = [sp[0] for sp in msplit[split][0]]
# Go through sequences folders
for fid, folder in enumerate(folders):
# Print progression
print('[{}/{}]'.format(str(fid+1), str(len(folders))), end='\r')
sys.stdout.flush()
# Check if we are in a correct sequence folder
if not os.path.isdir(path + folder + '/rgbjpg'):
error('No "rgbjpg" folder in ' + path + folder)
if not os.path.isdir(path + folder + '/depth'):
error('No "depth" folder in ' + path + folder)
# Load annotations
body_mat = loadmat(path + folder + '/body.mat')
nb_frames, nb_body = body_mat['body'].shape
height, width, channels = None, None, None
# dheight, dwidth = None, None
classes = load_classes(dataDir, dataType, folder)
feature_images = []
# scene_depths = []
scene_joints = []
scene_trackingstates = []
scene_bodies = []
for f in range(nb_frames):
trackingstates = []
joints_list = []
bodies = []
# Load image
img_fpath = '{}{}/rgbjpg/{:04d}.jpg'.format(path, folder, f+1)
# img_fpath = path + folder + '/rgbjpg/' + str(f + 1).zfill(4) + '.jpg'
if not os.path.isfile(img_fpath):
error('No such file: ' + img_fpath)
continue
imshape, image = load_image(img_fpath)
if None in [height, width, channels]:
height, width, channels = imshape
# Load depth
# depth_fpath = '{}{}/depth/{:04d}.mat'.format(path, folder, f + 1)
# if not os.path.isfile(depth_fpath):
# error('No such file: ' + depth_fpath)
# continue
# depth = loadmat(depth_fpath)['depth']
# if None in [dheight, dwidth]:
# dheight, dwidth = depth.shape
# Load joints coordinates
for b in range(nb_body):
isBodyTracked = body_mat['body'][f, b][0, 0][0][0][0]
if isBodyTracked != 1:
continue
tst, joints = load_joints(f, b, body_mat)
joints_list.append(joints)
trackingstates.append(tst)
bodies.append(b)
# If nobody, add zero coordinates
if len(bodies) < 1:
bodies = [-1]
joints = 25 * [[0.0, 0.0, 0.0]]
joints_list.append(joints)
# If multiple bodies, keep the first one
elif len(bodies) > 1:
warning('Multiple bodies in the same frame in {}'.format(folder))
bodies = bodies[0]
# Features
joints_list = np.array(joints_list, dtype=np.float32).flatten()
trackingstates = np.array(trackingstates, dtype=np.int64).flatten()
bodies = np.array(bodies, dtype=np.int64).flatten()
bytes_image = tf.compat.as_bytes(image)
feature_image = bytes_feature(bytes_image)
feature_images.append(feature_image)
# scene_depths.append(depth.flatten())
scene_joints.append(joints_list)
scene_trackingstates.append(trackingstates)
scene_bodies.append(bodies)
# ft_dict = gen_feature2(feature_image, joints_list, trackingstates,
# classes[f], bodies)
# feature = tf.train.Features(feature=ft_dict)
# ft_list.append(feature)
# Sequence Example
context = tf.train.Features(feature={
'name': bytes_feature(folder.encode('utf-8')),
'nb_frames': int64_feature(nb_frames),
'height': int64_feature(height),
'width': int64_feature(width),
# 'dheight': int64_feature(dheight),
# 'dwidth': int64_feature(dwidth)
})
# Feature lists
fl_classes = tf.train.FeatureList(feature=[int64_feature(c) for c in classes])
fl_tstates = tf.train.FeatureList(feature=[int64_feature_list(ts) for ts in scene_trackingstates])
fl_bodies = tf.train.FeatureList(feature=[int64_feature_list(b) for b in scene_bodies])
fl_images = tf.train.FeatureList(feature=feature_images)
# fl_depths = tf.train.FeatureList(feature=[int64_feature_list(dp) for dp in scene_depths])
fl_joints = tf.train.FeatureList(feature=[float_feature_list(j) for j in scene_joints])
feature_list = tf.train.FeatureLists(feature_list={
'classes': fl_classes,
'trackingStates': fl_tstates,
'bodies': fl_bodies,
'images': fl_images,
# 'depths': fl_depths,
'joints': fl_joints
})
ex = tf.train.SequenceExample(context=context, feature_lists=feature_list)
# Serialize
writer.write(ex.SerializeToString())
# Close writer
writer.close()
print('\n')
return
# Variables
# anchors = [[0.57273, 0.677385], [1.87446, 2.06253], [3.33843, 5.47434], [7.88282, 3.52778], [9.77052, 9.16828]]
dataDir = '/home/amusaal/DATA/Coco'
dataType = 'val2014'
overwrite = False
parser()
if "Coco" in dataDir:
gen_coco_example(dataDir, dataType)
elif "Cornell" in dataDir:
gen_cornell_example(dataDir, dataType, 'train_name')
gen_cornell_example(dataDir, dataType, 'test_name')
sys.stdout.flush()