Skip to content

Instantly share code, notes, and snippets.

@CookieLau
Last active April 5, 2023 13:03
Show Gist options
  • Save CookieLau/05bca97ab963ff09c04a87e8f7e06598 to your computer and use it in GitHub Desktop.
Save CookieLau/05bca97ab963ff09c04a87e8f7e06598 to your computer and use it in GitHub Desktop.
download tiny-ImageNet using `datasets` and save as ImageNet train/test split with individual categorical sub-directory
from datasets import load_dataset
import os
from PIL import Image
from tqdm import tqdm
os.makedirs("train", exist_ok=True)
os.chdir("train")
tiny_imagenet = load_dataset('Maysee/tiny-imagenet', split='train')
size = 100000
for i in tqdm(range(size)):
img = tiny_imagenet[i]['image']
label = tiny_imagenet[i]['label']
os.makedirs(str(label), exist_ok=True)
img.save(f"{label}/{i}.jpg")
os.chdir("../")
os.makedirs("val", exist_ok=True)
os.chdir("val")
tiny_imagenet = load_dataset('Maysee/tiny-imagenet', split='valid')
size = 10000
for i in tqdm(range(size)):
img = tiny_imagenet[i]['image']
label = tiny_imagenet[i]['label']
os.makedirs(str(label), exist_ok=True)
img.save(f"{label}/{i}.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment