Xianhua-He / cvpr2024-face-anti-spoofing-challenge

Accepted by CVPR Workshop 2024
MIT License
49 stars 7 forks source link

Always giving the same result #4

Closed 7sn-The-Dreamer closed 5 months ago

7sn-The-Dreamer commented 6 months ago

Greetings,

I have tried your work on my own images, but it always give a score of almost 1 at class index 1.

would you mind to explain the scores at class index 0 and 1?

Would you mind also to give your feedback about why it always give the same result if the face image is real or spoof?

Thanks a lot.

Xianhua-He commented 6 months ago

@7sn-The-Dreamer

Maybe you can provide more detailed information and I'll be happy to help you analyze possible problems?

7sn-The-Dreamer commented 6 months ago

I tried this code on many images where I pre cropped the face detected image:

`import argparse import builtins import os import sys import random import shutil import time import math import warnings import torch import torch.nn as nn import torch.nn.parallel import torch.backends.cudnn as cudnn import torch.distributed as dist import torch.optim import torch.utils.data import torch.utils.data.distributed

from nets.utils import get_model, load_pretrain, load_resume, ExponentialMovingAverage from collections import OrderedDict import cv2 from PIL import Image

import torchvision.transforms as T

from cv2_transform import transforms import albumentations as A from albumentations.pytorch import ToTensorV2

def read_img(imgPath):

transforms1 = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ColorTrans(mode=0),
])

transforms2 = A.Compose([
    A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
    ToTensorV2()
])

img = cv2.imread(imgPath)

data = transforms1(img)
data2 = transforms2(image=data)
data2 = data2['image'].reshape(1,3,224,224)

return data2

def network(eval_model, device):

net = get_model('resnet50', 2).to(device)
net_dict = net.state_dict()
data_dict = {
    key.replace('module.', ''): value for key, value in torch.load(eval_model, map_location=device)['state_dict'].items()}
net_dict.update(data_dict)
net.load_state_dict(net_dict)
net.eval()
return net

def main():

device = 'cuda:0'
eval_model = '...\\cvpr2024-face-anti-spoofing-challenge\\p1_resnet50_epoch199.pth'
net = network(eval_model, device)

input_data = read_img('...\\IMG_0720.jpg')

_, outputs = net(input_data.to(device=device))

scores = torch.softmax(outputs, dim=1).data.cpu().numpy()[:,1]

print(scores)

if name == 'main':

main()

`

Xianhua-He commented 6 months ago

@7sn-The-Dreamer I don't see any problems with the code you gave me. Are you Chinese? Maybe you can add me on WeChat (13381144493) to communicate in detail?

7sn-The-Dreamer commented 6 months ago

I am not Chinese and unfortunately I don’t have WeChat.

I hope if we can find alternative way for communication.

Using the code I wrote, if the face image is real or spoof, the resulting score is always 1. What this means? Does it mean spoof with probability of 1 ? or what?

Xianhua-He commented 6 months ago

yes, If the score output by the model is 1, it is spoof

7sn-The-Dreamer commented 6 months ago

Why it always give spoof result while some images are real?

Xianhua-He commented 6 months ago

@7sn-The-Dreamer What you are testing is the model I provided that was trained on the P1 protocol of the competition dataset. This model training is based on a very small data set and cannot achieve good results for any image. Because there are domain differences between different datasets. Our paper is an innovative method proposed for competition scenarios, and may not be suitable for any scenario. If you want to get better results, it is recommended that you build your own dataset for retraining.

mikeswf commented 5 months ago

@7sn-The-Dreamer The dataset is too small, the model could not work very well in every situation. I try it and test some pictures in the test set with your code, no wrong.

sfxeazilove commented 1 month ago

Hi, for this single image evaluation, are we saying it's not necessary to use a face detector?