-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
176 lines (154 loc) · 6.57 KB
/
utils.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
import os
import csv
import hashlib
import torchxrayvision as xrv
import torch
import torchvision
import torchvision.transforms
def get_cache_dir(data_dir, data_name):
h = hashlib.sha1(data_dir.encode()).hexdigest()
cache_dir = os.path.join("~", ".cache", "torchxrayvision", "datasets", f"{data_name}", h[:17] + ".pt")
cache_dir = os.path.expanduser(cache_dir)
return cache_dir
def load_data(cfg):
data_aug = torchvision.transforms.Compose([
xrv.datasets.ToPILImage(),
torchvision.transforms.RandomAffine(
cfg.data_aug_rot,
translate=(cfg.data_aug_trans, cfg.data_aug_trans),
scale=(1.0 - cfg.data_aug_scale, 1.0 + cfg.data_aug_scale)
),
torchvision.transforms.ToTensor()
])
transforms = torchvision.transforms.Compose([
xrv.datasets.XRayCenterCrop(),
xrv.datasets.XRayResizer(cfg.data_resize)
])
# Load and cache NIH Dataset ###
nih_dataset = None
if "nih" in cfg.train_datas or "nih" in cfg.val_data or "nih" in cfg.test_data:
imgdir = os.path.join(cfg.dataset_dir, "images-224-NIH")
cache_dir = get_cache_dir(imgdir, "nih")
if cfg.cache_dataset and os.path.exists(cache_dir):
nih_dataset, _ = torch.load(cache_dir)
else:
nih_dataset = xrv.datasets.NIH_Dataset(
imgpath=imgdir,
csvpath=os.path.join(cfg.dataset_dir, "Data_Entry_2017_v2020.csv.gz"),
bbox_list_path=os.path.join(cfg.dataset_dir, "BBox_List_2017.csv.gz"),
transform=transforms,
data_aug=None if cfg.test_data == "nih" else data_aug,
unique_patients=False
)
xrv.datasets.relabel_dataset(cfg.pathologies, nih_dataset)
if cfg.cache_dataset:
os.makedirs(os.path.dirname(cache_dir), exist_ok=True)
torch.save((nih_dataset, imgdir), cache_dir)
# Load and cache CHEXPERT Dataset ###
cx_dataset = None
if "cx" in cfg.train_datas or "cx" in cfg.val_data or "cx" in cfg.test_data:
imgdir = os.path.join(cfg.dataset_dir, "CheXpert-v1.0-small")
cache_dir = get_cache_dir(imgdir, "cx")
if cfg.cache_dataset and os.path.exists(cache_dir):
cx_dataset, _ = torch.load(cache_dir)
else:
cx_dataset = xrv.datasets.CheX_Dataset(
imgpath=imgdir,
csvpath=os.path.join(cfg.dataset_dir, "CheXpert-v1.0-small/train.csv"),
transform=transforms,
data_aug=None if cfg.test_data == "cx" else data_aug,
unique_patients=False
)
xrv.datasets.relabel_dataset(cfg.pathologies, cx_dataset)
if cfg.cache_dataset:
os.makedirs(os.path.dirname(cache_dir), exist_ok=True)
torch.save((cx_dataset, imgdir), cache_dir)
# Load and cache MIMIC_CH Dataset ###
mc_dataset = None
if "mc" in cfg.train_datas or "mc" in cfg.val_data or "mc" in cfg.test_data:
imgdir = os.path.join(cfg.dataset_dir, "images-224-MIMIC/files")
cache_dir = get_cache_dir(imgdir, "mc")
if cfg.cache_dataset and os.path.exists(cache_dir):
mc_dataset, _ = torch.load(cache_dir)
else:
mc_dataset = xrv.datasets.MIMIC_Dataset(
imgpath=imgdir,
csvpath=os.path.join(cfg.dataset_dir, "MIMICCXR-2.0/mimic-cxr-2.0.0-chexpert.csv.gz"),
metacsvpath=os.path.join(cfg.dataset_dir, "MIMICCXR-2.0/mimic-cxr-2.0.0-metadata.csv.gz"),
transform=transforms,
data_aug=None if cfg.test_data == "mc" else data_aug,
unique_patients=False
)
xrv.datasets.relabel_dataset(cfg.pathologies, mc_dataset)
if cfg.cache_dataset:
os.makedirs(os.path.dirname(cache_dir), exist_ok=True)
torch.save((mc_dataset, imgdir), cache_dir)
# Load and cache PADCHEST Dataset ###
pc_dataset = None
if "pc" in cfg.train_datas or "pc" in cfg.val_data or "pc" in cfg.test_data:
imgdir = os.path.join(cfg.dataset_dir, "PC/images-224")
cache_dir = get_cache_dir(imgdir, "pc")
if cfg.cache_dataset and os.path.exists(cache_dir):
pc_dataset, _ = torch.load(cache_dir)
else:
pc_dataset = xrv.datasets.PC_Dataset(
imgpath=imgdir,
csvpath=os.path.join(cfg.dataset_dir, "PC/PADCHEST_chest_x_ray_images_labels_160K_01.02.19.csv"),
transform=transforms,
data_aug=None if cfg.test_data == "pc" else data_aug,
unique_patients=False
)
xrv.datasets.relabel_dataset(cfg.pathologies, pc_dataset)
if cfg.cache_dataset:
os.makedirs(os.path.dirname(cache_dir), exist_ok=True)
torch.save((pc_dataset, imgdir), cache_dir)
print(f"\nCommon pathologies among all train and validation datasets: {cfg.pathologies}")
datasets = {
"nih": nih_dataset,
"cx": cx_dataset,
"mc": mc_dataset,
"pc": pc_dataset,
}
return datasets
def create_q_model(cfg, model):
num_features = model.fc.in_features
model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
model_features = torch.nn.Sequential(
model.quant,
model.conv1,
model.bn1,
model.relu,
model.maxpool,
model.layer1,
model.layer2,
model.layer3,
model.layer4,
model.avgpool,
model.dequant,
)
new_head = torch.nn.Sequential(
torch.nn.Dropout(p=cfg.dropout),
torch.nn.Linear(num_features, cfg.num_labels)
)
new_model = torch.nn.Sequential(
model_features,
torch.nn.Flatten(1),
new_head,
)
return new_model
def create_model(cfg, model):
if "resnet" in str(model.__class__):
num_features = model.fc.in_features
model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
model.fc = torch.nn.Sequential(
torch.nn.Dropout(p=cfg.dropout),
torch.nn.Linear(num_features, cfg.num_labels)
)
elif "densenet" in str(model.__class__):
num_features = model.classifier.in_features
model.features.conv0 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
model.classifier = torch.nn.Sequential(
torch.nn.Dropout(p=cfg.dropout),
torch.nn.Linear(num_features, cfg.num_labels)
)
return model