-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathembeddings.py
432 lines (378 loc) · 14.2 KB
/
embeddings.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import argparse
import clip
import json
import numpy as np
import os
import pandas as pd
import PIL
import sys
import torch
from models import CustomCLIPWrapper, init_img_model, init_txt_model
from pathlib import Path
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms as T
from tqdm import tqdm
parser = argparse.ArgumentParser()
parser.add_argument(
'--data_path',
required=True,
type=str,
help='Path to a train or test split csv'
)
parser.add_argument(
'--val_path',
default=None,
type=str,
help='Path to val split csv to append'
)
parser.add_argument(
'--embed_type',
required=True,
type=str,
help='Choose (image | text)'
)
parser.add_argument(
'--save_as',
required=True,
type=str,
help='filename for embeddings csv'
)
parser.add_argument(
'--chexpert_folder',
default=None,
type=str,
help='Path to folder with chexpert labelled sentences as csv files'
)
parser.add_argument(
'--write_after',
default=5000,
type=int,
help='Output csv in batches of at least --write_after size'
)
parser.add_argument(
'--config_file',
required=True,
type=str,
help='Path to config json'
)
parser.add_argument(
'--batch_size',
default=1,
type=int,
help='Batch size'
)
parser.add_argument(
'--num_workers',
default=0,
type=int,
help='Number of workers'
)
class TextDataset(Dataset):
def __init__(
self,
data: str,
val_data=None,
chexpert_data=None,
split_reports=False,
shuffle=False,
):
"""Create a text dataset from a csv file with reports.
Args:
data (str): Path to a csv file with image paths and reports.
val_data (str, optional): Path to a csv file with validation image paths and reports.
chexpert_data (str, optional): Path to the folder with the chexpert csv files.
split_reports (bool, optional): Whether or not reports should be split by sentences.
shuffle (bool, optional): Whether or not to have shuffling behavior during sampling. Defaults to False.
"""
super().__init__()
self.shuffle = shuffle
self.chexpert_data = chexpert_data
self.data = pd.read_csv(data, index_col=0)[["study_id", "report"]]
print("Data len:", len(self.data))
# append the validation set to the train set
if val_data is not None:
val_data = pd.read_csv(val_data, index_col=0)[["study_id", "report"]]
self.data = pd.concat([self.data, val_data], ignore_index=True)
print("Len after adding val data:", len(self.data))
self.data = self.data.dropna().reset_index(drop=True)
print("After dropping nans:", len(self.data))
self.data = self.data.drop_duplicates().reset_index(drop=True)
print("After dropping duplicates:", len(self.data))
# assuming the chexpert files are stored as a series of csv files
if self.chexpert_data is not None:
chexpert_folder = Path(self.chexpert_data)
chex_files = [*chexpert_folder.glob("**/*.csv")]
print(f"Found {len(chex_files)} chex csvs")
dfs = []
for chex_file in chex_files:
dfs.append(pd.read_csv(chex_file))
chex = pd.concat(
dfs, axis=0, ignore_index=True
) # want to explicitly keep indices (the study_ids)
chex = chex.dropna()
# every study is joined with every one of the chexpert-classified sentences (1 to M join)
# dropping the full reports
self.data = self.data.merge(
chex, left_on="study_id", right_on="mimic_id"
).drop(columns=["index", "report_x", "mimic_id", "report_y", "cat", "vals"])
self.data = self.data.rename(columns={"sents": "report"})
self.data = self.data.drop_duplicates().reset_index(drop=True)
print("Len after splitting by chexpert sentences:", len(self.data))
if split_reports:
print("splitting reports by sentence")
self.data = self.split_reports(self.data)
print("Len after splitting by sentences", len(self.data))
self.keys = list(self.data.index)
def __len__(self):
return len(self.keys)
def __getitem__(self, ind):
key = self.keys[ind]
study_id = self.data.loc[key]["study_id"]
text = str(self.data.loc[key]["report"])
text = text.replace("\n", "").replace("\r", "")
item = {"study_id": study_id, "text": text}
# Success
return item
def split_reports(self, data):
data["report"] = data["report"].str.split(".")
data = data.explode("report").reset_index(drop=True)
data = data.replace("", np.nan).dropna().reset_index(drop=True)
return data
class CLIPTextDataset(TextDataset):
def __init__(
self,
data: str,
val_data=None,
chexpert_data=None,
split_reports=False,
shuffle=False,
):
"""Create a CLIP specific text dataset from a csv file with reports.
Args:
data (str): Path to a csv file with image paths and reports.
val_data (str, optional): Path to a csv file with validation image paths and reports.
chexpert_data (str, optional): Path to the folder with the chexpert csv files.
split_reports (bool, optional): Whether or not reports should be split by sentences.
shuffle (bool, optional): Whether or not to have shuffling behavior during sampling. Defaults to False.
"""
super().__init__(data, val_data, chexpert_data, split_reports, shuffle)
# main difference is that CLIP requires tokenization at this stage
def __getitem__(self, ind):
item = super().__getitem__(ind)
item["tokens"] = clip.tokenize(item["text"], truncate=True)[0]
return item
class ImageDataset(Dataset):
def __init__(
self,
data: str,
val_data=None,
image_size=224,
shuffle=False,
):
"""Create an image dataset from a csv file with image paths.
Args:
data (str): Path to a csv file with image paths and reports.
val_data (str, optional): Path to a csv file with image paths and reports.
image_size (int, optional): The size of outputted images. Defaults to 224.
shuffle (bool, optional): Whether or not to have shuffling behavior during sampling. Defaults to False.
"""
super().__init__()
self.shuffle = shuffle
self.data = pd.read_csv(data, index_col=0)[["study_id", "path"]]
print("Data len:", len(self.data))
# append the validation set to the train set
if val_data is not None:
val_data = pd.read_csv(val_data, index_col=0)[["study_id", "path"]]
self.data = pd.concat([self.data, val_data], ignore_index=True)
print("Len after adding val data:", len(self.data))
self.data = self.data.dropna().reset_index(drop=True)
print("After dropping nans:", len(self.data))
self.data = self.data.drop_duplicates().reset_index(drop=True)
print("After dropping duplicates:", len(self.data))
self.keys = list(self.data.index)
self.image_transform = T.Compose(
[
T.Lambda(self.fix_img),
T.RandomResizedCrop(image_size, scale=(0.75, 1.0), ratio=(1.0, 1.0)),
T.ToTensor(),
T.Normalize(
(0.485, 0.456, 0.406), (0.229, 0.224, 0.225)
) # ResNet50 values
]
)
def __len__(self):
return len(self.keys)
def __getitem__(self, ind):
key = self.keys[ind]
study_id = self.data.loc[key]["study_id"]
image_filename = self.data.loc[key]["path"]
try:
image_tensor = self.image_transform(PIL.Image.open(image_filename))
except (PIL.UnidentifiedImageError, OSError):
print(
f"An exception occurred trying to load file {image_filename} at index {ind}. Exiting..."
)
exit(1)
# return study_id, image_filename, image_tensor, text
item = {
"study_id": study_id,
"image_filename": image_filename,
"image_tensor": image_tensor,
}
# Success
return item
def fix_img(self, img):
return img.convert("RGB") if img.mode != "RGB" else img
# liberally borrowed from https://github.com/rom1504/clip-retrieval/blob/main/clip_retrieval/clip_inference/mapper.py
class CLIPModel:
def __init__(self, model_path, image_encoder, text_encoder, tokenizer=None):
"""transforms images and texts into clip embeddings"""
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using:", self.device)
self.model = CustomCLIPWrapper.load_from_checkpoint(
checkpoint_path=model_path,
image_encoder=image_encoder,
text_encoder=text_encoder,
).to(self.device)
self.tokenizer = tokenizer
def __call__(self, item, embed_type="image"):
self.model.eval()
with torch.no_grad():
study_id = item["study_id"]
if embed_type == "image":
target = item["image_filename"]
image_features = self.model.model.encode_image(
item["image_tensor"].to(self.device)
)
image_features /= image_features.norm(dim=-1, keepdim=True)
embs = image_features.cpu().numpy()
elif embed_type == "text":
target = item["text"]
if self.model.using_clip:
text_tokens = item["tokens"].to(self.device)
else:
text_tokens = self.tokenizer(
target,
padding=True,
truncation=True,
max_length=128,
return_tensors="pt",
).to(self.device)
text_features = self.model.encode_text(text_tokens)
text_features /= text_features.norm(dim=-1, keepdim=True)
embs = text_features.cpu().numpy()
else:
print("invalid embed_type -- choose image or text, exiting...")
exit(1)
return {"study_id": study_id, "target": target, "embs": embs}
def save_to_pickle(outs, save_to):
# saving them in a df
study_id = []
target = []
embs = []
for out in outs:
study_id += out["study_id"]
target += out["target"]
embs += list(out["embs"])
embeddings = pd.DataFrame({"study_id": study_id, "target": target, "embs": embs})
embeddings.to_pickle(save_to)
def save_embeddings(
model,
dataset,
save_to,
embed_type="image",
write_after=50000,
batch_size=1,
num_workers=0,
):
dl = DataLoader(
dataset, batch_size=batch_size, num_workers=num_workers, drop_last=False
)
# computing embeddings
outs = []
write_after = write_after
count = 0
unwritten = False
for batch in tqdm(dl):
unwritten = True
outs.append(model(batch, embed_type))
if len(outs) * batch_size >= write_after:
save_path = Path(os.path.join(save_to, f"{count}.pkl"))
save_to_pickle(outs, save_path)
outs = []
count += 1
unwritten = False
if unwritten:
save_path = Path(os.path.join(save_to, f"{count}.pkl"))
save_to_pickle(outs, save_path)
def main(args):
args = parser.parse_args(args)
embed_type = args.embed_type
save_as = args.save_as
data_path = args.data_path
val_path = args.val_path
chexpert_folder = args.chexpert_folder
write_after = args.write_after
batch_size = args.batch_size
num_workers = args.num_workers
with open(args.config_file) as f:
config = json.load(f)
for c in config["models"]:
using_clip = False
if c["image_encoder"] == "clip" or c["text_encoder"] == "clip":
using_clip = True
device = "cuda" if torch.cuda.is_available() else "cpu"
clp, _ = clip.load("RN50", device=device)
for p in clp.parameters():
p.data = p.data.float()
if p.grad:
p.grad.data = p.grad.data.float()
image_encoder = clp.visual
text_encoder = clp.transformer
model = CLIPModel(
model_path=c["model_path"],
image_encoder=image_encoder,
text_encoder=text_encoder,
)
else:
image_encoder, _ = init_img_model(c["image_encoder"], c["embed_dim"])
text_encoder, tokenizer = init_txt_model(
c["text_encoder"], c["embed_dim"], add_projection=c["add_projection"]
)
model = CLIPModel(
model_path=c["model_path"],
image_encoder=image_encoder,
text_encoder=text_encoder,
tokenizer=tokenizer,
)
name = c["image_encoder"][:3] + c["text_encoder"][:3]
embs_dir = Path(f"out_{name}")
embs_subdir = Path(f"{save_as}_{embed_type}")
save_to = Path(os.path.join(embs_dir, embs_subdir))
if not save_to.exists():
os.makedirs(save_to)
if embed_type == "image":
data = ImageDataset(data=data_path, val_data=val_path)
elif embed_type == "text":
if using_clip:
data = CLIPTextDataset(
data=data_path, val_data=val_path, chexpert_data=chexpert_folder
)
else:
data = TextDataset(
data=data_path, val_data=val_path, chexpert_data=chexpert_folder
)
else:
print("Please choose embed_type = (image | text), exiting...")
exit(1)
save_embeddings(
model,
data,
save_to=save_to,
embed_type=embed_type,
write_after=write_after,
batch_size=batch_size,
num_workers=num_workers,
)
if __name__ == "__main__":
main(sys.argv[1:])