File size: 2,038 Bytes
f7a5cb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from lightning import LightningDataModule
from torch.utils.data import Dataset, DataLoader


class Datamodule(LightningDataModule):
    def __init__(
        self,
        train_dataset: Dataset,
        eval_dataset: Dataset,
        batch_train_size: int,
        num_workers: int,
        eval_batch_size: int = None,
    ):
        super().__init__()

        self.train_dataset = train_dataset
        self.eval_dataset = eval_dataset

        self.batch_train_size = batch_train_size
        self.eval_batch_size = (
            eval_batch_size if eval_batch_size is not None else batch_train_size
        )

        self.num_workers = num_workers

    def train_dataloader(self) -> DataLoader:
        """Load train set loader."""
        persistent_workers = True if self.num_workers > 0 else False

        dataloader = DataLoader(
            self.train_dataset,
            batch_size=self.batch_train_size,
            num_workers=self.num_workers,
            pin_memory=True,
            persistent_workers=persistent_workers,
        )
        return dataloader

    def val_dataloader(self) -> DataLoader:
        """Load val set loader."""
        persistent_workers = True if self.num_workers > 0 else False

        dataloader = DataLoader(
            self.eval_dataset,
            batch_size=self.eval_batch_size,
            num_workers=self.num_workers,
            pin_memory=True,
            persistent_workers=persistent_workers,
        )
        return dataloader

    def predict_dataloader(self) -> DataLoader:
        """Load predict set loader."""
        dataloader = DataLoader(
            self.eval_dataset,
            batch_size=self.eval_batch_size,
            num_workers=self.num_workers,
        )
        return dataloader

    def test_dataloader(self) -> DataLoader:
        """Load test set loader."""
        dataloader = DataLoader(
            self.eval_dataset,
            batch_size=self.eval_batch_size,
            num_workers=self.num_workers,
        )
        return dataloader