-
Notifications
You must be signed in to change notification settings - Fork 24
/
dataloader.py
71 lines (57 loc) · 2.73 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
import cv2
import numpy as np
import torch
import torchvision.transforms as transforms
from scipy import ndimage
from glob import glob
class segDataset(torch.utils.data.Dataset):
def __init__(self, root, training, transform=None):
super(segDataset, self).__init__()
self.root = root
self.training = training
self.transform = transform
self.IMG_NAMES = sorted(glob(self.root + '/*/images/*.jpg'))
self.BGR_classes = {'Water' : [ 41, 169, 226],
'Land' : [246, 41, 132],
'Road' : [228, 193, 110],
'Building' : [152, 16, 60],
'Vegetation' : [ 58, 221, 254],
'Unlabeled' : [155, 155, 155]} # in BGR
self.bin_classes = ['Water', 'Land', 'Road', 'Building', 'Vegetation', 'Unlabeled']
def __getitem__(self, idx):
img_path = self.IMG_NAMES[idx]
mask_path = img_path.replace('images', 'masks').replace('.jpg', '.png')
image = cv2.imread(img_path)
mask = cv2.imread(mask_path)
cls_mask = np.zeros(mask.shape)
cls_mask[mask == self.BGR_classes['Water']] = self.bin_classes.index('Water')
cls_mask[mask == self.BGR_classes['Land']] = self.bin_classes.index('Land')
cls_mask[mask == self.BGR_classes['Road']] = self.bin_classes.index('Road')
cls_mask[mask == self.BGR_classes['Building']] = self.bin_classes.index('Building')
cls_mask[mask == self.BGR_classes['Vegetation']] = self.bin_classes.index('Vegetation')
cls_mask[mask == self.BGR_classes['Unlabeled']] = self.bin_classes.index('Unlabeled')
cls_mask = cls_mask[:,:,0]
if self.training==True:
if self.transform:
image = transforms.functional.to_pil_image(image)
image = self.transform(image)
image = np.array(image)
# 90 degree rotation
if np.random.rand()<0.5:
angle = np.random.randint(4) * 90
image = ndimage.rotate(image,angle,reshape=True)
cls_mask = ndimage.rotate(cls_mask,angle,reshape=True)
# vertical flip
if np.random.rand()<0.5:
image = np.flip(image, 0)
cls_mask = np.flip(cls_mask, 0)
# horizonal flip
if np.random.rand()<0.5:
image = np.flip(image, 1)
cls_mask = np.flip(cls_mask, 1)
image = cv2.resize(image, (512,512))/255.0
cls_mask = cv2.resize(cls_mask, (512,512))
image = np.moveaxis(image, -1, 0)
return torch.tensor(image).float(), torch.tensor(cls_mask, dtype=torch.int64)
def __len__(self):
return len(self.IMG_NAMES)