-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_face_embeddings_from_dir.py
163 lines (127 loc) · 5.25 KB
/
extract_face_embeddings_from_dir.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import argparse
from tqdm import tqdm
import sys
import torch
import torchvision.transforms as transforms
import torchvision
from PIL import Image
from sklearn.preprocessing import normalize
import numpy as np
from utils.iresnet import iresnet100
from utils.irse import IR_101
import torchvision
import inspect
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
sys.path.insert(0, parent_dir)
import sys
sys.path.insert(0, 'IDiff-Face/')
from utils.iresnet import iresnet100, iresnet50
from utils.irse import IR_101
from utils.synface_resnet import LResNet50E_IR
from utils.moco import MoCo
def load_img_paths(datadir):
"""load num_imgs many FFHQ images"""
img_files = sorted(os.listdir(datadir))
return [os.path.join(datadir, f_name) for f_name in img_files if f_name.endswith(".jpg") or f_name.endswith(".png")]
class ImageInferenceDataset(torch.utils.data.Dataset):
def __init__(self, datadir):
self.img_paths = load_img_paths(datadir)
print("Number of images:", len(self.img_paths))
self.transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
]
)
def __getitem__(self, index):
"""Reads an image from a file and preprocesses it and returns."""
image = Image.open(self.img_paths[index])
if self.transform is not None:
image = self.transform(image)
return image, os.path.basename(self.img_paths[index])
def __len__(self):
"""Returns the total number of font files."""
return len(self.img_paths)
def load_elasticface(device):
print("loading ElasticFace model...")
ckpt = torch.load(os.path.join("utils", "Elastic_R100_295672backbone.pth"), map_location=device)
backbone = iresnet100(num_features=512).to(device)
backbone.load_state_dict(ckpt)
return backbone
def load_curricularface(device):
print("loading CurricularFace model...")
backbone = IR_101([112, 112]).to(device)
ckpt = torch.load(os.path.join("utils", "CurricularFace_Backbone.pth"), map_location=device)
backbone.load_state_dict(ckpt)
return backbone
def main(args):
device = torch.device(0)
bs = 1
print("Dataset:", args.data_dir)
if args.frm_name == "elasticface":
model = load_elasticface(device)
elif args.frm_name == "curricularface":
model = load_curricularface(device)
elif args.frm_name == "imagenet":
model = torchvision.models.resnet34(pretrained=True)
model.fc = torch.nn.Identity()
model = model.to(device)
elif args.frm_name == "idiff-face":
model = iresnet50(num_features=512).to(device)
ckpt = torch.load(os.path.join("utils", "54684backbone.pth"), map_location=device)
model.load_state_dict(ckpt)
elif args.frm_name == "sface":
model = iresnet50(num_features=512).to(device)
ckpt = torch.load(os.path.join("utils", "79232backbone.pth"), map_location=device)
model.load_state_dict(ckpt)
elif args.frm_name == "usynthface":
model = iresnet50(num_features=512)
model = MoCo(base_encoder=iresnet50, dim=512, K=32768).to(device)
ckpt = torch.load(os.path.join("utils", "checkpoint_051.pth"), map_location=device)
model.load_state_dict(ckpt, strict=False)
model = model.encoder_q
elif args.frm_name == "synface":
model = LResNet50E_IR([112, 96]).to(device)
ckpt = torch.load(os.path.join("utils", "model_10k_50_idmix_9197.pth"), map_location=device)["state_dict"]
model.load_state_dict(ckpt)
else:
raise NotImplementedError
model.eval()
dataset = ImageInferenceDataset(args.data_dir)
loader = torch.utils.data.DataLoader(dataset, batch_size=bs, shuffle=False, drop_last=False)
os.makedirs(args.out_dir, exist_ok=True)
print(f"Starting encoding ...")
content = {}
for i, (img_batch, filename_batch) in tqdm(enumerate(loader), total=len(loader)):
img_batch = torchvision.transforms.functional.resize(img_batch, 112)
if args.frm_name == "synface":
img_batch = torchvision.transforms.functional.center_crop(img_batch, [112, 96])
img_batch = img_batch.to(device)
emb_batch = model(img_batch)
emb_batch = torch.nn.functional.normalize(emb_batch).detach().cpu().numpy()
for emb, filename in zip(emb_batch, filename_batch):
content[filename.split(".")[0]] = emb
torch.save(content, os.path.join(args.out_dir, f"embeddings_{args.frm_name}_" + os.path.basename(args.data_dir) + ".npy"))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PyTorch Inference")
parser.add_argument(
"--frm_name",
type=str,
default="sface",
help="[elasticface, curricularface, imagenet, idiff-face, synface, usynthface, sface]",
)
parser.add_argument(
"--data_dir",
type=str,
default="IDiff-Face/data/ffhq_128",
help="path to data directory",
)
parser.add_argument(
"--out_dir",
type=str,
default="IDiff-Face/data",
help="directory to save embedding file to"
)
args = parser.parse_args()
main(args)