-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
199 lines (151 loc) · 6.2 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
import os
from PIL import Image
import cv2
import torch
from torch.utils import data
from torchvision import transforms
from torchvision.transforms import functional as F
import numbers
import numpy as np
import random
class ImageDataTest(data.Dataset):
def __init__(self, test_mode=1, sal_mode='e'):
if test_mode == 1:
if sal_mode == 'h':
self.image_root = './dataset/'
self.image_source = './test_lytro.lst'
self.test_fold = './results/'
with open(self.image_source, 'r') as f:
self.image_list = [x.strip() for x in f.readlines()]
self.image_num = len(self.image_list)
def __getitem__(self, item):
image, name = load_image(os.path.join(self.image_root, self.image_list[item % self.image_num].split()[0]))
MV_90 = load_views_90_test(os.path.join(self.image_root, self.image_list[item%self.image_num].split()[1]))
MV_0 = load_views_0_test(os.path.join(self.image_root, self.image_list[item%self.image_num].split()[1]))
MV_45 = load_views_45_test(os.path.join(self.image_root, self.image_list[item%self.image_num].split()[1]))
MV_M45 = load_views_M45_test(os.path.join(self.image_root, self.image_list[item%self.image_num].split()[1]))
image = torch.Tensor(image)
MV_90 = torch.Tensor(MV_90)
MV_0 = torch.Tensor(MV_0)
MV_45 = torch.Tensor(MV_45)
MV_M45 = torch.Tensor(MV_M45)
return {'image': image,'MV_90': MV_90, 'MV_0': MV_0, 'MV_45': MV_45, 'MV_M45': MV_M45, 'name': name}
def save_folder(self):
return self.test_fold
def __len__(self):
return self.image_num
def get_loader(batch_size, mode='train', num_thread=1, test_mode=0, sal_mode='e'):
shuffle = False
if mode == 'test':
dataset = ImageDataTest(test_mode=test_mode, sal_mode=sal_mode)
data_loader = data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_thread)
return data_loader, dataset
def load_image(pah):
if not os.path.exists(pah):
print('File Not Exists:',pah)
#img_name = pah[52:-4]
img_name = pah[57:-4]
name = img_name + '.png'
print('name', name)
im = cv2.imread(pah)
im = cv2.resize(im,(540,375)) ###
in_ = np.array(im, dtype=np.float32)
in_ -= np.array((104.00699, 116.66877, 122.67892))
in_ = in_.transpose((2, 0, 1))
return in_, name
def load_views_90_test(pah):
if not os.path.exists(pah):
print('File Not Exists:',pah)
img_path = pah[:55]
img_name = pah[46:54]
print(img_path)
print(img_name)
#exit()
view_n = 7
slice_for_5x5 = int(0.5 * (7 - view_n))
seq90d = list(range(14, 77, 9)[::-1][slice_for_5x5:9 - slice_for_5x5:]) # 90degree: [76, 67, 58, 49, 40, 31, 22, 13, 4 ]
im_s = cv2.imread(pah)
im_s = cv2.resize(im_s,(540,375))
if(im_s.shape[0]==540):
image_array = np.zeros((7,540,375,3))
elif(im_s.shape[0]==375):
image_array = np.zeros((7,375,540,3))
for i in range(7):
img_all_path = img_path + img_name+ '_'+str(seq90d[i]) + '.png'
print(img_all_path)
im = cv2.imread(img_all_path)
im = cv2.resize(im,(540,375))
in_ = np.array(im, dtype=np.float32)
in_ -= np.array((104.00699, 116.66877, 122.67892))
image_array[i,:,:,:] = in_
image_array = image_array.transpose((3,0,1,2))
return image_array
def load_views_0_test(pah):
if not os.path.exists(pah):
print('File Not Exists')
img_path = pah[:55]
img_name = pah[46:54]
view_n = 7
slice_for_5x5 = int(0.5 * (7 - view_n))
seq0d = list(range(38, 45, 1)[slice_for_5x5:9 - slice_for_5x5:])
im_s = cv2.imread(pah)
im_s = cv2.resize(im_s,(540,375))
if(im_s.shape[0]==540):
image_array = np.zeros((7,540,375,3))
elif(im_s.shape[0]==375):
image_array = np.zeros((7,375,540,3))
for i in range(7):
img_all_path = img_path + img_name+ '_'+str(seq0d[i]) + '.png'
im = cv2.imread(img_all_path)
im = cv2.resize(im,(540,375))
in_ = np.array(im, dtype=np.float32)
in_ -= np.array((104.00699, 116.66877, 122.67892))
image_array[i,:,:,:] = in_
image_array = image_array.transpose((3,0,1,2))
return image_array
def load_views_45_test(pah):
if not os.path.exists(pah):
print('File Not Exists')
img_path = pah[:55]
img_name = pah[46:54]
view_n = 7
slice_for_5x5 = int(0.5 * (7 - view_n))
seq45d = list(range(17, 73, 8)[::-1][slice_for_5x5:9 - slice_for_5x5:])
im_s = cv2.imread(pah)
im_s = cv2.resize(im_s,(540,375))
if(im_s.shape[0]==540):
image_array = np.zeros((7,540,375,3))
elif(im_s.shape[0]==375):
image_array = np.zeros((7,375,540,3))
for i in range(7):
img_all_path = img_path + img_name+ '_'+str(seq45d[i]) + '.png'
im = cv2.imread(img_all_path)
im = cv2.resize(im,(540,375))
in_ = np.array(im, dtype=np.float32)
in_ -= np.array((104.00699, 116.66877, 122.67892))
image_array[i,:,:,:] = in_
image_array = image_array.transpose((3,0,1,2))
return image_array
def load_views_M45_test(pah):
if not os.path.exists(pah):
print('File Not Exists')
img_path = pah[:55]
img_name = pah[46:54]
view_n = 7
slice_for_5x5 = int(0.5 * (7 - view_n))
seqM45d = list(range(11, 81, 10)[slice_for_5x5:9 - slice_for_5x5:])
im_s = cv2.imread(pah)
im_s = cv2.resize(im_s,(540,375))
if(im_s.shape[0]==540):
image_array = np.zeros((7,540,375,3))
elif(im_s.shape[0]==375):
image_array = np.zeros((7,375,540,3))
for i in range(7):
img_all_path = img_path + img_name+ '_'+str(seqM45d[i]) + '.png'
im = cv2.imread(img_all_path)
im = cv2.resize(im,(540,375))
in_ = np.array(im, dtype=np.float32)
in_ -= np.array((104.00699, 116.66877, 122.67892))
image_array[i,:,:,:] = in_
image_array = image_array.transpose((3,0,1,2))
return image_array