from typing import Optional import datasets import io import PIL import PIL.PngImagePlugin import os import hashlib import warnings ASSEMBLED_COLUMNS = ( 'sample_id', 'dataset_name', 'task_name', 'query', 'annotations', 'image', 'query_info', 'annotations_info', 'image_info', 'image_sha256' ) def _hash_bytes(b: bytes) -> str: m = hashlib.sha256() m.update(b) return m.hexdigest() def get_bigdocs_75m( formal_name: datasets.DatasetDict, user_local_path: Optional[str], load_from_cache_file:Optional[bool]=None, num_proc: Optional[int]=None, raise_on_missing: Optional[bool]=None, skip_bad_sha256: Optional[bool]=None, bigdocs_load_dataset_kwargs: Optional[dict]=None ) -> datasets.DatasetDict: """ Get a subset of BigDocs-7.5M Some parts of BigDocs-7.5M are distributed without their "image" column, and instead have an "img_id" column. The present function substitutes such images back in. For the following `formal_name`, the the user is responsible to download the specified dataset and specify its location through `user_local_path`. - COCOtext: http://images.cocodataset.org/zips/train2014.zip - pubtables-1m: https://www.microsoft.com/en-us/research/publication/pubtables-1m - TextOCR: https://dl.fbaipublicfiles.com/textvqa/images/train_val_images.zip Args: formal_name (`DatasetDict`): The BigDocs-7.5M dataset to augment with local images. user_local_path (`Optional[str]`, defaults to `None`): The local path containing the images to be linked. load_from_cache_file (`Optional[bool], defaults to `None`): Passed to `map`, `filter` and the likes. num_proc (`Optional[int], defaults to `None`): Passed to `map`, `filter` and the likes. raise_on_missing (`Optional[bool]`, defaults to `None`): Determines what to do when there is an error loading an image. - `True`: raise an error. - `None`: print a warning and skip the sample (default). - `False`: silently skip the sample. use_bad_sha256 (`Optional[bool], defaults to `None`): Determines what to do when the sha256 integrity test fails. - `True`: ignore the sha256 integrity test. - `None`: print a warning and skip samples with bad sha256 (default). - `False`: silently skip entries with bad sha256. load_dataset_kwargs (`Optional[dict]`, defaults to `None`): Arguments passed to datasets.load_dataset . """ if bigdocs_load_dataset_kwargs is None: bigdocs_load_dataset_kwargs = {} unprocessed = datasets.load_dataset("ServiceNow/BigDocs-7.5M", formal_name, **bigdocs_load_dataset_kwargs) def on_disk_processor(sample): img_path = os.path.join(user_local_path, sample['img_id']) # Load the image try: image = PIL.Image.open(img_path) except Exception as e: if raise_on_missing: raise RuntimeError(f"Error loading image at {img_path}\n{e}") if raise_on_missing is None: warnings.warn(f"Skipping due to error loading image {img_path}", RuntimeWarning) image = None # Sample will be filtered out if image is not None: # Place into `buffer` using PNG image format buffer = io.BytesIO() image.save(buffer, "png") # Reload the image with guaranteed PNG format image = PIL.Image.open(buffer) # Check sha256 if not skip_bad_sha256: sha256 = _hash_bytes(buffer.getvalue()) if sha256 != sample["image_sha256"]: image = None # Sample will be filtered out if skip_bad_sha256 is None: warnings.warn(f"Skipping due to bad sha256 for {img_path}", RuntimeWarning) return {"image": image} # Get the correct processor try: processor = { "COCOtext": on_disk_processor, "pubtables-1m": on_disk_processor, "TextOCR": on_disk_processor, }[formal_name] except KeyError: raise ValueError(f"Unknown formal_name: {formal_name}") if processor is on_disk_processor: assert user_local_path is not None, f"user_local_path is mandatory for formal_name={formal_name}" if processor is None: processed = unprocessed else: processed = unprocessed.map( processor, remove_columns="img_id", load_from_cache_file=load_from_cache_file, num_proc=num_proc ) # Drop missing images. if not raise_on_missing: processed = processed.filter((lambda image: image is not None), input_columns="image", num_proc=num_proc) # Column order processed = processed.select_columns(list(ASSEMBLED_COLUMNS)) return processed