-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathdata.py
187 lines (149 loc) · 6.27 KB
/
data.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
import re
import torch
from torch.utils.data.dataset import Dataset
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os
import scipy.io as sio
from dataParser import getMaskFileName, getImg
import torchvision.transforms as tr
class BraTSDatasetUnet(Dataset):
__file = []
__im = []
__mask = []
im_ht = 0
im_wd = 0
dataset_size = 0
def __init__(self, dataset_folder, train=True, keywords=["P1", "1", "flair"], im_size=[128, 128], transform=None):
self.__file = []
self.__im = []
self.__mask = []
self.im_ht = im_size[0]
self.im_wd = im_size[1]
self.transform = transform
folder = dataset_folder
# # Open and load text file including the whole training data
if train:
folder = dataset_folder + "Train/"
else:
folder = dataset_folder + "Test/"
for file in os.listdir(folder):
if file.endswith(".png"):
filename = os.path.splitext(file)[0]
filename_fragments = filename.split("_")
samekeywords = list(set(filename_fragments) & set(keywords))
if len(samekeywords) == len(keywords):
# 1. read file name
self.__file.append(filename)
# 2. read raw image
# TODO: I think we should open image only in getitem,
# otherwise memory explodes
# rawImage = getImg(folder + file)
self.__im.append(folder + file)
# 3. read mask image
mask_file = getMaskFileName(file)
# maskImage = getImg(folder + mask_file)
self.__mask.append(folder + mask_file)
# self.dataset_size = len(self.__file)
# print("lengths : ", len(self.__im), len(self.__mask))
self.dataset_size = len(self.__file)
if not train:
sio.savemat('filelist2.mat', {'data': self.__im})
def __getitem__(self, index):
img = getImg(self.__im[index])
mask = getImg(self.__mask[index])
img = img.resize((self.im_ht, self.im_wd))
mask = mask.resize((self.im_ht, self.im_wd))
# mask.show()
if self.transform is not None:
# TODO: Not sure why not take full image
img_tr = self.transform(img)
mask_tr = self.transform(mask)
# img_tr = self.transform(img[None, :, :])
# mask_tr = self.transform(mask[None, :, :])
return img_tr, mask_tr
# return img.float(), mask.float()
def __len__(self):
return len(self.__im)
class BraTSDatasetLSTM(Dataset):
__im = []
__mask = []
__im1 = []
__im3 = []
im_ht = 0
im_wd = 0
dataset_size = 0
def __init__(self, dataset_folder, train=True, keywords=["P1", "1", "flair"], im_size=[128, 128], transform=None):
self.__file = []
self.__im = []
self.__mask = []
self.im_ht = im_size[0]
self.im_wd = im_size[1]
self.transform = transform
folder = dataset_folder
# # Open and load text file including the whole training data
if train:
folder = dataset_folder + "Train/"
else:
folder = dataset_folder + "Test/"
# print("files : ", os.listdir(folder))
# print("Folder : ", folder)
max_file = 0
min_file = 10000000
for file in os.listdir(folder):
if file.endswith(".png"):
m = re.search('(P[0-9]*[_])([0-9]*)', file)
pic_num = int(m.group(2))
if pic_num > max_file:
max_file = pic_num
if pic_num < min_file:
min_file = pic_num
# print('min file number: ', min_file)
# print('max file number: ', max_file)
for file in os.listdir(folder):
if file.endswith(".png"):
filename = os.path.splitext(file)[0]
filename_fragments = filename.split("_")
samekeywords = list(set(filename_fragments) & set(keywords))
if len(samekeywords) == len(keywords):
# 1. read file name
# 2. read raw image
# TODO: I think we should open image only in getitem,
# otherwise memory explodes
# rawImage = getImg(folder + file)
if (filename_fragments[2] != str(min_file)) and (filename_fragments[2] != str(max_file)):
# print("TEST : ", filename_fragments[2])
self.__im.append(folder + file)
file1 = filename_fragments[0] + '_' + filename_fragments[1] + '_' + str(
int(filename_fragments[2]) - 1) + '_' + filename_fragments[3] + '.png'
self.__im1.append(folder + file1)
file3 = filename_fragments[0] + '_' + filename_fragments[1] + '_' + str(
int(filename_fragments[2]) + 1) + '_' + filename_fragments[3] + '.png'
self.__im3.append(folder + file3)
# 3. read mask image
mask_file = getMaskFileName(file)
# maskImage = getImg(folder + mask_file)
self.__mask.append(folder + mask_file)
# self.dataset_size = len(self.__file)
# print("lengths : ", len(self.__im), len(self.__mask))
self.dataset_size = len(self.__file)
def __getitem__(self, index):
img1 = getImg(self.__im1[index])
img = getImg(self.__im[index])
img3 = getImg(self.__im3[index])
mask = getImg(self.__mask[index])
# img.show()
# mask.show()
if self.transform is not None:
# TODO: Not sure why not take full image
img_tr1 = self.transform(img1)
img_tr = self.transform(img)
img_tr3 = self.transform(img3)
mask_tr = self.transform(mask)
# img_tr = self.transform(img[None, :, :])
# mask_tr = self.transform(mask[None, :, :])
return img_tr1, img_tr, img_tr3, mask_tr
# return img.float(), mask.float()
def __len__(self):
return len(self.__im)