-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_inference.py
86 lines (68 loc) · 2.28 KB
/
test_inference.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
import os
import sys
import yaml
import numpy as np
import pandas as pd
from tqdm import tqdm
import torch
import torch.nn as nn
from iteration import step
from utils.dataloader import get_testloader
from utils.load_checkpoint import load_checkpoint
from transformer.model import TransformerClassifier
with open("./config.yaml") as file:
config = yaml.safe_load(file)
if not config["model"]["ckpt"]:
print("Checkpoint is needed!!!")
sys.exit(0)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_dir = os.path.join(
config["model"]["model_loc"],
config["dataset"]["file_name"],
config["model"]["model"].split("/")[-1],
)
_model = TransformerClassifier(
config["model"]["model"],
hidden_states=config["hyperparameters"]["hidden_layers"],
dropout=config["hyperparameters"]["dropout"],
).to(device)
print(config["model"]["ckpt"])
_model, _, _, best_weighted_f1, _ = load_checkpoint(
config["model"]["ckpt"],
config["model"]["model"],
model_dir,
device,
_model,
None,
None,
)
print(f"Model with wted F1 = {best_weighted_f1} is loaded!!!")
test_loader = get_testloader(
config["dataset"]["data_dir"],
config["dataset"]["file_name"],
config["model"]["model"],
config["inference"]["batch_size"],
config["hyperparameters"]["max_len"],
)
_model.eval()
y_preds = np.array([])
probs = np.array([])
with torch.set_grad_enabled(False):
with tqdm(test_loader, desc="") as vepoch:
for batch_idx, batch in enumerate(vepoch):
ypred, probabilities = step(_model, batch, None, device, True)
ypred = ypred.cpu().numpy()
probabilities = probabilities.cpu().numpy()
y_preds = np.hstack((y_preds, ypred))
probs = np.hstack((probs, probabilities))
test_path = os.path.join(config["dataset"]["data_dir"], config["dataset"]["file_name"])
test = pd.read_csv(test_path, sep="\t", header=None)
if test.loc[0][0] == "Id" or test.loc[0][0] == "ID":
test = test.drop([0])
test = test.reset_index(drop=True)
test.columns = ["id", "text"]
# test['label'] = np.where(y_preds==0, 'Not', 'Off').astype(str)
test["label"] = y_preds
test["probability"] = probs
test.to_csv(f"{model_dir}/_test.csv", sep="\t", index=False)
print(f"Results stores in {model_dir}/_test.tsv")