MCPcopy Create free account
hub / github.com/ElliotVincent/SitsSCD / ImageDataModule

Class ImageDataModule

data/datamodule.py:6–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6class ImageDataModule(L.LightningDataModule):
7 def __init__(
8 self,
9 train_dataset,
10 val_dataset_out,
11 val_dataset_in,
12 test_dataset_out,
13 test_dataset_in,
14 global_batch_size,
15 num_workers,
16 num_nodes=1,
17 num_devices=1,
18 ):
19 super().__init__()
20 self._builders = {
21 "train": train_dataset,
22 "val_out": val_dataset_out,
23 "val_in": val_dataset_in,
24 "test_out": test_dataset_out,
25 "test_in": test_dataset_in,
26 }
27 self.num_workers = num_workers
28 self.batch_size = global_batch_size // (num_nodes * num_devices)
29 print(f"Each GPU will receive {self.batch_size} images")
30
31 @property
32 def num_classes(self):
33 if hasattr(self, "train_dataset"):
34 return self.train_dataset.num_classes
35 else:
36 return self._builders["train"]().num_classes
37
38 def setup(self, stage=None):
39 """Setup the datamodule.
40 Args:
41 stage (str): stage of the datamodule
42 Is be one of "fit" or "test" or None
43 """
44 print("Stage", stage)
45 start_time = time.time()
46 if stage == "fit" or stage is None:
47 self.train_dataset = self._builders["train"]()
48 self.val_dataset_out = self._builders["val_out"]()
49 self.val_dataset_in = self._builders["val_in"]()
50 print(f"Train dataset size: {len(self.train_dataset)}")
51 print(f"Out-of-domain val dataset size: {len(self.val_dataset_out)}")
52 print(f"In-domain val dataset size: {len(self.val_dataset_in)}")
53 else:
54 self.test_dataset_out = self._builders["test_out"]()
55 self.test_dataset_in = self._builders["test_in"]()
56 print(f"Out-of-domain test dataset size: {len(self.test_dataset_out)}")
57 print(f"In-domain test dataset size: {len(self.test_dataset_in)}")
58 end_time = time.time()
59 print(f"Setup took {(end_time - start_time):.2f} seconds")
60
61 def train_dataloader(self):
62 return DataLoader(
63 self.train_dataset,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected