-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
229 lines (181 loc) · 7.38 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import os.path as osp
import sys
import numpy as np
import pickle
from PIL import Image
import matplotlib.pyplot as plt
# plt.switch_backend('agg')
import time
import warnings
warnings.filterwarnings('ignore')
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils import data
import scipy.io as io
import scipy.misc as misc
import glob
import csv
from skimage import color
import skimage
""
def pil_loader(path):
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
""
class Unsplash_Dataset(data.Dataset):
def __init__(self, root, shuffle=False, mode='test', size=128, transform=None,
target_transform=None, types='', show_ab=False, loader=pil_loader):
tic = time.time()
self.root = root
self.loader = loader
self.image_transform = transform
# if large:
# self.size = 480
# self.imgpath = glob.glob(root + 'img_480/*.png')
# else:
# self.size = 224
# self.imgpath = glob.glob(root + 'img/*.png')
self.size = size
self.types = types
self.show_ab = show_ab # show ab channel in classify mode
# read split
self.train_file = set()
self.test_file = set()
self.path = []
if mode == 'train':
self.imgpath = glob.glob(root + 'train/*/*.jpg')
for item in self.imgpath:
self.path.append(item)
elif mode == 'test':
self.imgpath = glob.glob(root + 'test/*/*.jpg')
for item in self.imgpath:
self.path.append(item)
self.path = sorted(self.path)
np.random.seed(0)
if shuffle:
perm = np.random.permutation(len(self.path))
self.path = [self.path[i] for i in perm]
if types == 'classify':
ab_list = np.load('data/pts_in_hull.npy')
self.nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(ab_list)
print('Load %d images, used %fs' % (self.path.__len__(), time.time()-tic))
def __getitem__(self, index):
mypath = self.path[index]
img = self.loader(mypath) # PIL Image
img = np.array(img)
# Resize image if necessary
if (img.shape[0] != self.size) or (img.shape[1] != self.size):
img = skimage.transform.resize(img, (self.size, self.size))
# Convert to lab space
img_lab = color.rgb2lab(np.array(img)) # np array
if self.types == 'classify':
X_a = np.ravel(img_lab[:,:,1])
X_b = np.ravel(img_lab[:,:,2])
img_ab = np.vstack((X_a, X_b)).T
_, ind = self.nbrs.kneighbors(img_ab)
ab_class = np.reshape(ind, (self.size,self.size))
ab_class = torch.unsqueeze(torch.LongTensor(ab_class), 0)
# Normalize RGB images -1 to 1
img = (img * 2.) - 1.
# Rearrange channels RGB
img = torch.FloatTensor(np.transpose(img, (2,0,1)))
# Rearrange channels LAB
img_lab = torch.FloatTensor(np.transpose(img_lab, (2,0,1)))
# Normalize LAB images
img_l = torch.unsqueeze(img_lab[0], 0) / 100. # L channel 0-100
######## CHANGED FROM 110 to 128 #########
img_ab = (img_lab[1: : ] + 128) / 255. # ab channel -128 to 127
if self.types == 'classify':
if self.show_ab:
return img_l, ab_class, img_ab
return img_l, ab_class
elif self.types == 'raw':
return img_l, img_ab, img
# if self.show_ab:
# return img_l, img_ab, None
else:
return img_l, img_ab
def __len__(self):
return len(self.path)
class CIFAR_Dataset(data.Dataset):
def __init__(self, root, shuffle=False, mode='test', size=32, transform=None,
target_transform=None, types='', show_ab=False, loader=pil_loader):
tic = time.time()
self.root = root
self.loader = loader
self.image_transform = transform
if mode == 'test' and target_transform:
self.image_transform = target_transform
if mode == 'train':
dataset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=self.image_transform)
else:
dataset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=self.image_transform)
self.size = size
self.types = types
self.show_ab = show_ab # show ab channel in classify mode
images = []
for image, label in dataset:
images.append(image)
self.images = images
# import pdb; pdb.set_trace()
print('Load %d images, used %fs' % (len(images), time.time()-tic))
def __getitem__(self, index):
img = self.images[index]
# img = np.array(img)
img = np.transpose(img, (1, 2, 0))
# Resize image if necessary
if (img.shape[0] != self.size) or (img.shape[1] != self.size):
img = skimage.transform.resize(img, (self.size, self.size))
# Convert to lab space
img_lab = color.rgb2lab(np.array(img)) # np array
if self.types == 'classify':
X_a = np.ravel(img_lab[:,:,1])
X_b = np.ravel(img_lab[:,:,2])
img_ab = np.vstack((X_a, X_b)).T
_, ind = self.nbrs.kneighbors(img_ab)
ab_class = np.reshape(ind, (self.size,self.size))
ab_class = torch.unsqueeze(torch.LongTensor(ab_class), 0)
# Normalize RGB images -1 to 1
img = (img * 2.) - 1.
# Rearrange channels RGB
img = torch.FloatTensor(np.transpose(img, (2,0,1)))
# Rearrange channels LAB
img_lab = torch.FloatTensor(np.transpose(img_lab, (2,0,1)))
# Normalize LAB images
img_l = torch.unsqueeze(img_lab[0], 0) / 100. # L channel 0-100 -> 0-1
######## CHANGED FROM 110 to 128 #########
img_ab = (img_lab[1: : ] + 128) / 255. # ab channel -128 to 127 -> 0-1
if self.types == 'classify':
if self.show_ab:
return img_l, ab_class, img_ab
return img_l, ab_class
elif self.types == 'raw':
return img_l, img_ab, img
# if self.show_ab:
# return img_l, img_ab, None
else:
return img_l, img_ab
def __len__(self):
return len(self.images)
""
if __name__ == '__main__':
data_root = '/scratch/as3ek/image_colorization/data/unsplash_cropped/'
# normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
# std=[0.229, 0.224, 0.225])
image_transform = transforms.Compose([
transforms.ToTensor(),
])
und = CIFAR_Dataset(data_root, mode='train', types='raw',
transform=image_transform)
data_loader = data.DataLoader(und,
batch_size=32,
shuffle=False,
num_workers=4)
for i, (data, target_ab, target_rgb) in enumerate(data_loader):
print(i, data.size())
break