-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataset.py
77 lines (60 loc) · 2.51 KB
/
dataset.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
from torch.utils.data import Dataset
import cv2
from PIL import Image
import torch
from torchvision import transforms as T
import albumentations as A
class DroneDataset(Dataset):
def __init__(self, img_path, mask_path, X, datasetType, mean=None, std=None, transform=False, patch=False):
self.img_path = img_path
self.mask_path = mask_path
self.X = X
self.transform = transform
self.datasetType = datasetType
self.patches = patch
self.mean = mean
self.std = std
def __len__(self):
return len(self.X)
def _transfrom(self):
if self.datasetType=='TRAIN':
t = A.Compose(
[A.Resize(768, 1024,
interpolation=cv2.INTER_NEAREST),
A.HorizontalFlip(),
A.VerticalFlip(),
A.GridDistortion(p=0.2),
A.RandomBrightnessContrast((0,0.5),(0,0.5)),
A.GaussNoise()])
elif self.datasetType=='VAL':
t = A.Compose(
[A.Resize(768, 1024,
interpolation=cv2.INTER_NEAREST),
A.HorizontalFlip(),
A.GridDistortion(p=0.2)])
elif self.datasetType=='TEST':
t = A.Resize(768, 1024, interpolation=cv2.INTER_NEAREST)
return t
def __getitem__(self, idx):
img = cv2.imread(self.img_path + self.X[idx] + '.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mask = cv2.imread(self.mask_path + self.X[idx] + '.png', cv2.IMREAD_GRAYSCALE)
if self.transform:
aug = self._transfrom()(image=img, mask=mask)
img = Image.fromarray(aug['image'])
mask = aug['mask']
else:
img = Image.fromarray(img)
if self.datasetType=='TRAIN' or self.datasetType=='VAL':
img = T.Compose([T.ToTensor(), T.Normalize(self.mean, self.std)])(img)
mask = torch.from_numpy(mask).long()
if self.patches:
img, mask = self.tiles(img, mask)
return img, mask
def tiles(self, img, mask):
img_patches = img.unfold(1, 512, 512).unfold(2, 768, 768)
img_patches = img_patches.contiguous().view(3,-1, 512, 768)
img_patches = img_patches.permute(1,0,2,3)
mask_patches = mask.unfold(0, 512, 512).unfold(1, 768, 768)
mask_patches = mask_patches.contiguous().view(-1, 512, 768)
return img_patches, mask_patches