mk-minchul / AdaFace

MIT License
658 stars 121 forks source link

How do I change mtcnn to retinaface or other face detection model? How do I pass face aligment dots? #96

Open martinenkoEduard opened 1 year ago

martinenkoEduard commented 1 year ago

How do I change mtcnn to retinaface or other face detection model? How do I pass face alignment dots? Can someone share a working example?

ANDRESHZ commented 1 year ago

Hi @martinenkoEduard, I used something that comment @mk-minchul #28 , I solved by the block of de "try: Except" into align.py file and call in there and evaluation model of retinaface or YOLO pretrained for example in prediction mode.

So into align.py at get_aligned_face function just change exception code to:

def get_aligned_face(image_path, rgb_pil_image=None):
    if rgb_pil_image is None:
        img = Image.open(image_path).convert('RGB')
    else:
        assert isinstance(rgb_pil_image, Image.Image), 'Face alignment module requires PIL image or path to the image'
        img = rgb_pil_image
    # find face
    try:
        bboxes, faces = mtcnn_model.align_multi(img, limit=1)
        face = faces[0]
    except Exception as e: #CHANGE CODE HERE!!!!
        width, height = img.size
        #Function to call YOLO or RetinaFace and obtain x, y , w and h [bbox in percentage % format ]
        results = model(img)  # this example use YOLO  created and instanced before using https://docs.ultralytics.com/tasks/detect/#val
        x, y, w, h= results2xywh(results) #send the biggest face identify at the image from YOLO format
        left = width*(x)
        top = height*(y)
        right = width*(w)
        bottom = height*(h)
        img2=img.crop((left, top, right, bottom))
        face=img2.resize((112,112))
        #print('Face detection Failed due to error.')
        #print(e)
        #face = None
    return face

you do not forget to reload the modules and import.

I hope to you help you @martinenkoEduard . I have been trying to training with custom data an I found #75. Did you found some solution? or even you got a training well passed script? THAT WOULD BE GREAT!!

martinenkoEduard commented 1 year ago

Hi @martinenkoEduard, I used something that comment @mk-minchul #28 , I solved by the block of de "try: Except" into align.py file and call in there and evaluation model of retinaface or YOLO pretrained for example in prediction mode.

So into align.py at get_aligned_face function just change exception code to:

def get_aligned_face(image_path, rgb_pil_image=None):
    if rgb_pil_image is None:
        img = Image.open(image_path).convert('RGB')
    else:
        assert isinstance(rgb_pil_image, Image.Image), 'Face alignment module requires PIL image or path to the image'
        img = rgb_pil_image
    # find face
    try:
        bboxes, faces = mtcnn_model.align_multi(img, limit=1)
        face = faces[0]
    except Exception as e: #CHANGE CODE HERE!!!!
        width, height = img.size
        #Function to call YOLO or RetinaFace and obtain x, y , w and h [bbox in percentage % format ]
        results = model(img)  # this example use YOLO  created and instanced before using https://docs.ultralytics.com/tasks/detect/#val
        x, y, w, h= results2xywh(results) #send the biggest face identify at the image from YOLO format
        left = width*(x)
        top = height*(y)
        right = width*(w)
        bottom = height*(h)
        img2=img.crop((left, top, right, bottom))
        face=img2.resize((112,112))
        #print('Face detection Failed due to error.')
        #print(e)
        #face = None
    return face

you do not forget to reload the modules and import.

I hope to you help you @martinenkoEduard . I have been trying to training with custom data an I found #75. Did you found some solution? or even you got a training well passed script? THAT WOULD BE GREAT!!

Btw - do you need to retrain model if you changed a face detector? Or will work just fine?

I haven't found solution for this bug, yet..

ANDRESHZ commented 1 year ago

If you want to get better performance every time is necessary a fine tuning, but you can use a face detector is enough to get a nice results. @martinenkoEduard

harvestingmoon commented 1 year ago

Another simple solution you can try is using @insightface model. There they have a face_align under utils which uses ArcFace (together with SCRFD-10g) to align the facial features properly. However, it will be computationally expensive.

martinenkoEduard commented 1 year ago

What about yolo5 trained on faces? Has anyone tried it? Can someone share code with it?

harvestingmoon commented 1 year ago

I'd suggest sticking to MTCNN or TinaFace rather than using YOLOv5, accuracy in MTCNN is higher than YOLOv5