forked from yunjey/stargan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
110 lines (90 loc) · 3.57 KB
/
data_loader.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
import torch
import os
import random
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import ImageFolder
from PIL import Image
class CelebDataset(Dataset):
def __init__(self, image_path, metadata_path, transform, mode):
self.image_path = image_path
self.transform = transform
self.mode = mode
self.lines = open(metadata_path, 'r').readlines()
self.num_data = int(self.lines[0])
self.attr2idx = {}
self.idx2attr = {}
print ('Start preprocessing dataset..!')
self.preprocess()
print ('Finished preprocessing dataset..!')
if self.mode == 'train':
self.num_data = len(self.train_filenames)
elif self.mode == 'test':
self.num_data = len(self.test_filenames)
def preprocess(self):
attrs = self.lines[1].split()
for i, attr in enumerate(attrs):
self.attr2idx[attr] = i
self.idx2attr[i] = attr
self.selected_attrs = ['Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Male', 'Young']
self.train_filenames = []
self.train_labels = []
self.test_filenames = []
self.test_labels = []
lines = self.lines[2:]
random.shuffle(lines) # random shuffling
for i, line in enumerate(lines):
splits = line.split()
filename = splits[0]
values = splits[1:]
label = []
for idx, value in enumerate(values):
attr = self.idx2attr[idx]
if attr in self.selected_attrs:
if value == '1':
label.append(1)
else:
label.append(0)
if (i+1) < 2000:
self.test_filenames.append(filename)
self.test_labels.append(label)
else:
self.train_filenames.append(filename)
self.train_labels.append(label)
def __getitem__(self, index):
if self.mode == 'train':
image = Image.open(os.path.join(self.image_path, self.train_filenames[index]))
label = self.train_labels[index]
elif self.mode in ['test']:
image = Image.open(os.path.join(self.image_path, self.test_filenames[index]))
label = self.test_labels[index]
return self.transform(image), torch.FloatTensor(label)
def __len__(self):
return self.num_data
def get_loader(image_path, metadata_path, crop_size, image_size, batch_size, dataset='CelebA', mode='train'):
"""Build and return data loader."""
if mode == 'train':
transform = transforms.Compose([
transforms.CenterCrop(crop_size),
transforms.Scale(image_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
else:
transform = transforms.Compose([
transforms.CenterCrop(crop_size),
transforms.Scale(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
if dataset == 'CelebA':
dataset = CelebDataset(image_path, metadata_path, transform, mode)
elif dataset == 'RaFD':
dataset = ImageFolder(image_path, transform)
shuffle = False
if mode == 'train':
shuffle = True
data_loader = DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle)
return data_loader