-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess.py
More file actions
32 lines (26 loc) · 977 Bytes
/
preprocess.py
File metadata and controls
32 lines (26 loc) · 977 Bytes
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
import os
import numpy as np
import cv2
import torch
from torch.utils.data import Dataset
class DriveDataset(Dataset):
def __init__(self, images_path, masks_path):
self.images_path = images_path
self.masks_path = masks_path
self.n_samples = len(images_path)
def __getitem__(self, index):
""" Reading image """
image = cv2.imread(self.images_path[index], cv2.IMREAD_COLOR)
image = image/255.0 ## (512, 512, 3)
image = np.transpose(image, (2, 0, 1)) ## (3, 512, 512)
image = image.astype(np.float32)
image = torch.from_numpy(image)
""" Reading mask """
mask = cv2.imread(self.masks_path[index], cv2.IMREAD_GRAYSCALE)
mask = mask/255.0 ## (512, 512)
mask = np.expand_dims(mask, axis=0) ## (1, 512, 512)
mask = mask.astype(np.float32)
mask = torch.from_numpy(mask)
return image, mask
def __len__(self):
return self.n_samples