-
Notifications
You must be signed in to change notification settings - Fork 408
/
eval.py
117 lines (103 loc) · 4.76 KB
/
eval.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# !/usr/bin/env python
# coding:utf-8
"""
Tencent is pleased to support the open source community by making NeuralClassifier available.
Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for thespecific language governing permissions and limitations under
the License.
"""
import sys
import time
import torch
from torch.utils.data import DataLoader
import util
from config import Config
from dataset.classification_dataset import ClassificationDataset
from dataset.collator import ClassificationCollator
from dataset.collator import ClassificationType
from dataset.collator import FastTextCollator
from evaluate.classification_evaluate import \
ClassificationEvaluator as cEvaluator
from model.classification.drnn import DRNN
from model.classification.fasttext import FastText
from model.classification.textcnn import TextCNN
from model.classification.textvdcnn import TextVDCNN
from model.classification.textrnn import TextRNN
from model.classification.textrcnn import TextRCNN
from model.classification.transformer import Transformer
from model.classification.dpcnn import DPCNN
from model.classification.attentive_convolution import AttentiveConvNet
from model.classification.region_embedding import RegionEmbedding
from model.classification.hmcn import HMCN
from model.model_util import get_optimizer, get_hierar_relations
from util import ModeType
ClassificationDataset, ClassificationCollator, FastTextCollator, cEvaluator,
FastText, TextCNN, TextRNN, TextRCNN, DRNN, TextVDCNN, Transformer, DPCNN,
AttentiveConvNet, RegionEmbedding
def get_classification_model(model_name, dataset, conf):
model = globals()[model_name](dataset, conf)
model = model.cuda(conf.device) if conf.device.startswith("cuda") else model
return model
def load_checkpoint(file_name, conf, model, optimizer):
checkpoint = torch.load(file_name)
conf.train.start_epoch = checkpoint["epoch"]
model.load_state_dict(checkpoint["state_dict"])
optimizer.load_state_dict(checkpoint["optimizer"])
def eval(conf):
logger = util.Logger(conf)
model_name = conf.model_name
dataset_name = "ClassificationDataset"
collate_name = "FastTextCollator" if model_name == "FastText" \
else "ClassificationCollator"
test_dataset = globals()[dataset_name](conf, conf.data.test_json_files)
collate_fn = globals()[collate_name](conf, len(test_dataset.label_map))
test_data_loader = DataLoader(
test_dataset, batch_size=conf.eval.batch_size, shuffle=False,
num_workers=conf.data.num_worker, collate_fn=collate_fn,
pin_memory=True)
empty_dataset = globals()[dataset_name](conf, [])
model = get_classification_model(model_name, empty_dataset, conf)
optimizer = get_optimizer(conf, model)
load_checkpoint(conf.eval.model_dir, conf, model, optimizer)
model.eval()
is_multi = False
if conf.task_info.label_type == ClassificationType.MULTI_LABEL:
is_multi = True
predict_probs = []
standard_labels = []
evaluator = cEvaluator(conf.eval.dir)
for batch in test_data_loader:
if model_name == "HMCN":
(global_logits, local_logits, logits) = model(batch)
else:
logits = model(batch)
if not is_multi:
result = torch.nn.functional.softmax(logits, dim=1).cpu().tolist()
else:
result = torch.sigmoid(logits).cpu().tolist()
predict_probs.extend(result)
standard_labels.extend(batch[ClassificationDataset.DOC_LABEL_LIST])
(_, precision_list, recall_list, fscore_list, right_list,
predict_list, standard_list) = \
evaluator.evaluate(
predict_probs, standard_label_ids=standard_labels, label_map=empty_dataset.label_map,
threshold=conf.eval.threshold, top_k=conf.eval.top_k,
is_flat=conf.eval.is_flat, is_multi=is_multi)
logger.warn(
"Performance is precision: %f, "
"recall: %f, fscore: %f, right: %d, predict: %d, standard: %d." % (
precision_list[0][cEvaluator.MICRO_AVERAGE],
recall_list[0][cEvaluator.MICRO_AVERAGE],
fscore_list[0][cEvaluator.MICRO_AVERAGE],
right_list[0][cEvaluator.MICRO_AVERAGE],
predict_list[0][cEvaluator.MICRO_AVERAGE],
standard_list[0][cEvaluator.MICRO_AVERAGE]))
evaluator.save()
if __name__ == '__main__':
config = Config(config_file=sys.argv[1])
eval(config)