-
Notifications
You must be signed in to change notification settings - Fork 0
/
torch_cifar_loader.py
44 lines (33 loc) · 1.26 KB
/
torch_cifar_loader.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
import torch
import torchvision
import numpy as np
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
print(f"using {device} device")
"""
Load the datasets, return trainloader and testloader
"""
def get_trainloader_and_testloader(batch_size = 64):
from torchvision import datasets
from torchvision.transforms import ToTensor
trainset = datasets.CIFAR10(
root="torch_cifar10",
train=True,
download=True,
transform=ToTensor()
)
idxes = list(np.random.randint(len(trainset), size = 5000))
trainset = torch.utils.data.Subset(trainset,idxes)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = datasets.CIFAR10(
root="torch_cifar10",
train=False,
download=True,
transform=ToTensor()
)
idxes = list(np.random.randint(len(testset), size = 1000))
testset = torch.utils.data.Subset(testset,idxes)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=True, num_workers=2)
return trainloader, testloader
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')