-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_mnist_h5.py
More file actions
50 lines (40 loc) · 2.1 KB
/
create_mnist_h5.py
File metadata and controls
50 lines (40 loc) · 2.1 KB
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
import os
import h5py
import torch
from torchvision import datasets, transforms
def create_mnist_h5(out_path="mnist_grouped.h5", group_size=1000):
if os.path.exists(out_path):
print(f"[Info] {out_path} already exists. Skipping.")
return
print("[Info] Downloading and writing MNIST to HDF5...")
transform = transforms.ToTensor()
mnist_train = datasets.MNIST(root="./data", train=True, download=True, transform=transform)
mnist_test = datasets.MNIST(root="./data", train=False, download=True, transform=transform)
# Convert to tensors
train_imgs = torch.stack([img for img, _ in mnist_train]) # (60000, 1, 28, 28)
train_labels = torch.tensor([label for _, label in mnist_train])
test_imgs = torch.stack([img for img, _ in mnist_test]) # (10000, 1, 28, 28)
test_labels = torch.tensor([label for _, label in mnist_test])
def make_batches(x, size):
n = x.shape[0]
remainder = n % size
if remainder:
pad = size - remainder
if x.ndim == 1:
x = torch.cat([x, torch.zeros(pad, dtype=x.dtype)], dim=0)
else:
x = torch.cat([x, torch.zeros(pad, *x.shape[1:], dtype=x.dtype)], dim=0)
return x.view(-1, size, *x.shape[1:])
train_imgs_batched = make_batches(train_imgs, group_size)
train_labels_batched = make_batches(train_labels, group_size)
test_imgs_batched = make_batches(test_imgs, group_size)
test_labels_batched = make_batches(test_labels, group_size)
with h5py.File(out_path, "w") as f:
f.create_dataset("train/images", data=train_imgs_batched, compression="gzip")
f.create_dataset("train/labels", data=train_labels_batched, compression="gzip")
f.create_dataset("test/images", data=test_imgs_batched, compression="gzip")
f.create_dataset("test/labels", data=test_labels_batched, compression="gzip")
print(f"[Info] HDF5 dataset written to {out_path}")
print(f"Train set: {train_imgs_batched.shape}, Test set: {test_imgs_batched.shape}")
if __name__ == "__main__":
create_mnist_h5("mnist_grouped.h5", group_size=100)