-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
95 lines (75 loc) · 2.36 KB
/
test.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
import argparse
import json
import os
import pandas as pd
import torch
import wandb
from pytorch_lightning import Trainer
from transformers import AutoModel, AutoTokenizer
from data_loader.data_loaders import TextDataLoader
from model.model import STSModel
from utils.preprocessing import apply_preprocess
def main(arg):
## data reading
test_dir = os.path.join(arg.data_dir, "test.csv")
submission_dir = os.path.join(arg.data_dir, "sample_submission.csv")
test = pd.read_csv(test_dir)
submission = pd.read_csv(submission_dir)
## model/config loading
wandb.login()
run = wandb.init()
artifact = run.use_artifact(arg.model_path)
model_dir = artifact.download()
with open(f"{model_dir}/config.json", "r") as f:
config = json.load(f)
tokenizer = AutoTokenizer.from_pretrained(config["MODEL_NAME"])
model = AutoModel.from_pretrained(config["MODEL_NAME"])
tokens = "<PERSON>"
tokenizer.add_tokens(tokens)
model.resize_token_embeddings(len(tokenizer))
model = STSModel(config, model)
model.load_state_dict(
torch.load(f"{model_dir}/{arg.model_name}.ckpt")["state_dict"]
)
## processing
preprocess = False
test = apply_preprocess(test, arg.data_dir, "preprocessed_test.csv", preprocess)
test = test.dropna(subset=["sentence_1", "sentence_2"])
test = test.reset_index(drop=True)
dataloader = TextDataLoader(
tokenizer=tokenizer,
max_len=config["MAX_LEN"],
predict_data=test,
truncation=True,
)
trainer = Trainer(accelerator="gpu")
preds = trainer.predict(model, dataloader)
all_pred = [val for pred in preds for val in pred]
submission["target"] = all_pred
print(submission.head())
submission.to_csv("data/submission.csv", index=False)
if __name__ == "__main__":
args = argparse.ArgumentParser()
args.add_argument(
"-d",
"--data_dir",
default=None,
type=str,
help="directory path for data (default: None)",
)
args.add_argument(
"-m",
"--model_path",
default=None,
type=str,
help="artifact path for a model (default: all)",
)
args.add_argument(
"-n",
"--model_name",
default=None,
type=str,
help="name of the model to call (default: all)",
)
arg = args.parse_args()
main(arg)