-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatamodules.py
More file actions
51 lines (42 loc) · 1.75 KB
/
datamodules.py
File metadata and controls
51 lines (42 loc) · 1.75 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
51
from collections.abc import Callable
from typing import Optional
from torch.utils.data import DataLoader
from torch.utils.data import random_split
from torchvision.datasets import FashionMNIST
import pytorch_lightning as pl
class FashionMNISTDataModule(pl.LightningDataModule):
def __init__(
self,
data_dir: str,
batch_size: int,
transform: Optional[Callable],
val_fraction: float
):
super().__init__()
self.data_dir = data_dir
self.batch_size = batch_size
self.transform = transform
self.val_fraction = val_fraction
def prepare_data(self) -> None:
FashionMNIST(self.data_dir, train=True, download=True)
FashionMNIST(self.data_dir, train=False, download=True)
def setup(self, stage: Optional[str] = None) -> None:
if stage == "fit" or stage is None:
mnist_full = FashionMNIST(self.data_dir, train=True, transform=self.transform)
self.mnist_train, self.mnist_val = random_split(
mnist_full, [1-self.val_fraction, self.val_fraction]
)
if stage == "test" or stage is None:
self.mnist_test = FashionMNIST(self.data_dir, train=False, transform=self.transform)
def train_dataloader(self) -> DataLoader:
return DataLoader(
self.mnist_train, batch_size=self.batch_size, shuffle=True, pin_memory=True
)
def val_dataloader(self) -> DataLoader:
return DataLoader(
self.mnist_val, batch_size=2*self.batch_size, shuffle=False, pin_memory=True
)
def test_dataloader(self) -> DataLoader:
return DataLoader(
self.mnist_test, batch_size=2*self.batch_size, shuffle=False, pin_memory=True
)