-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataset.py
237 lines (200 loc) · 9.48 KB
/
dataset.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
230
231
232
233
234
235
236
import os
from PIL import Image
import torch
from torch.utils import data
from torchvision import transforms
import utils_fixation
import PIL
def preprocess(img, out_size=None, data='img'):
transformations = []
if data in ('img', 'sal'):
transformations.append(transforms.Resize(
out_size, interpolation=PIL.Image.LANCZOS))
else:
transformations.append(transforms.Resize(
out_size, interpolation=PIL.Image.NEAREST))
processing = transforms.Compose(transformations)
tensor = processing(img)
return tensor
class ImageData(data.Dataset):
def __init__(self, img_root, label_root, transform, t_transform, f_transform, filename=None, mode='Train'):
if filename is None:
self.image_path = list(map(lambda x: os.path.join(img_root, x), os.listdir(img_root)))
if mode == 'train':
self.label_path = list(
map(lambda x: os.path.join(label_root, x.split('/')[-1][:-3] + 'png'), self.image_path))
elif mode == 'test':
self.label_path = []
self.fix_path = []
else:
raise NotImplementedError
else:
lines = [line.rstrip('\n') for line in open(filename)]
self.image_path = list(map(lambda x: os.path.join(img_root, x.split(' ')[0]), lines))
if mode == 'train':
self.label_path = list(map(lambda x: os.path.join(img_root, x.split(' ')[1]), lines))
self.fix_path = list(map(lambda x: os.path.join(img_root, x.split(' ')[2]), lines))
elif mode == 'test':
self.label_path = []
self.fix_path = []
else:
raise NotImplementedError
# print(self.fix_path[0])
self.transform = transform
self.t_transform = t_transform
self.f_transform = f_transform
def __getitem__(self, item):
image = Image.open(self.image_path[item]).convert('RGB')
label = Image.open(self.label_path[item]).convert('L')
if 'SALICON' in self.image_path[item] and self.fix_path is not None:
fixation = utils_fixation.get_salicon_fixation_map(self.fix_path[item])
fixation = Image.fromarray(fixation)
elif 'CAT2000' in self.image_path[item] and self.fix_path is not None:
fixation = utils_fixation.get_cat2000_fixation_map(self.fix_path[item])
# fixation = Image.fromarray(fixation)
elif 'MIT1003' or 'MIT300' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
elif 'DHF1K' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
elif 'UCFSPORTS' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
elif 'PseudoSaliency_avg' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
elif 'DUT-OMRON' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
elif 'PASCAL-S' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
elif 'TORONTO' in self.image_path[item] and self.fix_path is not None:
fixation = Image.open(self.fix_path[item]).convert('L')
else:
fixation = None
shape = image.size # [w, h]
if shape[0]/shape[1]>0.8:
new_w = 384
new_h = 224
else:
new_w = 224
new_h = 384
image=preprocess(image, (new_h, new_w), data='img')
nonfixation=fixation
if self.transform is not None:
image = self.transform(image)
if self.t_transform is not None:
label = self.t_transform(label)
fixation = self.f_transform(fixation)
nonfixation =self.f_transform(nonfixation)
return image, label, fixation, nonfixation
def __len__(self):
return len(self.image_path)
class ImageDataTest(data.Dataset):
""" image dataset
img_root: image root (root which contain images)
label_root: label root (root which contains labels)
transform: pre-process for image
t_transform: pre-process for label
filename: MSRA-B use xxx.txt to recognize train-val-test data (only for MSRA-B)
"""
def __init__(self, img_root, label_root, transform, t_transform, filename=None, mode='Train'):
if filename is None:
self.image_path = list(map(lambda x: os.path.join(img_root, x), os.listdir(img_root)))
self.label_path = list(
map(lambda x: os.path.join(label_root, x.split('/')[-1][:-3] + 'png'), self.image_path))
else:
lines = [line.rstrip('\n') for line in open(filename)]
self.image_path = list(map(lambda x: os.path.join(img_root, x.split(' ')[0]), lines))
self.transform = transform
self.t_transform = t_transform
def __getitem__(self, item):
image = Image.open(self.image_path[item]).convert('RGB')
shape = image.size # [w, h]
if shape[0]/shape[1]>0.8:
new_w = 384
new_h = 224
else:
new_w = 224
new_h = 384
image = image.resize((new_w, new_h))
if self.transform is not None:
image = self.transform(image)
return image
def __len__(self):
return len(self.image_path)
def get_loader_test(img_root, label_root, img_size, batch_size, filename=None, mode='test', num_thread=4, pin=True):
t_transform = transforms.Compose([
transforms.ToTensor()
# transforms.Lambda(utils.normalize_tensor)
# transforms.Lambda(lambda x: torch.round(x)) # TODO: it maybe unnecessary
])
transform = transforms.Compose([
# transforms.Resize((img_size, img_size)),
# transforms.Resize((240, 320)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
dataset = ImageDataTest(img_root, label_root, None, None, filename=filename, mode='test')
# data_loader = data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=num_thread,
# pin_memory=pin)
return dataset
def select_nonsalient_points(label, fixation):
label_numpy = np.asarray(label) # [0, 255]
fixation_numpy = np.asarray(fixation) # [0, 255]
mask_salient = ((label_numpy > np.mean(label_numpy)) + (fixation_numpy > 0)) # True or False
mask_nonsalient = (mask_salient <= 0.0) # True or False
num_salpoint = np.sum((fixation_numpy > 0))
index_nonsalient = np.where(mask_nonsalient)
selected_index = np.random.randint(0, len(index_nonsalient[0]), num_salpoint)
points_nonsal_x, points_nonsal_y = (index_nonsalient[0][selected_index], index_nonsalient[1][selected_index]) # (x, y)
nonfixation_numpy=np.zeros_like(fixation_numpy)
for cord in zip(points_nonsal_x, points_nonsal_y):
nonfixation_numpy[cord] = 255
# print(cord)
show_image=False
if show_image:
plt.figure(0)
plt.imshow(label)
plt.figure(1)
plt.imshow(fixation)
plt.figure(2)
plt.imshow(mask_salient)
plt.figure(3)
plt.imshow(mask_nonsalient)
plt.figure(4)
plt.imshow(nonfixation_numpy)
plt.show()
return nonfixation_numpy
# get the dataloader (Note: without data augmentation)
def get_loader(img_root, label_root, img_size, batch_size, filename=None, mode='train', num_thread=1, pin=True):
if mode == 'train':
transform = transforms.Compose([
# transforms.Resize((img_size, img_size)),
# transforms.Resize((240, 320)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
t_transform = transforms.Compose([
# transforms.Resize((img_size, img_size)),
# transforms.Resize((240, 320)),
transforms.ToTensor(),
# transforms.Lambda(utils.normalize_tensor)
# transforms.Lambda(lambda x: torch.round(x)) # TODO: it maybe unnecessary
])
f_transform = transforms.Compose([
# transforms.Resize((img_size, img_size)),
# transforms.Resize((240, 320)),
transforms.ToTensor(),
transforms.Lambda(lambda fix: torch.gt(fix, 0.5))
])
dataset = ImageData(img_root, label_root, transform, t_transform, f_transform, filename=filename, mode='train')
data_loader = data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=num_thread,
pin_memory=pin)
return data_loader
else:
t_transform = transforms.Compose([
transforms.ToTensor()
# transforms.Lambda(utils.normalize_tensor)
# transforms.Lambda(lambda x: torch.round(x)) # TODO: it maybe unnecessary
])
dataset = ImageData(img_root, label_root, None, t_transform, None, filename=filename, mode='test')
# data_loader = data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=num_thread,
# pin_memory=pin)
return dataset