-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
249 lines (190 loc) · 8.22 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
学習モデルを用いて推論を行う
"""
import os
from os.path import isdir
import logging
import argparse
from os.path import isdir
from config import common_args, Parameters
from model_BERT import Classifier
from model_BERT_Conv import Classifier_Conv
from model_RoBERTa import Classifier_RoBERTa
from preprocess import make_dataset, make_dataset_roberta
from utils import dump_params, setup_params, get_device
from utils import set_logging
import numpy as np
import pandas as pd
import torch
from tqdm import tqdm
from transformers import AdamW, AutoModel, AutoTokenizer
from bs4 import BeautifulSoup
import shutil
logger = logging.getLogger(__name__)
def predict(params, cv, result_dir) -> None:
"""推論の実行
学習モデルから推論を行う
Args:
params: パラメータ
cv: 平均f1スコア
result_dir: 保存親ディレクトリ
Returns:
None
Examples:
関数の使い方
Raises:
例外の名前: 例外の説明
Yields:
戻り値の型: 戻り値についての説明
Note:
注意事項
"""
logger.info('inference test data...')
models = []
if '/' in params.model_name:
model_name_dir = params.model_name.split('/')[1] # model_nameに/が含まれていることがあるため、/以降のみを使う
else:
model_name_dir = params.model_name
for fold in range(params.num_split):
if params.use_cnn:
model = Classifier_Conv(params.model_name, num_classes=params.num_classes)
else:
model = Classifier(params.model_name, num_classes=params.num_classes)
model.load_state_dict(torch.load("./" + result_dir + "/" + params.models_dir + f"best_{model_name_dir}_{fold}.pth"))
model.to(params.device)
model.eval()
models.append(model)
tokenizer = AutoTokenizer.from_pretrained(params.model_name)
# テストデータ読み込み
test_df = pd.read_csv(params.test_file_path)
# 不要タグの削除-クリーニング
test_df['description'] = test_df['description'].apply(lambda x: BeautifulSoup(x, 'html.parser').get_text().lstrip())
# test_df = test_df.drop('index', axis=1)
test_df["labels"] = -1
test_dataset = make_dataset(params, test_df, tokenizer)
test_dataloader = torch.utils.data.DataLoader(
test_dataset, batch_size=params.valid_batch_size, shuffle=False)
with torch.no_grad():
progress = tqdm(test_dataloader, total=len(test_dataloader))
final_output = []
for batch in progress:
progress.set_description("<Test>")
attention_mask, input_ids, labels, token_type_ids = batch.values()
outputs = []
for model in models:
output = model(input_ids, attention_mask, token_type_ids)
outputs.append(output)
outputs = sum(outputs) / len(outputs)
outputs = torch.softmax(outputs, dim=1).cpu().detach().tolist()
outputs = np.argmax(outputs, axis=1)
final_output.extend(outputs)
submit = pd.read_csv(os.path.join(params.submit_sample_file_path), names=["id", "labels"])
submit["labels"] = final_output
submit["labels"] = submit["labels"] + 1
try:
submit.to_csv("./" + result_dir + "/output/submission_cv{}.csv".format(str(cv).replace(".", "")[:10]), index=False, header=False)
except NameError:
submit.to_csv("./" + result_dir + "/output/submission.csv", index=False, header=False)
logger.info('Inference complete!')
def predict_roberta(params, cv, result_dir) -> None:
"""推論の実行
学習モデルから推論を行う
Args:
params: パラメータ
cv: 平均f1スコア
result_dir: 保存親ディレクトリ
Returns:
None
Examples:
関数の使い方
Raises:
例外の名前: 例外の説明
Yields:
戻り値の型: 戻り値についての説明
Note:
注意事項
"""
logger.info('inference test data...')
models = []
if '/' in params.model_name:
model_name_dir = params.model_name.split('/')[1] # model_nameに/が含まれていることがあるため、/以降のみを使う
else:
model_name_dir = params.model_name
for fold in range(params.num_split):
model = Classifier_RoBERTa(params.model_name)
model.load_state_dict(torch.load("./" + result_dir + "/" + params.models_dir + f"best_{model_name_dir}_{fold}.pth"))
model.to(params.device)
model.eval()
models.append(model)
tokenizer = AutoTokenizer.from_pretrained(params.model_name)
# テストデータ読み込み
test_df = pd.read_csv(params.test_file_path)
# 不要タグの削除-クリーニング
test_df['description'] = test_df['description'].apply(lambda x: BeautifulSoup(x, 'html.parser').get_text().lstrip())
# test_df = test_df.drop('index', axis=1)
test_df["labels"] = -1
test_dataset = make_dataset_roberta(params, test_df, tokenizer)
test_dataloader = torch.utils.data.DataLoader(
test_dataset, batch_size=params.valid_batch_size, shuffle=False)
with torch.no_grad():
progress = tqdm(test_dataloader, total=len(test_dataloader))
final_output = []
for batch in progress:
progress.set_description("<Test>")
attention_mask, input_ids, labels = batch.values()
outputs = []
for model in models:
output = model(input_ids, attention_mask)
outputs.append(output)
outputs = sum(outputs) / len(outputs)
outputs = torch.softmax(outputs, dim=1).cpu().detach().tolist()
outputs = np.argmax(outputs, axis=1)
final_output.extend(outputs)
submit = pd.read_csv(os.path.join(params.submit_sample_file_path), names=["id", "labels"])
submit["labels"] = final_output
submit["labels"] = submit["labels"] + 1
try:
submit.to_csv("./" + result_dir + "/output/submission_cv{}.csv".format(str(cv).replace(".", "")[:10]), index=False, header=False)
except NameError:
submit.to_csv("./" + result_dir + "/output/submission.csv", index=False, header=False)
logger.info('Inference complete!')
if __name__ == "__main__":
# import doctest
# doctest.testmod()
# コマンドライン引数の設定
parser = argparse.ArgumentParser()
parser = common_args(parser) # コマンドライン引数引数を読み込み
# parser.add_argument("--main") # 実行スクリプト固有のコマンドライン引数があればここに記入する.
args = parser.parse_args()
params = Parameters(**setup_params(vars(args), args.parameters)) # args,run_date,git_revisionなどを追加した辞書を取得
vars(params).update({'device': str(get_device())}) # 空きGPU検索
# 結果出力用ファイルの作成
result_dir = params.args['load_model'] # 結果出力ディレクトリ
# os.mkdir(result_dir) # 実行日時を名前とするディレクトリを作成
if isdir(result_dir + "/output"):
shutil.rmtree(result_dir + "/output")
os.mkdir(result_dir + "/output")
if isdir(result_dir + "/inference"):
shutil.rmtree(result_dir + "/inference")
os.mkdir(result_dir + "/inference")
dump_params(params, f'{result_dir}' + "/inference") # パラメータを出力
cv = 0
if '/' in params.model_name:
model_name_dir = params.model_name.split('/')[1] # model_nameに/が含まれていることがあるため、/以降のみを使う
else:
model_name_dir = params.model_name
with open(f"./{result_dir}/{model_name_dir}_result.txt", mode='r') as f:
lines = f.readlines()
s_lines = [line.strip() for line in lines]
for line in s_lines:
if 'CV' in line:
cv = float(line.split(':')[1].strip())
# ログ設定
logger = logging.getLogger(__name__)
set_logging(result_dir + "/inference") # ログを標準出力とファイルに出力するよう設定
logger.info('parameters: ')
logger.info(params)
if params.args['roberta']:
predict_roberta(params, cv, result_dir)
else:
predict(params, cv, result_dir)