File size: 4,614 Bytes
4321e7e |
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 |
import gc
import os
import sys
import argparse
import pandas as pd
import torch
from torch.utils.data import DataLoader
from transformers import AutoTokenizer
sys.path.append(".")
from utils import seed_everything
from models import PLTNUM
from datasets import PLTNUMDataset
def parse_args():
parser = argparse.ArgumentParser(
description="Prediction script for protein sequence classification/regression."
)
parser.add_argument(
"--data_path",
type=str,
required=True,
help="Path to the input data.",
)
parser.add_argument(
"--model",
type=str,
default="westlake-repl/SaProt_650M_AF2",
help="Pretrained model name or path.",
)
parser.add_argument(
"--architecture",
type=str,
default="SaProt",
help="Model architecture: 'ESM2', 'SaProt', or 'LSTM'.",
)
parser.add_argument(
"--model_path",
type=str,
required=True,
help="Path to the model for prediction.",
)
parser.add_argument("--batch_size", type=int, default=4, help="Batch size.")
parser.add_argument(
"--seed",
type=int,
default=42,
help="Seed for reproducibility.",
)
parser.add_argument(
"--use_amp",
action="store_true",
default=False,
help="Use AMP for mixed precision prediction.",
)
parser.add_argument(
"--num_workers",
type=int,
default=4,
help="Number of workers for data loading.",
)
parser.add_argument(
"--max_length",
type=int,
default=512,
help="Maximum input sequence length. Two tokens are used fo <cls> and <eos> tokens. So the actual length of input sequence is max_length - 2. Padding or truncation is applied to make the length of input sequence equal to max_length.",
)
parser.add_argument(
"--used_sequence",
type=str,
default="left",
help="Which part of the sequence to use: 'left', 'right', 'both', or 'internal'.",
)
parser.add_argument(
"--padding_side",
type=str,
default="right",
help="Padding side: 'right' or 'left'.",
)
parser.add_argument(
"--output_dir",
type=str,
default="./output",
help="Output directory.",
)
parser.add_argument(
"--task",
type=str,
default="classification",
help="Task type: 'classification' or 'regression'.",
)
parser.add_argument(
"--sequence_col",
type=str,
default="aa_foldseek",
help="Column name fot the input sequence.",
)
return parser.parse_args()
def predict_fn(valid_loader, model, cfg):
model.eval()
predictions = []
for inputs, _ in valid_loader:
inputs = inputs.to(cfg.device)
with torch.no_grad():
with torch.cuda.amp.autocast(enabled=cfg.use_amp):
preds = (
torch.sigmoid(model(inputs))
if cfg.task == "classification"
else model(inputs)
)
predictions += preds.cpu().tolist()
return predictions
def predict(folds, model_path, cfg):
dataset = PLTNUMDataset(cfg, folds, train=False)
loader = DataLoader(
dataset,
batch_size=cfg.batch_size,
shuffle=False,
num_workers=cfg.num_workers,
pin_memory=True,
drop_last=False,
)
model = PLTNUM(cfg)
model.load_state_dict(torch.load(model_path, map_location=cfg.device))
model.to(cfg.device)
predictions = predict_fn(loader, model, cfg)
folds["raw prediction values"] = predictions
if cfg.task == "classification":
folds["binary prediction values"] = [1 if x > 0.5 else 0 for x in predictions]
torch.cuda.empty_cache()
gc.collect()
return folds
if __name__ == "__main__":
config = parse_args()
config.token_length = 2 if config.architecture == "SaProt" else 1
config.device = "cuda" if torch.cuda.is_available() else "cpu"
if not os.path.exists(config.output_dir):
os.makedirs(config.output_dir)
if config.used_sequence == "both":
config.max_length += 1
seed_everything(config.seed)
df = pd.read_csv(config.data_path)
tokenizer = AutoTokenizer.from_pretrained(
config.model, padding_side=config.padding_side
)
config.tokenizer = tokenizer
result = predict(df, config.model_path, config)
result.to_csv(os.path.join(config.output_dir, "result.csv"), index=False)
|