-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
281 lines (235 loc) · 9.61 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import matplotlib.image as mpimg
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import os,sys
from PIL import Image
from tqdm import tqdm
from scipy import ndimage
import torch.nn.functional as F
import torch as tc
from torch.utils.data import DataLoader
from torch.utils.data import TensorDataset
from torch.utils.data import Dataset
from helpers_img import *
from NeuralNets import *
from training_NN import *
from preprocessing import *
# Class to create the dataframe for U-Net
class DatasetUNet(tc.utils.data.Dataset):
# Constructor of the class
def __init__(self, root_dir, bound=None, do_prep=False, do_flip = False,
normalize = False, noise=False, is_simple_noise=False, rot = False):
''' bound: to take a subset of images;
do_prep: preprocessing;
do_flip: flip images;
normalize: normalize the tensor;
noise: add noise;
is_simple_noise: decide if add noise to original image or mask;
rot: rotate images;'''
self.image_dir = root_dir + "images/"
self.files = os.listdir(self.image_dir)
self.gt_dir = root_dir + "groundtruth/"
self.do_prep = do_prep
self.do_flip = do_flip
self.normalize = normalize
self.noise = noise
self.is_simple_noise = is_simple_noise
self.rot = rot
if bound!=None:
del self.files[0:bound[0]]
del self.files[bound[1]:]
def __len__(self):
''' Return the images number'''
return len(self.files)
def true_len(self):
''' Return the dataset length '''
return len(self.files)*(1+3*self.rot)*(1+1*self.noise)*(1+1*self.do_flip)
def __getitem__(self, index):
''' Return an image'''
# Load images
image = load_image(self.image_dir + self.files[index])
gt_image = load_image(self.gt_dir + self.files[index])
# Preprocess image
if self.do_prep:
_, laplacian_image = add_laplacian(image)
sobel = add_sobel(image)
segment = add_segment(image)
image=np.concatenate((image,laplacian_image,sobel,segment),axis = 2)
image,gt_image=[image],[gt_image]
# Rotate images
if self.rot:
image,gt_image = rotation(image,gt_image, diagonal = True)
# Flip images
if self.do_flip:
image,gt_image = flip(image,gt_image)
# Image to tensor
image,gt_image = self.from_list_to_tensor(image), self.from_list_to_tensor(gt_image)
# Normalize tensor
if self.normalize:
image = (image - image.mean())/image.std()
# Add noise
if self.noise:
image,gt_image = self.add_noise(image, gt_image, is_simple=self.is_simple_noise)
return image,gt_image
def from_list_to_tensor(self,dataset):
''' Cast a list of image in a tensor of appropriate size'''
dataset = np.array(dataset)
try :
N,rows,columns,features = dataset.shape
except:
N,rows,columns = dataset.shape
features=1
dataset=dataset.reshape(N,rows,columns,features)
dataset_tensor = tc.Tensor(N, features, rows, columns)
for j in range(N):
dataset_tensor[j] = tc.tensor(np.array([dataset[j,:,:,i] for i in range(features)]))
return dataset_tensor
def add_noise(self, dataset,label, is_simple=True):
'''Add noise to the dataset.
dataset : tensor type
label : tensor type'''
# If simple
if is_simple:
mean, std = dataset.mean(), dataset.std()
# The noise has the 20% of the image standard deviation
noise = np.random.normal(loc = mean, scale = std/5, size = dataset.size())
dataset_with_noise = dataset + tc.tensor(noise).type(tc.FloatTensor)
dataset = tc.cat((dataset,dataset_with_noise),dim = 0)
label = label.type(tc.FloatTensor)
label = tc.cat((label, label), dim = 0)
else:
mean, std = 0, 0.05
noise = np.random.normal(mean, std, size = label.size())
label.type(float)
label_with_noise = label + tc.tensor(noise).type(tc.FloatTensor)
label = tc.cat((label,label_with_noise),dim=0)
dataset = tc.cat((dataset,dataset),dim=0)
return dataset, label
def get_features(self):
''' Returns the number of features'''
if self.do_prep:
features = 10
else:
features = 3
return features
def get_mini(self):
''' Return the number of images copy'''
return (1+3*self.rot)*(1+1*self.do_flip)*(1+1*self.noise)
# Class to create the dataset to test
class Testset(tc.utils.data.Dataset):
# Constructor of the class
def __init__(self, root_dir, nb_test_imgs, do_prep = False, normalize=False, expansion=False):
''' root_dir: set directory;
nb_test_imgs: number of test images;
do_prep: do preprocessing;
normalize: normalize the tensor;
expansion: reflect border;'''
self.root_dir = root_dir
self.nb_test_imgs = nb_test_imgs
self.normalize = normalize
self.do_prep = do_prep
self.expansion = expansion
def __getitem__(self,index):
''' Return an image'''
# Load image
dir_test = self.root_dir + 'test_'+str(index+1)+'/'
files_test = os.listdir(dir_test)
img_test = load_image(dir_test + files_test[0])
# Expand it
if self.expansion:
img_test = add_border(img_test,630)
original_img = img_test
# Preprocess it
if self.do_prep:
_, laplacian_image = add_laplacian(img_test)
sobel = add_sobel(img_test)
segment = add_segment(img_test)
img_test=np.concatenate((img_test,laplacian_image,sobel,segment),axis = 2)
img_test = self.from_list_to_tensor([img_test])
# Normalize it
if self.normalize:
img_test = (img_test-img_test.mean())/img_test.std()
return img_test, original_img
def __len__(self):
''' Return the images number '''
return self.nb_test_imgs
def from_list_to_tensor(self,dataset):
''' cast a list of image in a tensor of appropriate size'''
dataset = np.array(dataset)
try :
N,rows,columns,features = dataset.shape
except:
N,rows,columns = dataset.shape
features=1
dataset=dataset.reshape(N,rows,columns,features)
dataset_tensor = tc.Tensor(N, features, rows, columns)
for j in range(N):
dataset_tensor[j] = tc.tensor(np.array([dataset[j,:,:,i] for i in range(features)]))
return dataset_tensor
def get_features(self):
''' Returns the number of features'''
if self.do_prep:
features = 10
else:
features = 3
return features
class DatasetDeepNet(Dataset):
def __init__(self,root_dir, do_flip=False, do_rotation=False,do_train=False):
self.image_dir = root_dir + "images/"
self.files = os.listdir(self.image_dir)
self.gt_dir = root_dir + "groundtruth/"
self.rot_len=0
self.flip_len=0
self.train = do_train
self.initial_len=len(self.files)
# rotation
if do_rotation:
self.rot_len= len(self.files)
self.files = [*self.files*4]
#flip
if do_flip:
self.flip_len=len(self.files)
self.files= [*self.files*2]
def __len__(self):
return len(self.files)
def __getitem__(self,index):
image = [load_image(self.image_dir + self.files[index])]
gt_image = [load_image(self.gt_dir + self.files[index])]
if self.rot_len>0:
image,gt_image = rotation(image,gt_image)
if self.flip_len>0:
image,gt_image = flip(image,gt_image)
i = index//self.initial_len
image,gt_image = image[i],gt_image[i]
image = add_border(image,432)
train_sub_images = [img_crop_mod(image, 16, 16)]
train_mask_label = [img_crop(gt_image,16,16)]
train_mask_label = from_mask_to_vector(train_mask_label,0.25)
train_sub_images = transform_subIMG_to_Tensor(train_sub_images)
mean = train_sub_images.mean()
std = train_sub_images.std()
train_sub_images = (train_sub_images-mean)/std
if self.train:
train_sub_images, train_mask_label = reduce_dataset(train_sub_images,train_mask_label)
for l in range(10):
new_indices= np.random.permutation(len(train_mask_label))
train_sub_images=train_sub_images[new_indices]
train_mask_label=train_mask_label[new_indices]
return train_sub_images, 1*train_mask_label
class TestsetDeepNet(Dataset):
def __init__(self,root_dir, nb_test):
self.root_dir = root_dir
self.nb_test = nb_test
def __len__(self):
return self.nb_test
def __getitem__(self,index):
dir_test = self.root_dir + 'test_'+str(index+1)+'/'
files_test = os.listdir(dir_test)
img_test = load_image(dir_test + files_test[0])
original_img = img_test
img_test = add_border(img_test,608+32)
img_test=[img_crop_mod(img_test, 16, 16)]
img_test=transform_subIMG_to_Tensor(img_test)
img_test = (img_test-img_test.mean())/img_test.std()
return img_test,original_img