-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge_dataloader.py
171 lines (145 loc) · 5.61 KB
/
edge_dataloader.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
from torch.utils import data
import torchvision.transforms as transforms
import os
from pathlib import Path
from PIL import Image
import numpy as np
class Dataloader_BSDS500(data.Dataset):
"""
Dataloader BSDS500
"""
def __init__(self, root='data/HED-BSDS', split='train', transform=False, threshold=0.3, ablation=False):
self.root = root
self.split = split
self.threshold = threshold * 256
print('Threshold for ground truth: %f on BSDS' % self.threshold)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
self.transform = transforms.Compose([
transforms.ToTensor(),
normalize])
if self.split == 'train':
if ablation:
self.filelist = os.path.join(self.root, 'train200_pair.lst')
else:
self.filelist = os.path.join(self.root, 'train_pair.lst')
elif self.split == 'test':
if ablation:
self.filelist = os.path.join(self.root, 'val.lst')
else:
self.filelist = os.path.join(self.root, 'test.lst')
else:
raise ValueError("Invalid split type!")
with open(self.filelist, 'r') as f:
self.filelist = f.readlines()
def __len__(self):
return len(self.filelist)
def __getitem__(self, index):
if self.split == "train":
img_file, lb_file = self.filelist[index].split()
img_file = img_file.strip()
lb_file = lb_file.strip()
lb = np.array(Image.open(os.path.join(self.root, lb_file)), dtype=np.float32)
if lb.ndim == 3:
lb = np.squeeze(lb[:, :, 0])
assert lb.ndim == 2
threshold = self.threshold
lb = lb[np.newaxis, :, :]
lb[lb == 0] = 0
lb[np.logical_and(lb>0, lb<threshold)] = 2
lb[lb >= threshold] = 1
else:
img_file = self.filelist[index].rstrip()
with open(os.path.join(self.root, img_file), 'rb') as f:
img = Image.open(f)
img = img.convert('RGB')
img = self.transform(img)
if self.split == "train":
return img, lb
else:
img_name = Path(img_file).stem
return img, img_name
class BSDS_VOCLoader(data.Dataset):
"""
Dataloader BSDS500
"""
def __init__(self, root='data/HED-BSDS_PASCAL', split='train', transform=False, threshold=0.3, ablation=False):
self.root = root
self.split = split
self.threshold = threshold * 256
print('Threshold for ground truth: %f on BSDS_VOC' % self.threshold)
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
self.transform = transforms.Compose([
transforms.ToTensor(),
normalize])
if self.split == 'train':
if ablation:
self.filelist = os.path.join(self.root, 'bsds_pascal_train200_pair.lst')
else:
self.filelist = os.path.join(self.root, 'bsds_pascal_train_pair.lst')
elif self.split == 'test':
if ablation:
self.filelist = os.path.join(self.root, 'val.lst')
else:
self.filelist = os.path.join(self.root, 'test.lst')
else:
raise ValueError("Invalid split type!")
with open(self.filelist, 'r') as f:
self.filelist = f.readlines()
def __len__(self):
return len(self.filelist)
def __getitem__(self, index):
if self.split == "train":
img_file, lb_file = self.filelist[index].split()
img_file = img_file.strip()
lb_file = lb_file.strip()
lb = np.array(Image.open(os.path.join(self.root, lb_file)), dtype=np.float32)
if lb.ndim == 3:
lb = np.squeeze(lb[:, :, 0])
assert lb.ndim == 2
threshold = self.threshold
lb = lb[np.newaxis, :, :]
lb[lb == 0] = 0
lb[np.logical_and(lb>0, lb<threshold)] = 2
lb[lb >= threshold] = 1
else:
img_file = self.filelist[index].rstrip()
with open(os.path.join(self.root, img_file), 'rb') as f:
img = Image.open(f)
img = img.convert('RGB')
img = self.transform(img)
if self.split == "train":
return img, lb
else:
img_name = Path(img_file).stem
return img, img_name
class Custom_Loader(data.Dataset):
"""
Custom Dataloader
"""
def __init__(self, root='data/'):
self.root = root
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
self.transform = transforms.Compose([
transforms.ToTensor(),
normalize])
self.imgList = fold_files(os.path.join(root))
def __len__(self):
return len(self.imgList)
def __getitem__(self, index):
with open(os.path.join(self.root, self.imgList[index]), 'rb') as f:
img = Image.open(f)
img = img.convert('RGB')
img = self.transform(img)
filename = Path(self.imgList[index]).stem
return img, filename
def fold_files(foldname):
"""All files in the fold should have the same extern"""
allfiles = os.listdir(foldname)
if len(allfiles) < 1:
raise ValueError('No images in the data folder')
return None
else:
return allfiles