-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
62 lines (46 loc) · 1.99 KB
/
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
from models import SBERT_base_Model, BERT_base_Model, BERT_base_NLI_Model
from datasets import KorSTSDatasets, Collate_fn, KorSTSDatasets_for_BERT, KorNLIDatasets
from utils import test_step
from torch.utils.data import DataLoader
import torch
import torch.nn as nn
import yaml
import argparse
from tqdm import tqdm
import numpy as np
import pandas as pd
Datasets = {"SBERT": KorSTSDatasets, "BERT": KorSTSDatasets_for_BERT, "BERT_NLI": KorNLIDatasets}
Models = {"SBERT": SBERT_base_Model, "BERT": BERT_base_Model, "BERT_NLI": BERT_base_NLI_Model}
def main(config):
device = torch.device("cuda") if torch.cuda.is_available else torch.device("cpu")
print("prepare datasets")
datasets = Datasets[config["model_type"]](config["test_csv"], config["base_model"])
collate_fn = Collate_fn(datasets.pad_id, config["model_type"])
data_loader = DataLoader(
datasets,
collate_fn=collate_fn,
batch_size=config["batch_size"]
)
print("load model...")
model = Models[config["model_type"]](config["base_model"], config["dropout_prob"])
model.load_state_dict(torch.load(config["model_load_path"]))
print("model loaded from", config["model_load_path"])
model.to(device)
model.eval()
preds = []
with torch.no_grad():
for data in tqdm(data_loader):
pred = test_step(data, config["model_type"], device, model)
pred = pred.to(torch.device("cpu")).detach().numpy().flatten()
preds += list(pred)
output = pd.read_csv("NLP_dataset/sample_submission.csv")
preds = [round(np.clip(p, 0, 5), 1) for p in preds]
output['target'] = preds
output.to_csv("output.csv", index=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Training SBERT.')
parser.add_argument("--conf", type=str, default="sbert_config.yaml", help="config file path(.yaml)")
args = parser.parse_args()
with open(args.conf, "r") as f:
config = yaml.load(f, Loader=yaml.Loader)
main(config)