-
Notifications
You must be signed in to change notification settings - Fork 20
/
data.py
49 lines (41 loc) · 1.75 KB
/
data.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
# Copyright (C) 2019 Elvis Yu-Jing Lin <[email protected]>
#
# This work is licensed under the MIT License. To view a copy of this license,
# visit https://opensource.org/licenses/MIT.
"""Custom datasets for CelebA and CelebA-HQ."""
import numpy as np
import os
from skimage import io
import torch
import torch.utils.data as data
import torchvision.transforms as transforms
class CelebA(data.Dataset):
def __init__(self, data_path, attr_path, image_size, mode, selected_attrs):
super(CelebA, self).__init__()
self.data_path = data_path
att_list = open(attr_path, 'r', encoding='utf-8').readlines()[1].split()
atts = [att_list.index(att) + 1 for att in selected_attrs]
images = np.loadtxt(attr_path, skiprows=2, usecols=[0], dtype=np.str)
labels = np.loadtxt(attr_path, skiprows=2, usecols=atts, dtype=np.int)
if len(labels.shape) == 1:
labels = labels[:, None]
if mode == 'train':
self.images = images[:200000]
self.labels = labels[:200000]
if mode == 'test':
self.images = images[200000:]
self.labels = labels[200000:]
self.tf = transforms.Compose([
transforms.ToPILImage(),
transforms.CenterCrop(170),
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
self.length = len(self.images)
def __getitem__(self, index):
img = self.tf(io.imread(os.path.join(self.data_path, self.images[index])))
att = torch.tensor((self.labels[index] + 1) // 2)
return img, att
def __len__(self):
return self.length