SoftwareGift / FeatherNets_Face-Anti-spoofing-Attack-Detection-Challenge-CVPR2019

Code for 3rd Place Solution in Face Anti-spoofing Attack Detection Challenge @ CVPR2019,model only 0.35M!!! 1.88ms(CPU)
Other
928 stars 284 forks source link

I got a pretrained model .pth file, and an random image. How to run? #65

Closed congphase closed 3 years ago

congphase commented 4 years ago

I'm a newbie to this. Please explain me. I have successfully downloaded the pre-trained models from the Google Drive link. But I don't know how to start running it. The typical scenario is that I have a random input image that contains a face (not known to be REAL or SPOOF), what's the command to run to know whether it's real or spoof?

Thank you very much.

AyanGadpal commented 4 years ago

Load Model

net = models.FeatherNetB()
checkpoint = torch.load('/content/our_pretrained_models/FeatherNetB_bs32/_47_best.pth.tar', map_location='cpu')
model_dict = {}
state_dict = net.state_dict()
for (k,v) in checkpoint['state_dict'].items():
  # print(k)
  if k[7:] in state_dict:
    model_dict[k[7:]] = v
state_dict.update(model_dict)
net.load_state_dict(state_dict)

Set Testing Env

device = torch.device('cpu')
net.eval()
img_size = 224
ratio = 224.0 / float(img_size)
normalize = transforms.Normalize(mean=[0.14300402, 0.1434545, 0.14277956],
                                std=[0.10050353, 0.100842826, 0.10034215])
transform = transforms.Compose([transforms.Resize(int(224 * ratio)), 
transforms.CenterCrop(img_size),
                               transforms.ToTensor(), normalize])

Single Image inference

img= cv2.imread(PATH)
small_img = cv2.resize(img, (224,224))
pil_im = Image.fromarray(small_img)
img = transform(pil_im)
img = np.array(img)
img = np.expand_dims(img, 0)

input = torch.tensor(img, dtype=torch.float32, device=device)
input = input.to('cpu')
output_1 = net(input)

soft_output = torch.softmax(output_1, dim=-1)
preds = soft_output.to('cpu').detach().numpy()
_,predicted = torch.max(soft_output.data, 1)
predicted = predicted.to('cpu').detach().numpy()
print(predicted)
OrkhanHI commented 3 years ago

Load Model

net = models.FeatherNetB()
checkpoint = torch.load('/content/our_pretrained_models/FeatherNetB_bs32/_47_best.pth.tar', map_location='cpu')
model_dict = {}
state_dict = net.state_dict()
for (k,v) in checkpoint['state_dict'].items():
  # print(k)
  if k[7:] in state_dict:
    model_dict[k[7:]] = v
state_dict.update(model_dict)
net.load_state_dict(state_dict)

Set Testing Env

device = torch.device('cpu')
net.eval()
img_size = 224
ratio = 224.0 / float(img_size)
normalize = transforms.Normalize(mean=[0.14300402, 0.1434545, 0.14277956],
                                std=[0.10050353, 0.100842826, 0.10034215])
transform = transforms.Compose([transforms.Resize(int(224 * ratio)), 
transforms.CenterCrop(img_size),
                               transforms.ToTensor(), normalize])

Single Image inference

img= cv2.imread(PATH)
small_img = cv2.resize(img, (224,224))
pil_im = Image.fromarray(small_img)
img = transform(pil_im)
img = np.array(img)
img = np.expand_dims(img, 0)

input = torch.tensor(img, dtype=torch.float32, device=device)
input = input.to('cpu')
output_1 = net(input)

soft_output = torch.softmax(output_1, dim=-1)
preds = soft_output.to('cpu').detach().numpy()
_,predicted = torch.max(soft_output.data, 1)
predicted = predicted.to('cpu').detach().numpy()
print(predicted)

Hi, I am using FeatherNetB with _47_best.pth.tar checkpoint. As input image I am passing depth_image and output of predicted is just index number. What the predicted index number referring to? Is there any way to get spoof or real probability?