IrvingMeng / MagFace

MagFace: A Universal Representation for Face Recognition and Quality Assessment, CVPR2021, Oral
Apache License 2.0
626 stars 85 forks source link

Adjusting Class Count in Pretrained Model #66

Closed vkouam closed 11 months ago

vkouam commented 11 months ago

Hi,

I'm using a pretrained MagFace model (based on iResNet18) from the Model Zoo and trying to finetune it on my custom dataset with 2000 identities (about 10000 images). The dataset is described as recommended in the train.list file, where each line contains the image name (image path + image name), '0' as Separator and the identity (id). I consider the id as the identity of the person on the image (i.e.: _/path_to_img/212.png 0 2).

My understanding is that during MagFaces Training Identities in the Dataset are considered as classes (targets). So when attempting to start training with "--last_fc_size 2000," I encounter the following error:

**_> RuntimeError: Error(s) in loading state_dict for SoftmaxBuilder:

size mismatch for fc.weight: copying a param with shape torch.Size([512, 10572]) from checkpoint, the shape in current model is torch.Size([512, 2000])._**

Questions: What is considered a "class" during training? And how can I resolve this issue? How could I properly load parameters from a pretrained modell, so that I am sure, that everything goes well?

Thank you in advance for your assistance!

IrvingMeng commented 11 months ago

The fc.weight is of size [512, num_of_classes]. For finetuning, that weight should not be loaded as the num_of_classes are inconsistent.

The code may be helpful.

vkouam commented 11 months ago

Hi, thank you for your quick reply:)

I have adopted these functions for loading the weights and set the parameter '--last-fc-size 85742' to 3000 when executing the script 'run.sh', as I have 3000 identities in my training data set. The error message has now disappeared.

Thank you @IrvingMeng

vkouam commented 11 months ago

When preparing the images for training, I set the size to 112x112 with 3 channels. Is this sufficient for MagFace to be able to use the images correctly?

IrvingMeng commented 11 months ago

Hi, face images should be properly aligned by facial landmarks as indicated in: https://github.com/IrvingMeng/MagFace?tab=readme-ov-file#basic-training.

vkouam commented 11 months ago

Hi @IrvingMeng, I guess, I could use the indicated code for aligning my images. i.e. by calling the function _norm_crop(img, landmark, image_size=112, mode='arcface')_ ?

IrvingMeng commented 11 months ago

yep, you are right.

vkouam commented 11 months ago

Thank you, @IrvingMeng :)

vkouam commented 10 months ago

Hi, I please have a question to the landmarks list to be considered when calling the function _'normcrop(img, landmark)' , when trying to align properly the images. I first made a Landmark detection (with the module 'face_recognition') on my image and got as example the list of landmarks bellow:

[{'chin': [(21, 54), (23, 63), (24, 72), (26, 81), (29, 89), (34, 96), (41, 102), (48, 107), (57, 109), (65, 107), (72, 101), (78, 95), (83, 87), (85, 79), (86, 70), (88, 62), (89, 53)], 'left_eyebrow': [(27, 49), (31, 44), (37, 43), (43, 43), (50, 45)], 'right_eyebrow': [(62, 45), (68, 43), (74, 43), (80, 44), (84, 48)], 'nose_bridge': [(56, 52), (56, 57), (57, 61), (57, 67)], 'nose_tip': [(51, 71), (54, 72), (57, 73), (60, 72), (62, 71)], 'left_eye': [(36, 53), (40, 51), (44, 51), (48, 53), (44, 54), (40, 54)], 'right_eye': [(63, 54), (67, 51), (71, 51), (75, 53), (72, 54), (67, 54)], 'top_lip': [(43, 82), (48, 79), (53, 78), (57, 79), (60, 79), (66, 80), (70, 82), (68, 83), (61, 81), (57, 81), (53, 80), (45, 82)], 'bottom_lip': [(70, 82), (66, 88), (61, 91), (57, 92), (52, 91), (47, 88), (43, 82), (45, 82), (53, 88), (57, 89), (60, 88), (68, 83)]}]

Since the function estimate_norm from the face_align.py file expects a landmark shape of (5, 2), how could I bring the above landmark list into a suitable (5, 2) form? Which landmarks should be considered, for MagFace to be properly trained on the image?