-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcxr_dataset.py
More file actions
161 lines (113 loc) · 4.94 KB
/
cxr_dataset.py
File metadata and controls
161 lines (113 loc) · 4.94 KB
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
import os
import cv2
import pandas as pd
import torch
from torch.utils.data import Dataset
class CheXpert(Dataset):
def __init__(self, dataset_path, split='train', transform=None):
self.data_root = os.path.join(dataset_path, '')
self.dataset = pd.read_csv(os.path.join(dataset_path, f'{split}.csv'))
self.dataset = self.dataset.fillna(0)
self.dataset = self.dataset.replace(-1, 0)
self.class_names = {
0: "No Finding",
1: "Enlg. Cardiomed.",
2: "Cardiomegaly", # common
3: "Lung Opacity", # common
4: "Lung Lesion", # common
5: "Edema",
6: "Consolidation", # common
7: "Pneumonia",
8: "Atelectasis", # common
9: "Pneumothorax",
10: "Pleural Effusion", # common
11: "Pleural Other",
12: "Fracture",
13: "Support Devices"
}
if split == 'train' or split == 'valid':
self.listImagePaths = self.dataset['Path'].apply(lambda x: os.path.join(self.data_root, '..', x)).tolist()
self.listImageLabels = self.dataset[self.dataset.columns[5:]].values.astype('int')
elif split == 'test':
self.listImagePaths = self.dataset['Path'].apply(lambda x: os.path.join(self.data_root, x)).tolist()
self.listImageLabels = self.dataset[self.dataset.columns[1:]].values.astype('int')
self.transform = transform
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
image = cv2.imread(self.listImagePaths[idx])
label = self.listImageLabels[idx]
if self.transform is not None:
image = self.transform(image)
return image, label
class NIH_CXR(Dataset):
def __init__(self, dataset_path, split='train_val', transform=None):
self.data_root = os.path.join(dataset_path, '')
self.class_names = {
0: 'No Finding',
1: 'Atelectasis', # common
2: 'Cardiomegaly', # common
3: 'Effusion', # common
4: 'Infiltration',
5: 'Mass', # common (opacity)
6: 'Nodule', # common
7: 'Pneumonia',
8: 'Pneumothorax',
9: 'Consolidation', # common
10: 'Edema',
11: 'Emphysema',
12: 'Fibrosis',
13: 'Pleural_Thickening',
14: 'Hernia',
}
if split == 'train_val':
self.dataset = pd.read_csv(os.path.join(dataset_path, 'train_val_list.csv'))
elif split == 'test':
self.dataset = pd.read_csv(os.path.join(dataset_path, 'test_list.csv'))
self.listImagePaths = self.dataset['Image Index'].apply(lambda x: os.path.join(self.data_root, 'images', x)).tolist()
self.listImageLabels = self.dataset[self.dataset.columns[10:]].values.astype('int')
self.transform = transform
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
image = cv2.imread(self.listImagePaths[idx])
label = self.listImageLabels[idx]
if self.transform is not None:
image = self.transform(image)
return image, label
class DRR_RATE(Dataset):
def __init__(self, dataset_path, split='train', transform=None):
self.data_root = os.path.join(dataset_path, '')
self.class_names = {
0: "Cardiomegaly", # common
1: "Atelectasis", # common
2: "Lung Nodule", # common
3: "Lung Opacity", # common
4: "Pleural Effusion", # common
5: "Consolidation", # common
}
self.dataset = pd.read_csv(os.path.join(dataset_path, f'multi_abnormality_labels/{split}_predicted_labels.csv'))
self.listImagePaths = self.dataset['ImageName'].apply(
lambda x: os.path.join(self.data_root, f'{split}/AP', self.get_path(x))).tolist()
self.listImageLabels = self.dataset[[
'Cardiomegaly', 'Atelectasis', 'Lung nodule',
'Lung opacity', 'Pleural effusion', 'Consolidation']].values.astype('int')
self.transform = transform
def get_path(self, name):
parts = name.split('_')
folder = '_'.join(parts[:2])
subfolder = '_'.join(parts[:3])
rpath = os.path.join(folder, subfolder, name)
return rpath
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
image = cv2.imread(self.listImagePaths[idx])
label = self.listImageLabels[idx]
if self.transform is not None:
image = self.transform(image)
return image, label
if __name__ == '__main__':
dataloader = DRR_RATE('/vol/biomedic/users/bh1511/DRR-RATE')
dataloader.__getitem__(12)
print('Hello, World!')