[8bfbf9]: / lavis / datasets / datasets / pathvqa_dataset.py

Download this file

67 lines (44 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from torch.utils.data import Dataset
import pandas as pd
import os
from PIL import Image
from torchvision import transforms
from collections import defaultdict
import torch
import pickle
from lavis.datasets.datasets.base_dataset import BaseDataset
class ImageTextContrastiveCollator:
def __init__(self):
return
def __call__(self, batch):
inputs = defaultdict(list)
for data in batch:
inputs['image'].append(data['image'])
inputs['question'].append(data['question'])
inputs['answer'].append(data['answer'])
# inputs['image'] = torch.stack(inputs['image'])
return inputs
pkl_path = '../PathVQA/pvqa/qas/test_open_qa.pkl'
class PVQAdataset(BaseDataset):
def __init__(self, vis_processor, text_processor, ann_paths, vis_root):
super().__init__(vis_processor, text_processor, vis_root, ann_paths)
with open(pkl_path, 'rb') as f:
self.data = pickle.load(f)
def __len__(self):
return len(self.data)
def __getitem__(self, index):
question = self.data[index]['sent']
question = "Now that you are a pathologist, please answer the following questions based on the images. "+question
# question = question + "please output yes or no."
# question = "Now that you are a pathologist, please answer the following questions based on the images. " + question
answer = list(self.data[index]['label'].keys())[0]
img_path = os.path.join('../PathVQA/pvqa/images', 'test', self.data[index]['img_id'])+".jpg"
image = Image.open(img_path).convert('RGB')
image = self.vis_processor(image)
answer = self.text_processor(answer)
question = self.text_processor(question)
return {
"image": image,
"text_input": question,
"text_output": answer,
}