-
#18. Face Recognition for the Happy House연구실 2019. 10. 18. 22:46
- Face recognition 문제는 은 보통 두 가지로 나뉜다.
(1) Face Verification: "Is this the claimed person?", 1:1 matching problem
(2) Face Recognition: "Who is this person?", 1:K matching problem
- FaceNet은 얼굴 이미지를 128개의 원소로 이루어진 벡터로 인코딩하여 학습시키게 된다. 두 벡터를 비교해 같은 사람인지를 판별한다.
* Encoding face images into a 128-dimensional vector
(!) Using an ConvNet to compute encodings
- 96*96의 RGB 이미지를 사용, (m, nC, nH, nW) = (m, 3, 96, 96)
FRmodel = faceRecoModel(input_shape=(3, 96, 96))
- output은 (m, 128)
- 이미지간의 distance를 계산해 같은 사람인지를 판별한다.
(2) The Triplet Loss
- image x에 대해 그 인코딩을 f(x)라 하자.
- 학습은 (A, P, N) 총 3가지의 이미지로 수행한다(triplets of images).
- Anchor image
- Positive image: Anchor와 같은 사람의 이미지
- Negative image: Anchor와 다른 사람의 이미지
- 이때 triplet loss를 최소화시켜야 한다,
# GRADED FUNCTION: triplet_loss def triplet_loss(y_true, y_pred, alpha = 0.2): """ Implementation of the triplet loss as defined by formula (3) Arguments: y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. y_pred -- python list containing three objects: anchor -- the encodings for the anchor images, of shape (None, 128) positive -- the encodings for the positive images, of shape (None, 128) negative -- the encodings for the negative images, of shape (None, 128) Returns: loss -- real number, value of the loss """ anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2] # Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1 pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis = 1) # Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1 neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis = 1) # Step 3: subtract the two previous distances and add alpha. basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha) # Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples. loss = tf.reduce_sum(tf.maximum(basic_loss, 0)) return loss
* applying the model
(1) Face Verification
- 각 사람에 대한 encoding vector를 저장해놓는다.
- input으로 이미지와 그 사람이 누군지새로운 input에 대한 encoding을 계산하고, 미리 저장해놓은 vector와 비교해 threshold보다 작으면 같은 사람, 크면 다른 사람으로 판단한다.
# GRADED FUNCTION: verify def verify(image_path, identity, database, model): """ Function that verifies if the person on the "image_path" image is "identity". Arguments: image_path -- path to an image identity -- string, name of the person you'd like to verify the identity. Has to be a resident of the Happy house. database -- python dictionary mapping names of allowed people's names (strings) to their encodings (vectors). model -- your Inception model instance in Keras Returns: dist -- distance between the image_path and the image of "identity" in the database. door_open -- True, if the door should open. False otherwise. """ # Step 1: Compute the encoding for the image. Use img_to_encoding() see example above. (≈ 1 line) encoding = img_to_encoding(image_path, model) # Step 2: Compute distance with identity's image (≈ 1 line) dist = np.linalg.norm(database[identity] - encoding) # Step 3: Open the door if dist < 0.7, else don't open (≈ 3 lines) if dist < 0.7: print("It's " + str(identity) + ", welcome home!") door_open = True else: print("It's not " + str(identity) + ", please go away") door_open = False return dist, door_open
(2) Face Recognition
- input 이미지에 대해 encoding을 계산하고, 가장 가까운 distance를 가진 사람을 찾는다.
# GRADED FUNCTION: who_is_it def who_is_it(image_path, database, model): """ Implements face recognition for the happy house by finding who is the person on the image_path image. Arguments: image_path -- path to an image database -- database containing image encodings along with the name of the person on the image model -- your Inception model instance in Keras Returns: min_dist -- the minimum distance between image_path encoding and the encodings from the database identity -- string, the name prediction for the person on image_path """ ### START CODE HERE ### ## Step 1: Compute the target "encoding" for the image. Use img_to_encoding() see example above. ## (≈ 1 line) encoding = img_to_encoding(image_path, model) ## Step 2: Find the closest encoding ## # Initialize "min_dist" to a large value, say 100 (≈1 line) min_dist = 100 # Loop over the database dictionary's names and encodings. for (name, db_enc) in database.items(): # Compute L2 distance between the target "encoding" and the current "emb" from the database. (≈ 1 line) dist = np.linalg.norm(database[name] - encoding) # If this distance is less than the min_dist, then set min_dist to dist, and identity to name. (≈ 3 lines) if dist < min_dist: min_dist = dist identity = name ### END CODE HERE ### if min_dist > 0.7: print("Not in the database.") else: print ("it's " + str(identity) + ", the distance is " + str(min_dist)) return min_dist, identity
'연구실' 카테고리의 다른 글
#20. Dinosaur Island - Character-Level Language Modeling (0) 2019.10.24 #19. Building your Recurrent Neural Network - Step by Step (0) 2019.10.23 #17. Deep Learning & Art: Neural Style Transfer (0) 2019.10.18 #16. Autonomous driving - Car detection (0) 2019.10.15 #15. Residual Networks (0) 2019.10.14