guill
commited on
Commit
·
e457c68
1
Parent(s):
052b239
dataset
Browse files- dataset.py +31 -0
- test_dataset.json +0 -0
dataset.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import zipfile
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torch.utils.data import Dataset
|
| 6 |
+
|
| 7 |
+
class CustomDataset(Dataset):
|
| 8 |
+
def __init__(self, json_path, zip_path, split):
|
| 9 |
+
self.data = self.load_data(json_path, split)
|
| 10 |
+
self.zip_path = zip_path
|
| 11 |
+
|
| 12 |
+
def load_data(self, json_path, split):
|
| 13 |
+
with open(json_path, 'r', encoding='utf-8') as f:
|
| 14 |
+
data = json.load(f)
|
| 15 |
+
return data[split]
|
| 16 |
+
|
| 17 |
+
def __len__(self):
|
| 18 |
+
return len(self.data)
|
| 19 |
+
|
| 20 |
+
def __getitem__(self, idx):
|
| 21 |
+
item = self.data[idx]
|
| 22 |
+
image_path = item['image']
|
| 23 |
+
text = item['text']
|
| 24 |
+
|
| 25 |
+
with zipfile.ZipFile(self.zip_path, 'r') as zip_ref:
|
| 26 |
+
with zip_ref.open(image_path) as img_file:
|
| 27 |
+
image = Image.open(img_file)
|
| 28 |
+
return {"image": image, "text": text}
|
| 29 |
+
|
| 30 |
+
def load_dataset(json_path, zip_path, split):
|
| 31 |
+
return CustomDataset(json_path, zip_path, split)
|
test_dataset.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|