August 2018
Intermediate to advanced
438 pages
12h 3m
English
We will now be implementing a basic beam search-based algorithm to generate caption sequences:
from keras.preprocessing import image, sequence def get_raw_caption_sequences(model, word_to_index, image_features, max_caption_size, beam_size=1): start = [word_to_index['<START>']] caption_seqs = [[start, 0.0]] while len(caption_seqs[0][0]) < max_caption_size: temp_caption_seqs = [] for caption_seq in caption_seqs: partial_caption_seq = sequence.pad_sequences( [caption_seq[0]], maxlen=max_caption_size, padding='post') next_words_pred = model.predict( [np.asarray([image_features]), np.asarray(partial_caption_seq)])[0] next_words = np.argsort(next_words_pred)[-beam_size:] for word in next_words: ...
Read now
Unlock full access