Spaces:
Running
Running
Upload data/nocaps_dataset.py
Browse files- data/nocaps_dataset.py +32 -0
data/nocaps_dataset.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
|
4 |
+
from torch.utils.data import Dataset
|
5 |
+
from torchvision.datasets.utils import download_url
|
6 |
+
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
class nocaps_eval(Dataset):
|
10 |
+
def __init__(self, transform, image_root, ann_root, split):
|
11 |
+
urls = {'val':'https://storage.googleapis.com/sfr-vision-language-research/datasets/nocaps_val.json',
|
12 |
+
'test':'https://storage.googleapis.com/sfr-vision-language-research/datasets/nocaps_test.json'}
|
13 |
+
filenames = {'val':'nocaps_val.json','test':'nocaps_test.json'}
|
14 |
+
|
15 |
+
download_url(urls[split],ann_root)
|
16 |
+
|
17 |
+
self.annotation = json.load(open(os.path.join(ann_root,filenames[split]),'r'))
|
18 |
+
self.transform = transform
|
19 |
+
self.image_root = image_root
|
20 |
+
|
21 |
+
def __len__(self):
|
22 |
+
return len(self.annotation)
|
23 |
+
|
24 |
+
def __getitem__(self, index):
|
25 |
+
|
26 |
+
ann = self.annotation[index]
|
27 |
+
|
28 |
+
image_path = os.path.join(self.image_root,ann['image'])
|
29 |
+
image = Image.open(image_path).convert('RGB')
|
30 |
+
image = self.transform(image)
|
31 |
+
|
32 |
+
return image, int(ann['img_id'])
|