GithubHelp home page GithubHelp logo

martlgap / octuplet-loss Goto Github PK

View Code? Open in Web Editor NEW
43.0 43.0 7.0 173 KB

Repo for our Paper: Octuplet Loss: Make Your Face Recognition Model Robust to Image Resolution

License: MIT License

Python 100.00%

octuplet-loss's People

Contributors

martlgap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

octuplet-loss's Issues

Generating embedding database

Hello, are you able to share code to generate embeddings database from our own dataset? So that we can use for inference?

Thanks!

training dataset

hi Martlgap,

Your work was amazing. Are you willing to provide the dataset file ("/mnt/ssd2/test_embs.pkl") ? Meanwhile, the file seems contains both basic training data and downsimpled data, am I correct?

Thx

implementation with pytorch

Dear @Martlgap ,
first of all thank you for this magnificent work you have done. I wanted to ask you the steps I should take to achieve this face recognition with pytorch. I saw that in the github he released a python file that uses pytorch, I think it is the neural network. To implement everything and make it work what steps should I do starting from the 'pt_octuplet_loss.py' file.
Sorry for disturbing.
Thank you in advance for your reply.

Resolution of low resolution pictures And Batches

Dear @Martlgap ,
first of all thank you for this magnificent work you have done. I wanted to ask you two questions.
1.How many batches are there in one epoch。(I don't know how you generate minibatch. But I randomly selected different identity pictures as a minibatch, so I need to know this.)
2.Whether the resolution of low resolution pictures in one batch is random, or the resolution of low resolution pictures in one batch is the same, but different batches are different ?
Thank you in advance for your reply.

About data preprocessing for pretraining process

Thanks for your work
But I'm still puzzled about the data preprocessing for the pretrained face model. Apart from the finetuning process, is the data downsampling used to pretrain the face model?

cosine distance between different people is < 0.5

I based the code below on the example main.py from the hugging face model page.

Identical images produce a very small distance of 4.919836871231098e-09 as expected.

The different images used here produce a distance of 0.3032730731332305.

The aligned images appear to have been processed correctly at 112x112.

jennifer_aniston jpg_aligned

David_Schwimmer jpg_aligned

The original images can be found here.

https://resizing.flixster.com/-XZAfHZM39UwaGJIFWKAE8fS0ak=/v3/t/assets/30905_v9_bc.jpg
https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/David_Schwimmer_2011.jpg/800px-David_Schwimmer_2011.jpg

Apologies if this is a mistake on my part.

import numpy as np
import onnxruntime as rt
import mediapipe as mp
import cv2
import time
from skimage.transform import SimilarityTransform
from scipy.spatial.distance import cosine

# ---------------------------------------------------------------------------------------------------------------------
# INITIALIZATIONS

# Target landmark coordinates for alignment (used in training)
LANDMARKS_TARGET = np.array(
   [
       [38.2946, 51.6963],
       [73.5318, 51.5014],
       [56.0252, 71.7366],
       [41.5493, 92.3655],
       [70.7299, 92.2041],
   ],
   dtype=np.float32,
)

def compare(img_path1, img_path2):
   img1_embedding = infer(img_path1)
   img2_embedding = infer(img_path2)

   cos_dist = cosine(img1_embedding, img2_embedding)

   if cos_dist <= 0.5:
       print(f'{img_path1} and {img_path2} are the same')
   else:
       print(f'{img_path1} and {img_path2} are different')

   print(f'cosine distance = {cos_dist}')

def infer(img_path):
   img = cv2.imread(img_path)

   # Process the image with the face detector
   FACE_DETECTOR = mp.solutions.face_mesh.FaceMesh(
       refine_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5, max_num_faces=1
   )
   result = FACE_DETECTOR.process(img)

   if result.multi_face_landmarks:
       # Select 5 Landmarks (Eye Centers, Nose Tip, Left Mouth Corner, Right Mouth Corner)
       five_landmarks = np.asarray(result.multi_face_landmarks[0].landmark)[[470, 475, 1, 57, 287]]

       # Extract the x and y coordinates of the landmarks of interest
       landmarks = np.asarray(
           [[landmark.x * img.shape[1], landmark.y * img.shape[0]] for landmark in five_landmarks]
       )

   else:
       print(f"No faces detected in {img_path}")
       exit()

   # ---------------------------------------------------------------------------------------------------------------------
   # FACE ALIGNMENT

   # Align Image with the 5 Landmarks
   tform = SimilarityTransform()
   tform.estimate(landmarks, LANDMARKS_TARGET)
   tmatrix = tform.params[0:2, :]
   img_aligned = cv2.warpAffine(img, tmatrix, (112, 112), borderValue=0.0)
   # safe to disk
   cv2.imwrite(f"{img_path}_aligned.jpg", img_aligned)

   # ---------------------------------------------------------------------------------------------------------------------
   # FACE RECOGNITION

   # Inference face embeddings with onnxruntime
   input_image = (np.asarray([img_aligned]).astype(np.float32)).clip(0.0, 255.0).transpose(0, 3, 1, 2)
   FACE_RECOGNIZER = rt.InferenceSession("FaceTransformerOctupletLoss.onnx", providers=rt.get_available_providers())
   embedding = FACE_RECOGNIZER.run(None, {"input_image": input_image})[0][0]

   return embedding

if __name__ == "__main__":
   ds = "David_Schwimmer.jpg"
   ja = "jennifer_aniston.jpg"

   compare(ja, ds)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.