-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasets.py
212 lines (178 loc) · 5.92 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
from abc import abstractmethod
import os
import yaml
import numpy as np
from torch.utils.data import Dataset, DataLoader
import lightning as L
class BaseDataset(Dataset):
def __init__(self,
data_dir,
target_modality,
source_modality,
stage,
image_size,
norm=True,
padding=True
):
self.data_dir = data_dir
self.target_modality= target_modality
self.source_modality = source_modality
self.stage = stage
self.image_size = image_size
self.norm = norm
self.padding = padding
self.original_shape = None
@abstractmethod
def _load_data(self, contrast):
pass
def _pad_data(self, data):
""" Pad data to image_size x image_size """
H, W = data.shape[-2:]
pad_top = (self.image_size - H) // 2
pad_bottom = self.image_size - H - pad_top
pad_left = (self.image_size - W) // 2
pad_right = self.image_size - W - pad_left
return np.pad(data, ((0, 0), (pad_top, pad_bottom), (pad_left, pad_right)))
def _normalize(self, data):
return (data - 0.5) / 0.5
class NumpyDataset(BaseDataset):
def __init__(
self,
data_dir,
target_modality,
source_modality,
stage,
image_size,
norm=True,
padding=True
):
super().__init__(
data_dir,
target_modality,
source_modality,
stage,
image_size,
norm,
padding
)
# Load target images
self.target = self._load_data(self.target_modality)
self.source = self._load_data(self.source_modality)
# Get original shape
self.original_shape = self.target.shape[-2:]
# Load subject ids
self.subject_ids = self._load_subject_ids('subject_ids.yaml')
# Padding
if self.padding:
self.target = self._pad_data(self.target)
self.source = self._pad_data(self.source)
# Normalize
if self.norm:
self.target = self._normalize(self.target)
self.source = self._normalize(self.source)
# Expand channel dim
self.target = np.expand_dims(self.target, axis=1)
self.source = np.expand_dims(self.source, axis=1)
def _load_data(self, contrast):
data_dir = os.path.join(self.data_dir, contrast, self.stage)
files = [f for f in os.listdir(data_dir) if f.endswith('.npy')]
# Sort by slice index
files.sort(key=lambda x: int(x.split('_')[-1].split('.')[0]))
data = []
for file in files:
data.append(np.load(os.path.join(data_dir, file)))
return np.array(data).astype(np.float32)
def _load_subject_ids(self, filename):
subject_ids_path = os.path.join(self.data_dir, filename)
if os.path.exists(subject_ids_path):
with open(subject_ids_path, 'r') as f:
subject_ids = np.array(yaml.load(f, Loader=yaml.FullLoader))
else:
subject_ids = None
return subject_ids
def __len__(self):
return len(self.source)
def __getitem__(self, i):
return self.target[i], self.source[i], i
class DataModule(L.LightningDataModule):
def __init__(
self,
dataset_dir,
source_modality,
target_modality,
dataset_class,
image_size,
padding,
norm,
train_batch_size=1,
val_batch_size=1,
test_batch_size=1,
num_workers=1,
):
super().__init__()
self.save_hyperparameters()
self.dataset_dir = dataset_dir
self.train_batch_size = train_batch_size
self.val_batch_size = val_batch_size
self.test_batch_size = test_batch_size
self.source_modality = source_modality
self.target_modality = target_modality
self.image_size = image_size
self.padding = padding
self.norm = norm
self.num_workers = num_workers
self.dataset_class = globals()[dataset_class]
def setup(self, stage: str) -> None:
target_modality = self.target_modality
source_modality = self.source_modality
if stage == "fit":
self.train_dataset = self.dataset_class(
target_modality=target_modality,
source_modality=source_modality,
data_dir=self.dataset_dir,
stage='train',
image_size=self.image_size,
padding=self.padding,
norm=self.norm
)
self.val_dataset = self.dataset_class(
target_modality=target_modality,
source_modality=source_modality,
data_dir=self.dataset_dir,
stage='val',
image_size=self.image_size,
padding=self.padding,
norm=self.norm
)
if stage == "test":
self.test_dataset = self.dataset_class(
target_modality=target_modality,
source_modality=source_modality,
data_dir=self.dataset_dir,
stage='test',
image_size=self.image_size,
padding=self.padding,
norm=self.norm
)
def train_dataloader(self):
return DataLoader(
self.train_dataset,
batch_size=self.train_batch_size,
num_workers=self.num_workers,
shuffle=True,
drop_last=True
)
def val_dataloader(self):
return DataLoader(
self.val_dataset,
batch_size=self.val_batch_size,
num_workers=self.num_workers,
shuffle=False
)
def test_dataloader(self):
return DataLoader(
self.test_dataset,
batch_size=self.test_batch_size,
num_workers=self.num_workers,
shuffle=False
)