11"""PMC-OA dataset."""
22
33import os
4- from typing import Any , Callable , Dict , Literal , Optional , Tuple
4+ from typing import Any , Callable , Dict , Literal , Optional , Tuple , Union
55
6- import jsonlines
7- import pandas as pd
86import torch
97from omegaconf import MISSING
108from PIL import Image
119from torch .utils .data import Dataset
1210from torchvision import transforms
11+ import pyarrow .json as pj
12+ from pyarrow import csv
13+ import pyarrow as pa
1314
1415from mmlearn .conf import external_store
1516from mmlearn .constants import EXAMPLE_INDEX_KEY
@@ -30,7 +31,7 @@ def __init__(
3031 caption_key : str = "caption" ,
3132 csv_separator : str = "," ,
3233 transform : Optional [Callable [[Image .Image ], torch .Tensor ]] = None ,
33- tokenizer : Optional [Callable [[str ], torch .Tensor ]] = None ,
34+ tokenizer : Optional [Callable [[str ], Union [ torch .Tensor , dict ] ]] = None ,
3435 mask_generator : Optional [
3536 Callable [
3637 [Dict [str , torch .Tensor ], Any ],
@@ -104,13 +105,13 @@ def __len__(self) -> int:
104105 def __getitem__ (self , idx : int ) -> Example :
105106 """Return items in the dataset."""
106107 image_path = os .path .join (
107- self .root_dir , self .image_dir , self .image_filenames [idx ]
108+ self .root_dir , self .image_dir , self .image_filenames [idx ]. as_py ()
108109 )
109110
110111 with Image .open (image_path ) as img :
111112 images = self .transform (img )
112113
113- caption = str ( self .captions [idx ])
114+ caption = self .captions [idx ]. as_py ( )
114115 example = Example (
115116 {
116117 Modalities .RGB : images ,
@@ -141,19 +142,18 @@ def __getitem__(self, idx: int) -> Example:
141142
142143 def _csv_loader (
143144 self , input_filename : str , img_key : str , caption_key : str , sep : str
144- ) -> Tuple [Any , Any ]:
145+ ) -> Tuple [pa . ChunkedArray , pa . ChunkedArray ]:
145146 """Load images, captions from CSV data."""
146- df = pd .read_csv (input_filename , sep = sep )
147- images , captions = df [img_key ].tolist (), df [caption_key ].tolist ()
148- return images , captions
147+ table = csv .read_csv (
148+ input_filename ,
149+ parse_options = csv .ParseOptions (delimiter = sep , newlines_in_values = True ),
150+ )
151+ return table [img_key ], table [caption_key ]
149152
150153 def _jsonl_loader (
151154 self , input_filename : str , img_key : str , caption_key : str
152- ) -> Tuple [Any , Any ]:
155+ ) -> Tuple [pa . ChunkedArray , pa . ChunkedArray ]:
153156 """Load images, captions from JSON data."""
154- images , captions = [], []
155- with jsonlines .open (input_filename ) as reader :
156- for obj in reader :
157- images .append (obj [img_key ])
158- captions .append (obj [caption_key ])
159- return images , captions
157+ parse_options = pj .ParseOptions (newlines_in_values = True )
158+ table = pj .read_json (input_filename , parse_options = parse_options )
159+ return table [img_key ], table [caption_key ]
0 commit comments