-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_inference.py
More file actions
367 lines (305 loc) · 13.1 KB
/
main_inference.py
File metadata and controls
367 lines (305 loc) · 13.1 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
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
import argparse
import os
import random
import warnings
import lightning as L
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
import yaml
from joblib import Parallel, delayed
from lightning.pytorch.callbacks import (
EarlyStopping,
LearningRateMonitor,
ModelCheckpoint,
)
from lightning.pytorch.loggers import WandbLogger
from torch import nn
from torch.utils.data import DataLoader
from tqdm import tqdm
import wandb
from data.collate_fn import collate_fn, test_collate_fn
from data.dataset import EmbeddingDataset
from models.feat2loc_model import get_agg_model
from models.mlp import MLPClassifier
from utils.metrics import get_all_fold_metrics, get_all_metrics, get_mcc_threhsold
warnings.filterwarnings("ignore", category=UserWarning)
AGG_METHODS = [
"MaxPool",
"MeanPool",
"LightAttentionPool",
"MultiHeadAttentionPool",
]
PARAMETERS = [
"exp_name",
"category_level",
"metadata_file",
"clip_len",
"agg_method",
"loss",
"mlp_dropout",
]
LEVEL_CLASSES = yaml.safe_load(open("datasets/final/hierarchical_label_set.yaml"))
def set_random_seed(seed):
os.environ["PYTHONHASHSEED"] = str(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.set_float32_matmul_precision("high")
def get_embedding_dim(exp_name):
if exp_name == "ProtT5":
return 1024
elif exp_name == "ProtBert":
return 1024
elif exp_name == "ESM2":
return 2560
elif exp_name == "ESM3":
return 1536
elif "SubCell" in exp_name:
return 1536
elif "DINO" in exp_name:
return 768
else:
raise ValueError("Invalid exp_name")
def load_model_weights(model, model_path, prefix):
model_weights = torch.load(model_path, map_location="cpu", weights_only=False)
filtered_weights = {
k[len(prefix) + 1 :]: v
for k, v in model_weights["state_dict"].items()
if k.startswith(prefix)
}
op = model.load_state_dict(filtered_weights)
print(op, flush=True)
return model
def main(data_folder, exp_folder, embedding_folder, config):
exp_folder = (
exp_folder
+ "/"
+ config["exp_name"]
+ "_"
+ config["metadata_file"]
+ "/"
+ config["run_id"]
)
embedding_dim = get_embedding_dim(config["exp_name"])
categories = LEVEL_CLASSES[config["category_level"]]
n_categories = len(categories)
all_folds_thresholds = []
all_folds_preds = []
for ho_fold in range(5):
fold_exp_folder = f"{exp_folder}/fold_{ho_fold}"
if os.path.exists(f"{fold_exp_folder}/fold_{ho_fold}_val_predictions.csv"):
print(f"Loading predictions for fold {ho_fold}", flush=True)
val_preds_df = pd.read_csv(
f"{fold_exp_folder}/fold_{ho_fold}_val_predictions.csv"
)
val_fold_true = val_preds_df[[f"{cat}_true" for cat in categories]].values
val_fold_pred = F.sigmoid(
torch.from_numpy(
val_preds_df[[f"{cat}_pred" for cat in categories]].values
)
).numpy()
fold_thresholds = get_mcc_threhsold(val_fold_true, val_fold_pred)
all_folds_thresholds.append(fold_thresholds)
test_preds_df = pd.read_csv(
f"{fold_exp_folder}/fold_{ho_fold}_test_predictions.csv"
)
test_fold_true = test_preds_df[[f"{cat}_true" for cat in categories]].values
test_fold_pred = F.sigmoid(
torch.from_numpy(
test_preds_df[[f"{cat}_pred" for cat in categories]].values
)
).numpy()
all_folds_preds.append(test_fold_pred)
else:
exp_embedding_folder = (
f"{embedding_folder}/{config['exp_name']}-4k.h5"
if config["exp_name"] != "ESM3"
else f"{embedding_folder}/{config['exp_name']}-3k.h5"
)
print(f"Running inference for fold {ho_fold}", flush=True)
fold_model_path = f"{fold_exp_folder}/models/best_model_acc.ckpt"
valid_dataset = EmbeddingDataset(
exp_embedding_folder,
f"{data_folder}/{config['metadata_file']}.csv",
config["category_level"],
folds=[ho_fold],
clip_len=config["clip_len"],
# test_mode=True,
)
valid_loader = DataLoader(
valid_dataset, batch_size=64, shuffle=False, collate_fn=collate_fn
)
test_dataset = EmbeddingDataset(
exp_embedding_folder,
f"{data_folder}/hou_testset.csv",
config["category_level"],
clip_len=config["clip_len"],
test_mode=True,
)
test_loader = DataLoader(
test_dataset, batch_size=64, shuffle=False, collate_fn=test_collate_fn
)
agg_model = get_agg_model(
config["agg_method"], embedding_dim, config["clip_len"]
)
agg_model = load_model_weights(agg_model, fold_model_path, "model")
mlp_embedding_dim = (
embedding_dim * 2
if config["agg_method"] == "LightAttentionPool"
else embedding_dim
)
mlp_config = {
"input_dim": mlp_embedding_dim,
"num_classes": n_categories,
"hidden_dim": 512,
"num_hidden_layers": 2,
"dropout": 0.2,
}
mlp_model = MLPClassifier(**mlp_config)
mlp_model = load_model_weights(mlp_model, fold_model_path, "mlp_classifier")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mlp_model.to(device)
with torch.no_grad():
agg_model.eval()
mlp_model.eval()
fold_val_true = []
fold_val_pred = []
for i, batch in tqdm(enumerate(valid_loader), total=len(valid_loader)):
embeddings, masks, locations = batch
pooled_embedding, attention = agg_model(
embeddings.to(device), masks.to(device)
)
preds = mlp_model(pooled_embedding)
preds = torch.sigmoid(preds)
fold_val_true.append(locations.cpu().numpy())
fold_val_pred.append(preds.cpu().numpy())
fold_val_true = np.concatenate(fold_val_true, axis=0)
fold_val_pred = np.concatenate(fold_val_pred, axis=0)
val_preds_df = pd.DataFrame()
for i, cat in enumerate(categories):
val_preds_df[f"{cat}_true"] = fold_val_true[:, i]
val_preds_df[f"{cat}_pred"] = fold_val_pred[:, i]
val_preds_df.to_csv(
f"{fold_exp_folder}/val_fold_{ho_fold}_predictions.csv", index=False
)
fold_thresholds = get_mcc_threhsold(fold_val_true, fold_val_pred)
all_folds_thresholds.append(fold_thresholds)
test_fold_id = []
test_fold_seq = []
test_fold_true = []
test_fold_pred = []
test_fold_attn = []
for i, batch in tqdm(enumerate(test_loader), total=len(test_loader)):
id, seq, embedding, mask, locations = batch
pooled_embedding, attention = agg_model(
embedding.to(device), mask.to(device)
)
preds = mlp_model(pooled_embedding)
preds = torch.sigmoid(preds)
test_fold_id.extend(id)
test_fold_seq.extend(seq)
test_fold_true.append(locations)
test_fold_pred.append(preds)
test_fold_attn.append(attention)
test_fold_true = torch.cat(test_fold_true, dim=0).cpu().numpy()
test_fold_pred = torch.cat(test_fold_pred, dim=0).cpu().numpy()
test_fold_attn = torch.cat(test_fold_attn, dim=0)
all_folds_preds.append(test_fold_pred)
test_preds_df = pd.DataFrame(
{"id": test_fold_id, "sequence": test_fold_seq}
)
for i, cat in enumerate(categories):
test_preds_df[f"{cat}_true"] = test_fold_true[:, i]
test_preds_df[f"{cat}_pred"] = test_fold_pred[:, i]
test_preds_df.to_csv(
f"{fold_exp_folder}/test_fold_{ho_fold}_predictions.csv",
index=False,
)
torch.save(test_fold_attn, f"{fold_exp_folder}/fold_{ho_fold}_attention.pt")
test_fold_pred_bin = (test_fold_pred > fold_thresholds).astype(np.int16)
fold_metrics = get_all_metrics(
test_fold_true, test_fold_pred, test_fold_pred_bin, categories=categories
)
overall_metrics = {k: v for k, v in fold_metrics.items() if "perclass" not in k}
overall_metrics_df = pd.DataFrame.from_dict(overall_metrics, orient="index").T
overall_metrics_df.to_csv(
f"{exp_folder}/fold_{ho_fold}_overall_metrics.csv", index=False
)
perclass_metrics = {k: v for k, v in fold_metrics.items() if "perclass" in k}
perclass_metrics_df = pd.DataFrame.from_dict(perclass_metrics)
perclass_metrics_df["category"] = perclass_metrics["category_perclass"]
perclass_metrics_df.to_csv(
f"{exp_folder}/fold_{ho_fold}_perclass_metrics.csv", index=False
)
np.save(f"{exp_folder}/all_thresholds.npy", np.array(all_folds_thresholds))
all_fold_metrics = get_all_fold_metrics(
test_fold_true, all_folds_preds, all_folds_thresholds, categories
)
overall_metrics = {k: v for k, v in all_fold_metrics.items() if "perclass" not in k}
overall_metrics_df = pd.DataFrame.from_dict(overall_metrics, orient="index").T
overall_metrics_df.to_csv(
f"{exp_folder}/all_folds_overall_metrics.csv", index=False
)
perclass_metrics = {k: v for k, v in all_fold_metrics.items() if "perclass" in k}
perclass_metrics_df = pd.DataFrame.from_dict(perclass_metrics)
perclass_metrics_df = perclass_metrics_df.rename(
columns={k: k.replace("_perclass", "") for k in perclass_metrics_df.columns}
)
perclass_metrics_df.to_csv(
f"{exp_folder}/all_folds_perclass_metrics.csv", index=False
)
return overall_metrics
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run inference on sweep configs.")
parser.add_argument('--data_folder', type=str, required=True, help='Path to data folder')
parser.add_argument('--exp_folder', type=str, required=True, help='Path to experiment folder')
parser.add_argument('--embedding_folder', type=str, required=True, help='Path to embedding folder')
parser.add_argument('--save_folder', type=str, required=True, help='Path to save results (metrics/configs)')
args = parser.parse_args()
data_folder = args.data_folder
exp_folder = args.exp_folder
embedding_folder = args.embedding_folder
save_folder = args.save_folder
if os.path.exists(f"{save_folder}/sweep_config.csv"):
df = pd.read_csv(f"{save_folder}/sweep_config.csv")
else:
api = wandb.Api(timeout=29)
entity = api.default_entity
runs = api.runs(f"{entity}/seq2loc_sweep")
all_run_config = []
for i, run in tqdm(enumerate(runs), total=len(runs)):
if run.State != "finished":
print(f"{run.id} didn't finish. skipping...")
continue
if run.config["agg_method"] not in AGG_METHODS:
continue
run_config = run.config
run_config = {k: v for k, v in run_config.items() if k in PARAMETERS}
run_config["metadata_file"] = os.path.basename(
run_config["metadata_file"]
).split(".")[0]
run_config["run_id"] = run.id
all_run_config.append(run_config)
df = pd.DataFrame.from_dict(all_run_config)
df.to_csv(f"{save_folder}/sweep_config.csv", index=False)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.set_default_device(device)
all_metrics = []
def process_row(row):
config = row[PARAMETERS + ["run_id"]].to_dict()
print(f"Running configuration:", {k: v for k, v in config.items()}, flush=True)
row_metrics = main(data_folder, exp_folder, embedding_folder, config)
config.update(row_metrics)
return pd.DataFrame.from_dict(config, orient="index").T
num_jobs = -1 # Number of parallel jobs
results = Parallel(n_jobs=num_jobs)(
delayed(process_row)(row) for _, row in tqdm(df.iterrows(), total=len(df))
)
all_metrics_df = pd.concat(results, ignore_index=True)
print(all_metrics_df, flush=True)
all_metrics_df.to_csv(f"{save_folder}/overall_metrics.csv", index=False)