File size: 922 Bytes
e457c68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import zipfile
from PIL import Image
from torch.utils.data import Dataset

class CustomDataset(Dataset):
    def __init__(self, json_path, zip_path, split):
        self.data = self.load_data(json_path, split)
        self.zip_path = zip_path

    def load_data(self, json_path, split):
        with open(json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        return data[split]

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        item = self.data[idx]
        image_path = item['image']
        text = item['text']

        with zipfile.ZipFile(self.zip_path, 'r') as zip_ref:
            with zip_ref.open(image_path) as img_file:
                image = Image.open(img_file)
                return {"image": image, "text": text}

def load_dataset(json_path, zip_path, split):
    return CustomDataset(json_path, zip_path, split)