-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
executable file
·227 lines (179 loc) · 8.35 KB
/
datasets.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import torch
import os
import numpy as np
import cv2
import PIL
from skimage import io
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
# SOURCE DATALOADER
def get_source(train=True):
if train == True:
fil = []
for files in os.walk('./data/deep_pore_90_labels'):
fil.append(files[2])
file = []
for name in fil:
for i in name:
i = str(i)
file.append([i, i.split('.')[0]])
names = []
lbl = []
for f in file:
names.append(f[0])
lbl.append(f[1])
class imgdata(torch.utils.data.Dataset):
def __init__(self, data_dir, label_dir, img_name, istrain,
transform, labels=None):
self.data_dir = data_dir
self.label_dir = label_dir
self.img_name = img_name
self.istrain = istrain
self.transform = transform
self.labels = labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
patchimg = os.path.join(self.data_dir, self.img_name[idx])
labelimg = os.path.join(self.label_dir, self.img_name[idx])
pi = cv2.imread(patchimg)
li = cv2.imread(labelimg)
pi = PIL.Image.fromarray(pi)
li = PIL.Image.fromarray(li)
label = torch.from_numpy(np.array(int(self.img_name[idx].split('.')[0])))
if self.istrain:
return self.transform(pi), label, self.transform(li)
data_transform = transforms.Compose([
transforms.Grayscale(),
# transforms.Normalize((.5,),(.5,)),
transforms.ToTensor()
])
dataset = imgdata('./data/deep_pore_90_patch', './data/deep_pore_90_labels',
img_name=names, istrain=True, transform=data_transform,
labels=lbl)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True, num_workers=0)
return dataloader
elif train == False:
fil = []
for files in os.walk('./data/source_test_images/PoreGroundTruthSampleimage'):
fil.append(files[2])
file = []
for name in fil:
for i in name:
i = str(i)
file.append([i, i.split('.')[0]])
names = []
lbl = []
for f in file:
names.append(f[0])
lbl.append(f[1])
class imgdata(torch.utils.data.Dataset):
def __init__(self, data_dir, label_dir, img_name, istrain,
transform, labels=None):
self.data_dir = data_dir
self.label_dir = label_dir
self.img_name = img_name
self.istrain = istrain
self.transform = transform
self.labels = labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
patchimg = os.path.join(self.data_dir, self.img_name[idx])
labelimg = os.path.join(self.label_dir, self.img_name[idx])
pi = cv2.imread(patchimg)
li = cv2.imread(labelimg)
pi = PIL.Image.fromarray(pi)
li = PIL.Image.fromarray(li)
label = torch.from_numpy(np.array(int(self.img_name[idx].split('.')[0])))
if self.istrain:
return self.transform(pi), label, self.transform(li)
data_transform = transforms.Compose([
transforms.Grayscale(),
# transforms.Normalize((.5,),(.5,)),
transforms.ToTensor()
])
dataset = imgdata('./data/source_test_images/PoreGroundTruthSampleimage',
'./data/source_test_images/pore_GT_images_label',
img_name=names, istrain=True, transform=data_transform,
labels=lbl)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True, num_workers=0)
return dataloader
# TARGET DATALOADER
def get_target(train=True):
if train == True:
fil = []
for files in os.walk('./data/target_images/train-aug'):
fil.append(files[2])
file = []
for name in fil:
for i in name:
i = str(i)
file.append([i, i.split('.')[0]])
names = []
lbl = []
for f in file:
names.append(f[0])
lbl.append(f[1])
class imgdata(Dataset):
def __init__(self, data_dir, img_name, istrain, transform, labels=None):
self.data_dir = data_dir
self.img_name = img_name
self.istrain = istrain
self.transform = transform
self.labels = labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
patchimg = os.path.join(self.data_dir, self.img_name[idx])
pi = cv2.imread(patchimg)
label = torch.from_numpy(np.array(int(self.img_name[idx].split('.')[0])))
if self.istrain:
return self.transform(pi), label
data_transform = transforms.Compose([
# transforms.Normalize((.5,),(.5,)),
transforms.ToTensor()
])
dataset = imgdata('./data/target_images/train-aug',
img_name=names, istrain=True, transform=data_transform,
labels=lbl)
dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
return dataloader
elif train == False:
fil = []
for files in os.walk('./data/target_images/test-aug'):
fil.append(files[2])
file = []
for name in fil:
for i in name:
i = str(i)
file.append([i, i.split('.')[0]])
names = []
lbl = []
for f in file:
names.append(f[0])
lbl.append(f[1])
class imgdata(Dataset):
def __init__(self, data_dir, img_name, istrain, transform, labels=None):
self.data_dir = data_dir
self.img_name = img_name
self.istrain = istrain
self.transform = transform
self.labels = labels
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
patchimg = os.path.join(self.data_dir, self.img_name[idx])
pi = cv2.imread(patchimg)
label = torch.from_numpy(np.array(int(self.img_name[idx].split('.')[0])))
if self.istrain:
return self.transform(pi), label
data_transform = transforms.Compose([
# transforms.Normalize((.5,),(.5,)),
transforms.ToTensor()
])
dataset = imgdata('./data/target_images/test-aug',
img_name=names, istrain=True, transform=data_transform,
labels=lbl)
dataloader = DataLoader(dataset, batch_size=1, shuffle=True)
return dataloader