deepinsight / insightface

State-of-the-art 2D and 3D Face Analysis Project
https://insightface.ai
23.02k stars 5.37k forks source link

Train ArcFace on my own data from scratch #2176

Open beybars1 opened 1 year ago

beybars1 commented 1 year ago

Hi there,

I find the process of model training from scratch very confusing. Can anybody provide guides/advices/tips and other sort of help on how to train ArcFace by insightface, please?

I have already read this post, but still do not get a clear picture of this process.

Any help will be much appreciated.

marlowinnovations commented 1 year ago

Hope this helps you out:

1) Create a folder for your dataset. Separate your unique classes of images into their own folders inside of the dataset folder 2) Run im2rec.py to build a .lst file of your training data

python3 im2rec.py /path/to/training/data/folder --list --recursive python3 im2rec.py mydata /path/to/training/data/folder --list --recursive (creates mydata.lst)

This will recursively parse the images in the training data folder and build the list file.

3) Build your .rec database using the newly created .lst file

python3 im2rec.py /path/to/output/your/rec/file python3 im2rec.py mydata /path/to/output/your/rec/file (creates mydata.rec)

beybars1 commented 1 year ago

Hope this helps you out:

1. Create a folder for your dataset.  Separate your unique classes of images into their own folders inside of the dataset folder

2. Run im2rec.py to build a .lst file of your training data

python3 im2rec.py /path/to/training/data/folder --list --recursive python3 im2rec.py mydata /path/to/training/data/folder --list --recursive (creates mydata.lst)

This will recursively parse the images in the training data folder and build the list file.

3. Build your .rec database using the newly created .lst file

python3 im2rec.py /path/to/output/your/rec/file python3 im2rec.py mydata /path/to/output/your/rec/file (creates mydata.rec)

Thanks for your detailed response. I will try it out and come back with results ASAP.

Nomiluks commented 1 year ago

can anyone please guide me to where I can find im2rec.py?

beybars1 commented 1 year ago

can anyone please guide me to where I can find im2rec.py?

Here it is, link

YoungjaeDev commented 1 year ago

@beybars1 @Nomiluks @marlowinnovations Does it have to be datasets with face detected (maybe body not visible...) and aligned in dst points?

marlowinnovations commented 1 year ago

@beybars1 @Nomiluks @marlowinnovations Does it have to be datasets with face detected (maybe body not visible...) and aligned in dst points?

Crop and align into 112x112 images and then you can use im2rec

YoungjaeDev commented 1 year ago

@marlowinnovations Thank you for your fast reply... If I had to ask one more question, I would like to know whether cropping means doing it without considering the aspect-ratio with padding, or whether it is done by maintaining it.

marlowinnovations commented 12 months ago

@marlowinnovations Thank you for your fast reply... If I had to ask one more question, I would like to know whether cropping means doing it without considering the aspect-ratio with padding, or whether it is done by maintaining it.

Yah you'll want to keep the proper aspect ratio so the images don't get distorted. Whatever face alignment code you plan to use during inference, use the same one to crop and align your images for the dataset so its consistent.

karmelyoei commented 10 months ago

@beybars1 Hey, did you succeed in training arcface on your dataset? If so, is there any kind of full explanation or guidelines to follow??

beybars1 commented 10 months ago

@beybars1 Hey, did you succeed in training arcface on your dataset? If so, is there any kind of full explanation or guidelines to follow??

Hi!

Yes, I managed to train it, pretty successfully. Let's make it clear once again, for those who is struggling right now and those who probably will...

The general pipeline of any modern SOTA face recognition algorithms can be grouped as following:

  1. Face detection phase (You never know when exactly the person's will appear on the screen/camera - thus you need to detect a face.)
  2. Face alignment phase (The detected face is not that useful on its own, since different angles/tilts/yaws of detected faces make a significant distribution shift, we do not want it). In this phase, the detected face has to be aligned via particular warp transformation, which exploits facial landmarks (In case of ArcFace, the MTCNN detector was used, with 5 landmark points). For example, the authors of this repository (and maybe authors of generic ArcFace) used following alignment algorithm:
src = np.array([
      [30.2946, 51.6963],
      [65.5318, 51.5014],
      [48.0252, 71.7366],
      [33.5493, 92.3655],
      [62.7299, 92.2041] ], dtype=np.float32 )
if image_size[1]==112:
      src[:,0] += 8.0
dst = landmark.astype(np.float32)
tform = trans.SimilarityTransform()
tform.estimate(dst, src)

link to a source file Notice: After this phase your data (cropped faces) will be 112x112 aligned face images. It is convenient to preprocess all the train/val data before the training and validation phase. Since I/O bound will be high, if you want to detect/crop/align and train on the fly.

  1. Face embedding extraction phase. This is the core process of face recognition, here the model does a forward pass with input cropped image and outputs the (1x512) face embedding, which corresponds to the person image in a high dimension space (512-d). In order to make a comparison between two images (either between person A and person A, or person A and person B), we need to get their (1x512) embeddings and compare them with particular similarity/distance metric. As a rule of thumb, cosine similarity is chosen as default. Note that, cosine similarity should be applied only on the L-2 normalized embeddings.

I hope, I described the most important aspects of training process. I will update my personal ArcFace implementation repository soon. Stay tuned. Link to repo

karmelyoei commented 10 months ago

@beybars1 Thank you so much for your explanation cant wait to see your repo!

Zahra7143 commented 5 months ago

I'm still unable to train it on my dataset. Does anyone have any tutorials or resources that could help me with this?

sohan-addagudi123 commented 4 months ago

How do we set up and use our own validation set?