-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
45 lines (35 loc) · 1.35 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import net
import torch
import os
from face_alignment import align
import numpy as np
adaface_models = {
"ir_50": "pretrained/adaface_ir50_ms1mv2.ckpt",
}
def load_pretrained_model(architecture="ir_50"):
# load model and pretrained statedict
assert architecture in adaface_models.keys()
model = net.build_model(architecture)
statedict = torch.load(adaface_models[architecture])["state_dict"]
model_statedict = {key[6:]: val for key, val in statedict.items() if key.startswith("model.")}
model.load_state_dict(model_statedict)
model.eval()
return model
def to_input(pil_rgb_image):
np_img = np.array(pil_rgb_image)
brg_img = ((np_img[:, :, ::-1] / 255.0) - 0.5) / 0.5
tensor = torch.tensor([brg_img.transpose(2, 0, 1)]).float()
return tensor
if __name__ == "__main__":
model = load_pretrained_model("ir_50")
feature, norm = model(torch.randn(2, 3, 112, 112))
test_image_path = "face_alignment/test_images"
features = []
for fname in sorted(os.listdir(test_image_path)):
path = os.path.join(test_image_path, fname)
aligned_rgb_img = align.get_aligned_face(path)
bgr_tensor_input = to_input(aligned_rgb_img)
feature, _ = model(bgr_tensor_input)
features.append(feature)
similarity_scores = torch.cat(features) @ torch.cat(features).T
print(similarity_scores)