-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
133 lines (114 loc) · 4.2 KB
/
datasets.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
import os
from cv2 import transform
import torch
from torch._C import dtype
from torch.utils.data import DataLoader, Dataset
from torchvision import datasets
import torchvision.transforms as transforms
import torchvision
import glob
import PIL
import random
import math
import pickle
import numpy as np
#This script is a copy of the file with same name from FeNeRF repo
COLOR_MAP = {
0: [0, 0, 0],
1: [204, 0, 0],
2: [76, 153, 0],
3: [204, 204, 0],
4: [51, 51, 255],
5: [204, 0, 204],
6: [0, 255, 255],
7: [255, 204, 204],
8: [102, 51, 0],
9: [255, 0, 0],
10: [102, 204, 0],
11: [255, 255, 0],
12: [0, 0, 153],
13: [0, 0, 204],
14: [255, 51, 153],
15: [0, 204, 204],
16: [0, 51, 0],
17: [255, 153, 51],
18: [0, 204, 0]}
def get_dataset(name, subsample=None, batch_size=1, **kwargs):
dataset = globals()[name](**kwargs)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
drop_last=True,
pin_memory=False,
num_workers=4
)
return dataloader, 3
def get_dataset_distributed(name, world_size, rank, batch_size, **kwargs):
dataset = globals()[name](**kwargs)
sampler = torch.utils.data.distributed.DistributedSampler(
dataset,
num_replicas=world_size,
rank=rank,
)
dataloader = torch.utils.data.DataLoader(
dataset,
sampler=sampler,
batch_size=batch_size,
shuffle=False,
drop_last=True,
pin_memory=True,
num_workers=2,
)
return dataloader, 3
class FFHQDataset(Dataset):
"""
"""
def __init__(self, dataset_path, img_size, return_label=True, **kwargs):
img_base = 'ffhq_mask_img/*.png'
label_base = 'ffhq_mask_mask/*.png'
self.img_path = os.path.join(dataset_path, img_base)
self.label_path = os.path.join(dataset_path, label_base)
self.transform_image = transforms.Compose(
[transforms.Resize(320),
transforms.CenterCrop(256),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5]),
# transforms.RandomHorizontalFlip(p=0.5),
transforms.Resize((img_size, img_size))])
self.transform_label = transforms.Compose([
transforms.Resize(320, interpolation=PIL.Image.NEAREST),
transforms.CenterCrop(256),
transforms.ToTensor(),
# transforms.RandomHorizontalFlip(p=0.5),
transforms.Resize((img_size, img_size), interpolation=PIL.Image.NEAREST)])
self.data_img = sorted(glob.glob(self.img_path))
self.data_label = sorted(glob.glob(self.label_path))
self.return_label = return_label
assert len(self.data_img) == len(self.data_label)
self.color_map = COLOR_MAP
def __len__(self):
return len(self.data_img)
def _mask_labels(self, mask_np):
# label_size = len(self.color_map.keys())
label_size = 12
labels = np.zeros((label_size, mask_np.shape[0], mask_np.shape[1]))
for i, v in enumerate([0, 1, 2, 3, 4, 6, 8, 10, 13, 14, 15, 17]):
labels[i][mask_np==v] = 1.0
return labels
def __getitem__(self, index):
# img = PIL.Image.open(self.data_img[index]).convert('RGB')
img = PIL.Image.open(self.data_img[index])
label = PIL.Image.open(self.data_label[index]).convert('L')
################################
img = self.transform_image(img) # [-1, 1] after normalization
label = self.transform_label(label)
if random.random() > 0.5:
img = transforms.functional.hflip(img)
label = transforms.functional.hflip(label)
label = self._mask_labels((label * 255.)[0])
label = (label - 0.5) / 0.5
label = torch.tensor(label, dtype=torch.float)
if not self.return_label:
return img, 0
return img, label, 0