2022-09-28 22:37:15 +03:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
from torch.utils.data import Dataset
|
|
|
|
from torchvision.datasets.utils import download_url
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-06-23 05:58:20 +03:00
|
|
|
|
2022-09-28 22:37:15 +03:00
|
|
|
class nocaps_eval(Dataset):
|
2023-06-23 05:58:20 +03:00
|
|
|
def __init__(self, transform, image_root, ann_root, split):
|
|
|
|
urls = {
|
|
|
|
"val": "https://storage.googleapis.com/sfr-vision-language-research/datasets/nocaps_val.json",
|
|
|
|
"test": "https://storage.googleapis.com/sfr-vision-language-research/datasets/nocaps_test.json",
|
|
|
|
}
|
|
|
|
filenames = {"val": "nocaps_val.json", "test": "nocaps_test.json"}
|
|
|
|
|
|
|
|
download_url(urls[split], ann_root)
|
|
|
|
|
|
|
|
self.annotation = json.load(open(os.path.join(ann_root, filenames[split]), "r"))
|
2022-09-28 22:37:15 +03:00
|
|
|
self.transform = transform
|
|
|
|
self.image_root = image_root
|
2023-06-23 05:58:20 +03:00
|
|
|
|
2022-09-28 22:37:15 +03:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.annotation)
|
2023-06-23 05:58:20 +03:00
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2022-09-28 22:37:15 +03:00
|
|
|
ann = self.annotation[index]
|
2023-06-23 05:58:20 +03:00
|
|
|
|
|
|
|
image_path = os.path.join(self.image_root, ann["image"])
|
|
|
|
image = Image.open(image_path).convert("RGB")
|
|
|
|
image = self.transform(image)
|
|
|
|
|
|
|
|
return image, int(ann["img_id"])
|