Spaces:
Runtime error
Runtime error
Upload nlvr_dataset.py
Browse files- BLIP/data/nlvr_dataset.py +78 -0
BLIP/data/nlvr_dataset.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
from torch.utils.data import Dataset
|
| 6 |
+
from torchvision.datasets.utils import download_url
|
| 7 |
+
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
from data.utils import pre_caption
|
| 11 |
+
|
| 12 |
+
class nlvr_dataset(Dataset):
|
| 13 |
+
def __init__(self, transform, image_root, ann_root, split):
|
| 14 |
+
'''
|
| 15 |
+
image_root (string): Root directory of images
|
| 16 |
+
ann_root (string): directory to store the annotation file
|
| 17 |
+
split (string): train, val or test
|
| 18 |
+
'''
|
| 19 |
+
urls = {'train':'https://storage.googleapis.com/sfr-vision-language-research/datasets/nlvr_train.json',
|
| 20 |
+
'val':'https://storage.googleapis.com/sfr-vision-language-research/datasets/nlvr_dev.json',
|
| 21 |
+
'test':'https://storage.googleapis.com/sfr-vision-language-research/datasets/nlvr_test.json'}
|
| 22 |
+
filenames = {'train':'nlvr_train.json','val':'nlvr_dev.json','test':'nlvr_test.json'}
|
| 23 |
+
|
| 24 |
+
download_url(urls[split],ann_root)
|
| 25 |
+
self.annotation = json.load(open(os.path.join(ann_root,filenames[split]),'r'))
|
| 26 |
+
|
| 27 |
+
self.transform = transform
|
| 28 |
+
self.image_root = image_root
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def __len__(self):
|
| 32 |
+
return len(self.annotation)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def __getitem__(self, index):
|
| 36 |
+
|
| 37 |
+
ann = self.annotation[index]
|
| 38 |
+
|
| 39 |
+
image0_path = os.path.join(self.image_root,ann['images'][0])
|
| 40 |
+
image0 = Image.open(image0_path).convert('RGB')
|
| 41 |
+
image0 = self.transform(image0)
|
| 42 |
+
|
| 43 |
+
image1_path = os.path.join(self.image_root,ann['images'][1])
|
| 44 |
+
image1 = Image.open(image1_path).convert('RGB')
|
| 45 |
+
image1 = self.transform(image1)
|
| 46 |
+
|
| 47 |
+
sentence = pre_caption(ann['sentence'], 40)
|
| 48 |
+
|
| 49 |
+
if ann['label']=='True':
|
| 50 |
+
label = 1
|
| 51 |
+
else:
|
| 52 |
+
label = 0
|
| 53 |
+
|
| 54 |
+
words = sentence.split(' ')
|
| 55 |
+
|
| 56 |
+
if 'left' not in words and 'right' not in words:
|
| 57 |
+
if random.random()<0.5:
|
| 58 |
+
return image0, image1, sentence, label
|
| 59 |
+
else:
|
| 60 |
+
return image1, image0, sentence, label
|
| 61 |
+
else:
|
| 62 |
+
if random.random()<0.5:
|
| 63 |
+
return image0, image1, sentence, label
|
| 64 |
+
else:
|
| 65 |
+
new_words = []
|
| 66 |
+
for word in words:
|
| 67 |
+
if word=='left':
|
| 68 |
+
new_words.append('right')
|
| 69 |
+
elif word=='right':
|
| 70 |
+
new_words.append('left')
|
| 71 |
+
else:
|
| 72 |
+
new_words.append(word)
|
| 73 |
+
|
| 74 |
+
sentence = ' '.join(new_words)
|
| 75 |
+
return image1, image0, sentence, label
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|