-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdata.py
More file actions
51 lines (41 loc) · 1.78 KB
/
Copy pathdata.py
File metadata and controls
51 lines (41 loc) · 1.78 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 torch
import torch.nn as nn
import torchvision
from torchvision.transforms import Compose, ToTensor, Lambda
import numpy as np
import os
def make_dataloader(data, target, batch_size):
dataset = BasicDataset(data, target)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
return dataloader
class BasicDataset(torch.utils.data.Dataset):
def __init__(self, data, target):
super().__init__()
self.data = data
self.target = target
def __len__(self):
return self.data.size(0)
def __getitem__(self, idx):
return self.data[idx], self.target[idx]
def make_dataloader_emnist(batch_size, train=True, root_dir='./'):
transform = Compose([ToTensor(),
Lambda(lambda x: x.view(1, 28, 28)),
Lambda(lambda x: x.transpose(1, 2))])
try:
emnist = torchvision.datasets.EMNIST(root=root_dir, split='digits', train=train, download=False, transform=transform)
except RuntimeError:
path = os.path.join(os.path.abspath(root_dir), 'EMNIST')
yn = input(f'Dataset not found in {path}. Would you like to download it here? (y/n): ')
while True:
if yn not in ['y', 'n']:
yn = input('Please type \'y\' or \'n\': ')
else:
if yn == 'y':
emnist = torchvision.datasets.EMNIST(root=root_dir, split='digits', train=train,
download=True, transform=transform)
break
else:
print('Data will not be downloaded. Exiting script...')
quit()
dataloader = torch.utils.data.DataLoader(emnist, batch_size=batch_size, shuffle=True)
return dataloader