Anuj-Panthri commited on
Commit
3e96ee0
·
1 Parent(s): 448ac8f

changed face detection model to yolo_mobilenet_backbone

Browse files
app/__init__.py CHANGED
@@ -9,7 +9,8 @@ from face_recognition import helper as fr_helper
9
 
10
 
11
 
12
- face_detector=fd.face_detection("face_detection/Models/v1")
 
13
  face_detector.square_preprocessing=fd.square_pad()
14
  # face_recognizer=fr.face_recognition("face_recognition/Models/v1")
15
  # face_recognizer=fr.face_recognition("face_recognition/Models/mobilenet_basic_lfw")
 
9
 
10
 
11
 
12
+ # face_detector=fd.face_detection("face_detection/Models/v1")
13
+ face_detector=fd.face_detection("face_detection/Models/mobilenet")
14
  face_detector.square_preprocessing=fd.square_pad()
15
  # face_recognizer=fr.face_recognition("face_recognition/Models/v1")
16
  # face_recognizer=fr.face_recognition("face_recognition/Models/mobilenet_basic_lfw")
face_detection/Models/mobilenet/anchors.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ 1.374126791954040527e+00 1.648838758468627930e+00
2
+ 3.987907886505126953e+00 4.713618278503417969e+00
3
+ 2.353522062301635742e+00 2.840777397155761719e+00
4
+ 6.863890647888183594e+00 8.402585983276367188e+00
5
+ 6.510864496231079102e-01 7.812500000000000000e-01
face_detection/Models/mobilenet/config.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ p_thres=0.5
2
+ nms_thres=0.3
face_detection/Models/mobilenet/model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ee8452826fb51076f13878d77119bd7834a946df3fb396002c7b756cd07ba64e
3
+ size 13201720
face_detection/Models/v1/model.h5 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:90dd2a59c7acc25d967c9914a7a53362ed2229c56e3105f538e04388fce70390
3
- size 607092424
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5a50e569a9d63bd7e30fb1107b3b9164e2f9e2512d4c98fd13f04ccad15365b2
3
+ size 202503752
face_detection/create_load_model.py CHANGED
@@ -4,251 +4,38 @@ from tensorflow.keras import Model,layers
4
  from face_detection.config import class_names
5
 
6
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
7
- # custom layer for reshaping last layer
8
 
9
  # custom layer for reshaping last layer
10
-
11
  class yolo_reshape(tf.keras.layers.Layer):
12
- global num_anchors
13
- def __init__(self, **kwargs):
14
- super(yolo_reshape,self).__init__()
15
- self.last_item=(5+len(class_names))
16
- def call(self,output_layer):
17
- shape = [tf.shape(output_layer)[k] for k in range(4)]
18
- # print(shape)
19
- # tf.print(shape)
20
- return tf.reshape(output_layer,[shape[0],shape[1],shape[2],num_anchors,self.last_item])
21
-
22
- def create_model():
23
- global num_anchors
24
- def space_to_depth_x2(x):
25
- return tf.nn.space_to_depth(x,block_size=2)
26
-
27
- x_input=layers.Input(shape=(None,None,3))
28
- x=layers.Lambda(lambda x:x/255.)(x_input)
29
- x=layers.Conv2D(32,(3,3),strides=(1,1),padding='same',name='conv_1',use_bias=False)(x)
30
- x=layers.BatchNormalization(name='norm_1')(x)
31
- x=layers.LeakyReLU(alpha=0.1)(x)
32
- x=layers.MaxPooling2D(pool_size=(2,2))(x)
33
-
34
- x=layers.Conv2D(64,(3,3),strides=(1,1),padding='same',name='conv_2',use_bias=False)(x)
35
- x=layers.BatchNormalization(name='norm_2')(x)
36
- x=layers.LeakyReLU(alpha=0.1)(x)
37
- x=layers.MaxPooling2D(pool_size=(2,2))(x)
38
-
39
- x=layers.Conv2D(128,(3,3),strides=(1,1),padding='same',name='conv_3',use_bias=False)(x)
40
- x=layers.BatchNormalization(name='norm_3')(x)
41
- x=layers.LeakyReLU(alpha=0.1)(x)
42
-
43
- x=layers.Conv2D(64,(1,1),strides=(1,1),padding='same',name='conv_4',use_bias=False)(x)
44
- x=layers.BatchNormalization(name='norm_4')(x)
45
- x=layers.LeakyReLU(alpha=0.1)(x)
46
-
47
- x=layers.Conv2D(128,(3,3),strides=(1,1),padding='same',name='conv_5',use_bias=False)(x)
48
- x=layers.BatchNormalization(name='norm_5')(x)
49
- x=layers.LeakyReLU(alpha=0.1)(x)
50
- x=layers.MaxPooling2D(pool_size=(2,2))(x)
51
-
52
- x=layers.Conv2D(256,(3,3),strides=(1,1),padding='same',name='conv_6',use_bias=False)(x)
53
- x=layers.BatchNormalization(name='norm_6')(x)
54
- x=layers.LeakyReLU(alpha=0.1)(x)
55
-
56
- x=layers.Conv2D(128,(1,1),strides=(1,1),padding='same',name='conv_7',use_bias=False)(x)
57
- x=layers.BatchNormalization(name='norm_7')(x)
58
- x=layers.LeakyReLU(alpha=0.1)(x)
59
-
60
- x=layers.Conv2D(256,(3,3),strides=(1,1),padding='same',name='conv_8',use_bias=False)(x)
61
- x=layers.BatchNormalization(name='norm_8')(x)
62
- x=layers.LeakyReLU(alpha=0.1)(x)
63
- x=layers.MaxPooling2D(pool_size=(2,2))(x)
64
-
65
- x=layers.Conv2D(512,(3,3),strides=(1,1),padding='same',name='conv_9',use_bias=False)(x)
66
- x=layers.BatchNormalization(name='norm_9')(x)
67
- x=layers.LeakyReLU(alpha=0.1)(x)
68
-
69
- x=layers.Conv2D(256,(1,1),strides=(1,1),padding='same',name='conv_10',use_bias=False)(x)
70
- x=layers.BatchNormalization(name='norm_10')(x)
71
- x=layers.LeakyReLU(alpha=0.1)(x)
72
-
73
- x=layers.Conv2D(512,(3,3),strides=(1,1),padding='same',name='conv_11',use_bias=False)(x)
74
- x=layers.BatchNormalization(name='norm_11')(x)
75
- x=layers.LeakyReLU(alpha=0.1)(x)
76
-
77
- x=layers.Conv2D(256,(1,1),strides=(1,1),padding='same',name='conv_12',use_bias=False)(x)
78
- x=layers.BatchNormalization(name='norm_12')(x)
79
- x=layers.LeakyReLU(alpha=0.1)(x)
80
-
81
- x=layers.Conv2D(512,(3,3),strides=(1,1),padding='same',name='conv_13',use_bias=False)(x)
82
- x=layers.BatchNormalization(name='norm_13')(x)
83
- x=layers.LeakyReLU(alpha=0.1)(x)
84
-
85
- skip_connection = x
86
-
87
- x=layers.MaxPooling2D(pool_size=(2,2))(x)
88
-
89
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_14',use_bias=False)(x)
90
- x=layers.BatchNormalization(name='norm_14')(x)
91
- x=layers.LeakyReLU(alpha=0.1)(x)
92
-
93
- x=layers.Conv2D(512,(1,1),strides=(1,1),padding='same',name='conv_15',use_bias=False)(x)
94
- x=layers.BatchNormalization(name='norm_15')(x)
95
- x=layers.LeakyReLU(alpha=0.1)(x)
96
-
97
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_16',use_bias=False)(x)
98
- x=layers.BatchNormalization(name='norm_16')(x)
99
- x=layers.LeakyReLU(alpha=0.1)(x)
100
-
101
- x=layers.Conv2D(512,(1,1),strides=(1,1),padding='same',name='conv_17',use_bias=False)(x)
102
- x=layers.BatchNormalization(name='norm_17')(x)
103
- x=layers.LeakyReLU(alpha=0.1)(x)
104
-
105
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_18',use_bias=False)(x)
106
- x=layers.BatchNormalization(name='norm_18')(x)
107
- x=layers.LeakyReLU(alpha=0.1)(x)
108
-
109
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_19',use_bias=False)(x)
110
- x=layers.BatchNormalization(name='norm_19')(x)
111
- x=layers.LeakyReLU(alpha=0.1)(x)
112
-
113
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_20',use_bias=False)(x)
114
- x=layers.BatchNormalization(name='norm_20')(x)
115
- x=layers.LeakyReLU(alpha=0.1)(x)
116
 
117
- skip_connection=layers.Conv2D(64,(1,1),strides=(1,1),padding='same',name='conv_21',use_bias=False)(skip_connection)
118
- skip_connection=layers.BatchNormalization(name='norm_21')(skip_connection)
119
- skip_connection=layers.LeakyReLU(alpha=0.1)(skip_connection)
120
- skip_connection=layers.Lambda(space_to_depth_x2)(skip_connection) # halfs the resolution and add more depth
121
-
122
- x=layers.concatenate([skip_connection,x])
123
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_22',use_bias=False)(x)
124
- x=layers.BatchNormalization(name='norm_22')(x)
125
- x=layers.LeakyReLU(alpha=0.1)(x)
126
-
127
- x=layers.Conv2D((num_anchors*(5+len(class_names))),(1,1),strides=(1,1),padding='same',name='conv_23')(x)
128
- out=yolo_reshape()(x)
129
-
130
- model=Model(x_input,out,name='yolo_v2_model')
131
- # model.summary()
132
- return model
133
-
134
- def create_tiny_model():
135
- global num_anchors
136
-
137
- x_input=layers.Input(shape=(416,416,3))
138
- x=layers.Lambda(lambda x:x/255.)(x_input)
139
- x=layers.Conv2D(16,(3,3),strides=(1,1),padding='same',name='conv_1',use_bias=False)(x)
140
- x=layers.BatchNormalization(name='norm_1')(x)
141
- x=layers.LeakyReLU(alpha=0.1)(x)
142
- x=layers.MaxPooling2D(pool_size=(2,2),strides=(2,2))(x)
143
-
144
- x=layers.Conv2D(32,(3,3),strides=(1,1),padding='same',name='conv_2',use_bias=False)(x)
145
- x=layers.BatchNormalization(name='norm_2')(x)
146
- x=layers.LeakyReLU(alpha=0.1)(x)
147
- x=layers.MaxPooling2D(pool_size=(2,2),strides=(2,2))(x)
148
-
149
- x=layers.Conv2D(64,(3,3),strides=(1,1),padding='same',name='conv_3',use_bias=False)(x)
150
- x=layers.BatchNormalization(name='norm_3')(x)
151
- x=layers.LeakyReLU(alpha=0.1)(x)
152
- x=layers.MaxPooling2D(pool_size=(2,2),strides=(2,2))(x)
153
-
154
- x=layers.Conv2D(128,(3,3),strides=(1,1),padding='same',name='conv_4',use_bias=False)(x)
155
- x=layers.BatchNormalization(name='norm_4')(x)
156
- x=layers.LeakyReLU(alpha=0.1)(x)
157
- x=layers.MaxPooling2D(pool_size=(2,2),strides=(2,2))(x)
158
-
159
- x=layers.Conv2D(256,(3,3),strides=(1,1),padding='same',name='conv_5',use_bias=False)(x)
160
- x=layers.BatchNormalization(name='norm_5')(x)
161
- x=layers.LeakyReLU(alpha=0.1)(x)
162
- x=layers.MaxPooling2D(pool_size=(2,2),strides=(2,2))(x)
163
-
164
- x=layers.Conv2D(512,(3,3),strides=(1,1),padding='same',name='conv_6',use_bias=False)(x)
165
- x=layers.BatchNormalization(name='norm_6')(x)
166
- x=layers.LeakyReLU(alpha=0.1)(x)
167
- # x=layers.MaxPooling2D(pool_size=(2,2),strides=(1,1))(x)
168
-
169
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_7',use_bias=False)(x)
170
- x=layers.BatchNormalization(name='norm_7')(x)
171
- x=layers.LeakyReLU(alpha=0.1)(x)
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
- x=layers.Conv2D(1024,(3,3),strides=(1,1),padding='same',name='conv_8',use_bias=False)(x)
175
- x=layers.BatchNormalization(name='norm_8')(x)
176
- x=layers.LeakyReLU(alpha=0.1)(x)
177
-
178
- x=layers.Conv2D((num_anchors*(5+len(class_names))),(1,1),strides=(1,1),padding='same',name='conv_9')(x)
179
- out=yolo_reshape()(x)
180
-
181
- model=Model(x_input,out,name='yolo_v2_tiny_model')
182
- # model.summary()
183
- return model
184
 
185
  def load_model(path):
186
- # model=tf.keras.models.load_model(path,custom_objects={'yolo_dynamic_reshape':yolo_dynamic_reshape},compile=False)
187
- model=create_model()
188
- model.load_weights(path)
189
- # model=tf.keras.models.load_model(path)
190
  return model
191
-
192
-
193
- import struct
194
-
195
- def get_original_weights(model,path_to_weight = "./yolov2-voc.weights"):
196
- global num_anchors
197
- # path_to_weight = "./yolov2.weights"(trained on coco dataset (80 classes))
198
- if "yolov2-voc.weights" in path_to_weight: offset=5;nb_conv = 23;
199
- if "darknet19_448.conv.23" in path_to_weight: offset=4;nb_conv = 18;
200
- if "yolov2-tiny-voc.weights" in path_to_weight: offset=4;nb_conv = 9;
201
- print(offset,nb_conv)
202
-
203
- class WeightReader:
204
- def __init__(self, weight_file):
205
- self.offset = offset # an offset of 5 as first 5 values are non weight values(they are weight header)(for yolov2-voc.weights)
206
- # self.all_weights = np.fromfile(weight_file, dtype='float32')
207
- self.all_weights = open(weight_file,'rb')
208
- weight_header=struct.unpack(f'{offset}i', self.all_weights.read(offset*4))
209
- # print("weight Header(major, minor, revision, seen):",weight_header)
210
-
211
- def read_bytes(self, size):
212
- weights = struct.unpack('%df' % size, self.all_weights.read(size*4))
213
- # print(weights)
214
- # input("wait now forever")
215
- return np.array(weights)
216
-
217
- weight_reader = WeightReader(path_to_weight)
218
- # print("all_weights = {}".format(np.fromfile(path_to_weight, dtype='float32').shape[0]-weight_reader.offset))
219
-
220
- for i in range(1, nb_conv+1):
221
- conv_layer = model.get_layer('conv_' + str(i))
222
-
223
- if i < nb_conv:
224
- norm_layer = model.get_layer('norm_' + str(i))
225
-
226
- size = np.prod(norm_layer.get_weights()[0].shape)
227
-
228
- beta = weight_reader.read_bytes(size)
229
- gamma = weight_reader.read_bytes(size)
230
- mean = weight_reader.read_bytes(size)
231
- var = weight_reader.read_bytes(size)
232
-
233
- weights = norm_layer.set_weights([gamma, beta, mean, var])
234
-
235
- if len(conv_layer.get_weights()) > 1:
236
- bias = weight_reader.read_bytes(np.prod(conv_layer.get_weights()[1].shape))
237
- kernel = weight_reader.read_bytes(np.prod(conv_layer.get_weights()[0].shape))
238
- kernel = kernel.reshape(list(reversed(conv_layer.get_weights()[0].shape)))
239
- kernel = kernel.transpose([2,3,1,0])
240
- # print(kernel.shape)
241
- kernel=kernel.reshape([*kernel.shape[:-1],num_anchors,-1]) # reshape to this format so we change change position of p idx
242
- idx=4 # in darknet each object was encoded as [x,y,w,h,p,c] but we use [p,x,y,w,h,c]
243
- kernel=np.concatenate([kernel[...,idx:idx+1],kernel[...,:idx],kernel[...,idx+1:]],axis=-1) # setting p to idx 0
244
- # print(kernel.shape)
245
- kernel=kernel.reshape([*kernel.shape[:-2],-1])
246
- # print(kernel.shape)
247
- conv_layer.set_weights([kernel, bias])
248
- else:
249
- kernel = weight_reader.read_bytes(np.prod(conv_layer.get_weights()[0].shape))
250
- kernel = kernel.reshape(list(reversed(conv_layer.get_weights()[0].shape)))
251
- kernel = kernel.transpose([2,3,1,0])
252
- conv_layer.set_weights([kernel])
253
-
254
- return model
 
4
  from face_detection.config import class_names
5
 
6
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
 
7
 
8
  # custom layer for reshaping last layer
 
9
  class yolo_reshape(tf.keras.layers.Layer):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def __init__(self,num_anchors,last_item, **kwargs):
12
+ super(yolo_reshape, self).__init__(**kwargs)
13
+ self.last_item=last_item
14
+ self.num_anchors=num_anchors
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def call(self,output_layer):
17
+ shape = [tf.shape(output_layer)[k] for k in range(4)]
18
+ return tf.reshape(output_layer,[shape[0],shape[1],shape[2],self.num_anchors,self.last_item])
19
+
20
+ def compute_output_shape(self, input_shape):
21
+ return (input_shape[0],input_shape[1],input_shape[2],num_anchors,self.last_item, self.num_anchors,self.last_item)
22
+
23
+
24
+ def get_config(self):
25
+ config = super(yolo_reshape, self).get_config()
26
+ config.update(
27
+ {
28
+ "last_item": self.last_item,
29
+ "num_anchors": self.num_anchors
30
+ }
31
+ )
32
+ return config
33
 
34
+ @classmethod
35
+ def from_config(cls, config):
36
+ return cls(**config)
37
+
 
 
 
 
 
 
38
 
39
  def load_model(path):
40
+ model=tf.keras.models.load_model(path,compile=False,custom_objects={"yolo_reshape":yolo_reshape})
 
 
 
41
  return model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
test.py CHANGED
@@ -12,25 +12,23 @@ def new_distance(vectors):
12
  path="face_recognition/Models/mobilenet_basic_lfw/mobilenet_basic_lfw_model.h5"
13
  # path="face_recognition/Models/keras_mobilenet_emore_adamw/keras_mobilenet_emore_adamw.h5"
14
 
15
- model2=tf.keras.models.load_model(path,compile=False)
16
 
17
  x_input=tf.keras.layers.Input(shape=(112,112,3))
18
  x_preprocess=tf.keras.layers.Lambda(lambda x:(x - 127.5) * 0.0078125)(x_input)
19
- x=model2(x_preprocess,training=False)
20
  x=tf.keras.layers.Lambda(lambda x:tf.math.l2_normalize(x, axis=-1))(x)
21
  model2=tf.keras.Model(x_input,x)
22
  # model2.summary()
 
 
23
 
24
 
25
- # cv2.imread("")
26
- # for layer in model2.layers:
27
- # if isinstance(layer,tf.keras.layers.Activation):
28
- # print(layer.activation)
29
 
30
  img1=cv2.resize(cv2.imread("C:/Users/Home/Desktop/college stuff/vasu_dataset/all/priyanshi/7.jpg")[:,:,::-1],[112,112])[None]
31
  img2=cv2.resize(cv2.imread("C:/Users/Home/Desktop/college stuff/vasu_dataset/all/shivansh/12.jpg")[:,:,::-1],[112,112])[None]
32
- v1=(model2.predict(img1,verbose=0))
33
- v2=(model2.predict(img2,verbose=0))
34
 
35
 
36
  print("euclidean_distance:",euclidean_distance([v1,v2]))
@@ -38,9 +36,4 @@ print("new_distance:",new_distance([v1,v2]))
38
  if(new_distance([v1,v2])[0]>0.2830035090446472):
39
  print("matching")
40
  else:
41
- print("not matching")
42
-
43
-
44
-
45
- model2.save(path.rsplit("/",1)[0]+"/model.h5")
46
- print("model save to :\t",path.rsplit("/",1)[0]+"/model.h5")
 
12
  path="face_recognition/Models/mobilenet_basic_lfw/mobilenet_basic_lfw_model.h5"
13
  # path="face_recognition/Models/keras_mobilenet_emore_adamw/keras_mobilenet_emore_adamw.h5"
14
 
15
+ model=tf.keras.models.load_model(path,compile=False)
16
 
17
  x_input=tf.keras.layers.Input(shape=(112,112,3))
18
  x_preprocess=tf.keras.layers.Lambda(lambda x:(x - 127.5) * 0.0078125)(x_input)
19
+ x=model(x_preprocess,training=False)
20
  x=tf.keras.layers.Lambda(lambda x:tf.math.l2_normalize(x, axis=-1))(x)
21
  model2=tf.keras.Model(x_input,x)
22
  # model2.summary()
23
+ # model.save(path.rsplit("/",1)[0]+"/model.h5")
24
+ # print("model save to :\t",path.rsplit("/",1)[0]+"/model.h5")
25
 
26
 
 
 
 
 
27
 
28
  img1=cv2.resize(cv2.imread("C:/Users/Home/Desktop/college stuff/vasu_dataset/all/priyanshi/7.jpg")[:,:,::-1],[112,112])[None]
29
  img2=cv2.resize(cv2.imread("C:/Users/Home/Desktop/college stuff/vasu_dataset/all/shivansh/12.jpg")[:,:,::-1],[112,112])[None]
30
+ v1=(model.predict(img1,verbose=0))
31
+ v2=(model.predict(img2,verbose=0))
32
 
33
 
34
  print("euclidean_distance:",euclidean_distance([v1,v2]))
 
36
  if(new_distance([v1,v2])[0]>0.2830035090446472):
37
  print("matching")
38
  else:
39
+ print("not matching")