|
| 1 | +import pytorch_lightning as pl |
| 2 | +import alonet |
| 3 | +import torch |
| 4 | +from torch.utils.data.sampler import RandomSampler, SequentialSampler |
| 5 | + |
| 6 | + |
| 7 | +class BaseDataModule(pl.LightningDataModule): |
| 8 | + """ |
| 9 | + Base class for all data modules. |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__( |
| 13 | + self, args, **kwargs, |
| 14 | + ): |
| 15 | + super().__init__() |
| 16 | + alonet.common.pl_helpers.params_update(self, args, kwargs) |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def add_argparse_args(parent_parser): |
| 20 | + parser = parent_parser.add_argument_group("BaseDataModule") |
| 21 | + parser.add_argument("--batch_size", type=int, default=5, help="Batch size (Default: %(default)s)") |
| 22 | + parser.add_argument( |
| 23 | + "--num_workers", type=int, default=8, help="num_workers to use on the dataset (Default: %(default)s)" |
| 24 | + ) |
| 25 | + parser.add_argument("--sequential_sampler", action="store_true", help="sample data sequentially (no shuffle)") |
| 26 | + parser.add_argument( |
| 27 | + "--sample", action="store_true", help="Download a sample for train/val process (Default: %(default)s)" |
| 28 | + ) |
| 29 | + parser.add_argument("--train_on_val", action="store_true", help="Train on validation set (Default: %(default)s)") |
| 30 | + |
| 31 | + parser.add_argument("--no_aug", action="store_true", help="Disable data augmentation (Default: %(default)s)") |
| 32 | + return parent_parser |
| 33 | + |
| 34 | + @property |
| 35 | + def train_dataset(self): |
| 36 | + if not hasattr(self, "_train_dataset"): |
| 37 | + self.setup() |
| 38 | + return self._train_dataset |
| 39 | + |
| 40 | + @train_dataset.setter |
| 41 | + def train_dataset(self, new_dataset): |
| 42 | + self._train_dataset = new_dataset |
| 43 | + |
| 44 | + @property |
| 45 | + def val_dataset(self): |
| 46 | + if not hasattr(self, "_val_dataset"): |
| 47 | + self.setup() |
| 48 | + return self._val_dataset |
| 49 | + |
| 50 | + @val_dataset.setter |
| 51 | + def val_dataset(self, new_dataset): |
| 52 | + self._val_dataset = new_dataset |
| 53 | + |
| 54 | + @property |
| 55 | + def test_dataset(self): |
| 56 | + if not hasattr(self, "_test_dataset"): |
| 57 | + self.setup() |
| 58 | + return self._test_dataset |
| 59 | + |
| 60 | + @test_dataset.setter |
| 61 | + def test_dataset(self, new_dataset): |
| 62 | + self._test_dataset = new_dataset |
| 63 | + |
| 64 | + def train_transform(self, frames, **kwargs): |
| 65 | + """ |
| 66 | + A structure to select the train transform function. |
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + frames : aloscene.Frame |
| 70 | + Input frames |
| 71 | + Returns |
| 72 | + ------- |
| 73 | + aloscene.Frame |
| 74 | + """ |
| 75 | + if self.no_aug: |
| 76 | + return self._train_transform_no_aug(frames) |
| 77 | + else: |
| 78 | + return self._train_transform_aug(frames, **kwargs) |
| 79 | + |
| 80 | + def _train_transform_no_aug(self, frames): |
| 81 | + """ |
| 82 | + Train_transform with no data augmentation. |
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + frames : aloscene.Frame |
| 86 | + Input frames |
| 87 | + Returns |
| 88 | + ------- |
| 89 | + aloscene.Frame |
| 90 | + """ |
| 91 | + |
| 92 | + raise NotImplementedError("Should be implemented in child class.") |
| 93 | + |
| 94 | + def _train_transform_aug(self, frames): |
| 95 | + """ |
| 96 | + Train_transform with data augmentation. |
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + frames : aloscene.Frame |
| 100 | + Input frames |
| 101 | + Returns |
| 102 | + ------- |
| 103 | + aloscene.Frame |
| 104 | + """ |
| 105 | + |
| 106 | + raise NotImplementedError("Should be implemented in child class.") |
| 107 | + |
| 108 | + def val_transform(self, frames, **kwargs): |
| 109 | + """ |
| 110 | + Val transform. |
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + frames : aloscene.Frame |
| 114 | + Input frames |
| 115 | + Returns |
| 116 | + ------- |
| 117 | + aloscene.Frame |
| 118 | + """ |
| 119 | + |
| 120 | + raise NotImplementedError("Should be implemented in child class.") |
| 121 | + |
| 122 | + def setup(self, stage=None): |
| 123 | + """:attr:`train_dataset`, :attr:`val_dataset`, attr:`test_dataset` datasets setup |
| 124 | + Parameters |
| 125 | + ---------- |
| 126 | + stage : str, optional |
| 127 | + Stage either `fit`, `validate`, `test` or `predict`, by default None""" |
| 128 | + |
| 129 | + raise NotImplementedError("Should be implemented in child class.") |
| 130 | + |
| 131 | + def train_dataloader(self, sampler: torch.utils.data = None): |
| 132 | + """Get train dataloader |
| 133 | + Parameters |
| 134 | + ---------- |
| 135 | + sampler : torch.utils.data, optional |
| 136 | + Sampler to load batches, by default None |
| 137 | + Returns |
| 138 | + ------- |
| 139 | + torch.utils.data.DataLoader |
| 140 | + Dataloader for training process |
| 141 | + """ |
| 142 | + if sampler is None: |
| 143 | + sampler = RandomSampler if not self.sequential_sampler else SequentialSampler |
| 144 | + |
| 145 | + return self.train_dataset.train_loader(batch_size=self.batch_size, num_workers=self.num_workers, sampler=sampler) |
| 146 | + |
| 147 | + def val_dataloader(self, sampler: torch.utils.data = None): |
| 148 | + """Get val dataloader |
| 149 | + Parameters |
| 150 | + ---------- |
| 151 | + sampler : torch.utils.data, optional |
| 152 | + Sampler to load batches, by default None |
| 153 | + Returns |
| 154 | + ------- |
| 155 | + torch.utils.data.DataLoader |
| 156 | + Dataloader for validation process |
| 157 | + """ |
| 158 | + if sampler is None: |
| 159 | + sampler = SequentialSampler |
| 160 | + |
| 161 | + return self.val_dataset.train_loader(batch_size=self.batch_size, num_workers=self.num_workers, sampler=sampler) |
| 162 | + |
| 163 | + def test_dataloader(self, sampler: torch.utils.data = None): |
| 164 | + """Get test dataloader |
| 165 | + Parameters |
| 166 | + ---------- |
| 167 | + sampler : torch.utils.data, optional |
| 168 | + Sampler to load batches, by default None |
| 169 | + Returns |
| 170 | + ------- |
| 171 | + torch.utils.data.DataLoader |
| 172 | + Dataloader for inference process |
| 173 | + """ |
| 174 | + if sampler is None: |
| 175 | + sampler = SequentialSampler |
| 176 | + |
| 177 | + return self.test_dataset.train_loader(batch_size=self.batch_size, num_workers=self.num_workers, sampler=sampler) |
0 commit comments