Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. # generate batch via random sampling of images and captions for them,
  2. # we use `max_len` parameter to control the length of the captions (truncating long captions)
  3. def generate_batch(images_embeddings, indexed_captions, batch_size, max_len=None):
  4. """
  5. `images_embeddings` is a np.array of shape [number of images, IMG_EMBED_SIZE].
  6. `indexed_captions` holds 5 vocabulary indexed captions for each image:
  7. [
  8. [
  9. [vocab[START], vocab["image1"], vocab["caption1"], vocab[END]],
  10. [vocab[START], vocab["image1"], vocab["caption2"], vocab[END]],
  11. ...
  12. ],
  13. ...
  14. ]
  15. Generate a random batch of size `batch_size`.
  16. Take random images and choose one random caption for each image.
  17. Remember to use `batch_captions_to_matrix` for padding and respect `max_len` parameter.
  18. Return feed dict {decoder.img_embeds: ..., decoder.sentences: ...}.
  19. """
  20. batch_image_embeddings = list()
  21. batch_captions_matrix = list()
  22. for i in np.arange(batch_size):
  23. x = np.random.choice(images_embeddings.shape[0])
  24. batch_image_embeddings.append(images_embeddings[x])
  25. y = np.random.choice(5)
  26. batch_captions_matrix.append(indexed_captions[x][y])
  27.  
  28. batch_captions_matrix = batch_captions_to_matrix(batch_captions_matrix, pad_idx, max_len)
  29.  
  30. return {decoder.img_embeds: batch_image_embeddings,
  31. decoder.sentences: batch_captions_matrix}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement