-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_loader.py
170 lines (134 loc) · 6.16 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from torch.utils import data
from torchvision import transforms as T
import torchvision.transforms.functional as TF
from PIL import Image
from glob import glob
import pickle
import torch
import os
import numpy as np
import random
class My_Dataset(data.Dataset):
def __init__(self, image_dir, z_path, label_path, mode, transform):
self.image_dir = image_dir
self.z_path = z_path
self.label_path = label_path
self.mode = mode
self.transform = transform
self.preprocess()
def preprocess(self, num_test=5000):
""" Preprocess the label file. """
self.dataset = []
# selec_labels = set(np.load('bad_ids_dw.npy')) #set(np.load('hier_label.npy'))
for img_dir, z_path, label_path in zip(self.image_dir.split('+'), self.z_path.split('+'), self.label_path.split('+')):
filenames = os.listdir(img_dir)
noises = np.load(z_path)
labels = np.load(label_path)
for i, fn in enumerate(filenames):
idx = int(fn.split('.')[0])
if labels[idx] > 397: continue # not animals
# if labels[idx] not in selec_labels: continue
self.dataset.append([os.path.join(img_dir, fn), noises[idx], labels[idx]])
random.seed(999)
random.shuffle(self.dataset)
self.dataset = self.dataset[num_test:] if self.mode == 'train' else self.dataset[:num_test]
self.num_images = len(self.dataset)
print('# In {} set, # of data: {}'.format(self.mode, self.num_images))
print('Finished preprocessing the dataset...')
def __getitem__(self, index):
filename, z, label = self.dataset[index]
image = Image.open(filename)
return self.transform(image), torch.FloatTensor(z), torch.tensor(label)
def __len__(self):
"""Return the number of images."""
return self.num_images
class Real_Dataset(data.Dataset):
def __init__(self, real_dir, transform):
self.transform = transform
self.real_dir = real_dir
self.preprocess()
def preprocess(self):
""" Preprocess the label file. """
dir_names = os.listdir(self.real_dir)
with open('class2idx.pkl', 'rb') as f:
class2idx = pickle.load(f)
# selec_labels = set(np.load('bad_ids_dw.npy')) #set(np.load('hier_label.npy'))
self.dataset = []
for dir_name in dir_names:
label = class2idx[dir_name]
if label > 397: continue
# if label not in selec_labels: continue
filenames = glob(os.path.join(self.real_dir, dir_name, '*.JPEG'))
for fn in filenames:
self.dataset.append([fn, label])
self.num_images = len(self.dataset)
print('# In Real set, # of data: {}'.format(self.num_images))
print('Finished preprocessing the dataset...')
def __getitem__(self, index):
"""Return one image and a dummy label."""
filename, label = self.dataset[index]
image = Image.open(filename)
return self.transform(image), torch.tensor(label)
def __len__(self):
"""Return the number of images."""
return self.num_images
class Interpolate_Dataset(data.Dataset):
def __init__(self, image_dir, z_path, label_path, transform):
self.image_dir = image_dir
self.z_path = z_path
self.label_path = label_path
self.transform = transform
self.preprocess()
def preprocess(self, num_test=5000):
""" Preprocess the label file. """
scores = np.load('gan/models/intra_fid_scores.npy')
cherry_set = set([i for i, s in enumerate(scores) if s < 70])
self.dataset = []
for img_dir, z_path, label_path in zip(self.image_dir.split('+'), self.z_path.split('+'), self.label_path.split('+')):
filenames = os.listdir(img_dir)
noises = np.load(z_path)
labels = np.load(label_path)
for i, fn in enumerate(filenames):
idx = int(fn.split('.')[0])
if labels[idx] not in cherry_set: continue
self.dataset.append([fn, noises[idx], labels[idx]])
random.seed(999)
random.shuffle(self.dataset)
self.dataset = self.dataset[:num_test]
self.num_images = len(self.dataset)
print('# of data: {}'.format(self.num_images))
print('Finished preprocessing the dataset...')
def __getitem__(self, index):
"""Interpolate between the same catelog"""
filename, z, label = self.dataset[index]
filename2, z2, label2 = self.dataset[index+1]
image = Image.open(os.path.join(self.image_dir, filename))
image2 = Image.open(os.path.join(self.image_dir, filename2))
image = torch.stack((self.transform(image), self.transform(image2)), dim=0)
z = np.vstack([z, z2])
label = [label, label2]
return image, torch.FloatTensor(z), torch.tensor(label)
def __len__(self):
"""Return the number of images."""
return self.num_images
def get_loader(image_dir, z_path, label_path, mode, image_size=128, batch_size=16, num_workers=4, isReal=False, isInter=False):
"""Build and return a data loader."""
transform = T.Compose([
# T.Resize(image_size),
T.ToTensor(), # [0, 1]
T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # [-1, 1]
])
if isReal:
dataset = Real_Dataset(image_dir, transform=transform)
elif isInter:
dataset = Interpolate_Dataset(image_dir, z_path, label_path, transform=transform)
else:
dataset = My_Dataset(image_dir, z_path, label_path, mode, transform=transform)
data_loader = data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True,
drop_last = (mode=='train'),
num_workers=num_workers,
pin_memory = True
)
return data_loader