-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
executable file
·171 lines (148 loc) · 10.5 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
import os
import os.path
import cv2
import numpy as np
from torch.utils.data import Dataset
IMG_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm']
def load_image_label_list_from_npy(img_name_list, data_root):
cls_labels_dict = np.load('{}/cls_labels.npy'.format(data_root), allow_pickle=True).item()
return [cls_labels_dict[img_name] for img_name in img_name_list]
class ScribblePseudoDsDcData(Dataset):
def __init__(self, split='train', data_root=None, data_list=None, transform=None , path = 'pascal_2012_scribble',path_pesudo = 'bmp_ws_train_aug_dataset',path_distancemaps='distancemap_s_lambd_1',path_distancemapc='distancemap_c_lambd_e'):
self.split = split
self.indices = open('{}/ImageSets/SegmentationAug/{}'.format(data_root, data_list),'r').read().splitlines()
self.img_names = ['{}/JPEGImages/{}.jpg'.format(data_root, i) for i in self.indices]
self.lab_names = ['{}/{}/{}.png'.format(data_root, path, i) for i in self.indices]
self.pseudo_names = ['{}/{}/{}.png'.format(data_root, path_pesudo, i) for i in self.indices]
self.ds_names = ['{}/{}/{}.png'.format(data_root, path_distancemaps, i) for i in self.indices]
self.dc_names = ['{}/{}/{}.png'.format(data_root, path_distancemapc, i) for i in self.indices]
self.transform = transform
def __len__(self):
return len(self.img_names)
def __getitem__(self, index):
#image_path, label_path = self.data_list[index]
image_path=self.img_names[index]
label_path=self.lab_names[index]
label_pesudo_path = self.pseudo_names[index]
ds_path = self.ds_names[index] # distance map scribble path
dc_path = self.dc_names[index]# distance map cam path
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # BGR 3 channel ndarray wiht shape H * W * 3
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert cv2 read image from BGR order to RGB order
# image = np.float32(image)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) # GRAY 1 channel ndarray with shape H * W
label_pesudo = cv2.imread(label_pesudo_path,cv2.IMREAD_GRAYSCALE)
distancemap_s = cv2.imread(ds_path,cv2.IMREAD_GRAYSCALE)
distancemap_c = cv2.imread(dc_path,cv2.IMREAD_GRAYSCALE)
if image.shape[0] != label.shape[0] or image.shape[1] != label.shape[1] or image.shape[1] != label_pesudo.shape[1] or image.shape[1] != distancemap_s.shape[1] or image.shape[1] != distancemap_c.shape[1]:
raise (RuntimeError("Image & label shape mismatch: " + image_path + " " + label_path + "\n"))
if self.transform is not None:
image, label,label_pesudo,distancemap_s,distancemap_c = self.transform(image, label, label_pesudo,distancemap_s,distancemap_c )
distancemap_s = distancemap_s.float()
distancemap_c = distancemap_c.float()
return image, label,label_pesudo,distancemap_s,distancemap_c, image_path
class ScribblePseudoDsData(Dataset):
def __init__(self, split='train', data_root=None, data_list=None, transform=None , path = 'pascal_2012_scribble',path_pesudo = 'bmp_ws_train_aug_dataset',path_distancemaps='distancemap_s_lambd_1'):
self.split = split
self.indices = open('{}/ImageSets/SegmentationAug/{}'.format(data_root, data_list),'r').read().splitlines()
self.img_names = ['{}/JPEGImages/{}.jpg'.format(data_root, i) for i in self.indices]
self.lab_names = ['{}/{}/{}.png'.format(data_root, path, i) for i in self.indices]
self.pseudo_names = ['{}/{}/{}.png'.format(data_root, path_pesudo, i) for i in self.indices]
self.ds_names = ['{}/{}/{}.png'.format(data_root, path_distancemaps, i) for i in self.indices]
self.transform = transform
def __len__(self):
return len(self.img_names)
def __getitem__(self, index):
#image_path, label_path = self.data_list[index]
image_path=self.img_names[index]
label_path=self.lab_names[index]
label_pesudo_path = self.pseudo_names[index]
ds_path = self.ds_names[index] # distance map scribble path
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # BGR 3 channel ndarray wiht shape H * W * 3
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert cv2 read image from BGR order to RGB order
# image = np.float32(image)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) # GRAY 1 channel ndarray with shape H * W
label_pesudo = cv2.imread(label_pesudo_path,cv2.IMREAD_GRAYSCALE)
distancemap_s = cv2.imread(ds_path,cv2.IMREAD_GRAYSCALE)
if image.shape[0] != label.shape[0] or image.shape[1] != label.shape[1] or image.shape[1] != label_pesudo.shape[1] or image.shape[1] != distancemap_s.shape[1]:
raise (RuntimeError("Image & label shape mismatch: " + image_path + " " + label_path + "\n"))
if self.transform is not None:
image, label,label_pesudo,distancemap_s,_ = self.transform(image, label, label_pesudo,distancemap_s,distancemap_s )
distancemap_s = distancemap_s.float()
return image, label,label_pesudo,distancemap_s, image_path
class ScribblePseudoDcData(Dataset):
def __init__(self, split='train', data_root=None, data_list=None, transform=None , path = 'pascal_2012_scribble',path_pesudo = 'bmp_ws_train_aug_dataset',path_distancemapc='distancemap_c_lambd_1'):
self.split = split
self.indices = open('{}/ImageSets/SegmentationAug/{}'.format(data_root, data_list),'r').read().splitlines()
self.img_names = ['{}/JPEGImages/{}.jpg'.format(data_root, i) for i in self.indices]
self.lab_names = ['{}/{}/{}.png'.format(data_root, path, i) for i in self.indices]
self.pseudo_names = ['{}/{}/{}.png'.format(data_root, path_pesudo, i) for i in self.indices]
self.dc_names = ['{}/{}/{}.png'.format(data_root, path_distancemapc, i) for i in self.indices]
self.transform = transform
def __len__(self):
return len(self.img_names)
def __getitem__(self, index):
#image_path, label_path = self.data_list[index]
image_path=self.img_names[index]
label_path=self.lab_names[index]
label_pesudo_path = self.pseudo_names[index]
dc_path = self.dc_names[index] # distance map scribble path
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # BGR 3 channel ndarray wiht shape H * W * 3
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert cv2 read image from BGR order to RGB order
# image = np.float32(image)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) # GRAY 1 channel ndarray with shape H * W
label_pesudo = cv2.imread(label_pesudo_path,cv2.IMREAD_GRAYSCALE)
distancemap_c = cv2.imread(dc_path,cv2.IMREAD_GRAYSCALE)
if image.shape[0] != label.shape[0] or image.shape[1] != label.shape[1] or image.shape[1] != label_pesudo.shape[1] or image.shape[1] != distancemap_c.shape[1]:
raise (RuntimeError("Image & label shape mismatch: " + image_path + " " + label_path + "\n"))
if self.transform is not None:
image, label,label_pesudo,distancemap_c,_ = self.transform(image, label, label_pesudo,distancemap_c,distancemap_c )
distancemap_c = distancemap_c.float()
return image, label,label_pesudo,distancemap_c, image_path
class ScribblePseudoData(Dataset):
def __init__(self, split='train', data_root=None, data_list=None, transform=None , path = 'pascal_2012_scribble',path_pesudo = 'bmp_ws_train_aug_dataset'):
self.split = split
self.indices = open('{}/ImageSets/SegmentationAug/{}'.format(data_root, data_list),'r').read().splitlines()
self.img_names = ['{}/JPEGImages/{}.jpg'.format(data_root, i) for i in self.indices]
self.lab_names = ['{}/{}/{}.png'.format(data_root, path, i) for i in self.indices]
self.pseudo_names = ['{}/{}/{}.png'.format(data_root, path_pesudo, i) for i in self.indices]
self.transform = transform
def __len__(self):
return len(self.img_names)
def __getitem__(self, index):
#image_path, label_path = self.data_list[index]
image_path=self.img_names[index]
label_path=self.lab_names[index]
label_pesudo_path = self.pseudo_names[index]
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # BGR 3 channel ndarray wiht shape H * W * 3
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert cv2 read image from BGR order to RGB order
# image = np.float32(image)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) # GRAY 1 channel ndarray with shape H * W
label_pesudo = cv2.imread(label_pesudo_path,cv2.IMREAD_GRAYSCALE)
if image.shape[0] != label.shape[0] or image.shape[1] != label.shape[1] or image.shape[1] != label_pesudo.shape[1]:
raise (RuntimeError("Image & label shape mismatch: " + image_path + " " + label_path + "\n"))
if self.transform is not None:
image, label,label_pesudo,_,_ = self.transform(image, label, label_pesudo,label_pesudo,label_pesudo )
distancemap_c = distancemap_c.float()
return image, label,label_pesudo, image_path
class SingleLabelData(Dataset):
def __init__(self, split='train', data_root=None, data_list=None, transform=None , path = 'SegmentationClassAug'):
self.split = split
self.indices = open('{}/ImageSets/SegmentationAug/{}'.format(data_root, data_list),'r').read().splitlines()
self.img_names = ['{}/JPEGImages/{}.jpg'.format(data_root, i) for i in self.indices]
self.lab_names = ['{}/{}/{}.png'.format(data_root, path, i) for i in self.indices]
self.transform = transform # see transform.py
def __len__(self):
return len(self.img_names)
def __getitem__(self, index):
#image_path, label_path = self.data_list[index]
image_path=self.img_names[index]
label_path=self.lab_names[index]
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # BGR 3 channel ndarray wiht shape H * W * 3
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert cv2 read image from BGR order to RGB order
# image = np.float32(image)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) # GRAY 1 channel ndarray with shape H * W
if image.shape[0] != label.shape[0] or image.shape[1] != label.shape[1]:
raise (RuntimeError("Image & label shape mismatch: " + image_path + " " + label_path + "\n"))
if self.transform is not None:
image, label = self.transform(image, label)
return image, label, image_path