-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_loader.py
181 lines (147 loc) · 5.95 KB
/
data_loader.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
import os
from typing import List, Tuple
import pandas as pd
import pytorch_lightning as pl
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
class TrainDataset(Dataset):
def __init__(self, data: List[str], target_size=(128, 128)):
"""
Loads images from data
@param data:
paths to images
@param: target_size: tuple (int, int), default: (128, 128)
the desired output size
"""
super(TrainDataset, self).__init__()
self.target_size = target_size
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
# Load image
img = Image.open(self.data[idx]).convert('L')
# Pad to square
img = transforms.Pad(((img.height - img.width) // 2, 0), fill=0)(img)
# Resize
img = img.resize(self.target_size, Image.BICUBIC)
# Convert to tensor
img = transforms.ToTensor()(img)
return img
class TrainDataModule(pl.LightningDataModule):
def __init__(self, split_dir: str, target_size=(128, 128), batch_size: int = 32):
"""
Data module for training
@param split_dir: str
path to directory containing the split files
@param: target_size: tuple (int, int), default: (128, 128)
the desired output size
@param: batch_size: int, default: 32
batch size
"""
super(TrainDataModule, self).__init__()
self.target_size = target_size
self.batch_size = batch_size
train_csv_ixi = os.path.join(split_dir, 'ixi_normal_train.csv')
train_csv_fastMRI = os.path.join(split_dir, 'normal_train.csv')
val_csv = os.path.join(split_dir, 'normal_val.csv')
# Load csv files
train_files_ixi = pd.read_csv(train_csv_ixi)['filename'].tolist()
train_files_fastMRI = pd.read_csv(train_csv_fastMRI)['filename'].tolist()
val_files = pd.read_csv(val_csv)['filename'].tolist()
# Combine files
self.train_data = train_files_ixi + train_files_fastMRI
self.val_data = val_files
# Logging
print(f"Using {len(train_files_ixi)} IXI images "
f"and {len(train_files_fastMRI)} fastMRI images for training. "
f"Using {len(val_files)} images for validation.")
def train_dataloader(self):
return DataLoader(TrainDataset(self.train_data, self.target_size),
batch_size=self.batch_size,
shuffle=True)
def val_dataloader(self):
return DataLoader(TrainDataset(self.val_data, self.target_size),
batch_size=self.batch_size,
shuffle=False)
class TestDataset(Dataset):
def __init__(self, img_csv: str, pos_mask_csv: str, neg_mask_csv: str, target_size=(128, 128)):
"""
Loads anomalous images, their positive masks and negative masks from data_dir
@param img_csv: str
path to csv file containing filenames to the images
@param img_csv: str
path to csv file containing filenames to the positive masks
@param img_csv: str
path to csv file containing filenames to the negative masks
@param: target_size: tuple (int, int), default: (128, 128)
the desired output size
"""
super(TestDataset, self).__init__()
self.target_size = target_size
self.img_paths = pd.read_csv(img_csv)['filename'].tolist()
self.pos_mask_paths = pd.read_csv(pos_mask_csv)['filename'].tolist()
self.neg_mask_paths = pd.read_csv(neg_mask_csv)['filename'].tolist()
assert len(self.img_paths) == len(self.pos_mask_paths) == len(self.neg_mask_paths)
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx):
# Load image
img = Image.open(self.img_paths[idx]).convert('L')
img = img.resize(self.target_size, Image.BICUBIC)
img = transforms.ToTensor()(img)
# Load positive mask
pos_mask = Image.open(self.pos_mask_paths[idx]).convert('L')
pos_mask = pos_mask.resize(self.target_size, Image.NEAREST)
pos_mask = transforms.ToTensor()(pos_mask)
# Load negative mask
neg_mask = Image.open(self.neg_mask_paths[idx]).convert('L')
neg_mask = neg_mask.resize(self.target_size, Image.NEAREST)
neg_mask = transforms.ToTensor()(neg_mask)
return img, pos_mask, neg_mask
def get_test_dataloader(split_dir: str, pathology: str, target_size: Tuple[int, int], batch_size: int):
"""
Loads test data from split_dir
@param split_dir: str
path to directory containing the split files
@param pathology: str
pathology to load
@param batch_size: int
batch size
"""
img_csv = os.path.join(split_dir, f'{pathology}.csv')
pos_mask_csv = os.path.join(split_dir, f'{pathology}_ann.csv')
neg_mask_csv = os.path.join(split_dir, f'{pathology}_neg.csv')
return DataLoader(TestDataset(img_csv, pos_mask_csv, neg_mask_csv, target_size),
batch_size=batch_size,
shuffle=False,
drop_last=False)
def get_all_test_dataloaders(split_dir: str, target_size: Tuple[int, int], batch_size: int):
"""
Loads all test data from split_dir
@param split_dir: str
path to directory containing the split files
@param batch_size: int
batch size
"""
pathologies = [
'absent_septum',
'artefacts',
'craniatomy',
'dural',
'ea_mass',
'edema',
'encephalomalacia',
'enlarged_ventricles',
'intraventricular',
'lesions',
'mass',
'posttreatment',
'resection',
'sinus',
'wml',
'other'
]
return {pathology: get_test_dataloader(split_dir, pathology, target_size, batch_size)
for pathology in pathologies}