-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset.py
245 lines (200 loc) · 9.2 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
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
239
240
241
242
243
244
245
import numpy as np
import torch
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from PIL import Image
import json
from label_conversion import LabelConverter
from dice_loss import label_accuracy
from shuffle_puzzle import Puzzle_RandomShuffle
from visualization import *
from albumentations import *
from albumentations.pytorch import *
from customized_transform import *
class MICCAIDataset(Dataset):
def __init__(self, data_path="../data/", data_type = "train", version = "_min", transform_both=None, transform_image=None, transform_per_class=None):
#store some input
self.data_path = str(data_path)
self.data_type = str(data_type)
self.filename = data_path+"index/"+data_type+"_data"+version+".txt"
self.transform_both = transform_both
self.transform_image = transform_image
self.transform_per_class = transform_per_class
self.data = []
#parse the txt to store the necessary information of output
file = open(self.filename, 'r').readlines()
file = file[1:]
for i in range(len(file)):
file[i] = file[i].split(",")
entry = {}
entry["seq"] = file[i][0].strip()
entry["frame"] = file[i][1].strip().zfill(3)
self.data.append(entry)
# save label conversion object
self.label_converter = LabelConverter(data_path)
def __len__(self):
#return the length of the data numbers
return len(self.data)
def __getitem__(self, idx):
if self.data_type == "train" or self.data_type == "validattion":
prefix = "images/seq_"
label_path = self.data_path+prefix+self.data[idx]["seq"]+"/labels/frame"+self.data[idx]["frame"]+".png"
#parse label color to label number and resize it to 320x256
label = Image.open(label_path)
label = label.resize((320, 256))
label = np.array(label, dtype='int32')
label_indx = (label[:, :, 0] * 256 + label[:, :, 1]) * 256 + label[:, :, 2]
label = self.label_converter.color2label(label_indx)
else:
prefix = "test/seq_"
img_path = self.data_path+prefix+self.data[idx]["seq"]+"/left_frames/frame"+self.data[idx]["frame"]+".png"
#get img from file and resize it to 320x256 which is what we want
img = Image.open(img_path)
img = img.resize((320, 256))
img = np.array(img)
# augment dataset
if self.transform_both is not None:
augmented = self.transform_both(image=img,mask=label)
img = augmented['image']
label = augmented['mask']
if self.transform_per_class is not None:
print('per class')
augmented = self.transform_per_class(image=img,label=label)
img = augmented['image']
if self.transform_image is not None:
augmented = self.transform_image(image=img)
img = augmented['image']
img = torch.from_numpy(img).permute(2, 0, 1).float()
if self.data_type == "train" or self.data_type == "validattion":
label = torch.from_numpy(label).reshape([1,label.shape[0],label.shape[1]])
sample = {'img':img,'label':label,'indx':idx}
else:
sample = {'img':img,'idx':idx}
return sample
class Transformation_PretrainDataset(MICCAIDataset):
def __init__(self, data_path="../data/", data_type = "train", transform=None):
#store some input
super(Transformation_PretrainDataset, self).__init__(data_path, data_type, transform)
self.randomShiftScaleRotate = transforms.ShiftScaleRotate(prob=1.0)
def __getitem__(self, idx):
img_path = self.data_path+"images/seq_"+self.data[idx]["seq"]+"/left_frames/frame"+self.data[idx]["frame"]+".png"
label_path = self.data_path+"images/seq_"+self.data[idx]["seq"]+"/right_frames/frame"+self.data[idx]["frame"]+".png"
#get img from file and resize it to 320x256 which is what we want
img = Image.open(img_path)
img = img.resize((320, 256))
# change to 3 channels
img = np.array(img)
label = Image.open(label_path)
label = label.resize((320, 256))
label = np.array(label)
[label,_] = self.randomShiftScaleRotate(img,mask=None)
# apply normalization
norm = transforms.Normalize()
img = norm(img)
label = norm(label)
img = torch.from_numpy(img).permute(2, 0, 1)
label = torch.from_numpy(label).permute(2, 0, 1)
sample = {'img':img,'label':label,'idx':idx}
return sample
class Colorize_PretrainDataset(MICCAIDataset):
def __init__(self, data_path="../data/", data_type = "train", transform=None):
#store some input
super(Colorize_PretrainDataset, self).__init__(data_path, data_type, transform)
def __getitem__(self, idx):
img_path = self.data_path+"images/seq_"+self.data[idx]["seq"]+"/left_frames/frame"+self.data[idx]["frame"]+".png"
img = Image.open(img_path).convert('L')
img = img.resize((320, 256))
img = np.array(img)
# print(img.shape)
label = Image.open(img_path)
label = label.resize((320, 256))
label = np.array(label)
img = torch.from_numpy(img)
label = torch.from_numpy(label).permute(2, 0, 1)
sample = {'img':img,'label':label,'indx':idx}
return sample
class Shuffle_PretrainDataset(MICCAIDataset):
def __init__(self, data_path="../data/", data_type = "train", transform=None, n=30,seed =1):
self.data_path = data_path
self.data_type = data_type
self.transform = transform
self.filename = data_path+"index/"+data_type+"_data.txt"
self.n = n
self.seed = seed
self.data = []
#parse the txt to store the necessary information of output
file = open(self.filename, 'r').readlines()
file = file[1:]
for i in range(len(file)):
file[i] = file[i].split(",")
entry = {}
entry["seq"] = file[i][0].strip()
entry["frame"] = file[i][1].strip().zfill(3)
self.data.append(entry)
#store some input
#super(Shuffle_PretrainDataset, self).__init__(data_path, data_type, transform,n)
def __len__(self):
#return the length of the data numbers
return len(self.data)
def __getitem__(self, idx):
img_path = self.data_path+"images/seq_"+self.data[idx]["seq"]+"/left_frames/frame"+self.data[idx]["frame"]+".png"
img = Puzzle_RandomShuffle(img_path, self.n, self.seed)
img = img.resize((320, 256))
img = np.array(img)
label = Image.open(img_path)
label = label.resize((320, 256))
label = np.array(label)
# augment dataset
# if self.transform is not None:
# img,label = transforms.augment(img,label)
# # apply totensor and normalization only to img
# norm = transforms.Normalize()
# img = norm(img)
#pil2tensor = transforms.ToTensor()
#img = pil2tensor(img)
# apply normalization
norm = transforms.Normalize()
img = norm(img)
label = norm(label)
img = torch.from_numpy(img).permute(2, 0, 1)
label = torch.from_numpy(label).permute(2, 0, 1)
sample = {'img':img,'label':label,'indx':idx}
return sample
if __name__ == "__main__":
label_converter = LabelConverter()
train_both_aug = Compose([
Cutout(num_holes=8,p=0.5),
OneOf([
ShiftScaleRotate(rotate_limit=15,p=0.5),
HorizontalFlip(p=0.8),
])
])
train_image_aug = Compose([
OneOf([
RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2,p=0.7),
RandomGamma(gamma_limit=(50,200),p=0.7),
HueSaturationValue(p=0.7),
]),
MotionBlur(blur_limit=7,p=0.7),
RandomSpotlight(p=0.7),
Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5),p=1),
])
train_per_class_aug = Compose([
ThreadHueSaturationValue(hue_shift_limit= (-20,20), sat_shift_limit = (-30,30), val_shift_limit=(-20,20), always_apply=False,p=1.0),
Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5),p=1),
])
val_image_aug = Compose([
Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5),p=1),
])
train_dataset=MICCAIDataset(data_type="train", transform_both=None,transform_image=None,transform_per_class=train_per_class_aug)
train_generator = DataLoader(train_dataset,shuffle=False,batch_size=1,num_workers=1)
for i_batch, sample_batch in enumerate(train_generator):
img = sample_batch['img']
label = sample_batch['label']
print(img.shape)
print(label.shape)
imshow(img[0,:,:,:].permute(1,2,0),denormalize=True)
tmp = label_converter.label2color(label[0,:,:,:].permute(1,2,0))
imshow(tmp,denormalize=False)
break