-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_cls.py
134 lines (113 loc) · 5.68 KB
/
predict_cls.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import argparse
import json
import os
import numpy as np
import pandas as pd
import torch
from sklearn.metrics import accuracy_score
from tqdm import tqdm
from datasets import load_headerless_tsv
from model_pytorch import DoubleHeadModel, dotdict
from text_utils import TextEncoder
from train import transform_classification, predict
from utils import encode_dataset, np_softmax
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_file', required=True)
parser.add_argument('-o', '--output_file', required=True)
parser.add_argument('--n_batch', type=int, default=8)
parser.add_argument('--skip_preprocess', action='store_true')
parser.add_argument('--sentence_pair', action='store_true')
parser.add_argument('--force_delimiter', action='store_true')
parser.add_argument('--encoder_path', type=str, default='model/encoder_bpe_40000.json')
parser.add_argument('--bpe_path', type=str, default='model/vocab_40000.bpe')
parser.add_argument('--model_dir', required=True)
parser.add_argument('--mc_dropout_iter', type=int, default=0)
args = parser.parse_args()
meta = json.load(open(os.path.join(args.model_dir, 'meta.json'), 'r', encoding='utf8'))
text_encoder = TextEncoder(args.encoder_path, args.bpe_path)
encoder = text_encoder.encoder
n_vocab = len(text_encoder.encoder)
encoder['_start_'] = len(encoder)
if args.sentence_pair or args.force_delimiter:
encoder['_delimiter_'] = len(encoder)
encoder['_classify_'] = len(encoder)
clf_token = encoder['_classify_']
n_special = 2 + int('_delimiter_' in encoder)
n_ctx = meta['dh_model']['n_ctx']
max_len = meta['encoder']['max_len']
if args.sentence_pair:
max_len = min(max_len, n_ctx // 2 - 2)
texts, labels = load_headerless_tsv(args.input_file, sentence_pair=args.sentence_pair)
((X, Y),) = encode_dataset(*[(texts, labels)],
encoder=text_encoder,
skip_preprocess=args.skip_preprocess)
X, M = transform_classification(X, max_len, encoder['_start_'], clf_token,
n_vocab, n_special, n_ctx, encoder.get('_delimiter_'))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_gpu = torch.cuda.device_count()
n_batch_train = args.n_batch * max(n_gpu, 1)
meta['dh_model']['cfg'] = dotdict(meta['dh_model']['cfg'])
dh_model = DoubleHeadModel(**meta['dh_model'])
dh_model.to(device)
dh_model = torch.nn.DataParallel(dh_model)
path = os.path.join(args.model_dir, 'best_params')
if device == torch.device('cpu'):
map_location = lambda storage, loc: storage
else:
map_location = None
dh_model.load_state_dict(torch.load(path, map_location=map_location))
prediction_output = predict(X=X,
submission_dir=None,
filename=None,
pred_fn=lambda x: x,
label_decoder=None,
dh_model=dh_model,
n_batch_train=n_batch_train,
device=device)
predictions = np.argmax(prediction_output, axis=1)
if type(texts) is tuple:
df = pd.DataFrame({'question': texts[0], 'text': texts[1], 'label': labels, 'prediction': predictions})
else:
df = pd.DataFrame({'text': texts, 'label': labels, 'prediction': predictions})
df.to_csv(args.output_file,
index=False,
sep='\t',
header=False,
columns=['text', 'label', 'prediction'],
float_format='%.0f')
accuracy = accuracy_score(Y, predictions) * 100.
print('Accuracy: {}%'.format(accuracy))
basename = os.path.splitext(args.output_file)[0]
prediction_output_file = basename + '_output.npy'
np.savetxt(prediction_output_file, prediction_output)
prediction_probs = np_softmax(prediction_output)
prediction_probs_file = basename + '_probs.npy'
np.savetxt(prediction_probs_file, prediction_probs)
mc_dropout_prediction_output = []
for _ in tqdm(range(args.mc_dropout_iter)):
prediction_output = predict(X=X,
submission_dir=None,
filename=None,
pred_fn=lambda x: x,
label_decoder=None,
dh_model=dh_model,
n_batch_train=n_batch_train,
device=device,
enable_dropout=True)
mc_dropout_prediction_output.append(prediction_output)
if mc_dropout_prediction_output:
mc_dropout_prediction_output = np.asarray(mc_dropout_prediction_output)
mc_dropout_prediction_probs = np.zeros(mc_dropout_prediction_output.shape)
for i in range(mc_dropout_prediction_output.shape[0]):
mc_dropout_prediction_probs[i, ...] = np_softmax(mc_dropout_prediction_output[i, ...])
transpose_dims = (2, 1, 0)
mc_dropout_prediction_output = mc_dropout_prediction_output.transpose(transpose_dims)
mc_dropout_prediction_probs = mc_dropout_prediction_probs.transpose(transpose_dims)
for i in range(mc_dropout_prediction_output.shape[0]):
prediction_output_file = '{}_class{}_{}'.format(basename, i, 'output.npy')
np.savetxt(prediction_output_file, mc_dropout_prediction_output[i, ...])
prediction_probs_file = '{}_class{}_{}'.format(basename, i, 'probs.npy')
np.savetxt(prediction_probs_file, mc_dropout_prediction_probs[i, ...])
if __name__ == '__main__':
main()