-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
50 lines (37 loc) · 1.77 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
import config
import cv2
import torch
import numpy as np
import tifffile as tiff
from skimage import io, transform, filters
class HubDataset(torch.utils.data.Dataset):
def __init__(self, image_path, mask_path, pixel_size=None, augmentations=None):
self.image_path = image_path
self.mask_path = mask_path
self.augmentations = augmentations
self.pixel_size = pixel_size
def __len__(self):
return len(self.image_path)
def __getitem__(self,item):
image = tiff.imread(self.image_path[item])
mask = io.imread(self.mask_path[item])
mask = mask.reshape(mask.shape[0],mask.shape[1],1)
image = image.astype(np.float32)/255
mask = mask.astype(np.float32)/255
# s = self.pixel_size/0.4 * (config.IMAGE_SIZE/image.shape[0])
## resize
#image = cv2.resize(image,dsize=None, fx=s,fy=s,interpolation=cv2.INTER_LINEAR)
#mask = cv2.resize(mask, dsize=None, fx=s,fy=s,interpolation=cv2.INTER_LINEAR)
image = cv2.resize(image, dsize=(config.IMAGE_SIZE, config.IMAGE_SIZE),interpolation=cv2.INTER_LINEAR)
mask = cv2.resize(mask, dsize=(config.IMAGE_SIZE, config.IMAGE_SIZE),interpolation=cv2.INTER_LINEAR)
# image = image - np.min(image)
# image = image / np.max(image)
if self.augmentations is not None:
image, mask = self.augmentations(image, mask)
image = np.transpose(image, (2, 0, 1)).astype(np.float32)
mask = mask.reshape(mask.shape[0],mask.shape[1],1) ## small problem soving: that adds channels to remove bug
mask = np.transpose(mask, (2, 0, 1)).astype(np.float32)
return {
"image": torch.tensor(image, dtype=torch.float),
"mask" : torch.tensor(mask, dtype=torch.float),
}