-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbest_by_epoch.py
234 lines (185 loc) · 5.93 KB
/
best_by_epoch.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
from __future__ import print_function
import os
import yaml
import sys
import re
import glob
import pandas as pd
import subprocess
import pickle
import rouge
import bleu
global config
from multiprocessing import Pool
config_file = sys.argv[1]
def get_number_(s):
m = re.search("\d+(.)\d+",s)
return m.group(0)
def get_result_dir_name(dataset,epoch_num):
dirname = os.path.join(config['log_root'], config['exp_name'],"decode_%s_%imaxenc_%ibeam_%imindec_%imaxdec" % (dataset, config['max_enc_steps'], 1, config['min_dec_steps'], config['max_dec_steps']))
ckpt_name = "ckpt-" + str(epoch_num)
dirname += "_%s" % ckpt_name
return dirname
def get_metrics(f1,f2):
ref = []
decoded = []
count = 0
print(f1)
print(f2)
for i, j in zip(sorted(glob.glob(f1)),sorted(glob.glob(f2))):
ref_tex = ''
dec_tex = ''
for k in open(i).readlines():
dec_tex = dec_tex + k.strip()
for l in open(j).readlines():
ref_tex = ref_tex + l.strip()
ref.append(ref_tex)
decoded.append(dec_tex)
count = count + 1
print(len(decoded))
print(len(ref))
x = rouge.rouge(decoded,ref)
bl = bleu.moses_multi_bleu(decoded,ref) #replace by pycoco bleu
return 0,0,0, bl
#return x['rouge_1/f_score']*100,x['rouge_2/f_score']*100,x['rouge_l/f_score']*100, bl
def run_test(c):
os.system('python run_summarization.py --mode=decode --use_val_as_test=True --test_by_epoch=True --epoch_num ' + str(c) + ' --config_file ' + str(config_file))
def multi_test(epoch):
pool = Pool(processes=5)
res1 = pool.apply_async(run_test,[epoch[0]])
res2 = pool.apply_async(run_test,[epoch[1]])
res3 = pool.apply_async(run_test,[epoch[2]])
res4 = pool.apply_async(run_test,[epoch[3]])
res5 = pool.apply_async(run_test,[epoch[4]])
pool.close()
pool.join()
with open(config_file) as f:
config = yaml.load(f)
train_path = os.path.join(config['log_root'], config['exp_name'], "train")
file_list = glob.glob(train_path+'/*.index')
print(file_list)
count = []
for i in sorted(file_list):
m = re.search('\d+(.)index',i)
s = m.group(0)
m = re.search('\d+',s)
count.append(int(m.group(0)))
k = len(file_list)
p = 0
#print(count)
for i in range(5,k+5,5):
multi_test(count[p:i])
p = i
best_bleu = 0.0
rouge_1 = []
rouge_2 = []
rouge_l = []
bleu_4 = []
best_rouge_epoch = 1000
#print(file_list)
#print(count)
for k,c in enumerate(count):
w = get_result_dir_name('val',c)
try:
#print(w)
r1,r2,rl,bl = get_metrics(os.path.join(w,'decoded','*.txt'), os.path.join(w,'reference','*.txt'))
rouge_1.append(r1)
rouge_2.append(r2)
rouge_l.append(rl)
bleu_4.append(bl)
if bl > best_bleu:
best_bleu = bl
best_rouge_epoch = c
d = {'epoch':count[:k+1],'rouge_1':rouge_1, 'rouge_2':rouge_2, 'rouge_l': rouge_l,'bleu_4':bleu_4}
df = pd.DataFrame(d)
df.to_csv(config['exp_name']+'.csv',index=False)
except:
pass
print(k)
print(best_rouge_epoch)
os.system('python run_summarization.py --mode=decode --test_by_epoch=True --epoch_num ' + str(best_rouge_epoch) + ' --config_file ' + str(config_file))
'''
def get_number_(s):
m = re.search("\d+(.)\d+",s)
return m.group(0)
def get_rouge(results_path):
f = open(results_path,'r')
s = ''
for i in f.readlines():
s = s+ i
w = s.split('\n')
r1 = float(get_number_(w[2])) * 100
r2 = float(get_number_(w[7])) * 100
rl = float(get_number_(w[12])) * 100
return r1,r2,rl
def get_result_dir_name(dataset,epoch_num):
dirname = os.path.join(config['log_root'], config['exp_name'],"decode_%s_%imaxenc_%ibeam_%imindec_%imaxdec" % (dataset, config['max_enc_steps'], config['beam_size'], config['min_dec_steps'], config['max_dec_steps']))
ckpt_name = "ckpt-" + str(epoch_num)
dirname += "_%s" % ckpt_name
return dirname
def get_metrics(f1,f2):
ref = []
decoded = []
count = 0
for i, j in zip(sorted(glob.glob(f1)),sorted(glob.glob(f2))):
ref_tex = ''
dec_tex = ''
for k in open(i).readlines():
dec_tex = dec_tex + k.strip()
for l in open(j).readlines():
ref_tex = ref_tex + l.strip()
ref.append(ref_tex)
decoded.append(dec_tex)
count = count + 1
x = rouge.rouge(decoded,ref)
bl = bleu.moses_multi_bleu(decoded,ref)
return x['rouge_1/f_score']*100,x['rouge_2/f_score']*100,x['rouge_l/f_score']*100, bl
with open(config_file) as f:
config = yaml.load(f)
other_f = open(config['exp_name']+'.txt', 'w')
#copy graph
train_path = os.path.join(config['log_root'], config['exp_name'], "train")
#epoch_path = os.path.join(config['log_root'], config['exp_name'], "epoch")
train_projector = os.path.join(train_path, 'projector_config.pbtxt')
train_graph = os.path.join(train_path, 'graph.pbtxt')
#epoch_projector = os.path.join(epoch_path, 'projector_config.pbtxt')
#epoch_graph = os.path.join(epoch_path, 'graph.pbtxt')
#os.system('cp ' + train_graph + ' ' + epoch_graph)
#os.system('cp ' + train_projector + ' ' + epoch_projector)
file_list = glob.glob(train_path+'/*.index')
count = []
for i in sorted(file_list):
m = re.search('\d+(.)index',i)
s = m.group(0)
m = re.search('\d+',s)
count.append(int(m.group(0)))
best_bleu = 0.0
rouge_1 = []
rouge_2 = []
rouge_l = []
bleu_4 = []
best_rouge_epoch = 1000
print(file_list)
print(count)
for k,c in enumerate(count):
print(c)
p = os.system('python run_summarization.py --mode=decode --use_val_as_test=True --test_by_epoch=True --epoch_num ' + str(c) + ' --config_file ' + str(config_file))
w = get_result_dir_name('val',c)
r1,r2,rl,bl = get_metrics(os.path.join(w,'decoded','*.txt'), os.path.join(w,'reference','*.txt'))
rouge_1.append(r1)
rouge_2.append(r2)
rouge_l.append(rl)
bleu_4.append(bl)
if bl > best_bleu:
best_bleu = bl
best_rouge_epoch = c
d = {'epoch':count[:k+1],'rouge_1':rouge_1, 'rouge_2':rouge_2, 'rouge_l': rouge_l}
df = pd.DataFrame(d)
df.to_csv(config['exp_name']+'.csv',index=False)
print(k)
print(best_rouge_epoch)
best_rouge_epoch = 33
r1,r2,rl = get_rouge(get_result_file_name(config,'test',best_rouge_epoch))
print("%s\t%f\t%f\t%f\t"%(config['exp_name'],r1,r2,rl))
print("%s\t%f\t%f\t%f\t"%(config['exp_name'],r1,r2,rl),file=other_f)
'''