-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembedding.py
215 lines (159 loc) · 6.79 KB
/
embedding.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
import cv2
import tensorflow as tf
import numpy as np
import facenet
import face_recognition
from skimage import exposure
from skimage import feature
class Embedding(object):
def __init__(self, dataset):
self.dataset = dataset
def get_embeddings(self):
pass
class FN_Embedding(Embedding):
def __init__(self, dataset, mdlpath, imgsize, gpu_mem, batchsize=50):
super().__init__(dataset)
self.mdlpath = mdlpath
self.imgsize = imgsize
self.gpu_mem = gpu_mem
self.batchsize = batchsize
def get_embeddings(self):
"""
get_embeddings - function to get embeddings of a dataset using FaceNet
args self
returns data - array of embeddings for each image
labels - array of corresponding labels for each embedding
"""
results = dict()
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=self.gpu_mem)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
facenet.load_model(self.mdlpath)
# Get input and output tensors
images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
image_paths = []
batch_num = 0
data = []
labels = []
for cls in self.dataset:
for path in cls.image_paths:
image_paths.append(path)
labels.append(cls.name)
for i in range(0, len(image_paths), self.batchsize):
print("batch num: %d of %d" % (i / self.batchsize, len(image_paths) / self.batchsize))
#load data
images = facenet.load_data(image_paths=image_paths[i:i+self.batchsize],
do_random_crop=False, do_random_flip=False, image_size=self.imgsize, do_prewhiten=True)
feed_dict = {images_placeholder: images, phase_train_placeholder: False}
emb_array = sess.run(embeddings, feed_dict=feed_dict)
for emb in emb_array:
data.append(emb)
#convert list to numpy array for compatibility with the svm
data = np.asarray(data)
labels = np.asarray(labels)
return data, labels
class HOG_OCV_Embedding(Embedding):
def __init__(self, dataset):
super().__init__(dataset)
def get_embeddings(self):
"""
get_embeddings - function to get embeddings (in form of HOG) of a dataset using OpenCV
args self
returns data - array of embeddings for each image
labels - array of corresponding labels for each embedding
"""
#values and setup for the HOG Descriptor
win_size = (250, 250)
block_size = (10, 10)
cell_size = (5, 5)
block_stride = (5, 5)
nbins = 9
deriv_aperture = 1
win_sigma = 4.
histogram_norm_type = 0
l2_hys_threshold = 2.0000000000000001e-01
gamma_correction = 0
nlevels = 64
hog = cv2.HOGDescriptor(win_size,block_size,block_stride,cell_size,nbins,deriv_aperture,win_sigma,
histogram_norm_type,l2_hys_threshold,gamma_correction,nlevels)
data = []
labels = []
for cls in self.dataset:
for image in cls.image_paths:
img = cv2.imread(image)
img = cv2.resize(img, (250, 250))
#convert image to greyscale
#gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#calculate the hog for the image
descriptor = hog.compute(img)
#add data and label to respective lists
data.append(descriptor)
labels.append(cls.name)
#convert list to numpy array for compatibility with the svm
data = np.asarray(data)
labels = np.asarray(labels)
#return the array of hogs and labels
return data, labels
class HOG_SKI_Embedding(Embedding):
def __init__(self, dataset):
super().__init__(dataset)
def get_embeddings(self):
"""
get_embeddings - function to get embeddings (in form of HOG) of a dataset using Scikit
args self
returns data - array of embeddings for each image
labels - array of corresponding labels for each embedding
"""
data = []
labels = []
for cls in self.dataset:
for image in cls.image_paths:
img = cv2.imread(image)
#convert image to greyscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#calculate the hog for the image
descriptor = feature.hog(gray_img, orientations=9, pixels_per_cell=(5,5),
cells_per_block=(2,2), transform_sqrt=True)
#add data and label to respective lists
data.append(descriptor)
labels.append(cls.name)
#convert list to numpy array for compatibility with the svm
data = np.asarray(data)
labels = np.asarray(labels)
#return the array of hogs and labels
return data, labels
class DLIBEmbedding(Embedding):
"""
An implementation using Adam Geitgey's face_recognition library
This is an API that makes use of DLIB
args - dataset
"""
def __init__(self, dataset):
super().__init__(dataset)
def get_embeddings(self):
"""
get_embeddings - function to get embeddings using the face_recognition lib
args self
returns data - array of embeddings for each image
labels - array of corresponding labels for each embedding
"""
data = []
labels = []
for cls in self.dataset:
for image in cls.image_paths:
#load image into an array
img = face_recognition.load_image_file(image)
#calculate face encodings for each face
#(Grab index 0 because we know there is only one face per image)
encoding = face_recognition.face_encodings(img)
#add data and label to respective lists
for i in encoding:
data.append(i)
labels.append(cls.name)
#convert to numpy array for compatibility with the svm
data = np.asarray(data)
labels = np.asarray(labels)
#return the arrays of encodings and labels
return data, labels