FacePerceiver / FaRL

FaRL for Facial Representation Learning [Official, CVPR 2022]
https://arxiv.org/abs/2112.03109
MIT License
372 stars 22 forks source link

could you offer a script to reproduce face attribute #23

Open KeyKy opened 9 months ago

KeyKy commented 9 months ago

could you offer a script to reproduce the acc of face attribute?

KeyKy commented 9 months ago

I use the follow code to eval celebA. However I get mAcc 0.8932 in celeba-val, mAcc 0.8856. It is not the same as paper claimed 0.9139

import numpy as np
import sys
import os
import torch
sys.path.append(".")
device = "cuda" if torch.cuda.is_available() else "cpu"

import facer

face_attr = facer.face_attr("farl/celeba/224", device=device)

base_path = './datasets/face/celeba'

kv = {}
# with open(os.path.join(base_path, 'list_attr_celeba.txt'), 'r') as fid:
# with open(os.path.join(base_path, 'list_attr_celeba_val.txt'), 'r') as fid:
with open(os.path.join(base_path, 'list_attr_celeba_test.txt'), 'r') as fid:
    num_images = fid.readline()
    tags = fid.readline()

    for line in fid:
        splited = line.strip().split(' ')
        splited = [x for x in splited if len(x) != 0]
        label = np.array([float(p) for p in splited[1:]])
        kv[splited[0]] = label

acc = torch.zeros(40).float().to(device)
# count = [0.] * 40
count = 0.
with open(os.path.join(base_path, 'list_landmarks_align_celeba.txt'), 'r') as fid:
    num_images = fid.readline()
    tags = fid.readline()
    for line in fid:
        splited = line.strip().split(' ')
        splited = [x for x in splited if len(x) != 0]
        if not splited[0] in kv:
            continue
        points = np.array([float(p) for p in splited[1:]]).reshape(-1, 2)

        image_path = os.path.join(base_path, 'img_align_celeba_png', splited[0].replace('.jpg', '.png'))
        image = facer.hwc2bchw(facer.read_hwc(image_path)).to(device=device)

        faces = dict()
        faces['points'] = torch.from_numpy(points).float().to(device=device).unsqueeze(0)
        faces['image_ids'] = torch.Tensor([0]).long().to(device=device)

        with torch.inference_mode():
            faces = face_attr(image, faces)

        labels = face_attr.labels
        face1_attrs = faces["attrs"][0] # get the first face's attributes

        gt = torch.from_numpy(kv[splited[0]]).to(device)
        gt[gt == -1] = 0

        pd = (face1_attrs > 0.5).long()

        acc += (pd == gt).float()

        count += 1

        print('{}: {}'.format(count, (acc / count).mean()))

print(acc / count)
print((acc / count).mean())

CelebA-val image

CelebA-test image

Luochangjiang10 commented 4 months ago

I also encountered this problem and could not reproduce the acc of the paper.