forked from rajpurkarlab/CXR-RePaiR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieval_baseline_edit.py
319 lines (271 loc) · 12.2 KB
/
retrieval_baseline_edit.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import time
from cgi import test
from pathlib import Path
import h5py
import numpy as np
import pandas as pd
import torch
import torchvision.models as models
from PIL import Image
from scipy.spatial.distance import cdist
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.neighbors import KNeighborsClassifier
from torch.utils import data
from torchvision import transforms
from tqdm import tqdm
class Identity(torch.nn.Module):
def __init__(self):
super(Identity, self).__init__()
def forward(self, x):
return x
def get_valid_indices(train_path, test_path):
train_df = pd.read_csv(train_path, index_col=False)
test_study_ids = pd.read_csv(test_path, index_col=False)["study_id"]
train_df["study_id"] = train_df["filename"].astype(str).str[1:-4].astype(int)
train_study_ids = train_df["study_id"]
contained = train_study_ids.isin(test_study_ids)
not_present_indices = list(contained[contained == False].index)
return not_present_indices
class MIMICEncodingsDataset(data.Dataset):
def __init__(self, encodings_path):
encodings_file = h5py.File(encodings_path)
self.encodings = encodings_file.get("encodings")
self.reports = encodings_file.get("reports")
self.indices = get_valid_indices(
"/deep/group/data/med-data/mimic_cxr_impressions.csv",
"/deep/group/data/med-data/mimic-cxr-jpg-split/bootstrap_test/reports.csv",
)
def __len__(self):
return np.shape(self.encodings)[0]
# return len(self.indices)
def __getitem__(self, idx):
return self.encodings[idx], self.reports[idx]
# train_index = self.indices[idx]
# return self.encodings[train_index], self.reports[train_index]
class MIMICDataset(data.Dataset):
"""Represents an abstract HDF5 dataset."""
def __init__(self, img_path, txt_path, size=None, transform=None):
super().__init__()
self.img_dset = h5py.File(img_path, "r")["cxr_unprocessed"]
self.txt_dset = pd.read_csv(txt_path)["report"]
self.transform = transform
def __len__(self):
return len(self.txt_dset)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img = self.img_dset[idx] # np array, (320, 320)
img = np.expand_dims(img, axis=0)
img = np.repeat(img, 3, axis=0)
txt = self.txt_dset[idx] # python str
img = torch.from_numpy(img) # torch, (3, 320, 320)
if self.transform:
img = self.transform(img)
sample = {"img": img, "txt": txt}
return sample
class MIMICTestDataset(torch.utils.data.Dataset):
def __init__(self, img_path, transform=None):
super().__init__()
self.img_dset = h5py.File(img_path, "r")["cxr"]
self.transform = transform
def __len__(self):
return len(self.img_dset)
def __getitem__(self, idx):
img = self.img_dset[idx] # np array, (320, 320)
img = np.expand_dims(img, axis=0)
img = np.repeat(img, 3, axis=0)
img = torch.from_numpy(img) # torch, (3, 320, 320)
if self.transform:
img = self.transform(img)
return img
class CheXpertDataset(data.Dataset):
def __init__(self, img_path, transform=None):
super().__init__()
imgs_df = pd.read_csv(img_path)
root_path = "/deep/group/CheXpert/CodaLab/"
self.scale = 320
self.transform = transform
self.paths = []
for _path in imgs_df["Path"]:
if "view1" not in _path:
continue # TODO: check how to aggregate studies, consider them independent??
_pth = _path.replace("CheXpert-v1.0", "")
_pth = Path(root_path + _pth)
self.paths.append(_pth)
def __len__(self):
return len(self.paths)
def __getitem__(self, idx):
# img = Image.open(self.paths[idx]).resize((self.scale, self.scale)).convert('RGB')
_np_img = np.asarray(
Image.open(self.paths[idx]).resize(
(self.scale, self.scale), resample=Image.BICUBIC
)
) # these images all have diff sizes!
_np_img = _np_img.astype("float32")
img = np.expand_dims(_np_img, axis=0)
img = np.repeat(img, 3, axis=0)
img = torch.from_numpy(img)
if self.transform:
img = self.transform(img) # goes from H x W x C to C x H x W
return img
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# SET VARIABLES
save_train_encodings = False
retrieve_most_similar_train = True
batch_size = 256
chexpert_filepath = "/deep/group/CheXpert/CodaLab/test_image_paths.csv"
cxr_filepath = "/deep/group/data/med-data/cxr.h5"
txt_filepath = "/deep/group/data/med-data/mimic_cxr_impressions.csv"
encodings_type = "chexpert"
out_path = "/deep/u/markendo/CXR-RePaiR/results/CheXpert/Retrieval-Baseline/generated_reports.csv"
if encodings_type == "chexpert":
mimic_h5py_path = "/deep/group/report-clip/resnet_mimic_encodings/chexpert.h5"
# out_path = '/deep/u/markendo/R2Gen/out/retrieval_baseline_mimic_generated_reports.csv'
state_dict_path = "/deep/u/markendo/aihc-winter19-robustness/chexpert-model/classification_model_checkpoints/resnet18/1z6xfh2n/epoch=1-chexpert_competition_AUROC=0.88_v0.ckpt"
elif encodings_type == "moco-cxr":
mimic_h5py_path = "/deep/group/report-clip/resnet_mimic_encodings/moco-cxr.h5"
# out_path = '/deep/group/report-clip/aihc-win21-clip/notebooks/eval/out/moco_normalized/moco_normalized_generated_reports.csv'
state_dict_path = "/deep/group/aihc-bootcamp-spring2020/cxr_fewer_samples/experiments/ntruongv/r8w1n416s_20201202_resnet18-chexpert-0.0001-pretrained_20201202-093038_SLURM1931462/checkpoint_0000.pth.tar"
time_before = time.time()
# load resnet 18 model
model = models.resnet18(pretrained=False)
model.fc = Identity()
state_dict = torch.load(state_dict_path)
state_dict = state_dict["state_dict"]
if encodings_type == "chexpert":
for key in list(state_dict.keys()):
new_key = key.replace("model.model.", "")
if new_key != "fc.bias" and new_key != "fc.weight":
state_dict[new_key] = state_dict[key]
del state_dict[key]
elif encodings_type == "moco-cxr":
for key in list(state_dict.keys()):
if "encoder_q" in key: # discard encoder_k, only use query not key
new_key = key.replace("module.encoder_q.", "")
state_dict[new_key] = state_dict[key]
del state_dict[key]
del state_dict["fc.0.weight"]
del state_dict["fc.0.bias"]
del state_dict["fc.2.weight"]
del state_dict["fc.2.bias"]
model.load_state_dict(state_dict)
model = torch.nn.DataParallel(model)
model.to(device)
if save_train_encodings:
mimic_transform = transforms.Compose(
[
# means computed from sample in `cxr_stats` notebook
transforms.Normalize(
(101.48761, 101.48761, 101.48761), (83.43944, 83.43944, 83.43944)
),
]
)
mimic_dset = MIMICDataset(
img_path=cxr_filepath, txt_path=txt_filepath, transform=mimic_transform
)
mimic_loader = data.DataLoader(mimic_dset, batch_size=batch_size)
# get the images and impressions
if not os.path.exists(Path(mimic_h5py_path).parent.absolute()):
os.makedirs(Path(mimic_h5py_path).parent.absolute())
dset_size = len(mimic_loader.dataset)
with h5py.File(mimic_h5py_path, "w") as f:
with torch.no_grad():
encodings_dset = f.create_dataset("encodings", shape=(dset_size, 512))
reports_dset = f.create_dataset(
"reports", shape=(dset_size,), dtype=h5py.string_dtype()
)
for i, pack in enumerate(tqdm(mimic_loader)):
imgs = pack["img"]
resnet_encodings = model(imgs)
encodings_arr = resnet_encodings.data.cpu().numpy()
reports = pack["txt"]
start_index = i * batch_size
if i == len(mimic_loader) - 1:
encodings_dset[start_index:] = encodings_arr
reports_dset[start_index:] = reports
else:
encodings_dset[start_index : start_index + batch_size] = (
encodings_arr
)
reports_dset[start_index : start_index + batch_size] = reports
saved_file = h5py.File(mimic_h5py_path)
print(saved_file.get("encodings")[0])
print(saved_file.get("reports")[0])
# retrieve train examples that are most similar to train set encodings
if retrieve_most_similar_train:
# MIMIC-CXR test set
# CXR_FILEPATH = '/deep/group/data/med-data/mimic_test_cxr.h5'
# CXR_FILEPATH = '/deep/group/data/med-data/mimic-cxr-jpg-split/bootstrap_test/cxr.h5'
# mimic_transform = transforms.Compose([
# # means computed from sample in `cxr_stats` notebook
# transforms.Normalize((101.48761, 101.48761, 101.48761), (83.43944, 83.43944, 83.43944)),
# ])
# # mimic_transform = transforms.Compose([
# # transforms.Resize((224, 224)),
# # transforms.ToTensor(),
# # transforms.Normalize((0.485, 0.456, 0.406),
# # (0.229, 0.224, 0.225))])
# mimic_test_dset = MIMICTestDataset(CXR_FILEPATH, transform=mimic_transform)
# loader = data.DataLoader(mimic_test_dset, shuffle=False, batch_size=batch_size)
# CheXpert test set
chexpert_transform = transforms.Compose(
[
# transforms.ToTensor(),
# transforms.Normalize((.5020, .5020, .5020),(.085585, .085585, .085585)),
transforms.Normalize(
(129.4185, 129.4185, 129.4185), (73.3378, 73.3378, 73.3378)
)
]
)
torch_chexpert_dset = CheXpertDataset(
img_path=chexpert_filepath, transform=chexpert_transform
)
chexpert_loader = data.DataLoader(torch_chexpert_dset, batch_size=batch_size)
# data_mean = next(iter(chexpert_loader))
# print(data_mean[0].mean(), data_mean[0].std())
train_encodings_dset = MIMICEncodingsDataset(mimic_h5py_path)
train_encodings_loader = data.DataLoader(train_encodings_dset, batch_size=256)
output_reports = []
with torch.no_grad():
for i, images in enumerate(chexpert_loader):
images = images.to(device)
test_encodings_batch = model(images)
batch_size = len(test_encodings_batch)
highest_similarities = np.array([-1.0] * batch_size)
best_reports = [""] * batch_size
for train_encodings in tqdm(train_encodings_loader):
train_encodings_batch = train_encodings[0]
# Using torch operations (for gpu)
train_encodings_batch = train_encodings_batch.to(device)
test_encodings_batch_norm = (
test_encodings_batch / test_encodings_batch.norm(dim=1)[:, None]
)
train_encodings_batch_norm = (
train_encodings_batch / train_encodings_batch.norm(dim=1)[:, None]
)
sim = torch.mm(
test_encodings_batch_norm,
train_encodings_batch_norm.transpose(0, 1),
)
maxes = torch.max(sim, dim=1)
# Using np operations (for cpu)
# sim = cosine_similarity(test_encodings_batch.cpu(), train_encodings_batch)
# maxes = np.amax(sim, axis=1)
for minibatch_index in range(batch_size):
if (
maxes.values[minibatch_index]
> highest_similarities[minibatch_index]
):
highest_similarities[minibatch_index] = maxes.values[
minibatch_index
]
best_reports[minibatch_index] = train_encodings[1][
maxes.indices[minibatch_index]
].decode("utf-8")
# best_reports[minibatch_index] = train_encodings[1][np.argmax(sim[minibatch_index])].decode("utf-8")
output_reports.extend(best_reports)
print(time.time() - time_before)
_df = pd.DataFrame(output_reports)
_df.columns = ["report"]
_df.to_csv(out_path, index=False)