-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
36 lines (26 loc) · 1.16 KB
/
dataloader.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
import torch
import torchvision
from PIL import Image
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoader, Dataset
class Mnist_custom(torchvision.datasets.MNIST):
def __init__(self, **kwrgs):
super().__init__(**kwrgs)
def __getitem__(self, index):
img, target = self.data[index], int(self.targets[index])
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img.numpy(), mode="L")
img = img.resize((32, 32))
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
time = torch.randint(
0, 1000, (1,)
) # time value needed in diffusion models, might add time embeddings here only in future
return img, target, time
def get_dataloader(train=True):
mnist = Mnist_custom(root='./data', train=train, download=True, transform=ToTensor())
dataloader = DataLoader(mnist, batch_size=128, shuffle=True, drop_last=True)
return dataloader