forked from moaljazaery/TinyFaces-Face-Detector-TF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobileNetClassifierTrain.py
120 lines (74 loc) · 3.14 KB
/
MobileNetClassifierTrain.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
#!/usr/bin/env python
# coding: utf-8
#importing modules
import tensorflow as tf
import os
from PIL import Image
import numpy as np
from sklearn.model_selection import train_test_split
import dlib
images = []
labels = []
#function to detect faces using dlib
'''def detect_faces(image):
face_detector = dlib.get_frontal_face_detector()
detected_faces = face_detector(image, 1)
face_frames = [(x.left(), x.top(),
x.right(), x.bottom()) for x in detected_faces]
return face_frames
'''
#defining master directory
master_dir = ""
for dir_ in os.listdir( master_dir ):
for imagename in os.listdir( os.path.join( master_dir , dir_ ) ):
print( imagename )
path = os.path.join( master_dir , dir_ , imagename )
image = np.asarray( Image.open( path ) )
detected_faces = image
if len( detected_faces ) is not 0:
face = Image.fromarray( image ).crop( detected_faces[0] ).resize((128,128))
images.append( np.asarray( face ) / 255 )
labels.append( 0 if dir_ == 'with_mask' else 1 )
# In[ ]:
#np.save('/home/jarvis/Desktop/ShubhamWork/npyfiles/mask-nonmask-images.npy', images)
#np.save('/home/jarvis/Desktop/ShubhamWork/npyfiles/mask-nonmask-labels.npy', labels)
# In[ ]:
#images = np.load( '/home/jarvis/Desktop/ShubhamWork/npyfiles/mask-nonmask-images.npy' )
#labels = np.load( '/home/jarvis/Desktop/ShubhamWork/npyfiles/mask-nonmask-images.npy' )
# In[ ]:
X = np.array( images )
Y = tf.keras.utils.to_categorical( labels , num_classes=2 )
train_features, test_features, train_labels, test_labels = train_test_split( X , Y ,test_size=0.2)
# In[ ]:
print( train_features.shape )
print( train_labels.shape )
# In[ ]:
# In[ ]:
rate = 0.4
mobilenet = tf.keras.applications.MobileNetV2( include_top=False , input_shape=(64,64,3 ), pooling='avg' , weights='imagenet' )
x = tf.keras.layers.Dense( 512 , activation='relu' )( mobilenet.output )
x = tf.keras.layers.Dropout( rate )(x)
x = tf.keras.layers.Dense( 256 , activation='relu' )(x)
x = tf.keras.layers.Dropout( rate )(x)
x = tf.keras.layers.Dense( 128 , activation='relu' )(x)
x = tf.keras.layers.Dropout( rate )(x)
x = tf.keras.layers.Dense( 64 , activation='relu' )(x)
x = tf.keras.layers.Dropout( rate )(x)
x = tf.keras.layers.Dense( 32 , activation='relu' )(x)
x = tf.keras.layers.Dropout( rate )(x)
x = tf.keras.layers.Dense( 16 , activation='relu' )(x)
x = tf.keras.layers.Dropout( rate )(x)
outputs = tf.keras.layers.Dense( 2 , activation='softmax' )(x)
model = tf.keras.Model( mobilenet.input , outputs )
model.summary()
# In[ ]:
model.compile( loss='categorical_crossentropy' , optimizer=tf.keras.optimizers.Adam( learning_rate=0.0001 ) , metrics=[ 'acc' ] )
model.fit( train_features , train_labels , validation_data=( test_features , test_labels ) , epochs=100 , batch_size=1 )
# In[ ]:
model.evaluate( test_features , test_labels )
# In[ ]:
converter = tf.lite.TFLiteConverter.from_keras_model( model )
converter.post_training_quantization = True
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_LATENCY]
buffer = converter.convert()
open( 'Classifier.tflite' , 'wb' ).write( buffer )