-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataModule.py
79 lines (66 loc) · 2.9 KB
/
dataModule.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
77
78
79
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import pytorch_lightning as pl
from tqdm import tqdm
from torch.utils.data import DataLoader
from dataset import CTDataset3DWithTemplate
from utils.transform3D import get_transform
class DataModule(pl.LightningDataModule):
'''
O datamodul e organiza o carregamento de dados
'''
def __init__(self, hparams):
super().__init__()
self.save_hyperparameters(hparams)
def check_dataset(self):
# Tentar carregar todos os exemplos do dataset
for mode in ["train", "val"]:
for sample in tqdm(CTDataset3DWithTemplate(mode, labels_name=self.hparams.labels_name), leave=True, position=0, desc="Load testing..."):
pass
def setup(self, stage=None):
'''
Definição dos datasets de treino validação e teste e das transformadas.
'''
if (any(x==self.hparams.approach for x in self.hparams.approachs_used_3d)):
try:
self.hparams.train_transform = get_transform(self.hparams.train_transform_str)
self.hparams.eval_transform = get_transform(self.hparams.eval_transform_str)
self.train = CTDataset3DWithTemplate("train", labels_name=self.hparams.labels_name, transforms=None)
self.val = CTDataset3DWithTemplate("val", labels_name=self.hparams.labels_name, transforms=None)
except Exception as e:
print("Empty dataset!")
sys.exit(1)
else:
print("Modelo não especificado no arquivo dataModule.")
print("Size of training and validation datasets:",len(self.train),len(self.val))
def train_dataloader(self):
trainDataloader = DataLoader(self.train, batch_size=self.hparams.batch_size, num_workers=self.hparams.nworkers, shuffle=True)
sample = next(iter(trainDataloader))
img_batch = sample['image']
seg_batch = sample['label']
print('Train:')
print(f"\tFeature batch shape (image): {img_batch.shape}")
print(f"\tFeature batch shape (label): {seg_batch.shape}")
print(f"\tMin: {img_batch.min()} Max: {img_batch.max()}")
print(f"\tMin: {seg_batch.min()} Max: {seg_batch.max()}")
if self.hparams.datatype=='template':
template = sample['template']
print(f"\tFeature shape (template): {template.shape}")
print(f"\tMinMax (template): {template.min()} {template.max()}")
return trainDataloader
def val_dataloader(self):
valDataloader = DataLoader(self.val, batch_size=self.hparams.batch_size, num_workers=self.hparams.nworkers, shuffle=False)
sample = next(iter(valDataloader))
img_batch = sample['image']
seg_batch = sample['label']
print('Validation:')
print(f"\tFeature batch shape (image): {img_batch.shape}")
print(f"\tFeature batch shape (label): {seg_batch.shape}")
print(f"\tMin: {img_batch.min()} Max: {img_batch.max()}")
print(f"\tMin: {seg_batch.min()} Max: {seg_batch.max()}")
if self.hparams.datatype=='template':
template = sample['template']
print(f"\tFeature shape (template): {template.shape}")
print(f"\tMinMax (template): {template.min()} {template.max()}")
return valDataloader