Datasets:
Tasks:
Image Classification
Modalities:
Image
Sub-tasks:
multi-class-image-classification
Languages:
English
Size:
1M - 10M
ArXiv:
License:
How to use the parquet files?
#39
by
tian1327 - opened
Hi,
Could anyone please provide instructions on how to utilize the parquet files to access the raw image files? I remember that the images were previously provided in zip files, but now they are in parquet files. Can we still access the PNG files through it?
I would appreciate your help! Thank you!
Yes you can, e.g. this will save all the images in a directory of your choice
from pathlib import Path
from datasets import load_dataset, Image
# Login using e.g. `huggingface-cli login` to access this dataset
ds = load_dataset("ILSVRC/imagenet-1k", split="train")
ds = ds.cast_column("image", Image(decode=False))
output_dir = Path("path/to/output/dir")
for image in ds["image"]:
(output_dir / image["path"]).write_bytes(image["bytes"])
Note that we use Image(decode=False) do access the image path name and bytes instead of getting PIL.Image objects
a simple script for extracting
import os
from datasets import load_dataset
from tqdm import tqdm
dataset = load_dataset("ILSVRC/imagenet-1k")
label_names = dataset["train"].features["label"].names
def export_split(split_name, output_root):
split = dataset[split_name]
for idx, sample in enumerate(tqdm(split)):
image = sample["image"] # PIL Image
label = sample["label"] # int
class_name = label_names[label]
class_dir = os.path.join(output_root, split_name, class_name)
os.makedirs(class_dir, exist_ok=True)
save_path = os.path.join(class_dir, f"{idx}.jpg")
image.save(save_path)
export_split("train", "./imagenet")
export_split("validation", "./imagenet")