-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataset.py
178 lines (140 loc) · 6.04 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
from PIL import Image
import torch
from torch.utils import data
import transforms as trans
from torchvision import transforms
import random
from parameter import *
def load_list(file):
with open(file) as f:
lines = f.read().splitlines()
files = []
depths = []
labels = []
for line in lines:
files.append(line.split(' ')[0])
depths.append(line.split(' ')[1])
labels.append(line.split(' ')[2])
return files, depths, labels
def load_test_list(file):
with open(file) as f:
lines = f.read().splitlines()
files = []
depths = []
for line in lines:
files.append(line.split(' ')[0])
depths.append(line.split(' ')[1])
return files, depths
class ImageData(data.Dataset):
def __init__(self, img_root, transform, depth_transform, t_transform, label_32_transform, label_64_transform, label_128_transform, mode):
if mode == 'train':
self.image_path, self.depth_path, self.label_path = load_list(img_root)
else:
self.image_path, self.depth_path = load_test_list(img_root)
self.transform = transform
self.depth_transform = depth_transform
self.t_transform = t_transform
self.label_32_transform = label_32_transform
self.label_64_transform = label_64_transform
self.label_128_transform = label_128_transform
self.mode = mode
def __getitem__(self, item):
fn = self.image_path[item].split('/')
filename = fn[-1]
image = Image.open(self.image_path[item]).convert('RGB')
image_w, image_h = int(image.size[0]), int(image.size[1])
depth = Image.open(self.depth_path[item]).convert('L')
# data augmentation
if self.mode == 'train':
label = Image.open(self.label_path[item]).convert('L')
random_size = scale_size
new_img = trans.Scale((random_size, random_size))(image)
new_depth = trans.Scale((random_size, random_size))(depth)
new_label = trans.Scale((random_size, random_size), interpolation=Image.NEAREST)(label)
# random crop
w, h = new_img.size
if w != img_size and h != img_size:
x1 = random.randint(0, w - img_size)
y1 = random.randint(0, h - img_size)
new_img = new_img.crop((x1, y1, x1 + img_size, y1 + img_size))
new_depth = new_depth.crop((x1, y1, x1 + img_size, y1 + img_size))
new_label = new_label.crop((x1, y1, x1 + img_size, y1 + img_size))
# random flip
if random.random() < 0.5:
new_img = new_img.transpose(Image.FLIP_LEFT_RIGHT)
new_depth = new_depth.transpose(Image.FLIP_LEFT_RIGHT)
new_label = new_label.transpose(Image.FLIP_LEFT_RIGHT)
new_img = self.transform(new_img)
new_depth = self.depth_transform(new_depth)
new_depth = new_depth.expand(3, img_size, img_size)
label_256 = self.t_transform(new_label)
if self.label_32_transform is not None and self.label_64_transform is not None and self.label_128_transform is\
not None:
label_32 = self.label_32_transform(new_label)
label_64 = self.label_64_transform(new_label)
label_128 = self.label_128_transform(new_label)
return new_img, new_depth, label_256, label_32, label_64, label_128, filename
else:
image = self.transform(image)
depth = self.depth_transform(depth)
depth = depth.expand(3, img_size, img_size)
return image, depth, image_w, image_h, self.image_path[item]
def __len__(self):
return len(self.image_path)
def get_loader(img_root, img_size, batch_size, mode='train', num_thread=1):
shuffle = False
mean_bgr = torch.Tensor(3, 256, 256)
mean_bgr[0, :, :] = 104.008 # B
mean_bgr[1, :, :] = 116.669 # G
mean_bgr[2, :, :] = 122.675 # R
depth_mean_bgr = torch.Tensor(1, 256, 256)
depth_mean_bgr[0, :, :] = 115.8695
if mode == 'train':
transform = trans.Compose([
# trans.ToTensor image -> [0,255]
trans.ToTensor_BGR(),
trans.Lambda(lambda x: x - mean_bgr)
])
depth_transform = trans.Compose([
# trans.ToTensor image -> [0,255]
trans.ToTensor(),
trans.Lambda(lambda x: x - depth_mean_bgr)
])
t_transform = trans.Compose([
# transform.ToTensor label -> [0,1]
transforms.ToTensor(),
])
label_32_transform = trans.Compose([
trans.Scale((32, 32), interpolation=Image.NEAREST),
transforms.ToTensor(),
])
label_64_transform = trans.Compose([
trans.Scale((64, 64), interpolation=Image.NEAREST),
transforms.ToTensor(),
])
label_128_transform = trans.Compose([
trans.Scale((128, 128), interpolation=Image.NEAREST),
transforms.ToTensor(),
])
shuffle = True
else:
transform = trans.Compose([
trans.Scale((img_size, img_size)),
trans.ToTensor_BGR(),
trans.Lambda(lambda x: x - mean_bgr)
])
depth_transform = trans.Compose([
trans.Scale((img_size, img_size)),
trans.ToTensor(),
trans.Lambda(lambda x: x - depth_mean_bgr)
])
t_transform = trans.Compose([
trans.Scale((img_size, img_size), interpolation=Image.NEAREST),
transforms.ToTensor(),
])
if mode == 'train':
dataset = ImageData(img_root, transform, depth_transform, t_transform, label_32_transform, label_64_transform, label_128_transform, mode)
else:
dataset = ImageData(img_root, transform, depth_transform, t_transform, label_32_transform=None, label_64_transform=None, label_128_transform=None, mode=mode)
data_loader = data.DataLoader(dataset=dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_thread)
return data_loader