-
Notifications
You must be signed in to change notification settings - Fork 39
/
dataset.py
129 lines (97 loc) · 3.99 KB
/
dataset.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
import os
import random
import tarfile
from math import ceil, floor
from torch.utils import data
import numpy as np
from utils import image_loader, download
DATASET_TARBALL = "http://vis-www.cs.umass.edu/lfw/lfw-deepfunneled.tgz"
PAIRS_TRAIN = "http://vis-www.cs.umass.edu/lfw/pairsDevTrain.txt"
PAIRS_VAL = "http://vis-www.cs.umass.edu/lfw/pairsDevTest.txt"
def create_datasets(dataroot, train_val_split=0.9):
if not os.path.isdir(dataroot):
os.mkdir(dataroot)
dataroot_files = os.listdir(dataroot)
data_tarball_file = DATASET_TARBALL.split('/')[-1]
data_dir_name = data_tarball_file.split('.')[0]
if data_dir_name not in dataroot_files:
if data_tarball_file not in dataroot_files:
tarball = download(dataroot, DATASET_TARBALL)
with tarfile.open(tarball, 'r') as t:
t.extractall(dataroot)
images_root = os.path.join(dataroot, 'lfw-deepfunneled')
names = os.listdir(images_root)
if len(names) == 0:
raise RuntimeError('Empty dataset')
training_set = []
validation_set = []
for klass, name in enumerate(names):
def add_class(image):
image_path = os.path.join(images_root, name, image)
return (image_path, klass, name)
images_of_person = os.listdir(os.path.join(images_root, name))
total = len(images_of_person)
training_set += map(
add_class,
images_of_person[:ceil(total * train_val_split)])
validation_set += map(
add_class,
images_of_person[floor(total * train_val_split):])
return training_set, validation_set, len(names)
class Dataset(data.Dataset):
def __init__(self, datasets, transform=None, target_transform=None):
self.datasets = datasets
self.num_classes = len(datasets)
self.transform = transform
self.target_transform = target_transform
def __len__(self):
return len(self.datasets)
def __getitem__(self, index):
image = image_loader(self.datasets[index][0])
if self.transform:
image = self.transform(image)
return (image, self.datasets[index][1], self.datasets[index][2])
class PairedDataset(data.Dataset):
def __init__(self, dataroot, pairs_cfg, transform=None, loader=None):
self.dataroot = dataroot
self.pairs_cfg = pairs_cfg
self.transform = transform
self.loader = loader if loader else image_loader
self.image_names_a = []
self.image_names_b = []
self.matches = []
self._prepare_dataset()
def __len__(self):
return len(self.matches)
def __getitem__(self, index):
return (self.transform(self.loader(self.image_names_a[index])),
self.transform(self.loader(self.image_names_b[index])),
self.matches[index])
def _prepare_dataset(self):
raise NotImplementedError
class LFWPairedDataset(PairedDataset):
def _prepare_dataset(self):
pairs = self._read_pairs(self.pairs_cfg)
for pair in pairs:
if len(pair) == 3:
match = True
name1, name2, index1, index2 = \
pair[0], pair[0], int(pair[1]), int(pair[2])
else:
match = False
name1, name2, index1, index2 = \
pair[0], pair[2], int(pair[1]), int(pair[3])
self.image_names_a.append(os.path.join(
self.dataroot, 'lfw-deepfunneled',
name1, "{}_{:04d}.jpg".format(name1, index1)))
self.image_names_b.append(os.path.join(
self.dataroot, 'lfw-deepfunneled',
name2, "{}_{:04d}.jpg".format(name2, index2)))
self.matches.append(match)
def _read_pairs(self, pairs_filename):
pairs = []
with open(pairs_filename, 'r') as f:
for line in f.readlines()[1:]:
pair = line.strip().split()
pairs.append(pair)
return pairs