-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
83 lines (72 loc) · 2.91 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
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
from torch.utils import data
from torchvision import transforms
from torchvision.datasets import ImageFolder
from PIL import Image
import torch
import os
import random
import cv2
import numpy as np
class CelebA(data.Dataset):
def __init__(self, data_path, attr_path, image_size, mode, selected_attrs, stargan_selected_attrs):
super(CelebA, self).__init__()
self.data_path = data_path
self.attr_path = attr_path
self.selected_attrs = selected_attrs
self.stargan_selected_attrs = stargan_selected_attrs
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)
self.mode = mode
if mode == 'train':
self.images = images[:182000]
self.labels = labels[:182000]
if mode == 'valid':
self.images = images[182000:182637]
self.labels = labels[182000:182637]
if mode == 'test':
self.images = images[182637:]
self.labels = labels[182637:]
self.tf = transforms.Compose([
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)
# stargan
self.attr2idx = {}
self.idx2attr = {}
self.test_dataset = []
self.preprocess()
def preprocess(self):
"""Preprocess the CelebA attribute file."""
lines = [line.rstrip() for line in open(self.attr_path, 'r')]
all_attr_names = lines[1].split()
for i, attr_name in enumerate(all_attr_names):
self.attr2idx[attr_name] = i
self.idx2attr[i] = attr_name
if self.mode == 'train':
lines = lines[:182000]
if self.mode == 'valid':
lines = lines[182000:182637]
if self.mode == 'test':
lines = lines[182637:]
for i, line in enumerate(lines):
split = line.split()
filename = split[0]
values = split[1:]
label = []
for attr_name in self.stargan_selected_attrs:
idx = self.attr2idx[attr_name]
label.append(values[idx] == '1')
self.test_dataset.append([filename, label])
print('Finished preprocessing the CelebA dataset...')
def __getitem__(self, index):
img = self.tf(Image.open(os.path.join(self.data_path, self.images[index])))
att = torch.tensor((self.labels[index] + 1) // 2)
filename, label = self.test_dataset[index]
return img, att, torch.FloatTensor(label)
def __len__(self):
return self.length