forked from praveenVnktsh/Fast-Road-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
146 lines (105 loc) · 4.26 KB
/
dataloader.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from torch import functional
from torch.utils.data.dataset import Dataset
import torch
from torchvision.transforms import transforms
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import DataLoader
from torch.optim.rmsprop import RMSprop
import torchvision
import numpy as np
import pytorch_lightning as pl
from torchvision.transforms.functional import hflip
from configs import Configs
configs = Configs()
class TrainDataset(Dataset):
def __init__(self, dataset):
self.sequenceLength = 6
self.device = configs.device
self.size = (configs.image_size, configs.image_size)
self.data = dataset
self.length = (len(self.data) - self.sequenceLength) * 2
self.transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.size),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[
0.229, 0.224, 0.225]),
])
self.nonorm = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.size),
transforms.ToTensor(),
])
self.segmentation = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.size),
transforms.ToTensor(),
])
def __getitem__(self, index):
flip = False
if index >= self.length / 2:
flip = True
index = index % (self.length // 2)
image = self.transform(self.data[index]['front'])
seg = self.segmentation(self.data[index]['road']).bool().float()
real = self.nonorm(self.data[index]['front'])
if flip:
image = torchvision.transforms.functional.hflip(image)
seg = torchvision.transforms.functional.hflip(seg)
real = torchvision.transforms.functional.hflip(real)
return {'input': image, 'target': seg, 'real' : real}
def __len__(self):
return self.length
class TestDataset(Dataset):
def __init__(self, dataset):
self.sequenceLength = 6
self.device = configs.device
self.size = (configs.image_size, configs.image_size)
self.data = dataset
self.length = len(self.data)
self.transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.size),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[
0.229, 0.224, 0.225]),
])
self.nonorm = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.size),
transforms.ToTensor(),
])
self.segmentation = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(self.size),
transforms.ToTensor(),
])
def __getitem__(self, index):
image = self.transform(self.data[index]['front'])
seg = self.segmentation(self.data[index]['road']).bool().float()
real = self.nonorm(self.data[index]['front'])
return {'input': image, 'target': seg, 'real' : real}
def __len__(self):
return self.length
class lit_custom_data(pl.LightningDataModule):
def setup(self):
self.configs = Configs()
self.cpu = 0
self.pin = True
print('Loading dataset')
def train_dataloader(self):
dataset = TrainDataset(torch.load(self.configs.trainset))
return DataLoader(dataset, batch_size=self.configs.batchSize,
num_workers=self.cpu, pin_memory=self.pin)
def val_dataloader(self):
dataset = TrainDataset(torch.load(self.configs.valset))
return DataLoader(dataset, batch_size=self.configs.batchSize,
num_workers=self.cpu, pin_memory=self.pin)
def test_dataloader(self):
dataset = TestDataset(torch.load(self.configs.testset))
return DataLoader(dataset, batch_size=1, num_workers=self.cpu, pin_memory=self.pin)
if __name__ == "__main__":
device = 'cuda' if torch.cuda.is_available() else 'cpu'
path = "datasetsmall.pt"
# cd = CustomDataset(configs)
# print(cd[0]['input'].size())