-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
266 lines (218 loc) · 8.76 KB
/
Copy pathcode.py
File metadata and controls
266 lines (218 loc) · 8.76 KB
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
# -*- coding: utf-8 -*-
"""code
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1-5FaHd0LZQPvtdM4kTksspFfKbdb7s-N
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
import random
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Dense, Flatten,Layer
import tensorflow as tf
gpus= tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
POS_PATH=os.path.join('data','positive')
NEG_PATH=os.path.join('data','negative')
ANC_PATH=os.path.join('data','anchor')
#os.makedirs(POS_PATH)
#os.makedirs(NEG_PATH)
#os.makedirs(ANC_PATH)
#for directory in os.listdir('lfw-deepfunneled'):
# for file in os.listdir(os.path.join('lfw-deepfunneled', directory)):
# EX_PATH = os.path.join('lfw-deepfunneled', directory, file)
# NEW_PATH = os.path.join(NEG_PATH, file)
# os.replace(EX_PATH, NEW_PATH)
import uuid
#cap=cv2.VideoCapture(0)
os.path.join(ANC_PATH, '{}.jpg'.format(uuid.uuid1()))
# Establish a connection to the webcam
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
# Cut down frame to 250x250px
frame = frame[120:120+250,200:200+250, :]
# Collect anchors
if cv2.waitKey(1) & 0XFF == ord('a'):
# Create the unique file path
imgname = os.path.join(ANC_PATH, '{}.jpg'.format(uuid.uuid1()))
# Write out anchor image
cv2.imwrite(imgname, frame)
# Collect positives
if cv2.waitKey(1) & 0XFF == ord('p'):
# Create the unique file path
imgname = os.path.join(POS_PATH, '{}.jpg'.format(uuid.uuid1()))
# Write out positive image
cv2.imwrite(imgname, frame)
# Show image back to screen
cv2.imshow('Image Collection', frame)
# Breaking gracefully
if cv2.waitKey(1) & 0XFF == ord('q'):
break
# Release the webcam
cap.release()
# Close the image show frame
cv2.destroyAllWindows()
anchor = tf.data.Dataset.list_files(ANC_PATH+'\*.jpg').take(100)
positive = tf.data.Dataset.list_files(POS_PATH+'\*.jpg').take(100)
negative = tf.data.Dataset.list_files(NEG_PATH+'\*.jpg').take(100)
dir_test=anchor.as_numpy_iterator()
def preprocess(file_path):
# Read in image from file path
byte_img = tf.io.read_file(file_path)
# Load in the image
img = tf.io.decode_jpeg(byte_img)
# Preprocessing steps - resizing the image to be 100x100x3
img = tf.image.resize(img, (100,100))
# Scale image to be between 0 and 1
img = img / 255.0
# Return image
return img
positives = tf.data.Dataset.zip((anchor, positive, tf.data.Dataset.from_tensor_slices(tf.ones(len(anchor)))))
negatives = tf.data.Dataset.zip((anchor, negative, tf.data.Dataset.from_tensor_slices(tf.zeros(len(anchor)))))
data = positives.concatenate(negatives)
def preprocess_twin(input_img, validation_img, label):
return(preprocess(input_img), preprocess(validation_img), label)
data = data.map(preprocess_twin)
data = data.cache()
data = data.shuffle(buffer_size=1000)
train_data = data.take(round(len(data)*.7))
train_data = train_data.batch(16)
train_data = train_data.prefetch(8)
# Testing partition
test_data = data.skip(round(len(data)*.7))
test_data = test_data.take(round(len(data)*.3))
test_data = test_data.batch(16)
test_data = test_data.prefetch(8)
def make_embedding():
inp = Input(shape=(100,100,3), name='input_image')
# First block
c1 = Conv2D(64, (10,10), activation='relu')(inp)
m1 = MaxPooling2D(64, (2,2), padding='same')(c1)
# Second block
c2 = Conv2D(128, (7,7), activation='relu')(m1)
m2 = MaxPooling2D(64, (2,2), padding='same')(c2)
# Third block
c3 = Conv2D(128, (4,4), activation='relu')(m2)
m3 = MaxPooling2D(64, (2,2), padding='same')(c3)
# Final embedding block
c4 = Conv2D(256, (4,4), activation='relu')(m3)
f1 = Flatten()(c4)
d1 = Dense(4096, activation='sigmoid')(f1)
return Model(inputs=[inp], outputs=[d1], name='embedding')
embedding = make_embedding()
class L1Dist(Layer):
# Init method - inheritance
def __init__(self, **kwargs):
super().__init__()
# Magic happens here - similarity calculation
def call(self, input_embedding, validation_embedding):
return tf.math.abs(input_embedding - validation_embedding)
def make_siamese_model():
# Anchor image input in the network
input_image = Input(name='input_img', shape=(100,100,3))
# Validation image in the network
validation_image = Input(name='validation_img', shape=(100,100,3))
# Combine siamese distance components
siamese_layer = L1Dist()
siamese_layer._name = 'distance'
distances = siamese_layer(embedding(input_image), embedding(validation_image))
# Classification layer
classifier = Dense(1, activation='sigmoid')(distances)
return Model(inputs=[input_image, validation_image], outputs=classifier, name='SiameseNetwork')
siamese_model = make_siamese_model()
binary_cross_loss = tf.losses.BinaryCrossentropy()
opt = tf.keras.optimizers.Adam(1e-4)
checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, 'ckpt')
checkpoint = tf.train.Checkpoint(opt=opt, siamese_model=siamese_model)
@tf.function
def train_step(batch):
# Record all of our operations
with tf.GradientTape() as tape:
# Get anchor and positive/negative image
X = batch[:2]
# Get label
y = batch[2]
# Forward pass
yhat = siamese_model(X, training=True)
# Calculate loss
loss = binary_cross_loss(y, yhat)
print(loss)
# Calculate gradients
grad = tape.gradient(loss, siamese_model.trainable_variables)
# Calculate updated weights and apply to siamese model
opt.apply_gradients(zip(grad, siamese_model.trainable_variables))
# Return loss
return loss
from tensorflow.keras.metrics import Precision, Recall
EPOCHS = 20
#train(train_data, EPOCHS)
def train(data, EPOCHS):
# Loop through epochs
for epoch in range(1, EPOCHS+1):
print('\n Epoch {}/{}'.format(epoch, EPOCHS))
progbar = tf.keras.utils.Progbar(len(data))
# Creating a metric object
r = Recall()
p = Precision()
# Loop through each batch
for idx, batch in enumerate(data):
# Run train step here
loss = train_step(batch)
yhat = siamese_model.predict(batch[:2])
r.update_state(batch[2], yhat)
p.update_state(batch[2], yhat)
progbar.update(idx+1)
print(loss.numpy(), r.result().numpy(), p.result().numpy())
# Save checkpoints
if epoch % 10 == 0:
checkpoint.save(file_prefix=checkpoint_prefix)
siamese_model.save('siamesemodelv2.h5')
siamese_model = tf.keras.models.load_model('siamesemodelv2.h5',
custom_objects={'L1Dist':L1Dist, 'BinaryCrossentropy':tf.losses.BinaryCrossentropy})
def verify(model, detection_threshold, verification_threshold):
# Build results array
results = []
for image in os.listdir(os.path.join('application_data', 'verification_images')):
input_img = preprocess(os.path.join('application_data', 'input_image', 'input_image.jpg'))
validation_img = preprocess(os.path.join('application_data', 'verification_images', image))
# Make Predictions
result = model.predict(list(np.expand_dims([input_img, validation_img], axis=1)))
results.append(result)
# Detection Threshold: Metric above which a prediciton is considered positive
detection = np.sum(np.array(results) > detection_threshold)
# Verification Threshold: Proportion of positive predictions / total positive samples
verification = detection / len(os.listdir(os.path.join('application_data', 'verification_images')))
verified = verification > verification_threshold
return results, verified
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
frame = frame[120:120+250,200:200+250, :]
cv2.imshow('Verification', frame)
# Verification trigger
if cv2.waitKey(10) & 0xFF == ord('v'):
# Save input image to application_data/input_image folder
# hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# h, s, v = cv2.split(hsv)
# lim = 255 - 10
# v[v > lim] = 255
# v[v <= lim] -= 10
# final_hsv = cv2.merge((h, s, v))
# img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(os.path.join('application_data', 'input_image', 'input_image.jpg'), frame)
# Run verification
results, verified = verify(siamese_model, 0.5, 0.5)
print(verified)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()