-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleft_right.py
311 lines (202 loc) · 7.54 KB
/
left_right.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import tensorflow as tf
# In[ ]:
# from google.colab import drive
# drive.mount('/content/drive')
# In[ ]:
'''
Dataset description
Images of 150 persons from different angles
'''
import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
dataset_directory = 'input/'
dir_list = [None]*12
dir_list[0] = dataset_directory + 'v1_note4_dark/dark/'
dir_list[1] = dataset_directory + 'v1_note4_daylight/daylight/'
dir_list[2] = dataset_directory + 'v1_note4_office/office/'
dir_list[3] = dataset_directory + 'v1_oppo_dark/dark/'
dir_list[4] = dataset_directory + 'v1_oppo_daylight/daylight/'
dir_list[5] = dataset_directory + 'v1_oppo_office/office/'
dir_list[6] = dataset_directory + 'v2_note4_dark/dark/'
dir_list[7] = dataset_directory + 'v2_note4_daylight/daylight/'
dir_list[8] = dataset_directory + 'v2_note4_office/office/'
dir_list[9] = dataset_directory + 'v2_oppo_dark/dark/'
dir_list[10] = dataset_directory + 'v2_oppo_daylight/daylight/'
dir_list[11] = dataset_directory + 'v2_oppo_office/office/'
from glob import glob
all_images = []
for direc in dir_list:
# print(len(os.listdir(direc + 'S1/')) + len(os.listdir(direc + 'S2/')))
images = glob(direc+'S1/'+'*.png')
all_images += images
images = glob(direc+'S2/'+'*.png')
all_images += images
#till here, we've got a list of names of all images
#image name: <id_of that_person>_<l/r>_<img_no.>.png
# In[ ]:
print(len(all_images))
# all_images = all_images[: len(all_images) // 10]
# print(len(all_images))
# In[ ]:
ids = {}
for img_name in all_images:
# _,_,_,_,_,_,x = img_name.split('/')
temp = img_name.split('/')
# temp = temp[len(temp) - 1]
# temp = temp.split('\\')
x = temp[len(temp) - 1]
a,b,c = x.split('_')
if(a not in ids):
ids[a]=[]
ids[a].append(img_name)
# In[ ]:
# No of people
len(ids)
# In[ ]:
ids_to_labels = []
for key in ids:
ids_to_labels.append(key)
import random
random.Random(42).shuffle(ids_to_labels)
# In[ ]:
val_size = 0.15
test_size = 0.15
test_ids_to_labels = ids_to_labels[: round(test_size * len(ids_to_labels))]
val_ids_to_labels = ids_to_labels[round(test_size * len(ids_to_labels)) : round(test_size * len(ids_to_labels)) + round(val_size * len(ids_to_labels))]
train_ids_to_labels = ids_to_labels[round(test_size * len(ids_to_labels)) + round(val_size * len(ids_to_labels)) : ]
print(len(train_ids_to_labels), len(val_ids_to_labels), len(test_ids_to_labels))
# In[ ]:
train_all_files = []
for id in train_ids_to_labels:
for file_name in ids[id]:
train_all_files.append(file_name)
val_all_files = []
for id in val_ids_to_labels:
for file_name in ids[id]:
val_all_files.append(file_name)
test_all_files = []
for id in test_ids_to_labels:
for file_name in ids[id]:
test_all_files.append(file_name)
random.Random(42).shuffle(train_all_files)
random.Random(42).shuffle(val_all_files)
random.Random(42).shuffle(test_all_files)
print(len(train_all_files) + len(val_all_files) + len(test_all_files))
# In[ ]:
BATCH_SIZE = 100
# In[ ]:
def decode_img(img):
# convert the compressed string to a 3D uint8 tensor
img = tf.image.decode_jpeg(img, channels=3)
# Use `convert_image_dtype` to convert to floats in the [0,1] range.
img = tf.image.convert_image_dtype(img, tf.float32)
# resize the image to the desired size.
return img
# In[ ]:
flag = 0
# In[ ]:
def get_training_data():
global flag
while True:
fname = train_all_files[random.randint(1, len(train_all_files)) - 1]
temp = fname.split('/')
temp = temp[len(temp) - 1]
a,b,c = temp.split('_')
eye_id = b
x = decode_img(tf.io.read_file(fname))
x = tf.convert_to_tensor(x, dtype=tf.float32)
x1 = [x[:, : 256, :], x[:, 256 : 512, :], x[:, 512 : 768, :], x[:, 768 : 1024, :], x[:, 1024 : 1280, :]]
for i in range(5):
if eye_id == 'l':
yield x1[i], [1.0]
else:
yield x1[i], [0.0]
# In[ ]:
training_dataset = tf.data.Dataset.from_generator(get_training_data,
output_types=(tf.float32, tf.float32),
output_shapes=((256, 256, 3), 1))
training_dataset = training_dataset.batch(100)
# In[ ]:
# list(training_dataset.take(1).as_numpy_iterator())
# temp = list(training_dataset.take(4).as_numpy_iterator())
# In[ ]:
def get_validation_data():
global flag
while True:
fname = val_all_files[random.randint(1, len(val_all_files)) - 1]
temp = fname.split('/')
temp = temp[len(temp) - 1]
a,b,c = temp.split('_')
eye_id = b
x = decode_img(tf.io.read_file(fname))
x = tf.convert_to_tensor(x, dtype=tf.float32)
x1 = [x[:, : 256, :], x[:, 256 : 512, :], x[:, 512 : 768, :], x[:, 768 : 1024, :], x[:, 1024 : 1280, :]]
for i in range(5):
if eye_id == 'l':
yield x1[i], [1.0]
else:
yield x1[i], [0.0]
# In[ ]:
validation_dataset = tf.data.Dataset.from_generator(get_validation_data,
output_types=(tf.float32, tf.float32),
output_shapes=((256, 256, 3), 1))
validation_dataset = validation_dataset.batch(100)
# In[ ]:
def get_testing_data():
global flag
while True:
fname = test_all_files[random.randint(1, len(test_all_files)) - 1]
temp = fname.split('/')
temp = temp[len(temp) - 1]
a,b,c = temp.split('_')
eye_id = b
x = decode_img(tf.io.read_file(fname))
x = tf.convert_to_tensor(x, dtype=tf.float32)
x1 = [x[:, : 256, :], x[:, 256 : 512, :], x[:, 512 : 768, :], x[:, 768 : 1024, :], x[:, 1024 : 1280, :]]
for i in range(5):
if eye_id == 'l':
yield x1[i], [1.0]
else:
yield x1[i], [0.0]
# In[ ]:
testing_dataset = tf.data.Dataset.from_generator(get_testing_data,
output_types=(tf.float32, tf.float32),
output_shapes=((256, 256, 3), 1))
testing_dataset = testing_dataset.batch(100)
# In[ ]:
def Classifier():
inp = tf.keras.layers.Input(shape=[256, 256, 3])
conv = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(inp)
conv = tf.keras.layers.MaxPool2D(pool_size=(2,2))(conv)
conv = tf.keras.layers.Conv2D(32,(3,3),activation='relu')(conv)
conv = tf.keras.layers.MaxPool2D(pool_size=(2,2))(conv)
conv = tf.keras.layers.Conv2D(64,(3,3),activation='relu')(conv)
conv = tf.keras.layers.MaxPool2D(pool_size=(2,2))(conv)
conv = tf.keras.layers.Conv2D(64,(3,3),activation='relu')(conv)
conv = tf.keras.layers.MaxPool2D(pool_size=(2,2))(conv)
conv = tf.keras.layers.Conv2D(128,(3,3),activation='relu')(conv)
conv = tf.keras.layers.MaxPool2D(pool_size=(2,2))(conv)
conv = tf.keras.layers.Conv2D(128,(3,3),activation='relu')(conv)
conv = tf.keras.layers.MaxPool2D(pool_size=(2,2))(conv)
x = tf.keras.layers.Flatten()(conv)
x = tf.keras.layers.Dense(64, activation='relu')(x)
last = tf.keras.layers.Dense(1, activation='sigmoid')(x)
return tf.keras.Model(inputs=inp, outputs=last)
# In[ ]:
clas = Classifier()
clas.compile(optimizer='adam', loss='binary_crossentropy', metrics=['binary_accuracy'])
clas.summary()
# In[ ]:
clas.fit(x=training_dataset, epochs=10, validation_data=validation_dataset,
steps_per_epoch=len(train_all_files) // 40,
validation_steps=len(val_all_files) // 40,
callbacks=[], use_multiprocessing=True)
# In[ ]:
clas.save('saved_model/left_right')
# In[ ]:
testing_steps = len(test_all_files) // 40
clas.evaluate(x=testing_dataset, steps=testing_steps, use_multiprocessing=True)