flickr30k_dataset.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os
  2. import json
  3. from torch.utils.data import Dataset
  4. from torchvision.datasets.utils import download_url
  5. from PIL import Image
  6. from data.utils import pre_caption
  7. class flickr30k_train(Dataset):
  8. def __init__(self, transform, image_root, ann_root, max_words=30, prompt=''):
  9. '''
  10. image_root (string): Root directory of images (e.g. flickr30k/)
  11. ann_root (string): directory to store the annotation file
  12. '''
  13. url = 'https://storage.googleapis.com/sfr-vision-language-research/datasets/flickr30k_train.json'
  14. filename = 'flickr30k_train.json'
  15. download_url(url,ann_root)
  16. self.annotation = json.load(open(os.path.join(ann_root,filename),'r'))
  17. self.transform = transform
  18. self.image_root = image_root
  19. self.max_words = max_words
  20. self.prompt = prompt
  21. self.img_ids = {}
  22. n = 0
  23. for ann in self.annotation:
  24. img_id = ann['image_id']
  25. if img_id not in self.img_ids.keys():
  26. self.img_ids[img_id] = n
  27. n += 1
  28. def __len__(self):
  29. return len(self.annotation)
  30. def __getitem__(self, index):
  31. ann = self.annotation[index]
  32. image_path = os.path.join(self.image_root,ann['image'])
  33. image = Image.open(image_path).convert('RGB')
  34. image = self.transform(image)
  35. caption = self.prompt+pre_caption(ann['caption'], self.max_words)
  36. return image, caption, self.img_ids[ann['image_id']]
  37. class flickr30k_retrieval_eval(Dataset):
  38. def __init__(self, transform, image_root, ann_root, split, max_words=30):
  39. '''
  40. image_root (string): Root directory of images (e.g. flickr30k/)
  41. ann_root (string): directory to store the annotation file
  42. split (string): val or test
  43. '''
  44. urls = {'val':'https://storage.googleapis.com/sfr-vision-language-research/datasets/flickr30k_val.json',
  45. 'test':'https://storage.googleapis.com/sfr-vision-language-research/datasets/flickr30k_test.json'}
  46. filenames = {'val':'flickr30k_val.json','test':'flickr30k_test.json'}
  47. download_url(urls[split],ann_root)
  48. self.annotation = json.load(open(os.path.join(ann_root,filenames[split]),'r'))
  49. self.transform = transform
  50. self.image_root = image_root
  51. self.text = []
  52. self.image = []
  53. self.txt2img = {}
  54. self.img2txt = {}
  55. txt_id = 0
  56. for img_id, ann in enumerate(self.annotation):
  57. self.image.append(ann['image'])
  58. self.img2txt[img_id] = []
  59. for i, caption in enumerate(ann['caption']):
  60. self.text.append(pre_caption(caption,max_words))
  61. self.img2txt[img_id].append(txt_id)
  62. self.txt2img[txt_id] = img_id
  63. txt_id += 1
  64. def __len__(self):
  65. return len(self.annotation)
  66. def __getitem__(self, index):
  67. image_path = os.path.join(self.image_root, self.annotation[index]['image'])
  68. image = Image.open(image_path).convert('RGB')
  69. image = self.transform(image)
  70. return image, index