-
Notifications
You must be signed in to change notification settings - Fork 11
/
data_depth.py
116 lines (98 loc) · 4.04 KB
/
data_depth.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
import os
from PIL import Image
import torch.utils.data as data
import torchvision.transforms as transforms
import torch
class SalObjDataset(data.Dataset):
def __init__(self, image_root, gt_root, trainsize):
self.trainsize = trainsize
self.depth = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg')
or f.endswith('.png')]
self.gts = [gt_root + f for f in os.listdir(gt_root) if f.endswith('.jpg')
or f.endswith('.png')]
self.depth = sorted(self.depth)
self.gts = sorted(self.gts)
self.filter_files()
self.size = len(self.depth)
self.depth_transform = transforms.Compose([
transforms.Resize((self.trainsize, self.trainsize)),
transforms.ToTensor()])
self.gt_transform = transforms.Compose([
transforms.Resize((self.trainsize, self.trainsize)),
transforms.ToTensor()])
def __getitem__(self, index):
depth = self.binary_loader(self.depth[index])
gt = self.binary_loader(self.gts[index])
depth = self.depth_transform(depth)
depth = torch.div(depth.float(),255.0)
gt = self.gt_transform(gt)
return depth, gt
def filter_files(self):
assert len(self.depth) == len(self.gts)
depth = []
gts = []
for depth_path, gt_path in zip(self.depth, self.gts):
dep = Image.open(depth_path)
gt = Image.open(gt_path)
# if dep.size == gt.size:
depth.append(depth_path)
gts.append(gt_path)
self.depth = depth
self.gts = gts
def rgb_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def binary_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('L')
def resize(self, img, gt):
assert img.size == gt.size
w, h = img.size
if h < self.trainsize or w < self.trainsize:
h = max(h, self.trainsize)
w = max(w, self.trainsize)
return img.resize((w, h), Image.BILINEAR), gt.resize((w, h), Image.NEAREST)
else:
return img, gt
def __len__(self):
return self.size
def get_loader(image_root, gt_root, batchsize, trainsize, shuffle=True, pin_memory=True):
dataset = SalObjDataset(image_root, gt_root, trainsize)
data_loader = data.DataLoader(dataset=dataset,
batch_size=batchsize,
shuffle=shuffle,
pin_memory=pin_memory)
return data_loader
class test_dataset:
def __init__(self, image_root, gt_root, testsize):
self.testsize = testsize
self.images = [image_root + f for f in os.listdir(image_root) if f.endswith('.jpg') or f.endswith('.png')]
self.gts = [gt_root + f for f in os.listdir(gt_root) if f.endswith('.jpg')
or f.endswith('.png')]
self.images = sorted(self.images)
self.gts = sorted(self.gts)
self.depth_transform = transforms.Compose([transforms.Resize((self.testsize, self.testsize)),transforms.ToTensor()])
self.gt_transform = transforms.ToTensor()
self.size = len(self.images)
self.index = 0
def load_data(self):
image = self.binary_loader(self.images[self.index])
image = self.depth_transform(image).unsqueeze(0)
image = torch.div(image.float(),255.0)
gt = self.binary_loader(self.gts[self.index])
gt = self.gt_transform(gt)
name = self.images[self.index].split('\\')[-1]
if name.endswith('.jpg'):
name = name.split('.jpg')[0] + '.png'
self.index += 1
return image, gt, name
def rgb_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def binary_loader(self, path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('L')