-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataloader.py
61 lines (43 loc) · 2 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
import os
from skimage import io, transform, color, img_as_ubyte
import numpy as np
from torch.utils.data import Dataset
import cv2
import torch
import torchvision.transforms as pytorch_transforms
import torch.nn.functional as F
from albumentations.pytorch.transforms import ToTensor
import albumentations as A
class BinaryLoader(Dataset):
def __init__(self, data_name, jsfiles, transforms, pixel_mean=[123.675, 116.280, 103.530], pixel_std=[58.395, 57.12, 57.375]):
self.path = f'/data/xq/sam_med/datasets/{data_name}'
self.jsfiles = jsfiles
self.img_tesnor = pytorch_transforms.Compose([pytorch_transforms.ToTensor(), ])
self.transforms = transforms
self.img_size = 1024
self.pixel_mean = torch.Tensor(pixel_mean).view(-1, 1, 1)
self.pixel_std = torch.Tensor(pixel_mean).view(-1, 1, 1)
def __len__(self):
return len(self.jsfiles)
def __getitem__(self,idx):
image_id = list(self.jsfiles[idx].split('.'))[0]
image_path = os.path.join(self.path,'image_1024/',image_id)
mask_path = os.path.join(self.path,'masks_binary/',image_id)
img = io.imread(image_path+'.png')[:,:,:3].astype('float32')
mask = io.imread(mask_path+'.png', as_gray=True)
data_group = self.transforms(image=img, mask=mask)
img_resized = data_group['image']
mask = data_group['mask']
img = self.img_tesnor(img)
img = self.preprocess(img)
return (img_resized, img, mask, image_id)
def preprocess(self, x):
"""Normalize pixel values and pad to a square input."""
# Normalize colors
x = (x - self.pixel_mean) / self.pixel_std
# Pad
h, w = x.shape[-2:]
padh = self.img_size - h
padw = self.img_size - w
x = F.pad(x, (0, padw, 0, padh))
return x