a b/PathBLIP/dataset.py
1
from torch.utils.data import Dataset
2
import pandas as pd
3
import os
4
from PIL import Image
5
from torchvision import transforms
6
from collections import defaultdict
7
import torch
8
9
class ImageTextContrastiveCollator:
10
    def __init__(self):
11
        return
12
    def __call__(self, batch):
13
        inputs = defaultdict(list)
14
        for data in batch:
15
            inputs['image'].append(data['image'])
16
            inputs['text_input'].append(data['text_input'])
17
            inputs['text_output'].append(data['text_output'])
18
            
19
20
        # inputs['image'] = torch.stack(inputs['image'])
21
22
        return inputs
23
24
class Quiltdataset(Dataset):
25
    def __init__(self):
26
        # self.df = pd.read_csv(csv_path)
27
        self.df = pd.read_csv('../BLIP/LAVIS-main/quilt.csv')
28
        self.df = self.df.dropna(axis=0, subset=['pathology'])[400000:]
29
        normalize = transforms.Normalize(
30
            (0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)
31
        )
32
33
        self.transform = transforms.Compose(
34
            [
35
                transforms.RandomResizedCrop(224, scale=(0.2, 1.0)),
36
                transforms.RandomHorizontalFlip(),
37
                transforms.ToTensor(),
38
                normalize,
39
            ]
40
        )
41
        
42
    def __len__(self):
43
        return len(self.df)
44
    def __getitem__(self, index):
45
        caption = self.df.iloc[index]['caption']
46
        if type(caption) == float:
47
            caption = "This is a image about the pathology."
48
        img_path = self.df.iloc[index]['image_path']
49
        # img_path = os.path.join("../", img_path)
50
        # image = Image.open(img_path).convert('RGB')
51
        # image = self.transform(image)
52
        # caption = self.text_processor(caption)
53
        # img = self.transform(img)
54
        
55
        caption = caption.split()
56
        prefix = caption[:int(len(caption) * 0.2)]
57
        subfix = caption[int(len(caption) * 0.2):]
58
        prefix = " ".join(prefix)
59
        subfix = " ".join(subfix)
60
        return {
61
            "image": img_path,
62
            "text_input": prefix,
63
            "text_output": subfix,
64
        }
65
        # return {
66
        #     "image": img_path,
67
        #     "text_input": caption,
68
        #     "text_output": caption,
69
        # }
70
         
71
        
72
        
73
if __name__ == '__main__':
74
    test = Quiltdataset()
75
    print(test.__len__())
76
    print(test.__getitem__(0))
77
    print(test.__getitem__(1))
78
    
79
    
80