-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_module.py
126 lines (107 loc) · 4.05 KB
/
data_module.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
import os
from argparse import ArgumentParser
import pytorch_lightning as pl
import torch
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms as T
from utils.preprocess_data import preprocess_data
class FaceIDDataset(Dataset):
def __init__(self, args, split):
self.hparams = args
self.split = split
if self.split == "train":
self.no_people = self.hparams.no_people_train
self.transform = T.Compose(
[
T.RandomHorizontalFlip(),
T.RandomResizedCrop(self.hparams.img_size, scale=(0.5, 1.0)),
]
)
else:
self.no_people = self.hparams.no_people_val
self.transform = T.CenterCrop(self.hparams.img_size)
def __len__(self):
return self.hparams.poses_per_person * self.no_people
def __getitem__(self, index):
# First photo is deterministic. Second photo is random.
# Get 1st photo
real_index = index % (self.no_people * self.hparams.poses_per_person)
person_id = real_index // self.hparams.poses_per_person
pose_id = real_index % self.hparams.poses_per_person
x_ref = torch.load(
os.path.join(
self.hparams.data_path,
self.split,
"person" + str(person_id) + "_pose" + str(pose_id) + ".pt",
)
)
# Get 2nd photo
if torch.rand(1).item() < 0.5:
# Same person
pose2_valid_choices = list(
set(range(self.hparams.poses_per_person)) - set([pose_id])
)
pose2_id = pose2_valid_choices[
torch.randint(len(pose2_valid_choices), (1,)).item()
]
x = torch.load(
os.path.join(
self.hparams.data_path,
self.split,
"person" + str(person_id) + "_pose" + str(pose2_id) + ".pt",
)
)
y = 1
else:
# Different person
person2_valid_choices = list(set(range(self.no_people)) - set([person_id]))
person2_id = person2_valid_choices[
torch.randint(len(person2_valid_choices), (1,)).item()
]
pose2_id = torch.randint(self.hparams.poses_per_person, (1,)).item()
x = torch.load(
os.path.join(
self.hparams.data_path,
self.split,
"person" + str(person2_id) + "_pose" + str(pose2_id) + ".pt",
)
)
y = -1
x_ref = self.transform(x_ref)
x = self.transform(x)
return x_ref, x, y
class FaceIDDataModule(pl.LightningDataModule):
def __init__(self, args):
super().__init__()
self.hparams = args
@staticmethod
def add_data_specific_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
parser.add_argument("--img_size", type=int, default=256)
parser.add_argument("--no_people_train", type=int, default=26)
parser.add_argument("--no_people_val", type=int, default=5)
parser.add_argument("--poses_per_person", type=int, default=51)
return parser
def prepare_data(self):
if bool(self.hparams.preprocess_data):
print("Preprocessing data...")
preprocess_data(self.hparams.data_path)
print("Finished preprocessing data.")
def train_dataloader(self):
dataset = FaceIDDataset(self.hparams, "train")
return DataLoader(
dataset,
batch_size=self.hparams.batch_size,
num_workers=self.hparams.no_workers,
drop_last=True,
pin_memory=True,
shuffle=True,
)
def val_dataloader(self):
dataset = FaceIDDataset(self.hparams, "val")
return DataLoader(
dataset,
batch_size=self.hparams.batch_size,
num_workers=self.hparams.no_workers,
pin_memory=True,
)