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)