-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc3_Lightning_Data_module.py
76 lines (59 loc) · 3.13 KB
/
doc3_Lightning_Data_module.py
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
70
71
72
73
74
75
76
import torch
from torchvision.datasets import ImageFolder
import pytorch_lightning as pl
from torchvision import datasets, transforms
import os
class CustomData(pl.LightningDataModule):
def __init__(self, data_dir, train_batch_size, val_batch_size, test_data=False):
super(CustomData, self).__init__()
self.data_dir = data_dir
self.train_batch_size = train_batch_size
self.val_batch_size = val_batch_size
self.test_data = test_data
self.train_image_dataset = ImageFolder
self.val_image_dataset = ImageFolder
self.test_image_dataset = ImageFolder
self.train_data_transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
self.val_data_transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.CenterCrop(128),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
self.test_data_transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.CenterCrop(128),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
def setup(self, stage):
self.train_image_dataset = datasets.ImageFolder(os.path.join(self.data_dir, 'train'), self.train_data_transform)
train_data_size = len(self.train_image_dataset)
class_names = self.train_image_dataset.classes
self.val_image_dataset = datasets.ImageFolder(os.path.join(self.data_dir, 'val'), self.val_data_transform)
val_data_size = len(self.val_image_dataset)
if self.test_data:
self.test_image_dataset = datasets.ImageFolder(os.path.join(self.data_dir, 'test'), self.test_data_transform)
test_data_size = len(self.test_image_dataset)
else:
test_data_size = 0
print(f'Train dataset sizes: {train_data_size}, Val dataset sizes: {val_data_size}, Test dataset sizes: {test_data_size}'
f'\nClass names: {class_names}')
def train_dataloader(self):
return torch.utils.data.DataLoader(self.train_image_dataset, batch_size=self.train_batch_size,
shuffle=True, num_workers=2)
def val_dataloader(self):
return torch.utils.data.DataLoader(self.val_image_dataset, batch_size=self.val_batch_size,
shuffle=False, num_workers=2)
def test_dataloader(self):
if self.test_data:
return torch.utils.data.DataLoader(self.test_image_dataset, batch_size=self.val_batch_size,
shuffle=True, num_workers=2)
else:
return torch.utils.data.DataLoader(self.val_image_dataset, batch_size=self.val_batch_size,
shuffle=True, num_workers=2)