-
Notifications
You must be signed in to change notification settings - Fork 8
/
prepare_dataset.py
65 lines (45 loc) · 1.76 KB
/
prepare_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#
# Copyright (C) 2023 Apple Inc. All rights reserved.
#
import os
import pickle
from PIL import Image
from tqdm import tqdm
from torchvision.datasets import CIFAR100
def save_pngs(untar_dir: str, split: str) -> None:
"""Save loaded data as png files.
:param untar_dir: Path to untared dataset.
:param split: Split name (e.g., train, test)
"""
split_map = {"train": "training", "test": "validation"}
split_dir = os.path.join(untar_dir, split_map.get(split))
os.makedirs(split_dir, exist_ok=True)
for i in range(100):
class_dir = os.path.join(split_dir, str(i))
os.makedirs(class_dir, exist_ok=True)
with open(os.path.join(untar_dir, split), "rb") as f:
data_dict = pickle.load(f, encoding="latin1")
data = data_dict.get("data") # numpy array
# Reshape and cast
data = data.reshape(data.shape[0], 3, 32, 32)
data = data.transpose(0, 2, 3, 1).astype("uint8")
labels = data_dict.get("fine_labels")
for i, (datum, label) in tqdm(enumerate(zip(data, labels)), total=len(labels)):
image = Image.fromarray(datum)
image = image.convert("RGB")
file_path = os.path.join(split_dir, str(label), "{}.png".format(i))
image.save(file_path)
def get_cifar100() -> None:
"""Get and reformat cifar100 dataset.
See https://www.cs.toronto.edu/~kriz/cifar.html for dataset description.
"""
data_store_dir = "data_store"
if not os.path.exists(data_store_dir):
os.makedirs(data_store_dir)
dataset = CIFAR100(root=data_store_dir, download=True)
# Load files and convert to PNG
untar_dir = os.path.join(data_store_dir, dataset.base_folder)
save_pngs(untar_dir, "test")
save_pngs(untar_dir, "train")
if __name__ == "__main__":
get_cifar100()