-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detect.py
125 lines (90 loc) · 3.27 KB
/
face_detect.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
import os
import json
import cv2
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from util import load_image, load_labels, visualise
from neural_network import build_model, FaceTracker, localization_loss
from eval_performance import plot_perf
from predict import predict
#LOADING THE DATA AND LOADING IT ONTO THE PIPELINE
train_images=tf.data.Dataset.list_files('./aug_data/train/images/*jpg', shuffle=False)
train_images=train_images.map(load_image)
train_images=train_images.map(lambda x: tf.image.resize(x, (120, 120 )))
train_images=train_images.map(lambda x:x/255)
test_images=tf.data.Dataset.list_files('./aug_data/test/images/*jpg', shuffle= False)
test_images=test_images.map(load_image)
test_images=test_images.map(lambda x: tf.image.resize(x, (120, 120 )))
test_images=test_images.map(lambda x:x/255)
val_images=tf.data.Dataset.list_files('./aug_data/val/images/*jpg', shuffle = False)
val_images=val_images.map(load_image)
val_images=val_images.map(lambda x: tf.image.resize(x, (120, 120 )))
val_images=val_images.map(lambda x:x/255)
#LOADING THE LABELS
train_labels = tf.data.Dataset.list_files('aug_data/train/labels/*json', shuffle = False)
train_labels = train_labels.map(lambda x: tf.py_function(load_labels, [x], [tf.uint8, tf.float16]))
test_labels = tf.data.Dataset.list_files('aug_data/test/labels/*json', shuffle = False)
test_labels = test_labels.map(lambda x: tf.py_function(load_labels, [x], [tf.uint8, tf.float16]))
val_labels = tf.data.Dataset.list_files('aug_data/val/labels/*json', shuffle = False)
val_labels = val_labels.map(lambda x: tf.py_function(load_labels, [x], [tf.uint8, tf.float16]))
'''
#NUMBER OF DATA VALUES
print(len(train_images))
print(len(test_images))
print(len(val_images))
print(len(train_labels))
print(len(test_labels))
print(len(val_labels))
o/p
4400
700
1200
4400
700
1200
'''
train=tf.data.Dataset.zip((train_images,train_labels))
train=train.shuffle(5000)
train=train.batch(8)
train= train.prefetch(4)
test = tf.data.Dataset.zip((test_images,test_labels))
test = test.shuffle(100)
test = test.batch(8)
test = test.prefetch(4)
val = tf.data.Dataset.zip( (val_images, val_labels))
val = val.shuffle(1500)
val = val.batch(8)
val = val.prefetch(4)
data_samples = train.as_numpy_iterator()
'''
#TO VISUALISE THE IMAGES
res = data_samples.next()
visualise(res)
'''
#TRAIN THE MODEL
batches_per_epoch = len(train)
lr_decay = (1.0/0.75 - 1)/batches_per_epoch
opt = tf.keras.optimizers.legacy.Adam(learning_rate = 0.0001, decay = lr_decay)
class_loss = tf.keras.losses.BinaryCrossentropy()
regress_loss = localization_loss
facetracker = build_model()
model = FaceTracker(facetracker)
model.compile(opt , classloss=class_loss, localizationloss= regress_loss)
logdir='logs'
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir= logdir)
hist = model.fit(train, epochs=40, validation_data = val, callbacks = [tensorboard_callback])
#PLOT PERFORMANCE METRICS
plot_perf(hist)
#SAVE AND LOAD THE MODEL
import tensorflow as tf
from tensorflow.keras.models import load_model
import cv2
import numpy as np
facetracker.save('facetracker.h5')
facetracker = load_model('facetracker.h5')
#MAKE PREDICTIONS
test_data = test.as_numpy_iterator()
test_sample = test_data.next()
y_pred = facetracker(test_sample[0])
predict(test_sample, y_pred)