-
Notifications
You must be signed in to change notification settings - Fork 7
/
msu_leaves_dataset.py
154 lines (136 loc) · 6.78 KB
/
msu_leaves_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
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
147
148
149
150
151
152
153
154
import torch
from torch.utils.data import Dataset, DataLoader
import cv2
import glob
import numpy as np
import random
import torchvision.transforms.functional as TF
import torchvision.transforms as transforms
from numba import njit
class MSUDenseLeavesDataset(Dataset):
def __init__(self, filepath, num_targets, random_augmentation=False, augm_probability=0.2):
self.filepath = filepath
# each sample is made of image-labels-mask (important to sort by name!)
self.images = sorted(glob.glob(filepath + '*_img.png'))
self.labels = sorted(glob.glob(filepath + '*_label.png'))
self.masks = sorted(glob.glob(filepath + '*_mask.png'))
self.n_samples = len(self.images)
self.multiscale_loss_targets = num_targets
self.augmentation = random_augmentation
self.probability = augm_probability
def __len__(self):
return self.n_samples
def __getitem__(self, item):
# read image-labels-mask and return them
image = cv2.imread(self.images[item])
label = cv2.imread(self.labels[item])
mask = cv2.imread(self.masks[item])
# HxWxC-->CxHxW, bgr->rgb and tensor transformation
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = torch.from_numpy(image.transpose(2, 0, 1) / 255.).float()
# todo bug in generating dataset made following map have 3 (identical)
# channels instead of 1
label = label[:, :, 0] / 255.
mask = mask[:, :, 0] / 255.
# label = torch.from_numpy(label).long()
# mask = torch.from_numpy(mask).float()
# augmentation
if self.augmentation:
# augment data randomly
if random.random() > (1 - self.probability):
# rotate
angle = random.randint(0, 90)
flip_rnd = random.random()
# print('Rotating about', angle,'angle and additionally flipping with probability', flip_rnd)
def rotate(img, angle):
img = TF.to_pil_image(img)
img = TF.rotate(img, angle)
# additionally flip image with some prob
trans = []
if flip_rnd > (1-0.2):
trans.append(transforms.RandomVerticalFlip(1.0))
if flip_rnd > (1-0.4):
trans.append(transforms.RandomHorizontalFlip(1.0))
trans.append(transforms.ToTensor())
flip = transforms.Compose(trans)
return flip(img)
image = rotate(image, angle)
label = rotate(torch.from_numpy(label).float(), angle).squeeze().numpy()
mask = rotate(torch.from_numpy(mask).float(), angle).squeeze().numpy()
# print((label.shape, mask.shape), (label.dtype, mask.dtype))
# labels multiscale resizing
targets, masks = multiscale_target(self.multiscale_loss_targets, label, mask)
# reverse order of multiscale labels (long cast for CE loss)
return image, [torch.from_numpy(t).unsqueeze(0) for t in reversed(targets)], [torch.from_numpy(m).unsqueeze(0) for m in reversed(masks)]
# normal multiscale does not achieve same results
# def multiscale(n_scaling, target, mask):
# target = target.astype(np.float32)
# mask = mask.astype(np.float32)
# targets = [target]
# masks = [mask]
# parent_t = target
# parent_mask = mask
# for t in range(n_scaling):
# scaled_t = cv2.resize(parent_t, (int(parent_t.shape[0]/2), int(parent_t.shape[1]/2)), interpolation=cv2.INTER_NEAREST).astype(np.float32)
# scaled_m = cv2.resize(parent_mask, (int(parent_t.shape[0]/2), int(parent_t.shape[1]/2)), interpolation=cv2.INTER_NEAREST).astype(np.float32)
# targets.append(scaled_t)
# masks.append(scaled_m)
#
# parent_t = scaled_t
# parent_mask = scaled_m
# return targets, masks
@njit
def multiscale_target(n_targets, target, mask):
# targets = np.empty((self.multiscale_loss_targets, target.shape[0], target.shape[1]))
targets = [target.astype(np.float32)]
masks = [mask.astype(np.float32)]
# outputs as many targets as the number of evaluations in the multiscale loss
parent_target = target.astype(np.float32).copy() # remember to uniform to same type(float32) or numba will complain
parent_mask = mask.astype(np.float32).copy()
for t in range(n_targets - 1):
scaled_target = np.zeros((int(parent_target.shape[0] / 2), int(parent_target.shape[1] / 2))).astype(np.float32)
scaled_mask = np.zeros((int(parent_target.shape[0] / 2), int(parent_target.shape[1] / 2))).astype(np.float32)
for y, i in enumerate(range(1, parent_target.shape[0] - 1, 2)):
for x, j in enumerate(range(1, parent_target.shape[1] - 1, 2)):
# check neighbour pixels for edges (clockwise check order)
if parent_target[i, j - 1] == 1.0 or parent_target[i - 1, j] == 1.0 or \
parent_target[i, j + 1] == 1.0 or parent_target[i + 1, j] == 1.0:
scaled_target[y, x] = 1.0
scaled_mask[y, x] = 1.0
# else if any of its parents are unknown it is unknown,
elif parent_target[i, j - 1] == 0.0 or parent_target[i - 1, j] == 0.0 or \
parent_target[i, j + 1] == 0.0 or parent_target[i + 1, j] == 0.0:
scaled_target[y, x] = 0.0
scaled_mask[y, x] = 0.0
if parent_mask[i, j - 1] == 1.0 or parent_mask[i - 1, j] == 1.0 or \
parent_mask[i, j + 1] == 1.0 or parent_mask[i + 1, j] == 1.0:
# interior pixel
scaled_mask[y, x] = 1.0
targets.append(scaled_target)
masks.append(scaled_mask)
parent_target = scaled_target
parent_mask = scaled_mask
return targets, masks
if __name__ == '__main__':
dataset = MSUDenseLeavesDataset('/home/nick/datasets/DenseLeaves/leaves_edges/', num_targets=5,
random_augmentation=True,
augm_probability=1.0)
dataloader = DataLoader(dataset, batch_size=24)
img, l, m = dataset[10]
print(img.shape) # , l.shape, m.shape)
img = img.permute(1, 2, 0).numpy() * 255
img = img.astype(np.uint8)
print(img.shape)
cv2.imshow('img', cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
cv2.waitKey(0)
for target, mask in zip(l, m):
target = target.squeeze().numpy()
mask = mask.squeeze().numpy()
print(target.shape, mask.shape)
cv2.imshow('imga', target)
cv2.imshow('imgb', mask)
cv2.waitKey(0)
# cv2.imshow('labels', l.numpy().astype(np.uint8)*255)
# cv2.waitKey(0)
# cv2.imshow('mask', m.numpy().astype(np.uint8)*255)
# cv2.waitKey(0)