-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataloader.py
More file actions
executable file
·238 lines (173 loc) · 7.63 KB
/
dataloader.py
File metadata and controls
executable file
·238 lines (173 loc) · 7.63 KB
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
228
229
230
231
232
233
234
235
236
237
238
# load all into cpu
# do cropping to patch size
# restrict number of slices (see null slices)
# normalize range
# test code by outputting few patches
# training, testing, val
import torch
import os, glob, sys
import numpy as np
from PIL import Image
from os.path import join as pjoin
from torchvision import transforms
from torch.utils import data
from skimage import io
class CREMI(data.Dataset):
def __init__(self, listpath, filepaths, is_training=False):
self.listpath = listpath
self.imgfile = filepaths[0]
self.gtfile = filepaths[1]
self.dataCPU = {}
self.dataCPU['image'] = []
self.dataCPU['label'] = []
self.indices = []
self.crop_size = 128
self.is_training = is_training
self.loadCPU()
def loadCPU(self):
img = io.imread(self.imgfile)
gt = io.imread(self.gtfile)
_, h,w = np.shape(img)
img_tiff = np.zeros((h,w))
gt_tiff = np.zeros((h,w))
img_tiff = np.array(img)
gt_tiff = 1 - np.array(gt)/255
img = torch.tensor(img_tiff)
gt = torch.tensor(gt_tiff)
with open(self.listpath, 'r') as f:
mylist = f.readlines()
mylist = [x.rstrip('\n') for x in mylist]
for i, entry in enumerate(mylist):
meanval = torch.Tensor.float(img[int(entry)]).mean()
stdval = torch.Tensor.float(img[int(entry)]).std()
self.indices.append((i))
#cpu store
self.dataCPU['image'].append((img[int(entry)] - meanval) / stdval)
self.dataCPU['label'].append(gt[int(entry)])
def __len__(self): # total number of 2D slices
return len(self.indices)
def __getitem__(self, index): # return CHW torch tensor
index = self.indices[index]
torch_img = self.dataCPU['image'][index] #HW
torch_gt = self.dataCPU['label'][index] #HW
if self.is_training is True:
# crop to patchsize. compute top-left corner first
H, W = torch_img.shape
corner_h = np.random.randint(low=0, high=H-self.crop_size)
corner_w = np.random.randint(low=0, high=W-self.crop_size)
torch_img = torch_img[corner_h:corner_h+self.crop_size, corner_w:corner_w+self.crop_size]
torch_gt = torch_gt[corner_h:corner_h+self.crop_size, corner_w:corner_w+self.crop_size]
# else:
# torch_img = torch_img[0:1248, 0:1248]
# torch_gt = torch_gt[0:1248, 0:1248]
torch_img = torch.unsqueeze(torch_img,dim=0).repeat(1,1,1)
torch_gt = torch.unsqueeze(torch_gt,dim=0)
return torch_img, torch_gt
class ISBI2013(data.Dataset):
def __init__(self, listpath, filepaths, is_training=False):
self.listpath = listpath
self.imgfile = filepaths[0]
self.gtfile = filepaths[1]
self.dataCPU = {}
self.dataCPU['image'] = []
self.dataCPU['label'] = []
self.indices = []
self.crop_size = 128
self.is_training = is_training
self.loadCPU()
def loadCPU(self):
img = io.imread(self.imgfile)
gt = io.imread(self.gtfile)
_, h,w = np.shape(img)
img_tiff = np.zeros((h,w))
gt_tiff = np.zeros((h,w))
img_tiff = np.array(img)
gt_tiff = 1 - np.array(gt)/255
img = torch.tensor(img_tiff)
gt = torch.tensor(gt_tiff)
with open(self.listpath, 'r') as f:
mylist = f.readlines()
mylist = [x.rstrip('\n') for x in mylist]
for i, entry in enumerate(mylist):
meanval = torch.Tensor.float(img[int(entry)]).mean()
stdval = torch.Tensor.float(img[int(entry)]).std()
self.indices.append((i))
#cpu store
self.dataCPU['image'].append((img[int(entry)] - meanval) / stdval)
self.dataCPU['label'].append(gt[int(entry)])
def __len__(self): # total number of 2D slices
return len(self.indices)
def __getitem__(self, index): # return CHW torch tensor
index = self.indices[index]
torch_img = self.dataCPU['image'][index] #HW
torch_gt = self.dataCPU['label'][index] #HW
if self.is_training is True:
# crop to patchsize. compute top-left corner first
H, W = torch_img.shape
corner_h = np.random.randint(low=0, high=H-self.crop_size)
corner_w = np.random.randint(low=0, high=W-self.crop_size)
torch_img = torch_img[corner_h:corner_h+self.crop_size, corner_w:corner_w+self.crop_size]
torch_gt = torch_gt[corner_h:corner_h+self.crop_size, corner_w:corner_w+self.crop_size]
torch_img = torch.unsqueeze(torch_img,dim=0).repeat(1,1,1)
torch_gt = torch.unsqueeze(torch_gt,dim=0)
return torch_img, torch_gt
class DRIVE(data.Dataset):
def __init__(self, listpath, folderpaths, is_training=False):
self.listpath = listpath
self.imgfolder = folderpaths[0]
self.gtfolder = folderpaths[1]
self.dataCPU = {}
self.dataCPU['image'] = []
self.dataCPU['label'] = []
self.indices = []
self.to_tensor = transforms.ToTensor()
self.crop_size = 128
self.is_training = is_training
self.loadCPU()
def loadCPU(self):
with open(self.listpath, 'r') as f:
mylist = f.readlines()
mylist = [x.rstrip('\n') for x in mylist]
for i, entry in enumerate(mylist):
components = entry.split('.')
filename = components[0]
im_path = pjoin(self.imgfolder, filename) + '.tif'
gt_path = pjoin(self.gtfolder, filename) + '_manual1.gif'
img = Image.open(im_path)
gt = Image.open(gt_path)
img = self.to_tensor(img)
gt = self.to_tensor(gt)
#normalize within a channel
for j in range(img.shape[0]):
meanval = img[j].mean()
stdval = img[j].std()
img[j] = (img[j] - meanval) / stdval
self.indices.append((i))
#cpu store
self.dataCPU['image'].append(img)
self.dataCPU['label'].append(gt)
def __len__(self): # total number of 2D slices
return len(self.indices)
def __getitem__(self, index): # return CHW torch tensor
index = self.indices[index]
torch_img = self.dataCPU['image'][index] #HW
torch_gt = self.dataCPU['label'][index] #HW
if self.is_training is True:
# crop to patchsize. compute top-left corner first
C, H, W = torch_img.shape
corner_h = np.random.randint(low=0, high=H-self.crop_size)
corner_w = np.random.randint(low=0, high=W-self.crop_size)
torch_img = torch_img[:, corner_h:corner_h+self.crop_size, corner_w:corner_w+self.crop_size]
torch_gt = torch_gt[:, corner_h:corner_h+self.crop_size, corner_w:corner_w+self.crop_size]
return torch_img, torch_gt
if __name__ == "__main__":
flag = "training"
dst = CREMI('data-lists/CREMI/validation-list.csv', ['data/CREMI/train-volume.tif', 'data/CREMI/train-labels.tif'])
# dst = ISBI2013('data-lists/ISBI2013/train-list.csv', ['data/ISBI2013/train-volume.tif', 'data/ISBI2013/train-labels.tif'], is_training= True)
# dst = DRIVE('data-lists/DRIVE/train-list.csv', ['data/DRIVE/images', 'data/DRIVE/1st_manual'], is_training= True)
validationloader = data.DataLoader(dst, shuffle=False, batch_size=1, num_workers=1)
## dataloader check
# import pdb; pdb.set_trace()
batch = next(iter(validationloader))
input, target = batch
# import pdb; pdb.set_trace()