DeepFace

김성빈·2024년 6월 14일
0

Modern Computer Vision

목록 보기
116/117

1. 모듈 다운로드

!pip install deepface
!pip install dlib

고용량 RAM ON

2. 얼굴 랜드마크 시연


랜드마크 모델을 사용해서 이미지에 랜드마크 건설

p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)

3. 딥페이스를 이용하여 나이, 성별, 감정 표현 및 민족성 획득

얼굴의 랜드마크를 찾는 모델만있나?

그건 아니다 세상 똑똑이들이 만들어둔 모델들이 많다.

!gdown --id 1Id32-d-nS9BooBLLkw1PQhvLWWAukCsq
!gdown --id 1txWignSWdELl8cWdZHYqIlSE2ZRjI8WI
!gdown --id 1d_tQRWjvQ5i4lZyUfFEfRj7LzXWXseBY
!gdown --id 1kWp2CVg_xTIFqdZAwfN_86A3grim9NyI

!mv facial_expression_model_weights.zip /root/.deepface/weights/facial_expression_model_weights.zip
!mv age_model_weights.h5 /root/.deepface/weights/age_model_weights.h5
!mv gender_model_weights.h5 /root/.deepface/weights/gender_model_weights.h5
!mv race_model_single_batch.zip /root/.deepface/weights/race_model_single_batch.zip

그럼 저위의 오리지널 미식가 아저씨의 정보를 예측해보자.

MTCNN 백엔드 사용 얼굴 감지

from deepface import DeepFace

obj = DeepFace.analyze(img_path =  "/content/me.jpg", actions = ['age', 'gender', 'race', 'emotion'])


나이..를 제외하고는 다 맞다.

얼추 잘못봤을때 표정이 화난걸로 오해할수도 있는데,

표정의 상태를 "놀람"으로 높은 정확도로 예측했다.

다른 이미지로 예측 및 시각화


민지를 잘생쁨이라고 많이들 말하는데,

모델이 남자 87 , 여자 13로 예측했다. 진짜 맞는것같다.

아시아인, 행복함

Deep Face 백엔드 사용하여 얼굴 감지

달라진게 없다.

똑같이 MTCNN 백엔드를 사용하여 얼굴을 감지한 결과와 똑같다.

4. 유사도 비교

result  = DeepFace.verify("/content/me.jpg", "/content/minji.jpg")
print("Is verified: ", result["verified"])
result

VGG-face 모델을 사용해서 미식가 아저씨와 민지의 유사도를 비교한다.

일단 둘이 다르다는것을 알려주고 0.88로 0에서 매우 떨어진 숫자이다.

그렇다면 위의 화이트 민지와 블루 민지를 비교해보자.

result  = DeepFace.verify("/content/6.jpg", "/content/minji.jpg")
print("Is verified: ", result["verified"])
result

둘이 똑같다고 예측을 하며

0.22로 0과 가까운수치이다.

거리 측정 방식 변경

따로 거리 distance의 형식을 선택해주지 않으면 cosine으로 기본값이 설정된다.

그래서 d_metric인자를 추가해 원하는 것을 넣어주면 된다.

주요 거리 측정 방식

  • 코사인 거리 (Cosine Similarity):

일반적으로 텍스트 분석에서 많이 사용되지만, 얼굴 인식에서도 방향성에 중점을 둔 비교에 유용

  • 유클리드 거리 (Euclidean Distance):

다양한 분야에서 널리 사용되며, 얼굴 인식에서도 기본적으로 많이 사용

  • 유클리드 L2 거리 (Euclidean L2 Distance):

얼굴 임베딩 벡터를 정규화한 후 비교할 때 유용

위 특징을 알고 다음에 상황에 맞게 무언가를 바꿔서 사용할수있다

라는정도로 알아두자.

유사도 한번에 처리

from deepface import DeepFace
import pandas as pd

df = DeepFace.find(img_path = "/content/minji.jpg", db_path = '/content/training_faces', detector_backend = 'ssd')
df

training_faces 폴더 안의 이미지 중 minji = 화이트 민지와 유사도가 가장 높은 이미지를 찾는 코드

결과

24-06-14 00:47:44 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:47:45 - find function duration 0.09854912757873535 seconds
[                        identity                                      hash  \
 0  /content/training_faces/6.jpg  8356576d1f79488ec043b32531a4c9fbb2df367d   
 
    target_x  target_y  target_w  target_h  source_x  source_y  source_w  \
 0        57        37        72        96       252       113       151   
 
    source_h  threshold  distance  
 0       220       0.68  0.225453  ]

보면 training_faces/6.jpg 가 유사도가 가장 높으며 distance는 0.22이다.

6.jpg는 블루민지이고, 참고로 training_faces 폴더 안에는 다양한 이미지 파일이 있다.

다른 모델 시도

이거는 계속 에러가 발생해 에러코드를 확인하니까

Tensorflow 버전 문제라 버전을 낮춰주고 다시 시작했다.

# TensorFlow 버전 다운그레이드
!pip install tensorflow==2.12

# 런타임 다시 시작 필요
import os
os.kill(os.getpid(), 9)

출력값

24-06-14 00:57:54 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:57:56 - find function duration 1.9901697635650635 seconds
Results for model VGG-Face: [                        identity                                      hash  \
0  /content/training_faces/6.jpg  8356576d1f79488ec043b32531a4c9fbb2df367d   

   target_x  target_y  target_w  target_h  source_x  source_y  source_w  \
0        57        37        72        96       252       113       151   

   source_h  threshold  distance  
0       220       0.68  0.225453  ]
24-06-14 00:57:56 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:57:59 - find function duration 2.9760186672210693 seconds
Results for model Facenet: [                        identity                                      hash  \
0  /content/training_faces/6.jpg  8356576d1f79488ec043b32531a4c9fbb2df367d   

   target_x  target_y  target_w  target_h  source_x  source_y  source_w  \
0        57        37        72        96       252       113       151   

   source_h  threshold  distance  
0       220        0.4  0.203414  ]
24-06-14 00:57:59 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:58:01 - find function duration 1.4572718143463135 seconds
Results for model OpenFace: [Empty DataFrame
Columns: [identity, hash, target_x, target_y, target_w, target_h, source_x, source_y, source_w, source_h, threshold, distance]
Index: []]
24-06-14 00:58:01 - Found 11 newly added image(s), 0 removed image(s), 0 replaced image(s).
Finding representations:   0%|          | 0/11 [00:00<?, ?it/s]24-06-14 00:58:12 - VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...
Downloading...
From: https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip
To: /root/.deepface/weights/VGGFace2_DeepFace_weights_val-0.9034.h5.zip

  0%|          | 0.00/511M [00:00<?, ?B/s]
  3%|▎         | 16.3M/511M [00:00<00:03, 161MB/s]
 13%|█▎        | 67.1M/511M [00:00<00:01, 362MB/s]
 23%|██▎       | 119M/511M [00:00<00:00, 432MB/s] 
 33%|███▎      | 168M/511M [00:00<00:00, 456MB/s]
 44%|████▎     | 223M/511M [00:00<00:00, 488MB/s]
 53%|█████▎    | 273M/511M [00:00<00:00, 490MB/s]
 63%|██████▎   | 322M/511M [00:00<00:00, 490MB/s]
 73%|███████▎  | 374M/511M [00:00<00:00, 497MB/s]
 83%|████████▎ | 425M/511M [00:00<00:00, 501MB/s]
100%|██████████| 511M/511M [00:01<00:00, 473MB/s]
Finding representations:  18%|█▊        | 2/11 [00:22<01:24,  9.38s/it]24-06-14 00:58:23 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_3.jpg: Face could not be detected in /content/training_faces/Nidia_3.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations:  64%|██████▎   | 7/11 [00:28<00:09,  2.41s/it]24-06-14 00:58:29 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_2.jpg: Face could not be detected in /content/training_faces/Nidia_2.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations: 100%|██████████| 11/11 [00:34<00:00,  3.12s/it]
24-06-14 00:58:35 - There are now 11 representations in ds_model_deepface_detector_ssd_aligned_normalization_base_expand_0.pkl
24-06-14 00:58:35 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:58:37 - find function duration 35.9246301651001 seconds
Results for model DeepFace: [                        identity                                      hash  \
0  /content/training_faces/6.jpg  8356576d1f79488ec043b32531a4c9fbb2df367d   

   target_x  target_y  target_w  target_h  source_x  source_y  source_w  \
0        57        37        72        96       252       113       151   

   source_h  threshold  distance  
0       220       0.23  0.202124  ]
24-06-14 00:58:37 - Found 11 newly added image(s), 0 removed image(s), 0 replaced image(s).
Finding representations:   0%|          | 0/11 [00:00<?, ?it/s]24-06-14 00:58:37 - deepid_keras_weights.h5 will be downloaded...
Downloading...
From: https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5
To: /root/.deepface/weights/deepid_keras_weights.h5

100%|██████████| 1.61M/1.61M [00:00<00:00, 39.9MB/s]
Finding representations:  36%|███▋      | 4/11 [00:02<00:02,  2.52it/s]24-06-14 00:58:39 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_3.jpg: Face could not be detected in /content/training_faces/Nidia_3.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations:  73%|███████▎  | 8/11 [00:02<00:00,  5.53it/s]24-06-14 00:58:39 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_2.jpg: Face could not be detected in /content/training_faces/Nidia_2.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations: 100%|██████████| 11/11 [00:03<00:00,  3.42it/s]
24-06-14 00:58:40 - There are now 11 representations in ds_model_deepid_detector_ssd_aligned_normalization_base_expand_0.pkl
24-06-14 00:58:40 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:58:40 - find function duration 3.3007586002349854 seconds
Results for model DeepID: [Empty DataFrame
Columns: [identity, hash, target_x, target_y, target_w, target_h, source_x, source_y, source_w, source_h, threshold, distance]
Index: []]
24-06-14 00:58:40 - Found 11 newly added image(s), 0 removed image(s), 0 replaced image(s).
Finding representations:   0%|          | 0/11 [00:00<?, ?it/s]24-06-14 00:58:41 - arcface_weights.h5 will be downloaded to /root/.deepface/weights/arcface_weights.h5
Downloading...
From: https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5
To: /root/.deepface/weights/arcface_weights.h5

  0%|          | 0.00/137M [00:00<?, ?B/s]
 18%|█▊        | 24.1M/137M [00:00<00:00, 240MB/s]
 39%|███▉      | 53.5M/137M [00:00<00:00, 269MB/s]
 62%|██████▏   | 85.5M/137M [00:00<00:00, 292MB/s]
100%|██████████| 137M/137M [00:00<00:00, 295MB/s]
Finding representations:  18%|█▊        | 2/11 [00:04<00:15,  1.68s/it]24-06-14 00:58:44 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_3.jpg: Face could not be detected in /content/training_faces/Nidia_3.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations:  82%|████████▏ | 9/11 [00:05<00:00,  3.76it/s]24-06-14 00:58:45 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_2.jpg: Face could not be detected in /content/training_faces/Nidia_2.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations: 100%|██████████| 11/11 [00:06<00:00,  1.72it/s]
24-06-14 00:58:46 - There are now 11 representations in ds_model_arcface_detector_ssd_aligned_normalization_base_expand_0.pkl
24-06-14 00:58:46 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:58:46 - find function duration 6.631281137466431 seconds
Results for model ArcFace: [                        identity                                      hash  \
0  /content/training_faces/6.jpg  8356576d1f79488ec043b32531a4c9fbb2df367d   

   target_x  target_y  target_w  target_h  source_x  source_y  source_w  \
0        57        37        72        96       252       113       151   

   source_h  threshold  distance  
0       220       0.68  0.270221  ]
24-06-14 00:58:46 - Found 11 newly added image(s), 0 removed image(s), 0 replaced image(s).
Finding representations:   0%|          | 0/11 [00:00<?, ?it/s]24-06-14 00:58:47 - dlib_face_recognition_resnet_model_v1.dat is going to be downloaded
Downloading...
From: http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2
To: /root/.deepface/weights/dlib_face_recognition_resnet_model_v1.dat.bz2

  0%|          | 0.00/21.4M [00:00<?, ?B/s]
  2%|▏         | 524k/21.4M [00:00<00:34, 599kB/s]
  5%|▍         | 1.05M/21.4M [00:01<00:17, 1.13MB/s]
 10%|▉         | 2.10M/21.4M [00:01<00:08, 2.30MB/s]
 17%|█▋        | 3.67M/21.4M [00:01<00:04, 4.02MB/s]
 34%|███▍      | 7.34M/21.4M [00:01<00:01, 8.55MB/s]
 51%|█████▏    | 11.0M/21.4M [00:01<00:00, 12.0MB/s]
 69%|██████▊   | 14.7M/21.4M [00:01<00:00, 14.5MB/s]
100%|██████████| 21.4M/21.4M [00:02<00:00, 10.0MB/s]
Finding representations:  36%|███▋      | 4/11 [00:06<00:08,  1.16s/it]24-06-14 00:58:52 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_3.jpg: Face could not be detected in /content/training_faces/Nidia_3.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations:  73%|███████▎  | 8/11 [00:06<00:01,  2.23it/s]24-06-14 00:58:53 - 🔴 Exception while extracting faces from /content/training_faces/Nidia_2.jpg: Face could not be detected in /content/training_faces/Nidia_2.jpg.Please confirm that the picture is a face photo or consider to set enforce_detection param to False.
Finding representations: 100%|██████████| 11/11 [00:07<00:00,  1.51it/s]
24-06-14 00:58:54 - There are now 11 representations in ds_model_dlib_detector_ssd_aligned_normalization_base_expand_0.pkl
24-06-14 00:58:54 - Searching /content/minji.jpg in 11 length datastore
24-06-14 00:58:54 - find function duration 7.35061502456665 seconds
Results for model Dlib: [                              identity  \
0        /content/training_faces/6.jpg   
1  /content/training_faces/Nidia_4.jpg   
2  /content/training_faces/Nidia_1.jpg   

                                       hash  target_x  target_y  target_w  \
0  8356576d1f79488ec043b32531a4c9fbb2df367d        57        37        72   
1  992c672cbb40152de9c37d6628d14daf080a1728       321        74       167   
2  8c5eaabf7e11dfb6fc7ffaaacc233ab4d0c0f43e       136        91        63   

   target_h  source_x  source_y  source_w  source_h  threshold  distance  
0        96       252       113       151       220       0.07  0.030508  
1       246       252       113       151       220       0.07  0.062589  
2       105       252       113       151       220       0.07  0.065299  ]
                              identity  \
0        /content/training_faces/6.jpg   
1        /content/training_faces/6.jpg   
2        /content/training_faces/6.jpg   
3        /content/training_faces/6.jpg   
4        /content/training_faces/6.jpg   
5  /content/training_faces/Nidia_4.jpg   
6  /content/training_faces/Nidia_1.jpg   

                                       hash  target_x  target_y  target_w  \
0  8356576d1f79488ec043b32531a4c9fbb2df367d        57        37        72   
1  8356576d1f79488ec043b32531a4c9fbb2df367d        57        37        72   
2  8356576d1f79488ec043b32531a4c9fbb2df367d        57        37        72   
3  8356576d1f79488ec043b32531a4c9fbb2df367d        57        37        72   
4  8356576d1f79488ec043b32531a4c9fbb2df367d        57        37        72   
5  992c672cbb40152de9c37d6628d14daf080a1728       321        74       167   
6  8c5eaabf7e11dfb6fc7ffaaacc233ab4d0c0f43e       136        91        63   

   target_h  source_x  source_y  source_w  source_h  threshold  distance  \
0        96       252       113       151       220       0.68  0.225453   
1        96       252       113       151       220       0.40  0.203414   
2        96       252       113       151       220       0.23  0.202124   
3        96       252       113       151       220       0.68  0.270221   
4        96       252       113       151       220       0.07  0.030508   
5       246       252       113       151       220       0.07  0.062589   
6       105       252       113       151       220       0.07  0.065299   

      model  
0  VGG-Face  
1   Facenet  
2  DeepFace  
3   ArcFace  
4      Dlib  
5      Dlib  
6      Dlib  

요약하자면 VGG-Face, Facenet, DeepFace, ArcFace, Dlib 모델을 사용하여 /content/minji.jpg 이미지를 /content/training_faces 폴더 내의 이미지들과 비교

대부분의 모델(VGG-Face, Fac
enet, DeepFace, ArcFace, Dlib)은 블루민지와 화이트 민지가 동일 인물임을 인식했다.

OpenFace와 DeepID 모델은 유사한 얼굴을 찾지 못했다.

Dlib 모델은 /content/training_faces/Nidia_4.jpg와 /content/training_faces/Nidia_1.jpg에서도 낮은 거리 값을 반환했지만, 임계값 기준으로는 동일 인물로 인식하지 않았다.

이 결과를 바탕으로 각 모델의 성능을 평가할 수 있으며, 특정 모델이 더 잘 맞는지 확인할 수 있다.

profile
감사합니다. https://www.youtube.com/channel/UCxlkiu9_aWijoD7BannNM7w

0개의 댓글

관련 채용 정보