-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaptioner.py
More file actions
80 lines (80 loc) · 3.5 KB
/
Captioner.py
File metadata and controls
80 lines (80 loc) · 3.5 KB
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
from nltk.translate.bleu_score import corpus_bleu
from meteor import meteor_score
import cv2
import time
from tqdm import tqdm
import numpy as np
from Trainer import Trainer
import tensorflow as tf
import matplotlib.pyplot as plt
class Captioner(Trainer):
"""
Inherited from Trainer in Trainer.py
pls refer to that for input arguments
"""
def train(self):
train_plot , val_plot = [] , []
for epoch in range(self.start_epoch,self.nb_epochs):
start=time.time()
total_loss , total_valloss= 0.0 , 0.0
for (batch, (img_tensor, sentence,mask)) in tqdm(enumerate(self.data.train_dataset)):
batch_loss, t_loss = self.train_step(img_tensor, sentence,mask)
total_loss += t_loss
if batch % 100 == 0:
print ('Epoch {} Batch {} Loss {:.4f}'.format(
epoch + 1, batch, batch_loss.numpy() / int(sentence.shape[1])))
for (batch, (img_tensor, sentence,mask)) in tqdm(enumerate(self.data.val_dataset)):
batch_loss, t_loss = self.val_step(img_tensor, sentence,mask)
total_valloss += t_loss
if batch % 100 == 0:
print ('Epoch {} Batch {} ValLoss {:.4f}'.format(
epoch + 1, batch, batch_loss.numpy() / int(sentence.shape[1])))
train_plot.append(total_loss / self.data.train_steps)
val_plot.append(total_valloss / self.data.val_steps)
if (epoch+1) % 5 == 0:
self.cpkt_manager.save()
print ('Epoch {} Loss {:.6f} ValLoss {:6f}'.format(epoch + 1,
total_loss/self.data.train_steps,total_valloss/self.data.val_steps))
print ('Time taken for 1 epoch {} sec\n'.format(time.time() - start))
if len(val_plot)>1:
if val_plot[-1]>val_plot[-2]:
print("\nEarly Stopping....")
break
plt.plot(train_plot)
plt.plot(val_plot)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Loss Plot')
plt.show()
def test(self):
test_loss = 0.0
meteor = []
bleu_ref = []
bleu_hyp = []
for (img_name,img_tensor,sentence,mask) in tqdm(self.data.test_dataset):
batch_loss, t_loss = self.val_step(img_tensor,sentence,mask)
result = self.test_step(img_tensor,sentence)
result = result.strip()[7:-5].strip()
img_name = img_name.numpy()[0].decode('utf8')
ref = self.data.caption_dict[img_name]
meteor.append(meteor_score(ref,result))
bleu_ref.append(ref)
bleu_hyp.append(result)
test_loss += t_loss
print("Test Loss {}\nBleu Score:\n".format(test_loss))
print('BLEU-1: %f' % corpus_bleu(bleu_ref, bleu_hyp, weights=(1.0, 0, 0, 0)))
print('BLEU-2: %f' % corpus_bleu(bleu_ref, bleu_hyp, weights=(0.5, 0.5, 0, 0)))
print('BLEU-3: %f' % corpus_bleu(bleu_ref, bleu_hyp, weights=(0.3, 0.3, 0.3, 0)))
print('BLEU-4: %f' % corpus_bleu(bleu_ref, bleu_hyp, weights=(0.25, 0.25, 0.25, 0.25)))
print('Meteor: {}'.format(np.mean(meteor)))
def caption_generator(self,image_path):
img_path = tf.convert_to_tensor(image_path)
img_path , img_tensor = self.data.load_image(img_path)
batch_features = tf.convert_to_tensor(self.data.model_new.predict(tf.reshape(img_tensor,(-1,self.data.img_shape[0],self.data.img_shape[1],3))))
print(batch_features.shape)
result = self.test_step(tf.reshape(batch_features,(1,self.data.img_features)),img_path)
print('ID {} , \nCaption:\nMax Search:{}'.format(image_path, result))
plt.imshow(cv2.cvtColor(cv2.imread(image_path),cv2.COLOR_BGR2RGB))
plt.title(' '.join(result.split()[:-1]))
plt.axis("off")
plt.show()