-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
62 lines (42 loc) · 1.45 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
import config
import utils
import numpy as np
from tqdm import tqdm
from nltk.translate.bleu_score import sentence_bleu
def check_accuracy(dataset, model):
print('=> Testing')
model.eval()
bleu1_score = []
bleu2_score = []
bleu3_score = []
bleu4_score = []
for image, caption in tqdm(dataset):
image = image.to(config.DEVICE)
generated = model.generate_caption(image.unsqueeze(0), max_length=len(caption.split(' ')))
bleu1_score.append(
sentence_bleu([caption.split()], generated, weights=(1, 0, 0, 0))
)
bleu2_score.append(
sentence_bleu([caption.split()], generated, weights=(0.5, 0.5, 0, 0))
)
bleu3_score.append(
sentence_bleu([caption.split()], generated, weights=(0.33, 0.33, 0.33, 0))
)
bleu4_score.append(
sentence_bleu([caption.split()], generated, weights=(0.25, 0.25, 0.25, 0.25))
)
print(f'=> BLEU 1: {np.mean(bleu1_score)}')
print(f'=> BLEU 2: {np.mean(bleu2_score)}')
print(f'=> BLEU 3: {np.mean(bleu3_score)}')
print(f'=> BLEU 4: {np.mean(bleu4_score)}')
def main():
all_dataset = utils.load_dataset(raw_caption=True)
model = utils.get_model_instance(all_dataset.vocab)
utils.load_checkpoint(model)
_, test_dataset = utils.train_test_split(dataset=all_dataset)
check_accuracy(
test_dataset,
model
)
if __name__ == '__main__':
main()