-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
160 lines (126 loc) · 4.85 KB
/
validate.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
import os
import argparse
import json
import pickle
from tqdm import tqdm
import time
import torch
import numpy as np
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn.functional as F
import logging
from torch.utils.data import DataLoader
import math
from datasets.recodata import RecoData
from datasets.config import ModelConfig
from gather import gather as gather_all
from models.gerl import GERL
from utils.log_util import convert_omegaconf_to_dict
from utils.train_util import set_seed
from utils.train_util import save_checkpoint_by_epoch
from utils.eval_util import group_labels
from utils.eval_util import cal_metric
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
def run(cfg, rank, test_dataset_file, device, model, news_title):
set_seed(7)
model.to(device)
model.eval()
test_dataset = RecoData(cfg.mc, test_dataset_file, news_title)
valid_data_loader = DataLoader(test_dataset, batch_size=cfg.batch_size, shuffle=False)
if ((cfg.gpus < 2) or (cfg.gpus > 1 and rank == 0)):
data_iter = tqdm(enumerate(valid_data_loader),
desc="EP_test:%d" % 1,
total=len(valid_data_loader),
bar_format="{l_bar}{r_bar}")
else:
data_iter = enumerate(valid_data_loader)
with torch.no_grad():
preds, truths, imp_ids = list(), list(), list()
for i, data in data_iter:
imp_ids += data[:, 0].cpu().numpy().tolist()
data = data.to(device)
# 1. Forward
pred = model(data[:, 2:], test_mode=True)
if pred.dim() > 1:
pred = pred.squeeze()
try:
preds += pred.cpu().numpy().tolist()
except:
print(data.size())
preds.append(int(pred.cpu().numpy()))
truths += data[:, 1].long().cpu().numpy().tolist()
tmp_dict = {}
tmp_dict['imp'] = imp_ids
tmp_dict['labels'] = truths
tmp_dict['preds'] = preds
with open(cfg.result_path + 'tmp_small_{}.json'.format(rank), 'w', encoding='utf-8') as f:
json.dump(tmp_dict, f)
def gather(cfg, turn, validate=False):
output_path = cfg.result_path
filenum = cfg.gpus
preds = []
labels = []
imp_indexes = []
for i in range(filenum):
with open(output_path + 'tmp_small_{}.json'.format(i), 'r', encoding='utf-8') as f:
cur_result = json.load(f)
imp_indexes += cur_result['imp']
labels += cur_result['labels']
preds += cur_result['preds']
tmp_dict = {}
tmp_dict['imp'] = imp_indexes
tmp_dict['labels'] = labels
tmp_dict['preds'] = preds
with open(cfg.result_path + 'tmp_{}.json'.format(turn), 'w', encoding='utf-8') as f:
json.dump(tmp_dict, f)
def split_dataset(dataset, gpu_count):
sub_len = math.ceil(len(dataset) / gpu_count)
data_list = []
for i in range(gpu_count):
s = i * sub_len
e = (i + 1) * sub_len
data_list.append(dataset[s: e])
return data_list
def main(cfg):
set_seed(7)
file_num = cfg.filenum
cfg.result_path = './result/'
print('load config')
model_cfg = ModelConfig()
cfg.mc = model_cfg
print('load news info')
news_title = np.load('data/news_info.npy')
model = GERL(model_cfg)
saved_model_path = os.path.join('./checkpoint/', 'model.ep{0}'.format(cfg.epoch))
print("Load from:", saved_model_path)
if not os.path.exists(saved_model_path):
print("Not Exist: {}".format(saved_model_path))
return []
model.cpu()
pretrained_model = torch.load(saved_model_path, map_location='cpu')
print(model.load_state_dict(pretrained_model, strict=False))
for point_num in range(file_num):
print("processing data/raw/test-{}.npy".format(point_num))
valid_dataset = np.load("data/raw/test-{}.npy".format(point_num))
dataset_list = split_dataset(valid_dataset, cfg.gpus)
processes = []
for rank in range(cfg.gpus):
cur_device = torch.device("cuda:{}".format(rank))
p = mp.Process(target=run, args=(cfg, rank, dataset_list[rank], cur_device, model, news_title))
p.start()
processes.append(p)
for p in processes:
p.join()
gather(cfg, point_num)
gather_all(cfg.result_path, file_num, validate=False, save=True)
if __name__ == '__main__':
mp.set_start_method('spawn')
parser = argparse.ArgumentParser()
parser.add_argument('--filenum', type=int, default=40)
parser.add_argument('--batch_size', type=int, default=128, help='input batch size')
parser.add_argument('--gpus', type=int, default=2, help='gpu_num')
parser.add_argument('--epoch', type=int, default=0, help='the number of epochs load checkpoint')
opt = parser.parse_args()
logging.warning(opt)
main(opt)