-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheth_mugs_dataset.py
63 lines (50 loc) · 2.33 KB
/
eth_mugs_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
"""ETH Mugs Dataset."""
import os
from PIL import Image
import torch
from torch.utils.data import Dataset
from torchvision import transforms
from torchvision.io import read_image, ImageReadMode
from utils import IMAGE_SIZE, load_mask
# This is only an example - you DO NOT have to use it exactly in this form!
class ETHMugsDataset(Dataset):
"""Torch dataset template shared as an example."""
#Note: mode = train should be changed to just mode, otherwise will be train even when test arg passed
def __init__(self, root_dir, mode):
"""This dataset class loads the ETH Mugs dataset.
It will return the resized image according to the scale and mask tensors
in the original resolution.
Args:
root_dir (str): Path to the root directory of the dataset.
mode (str): Mode of the dataset. It can be "train", "val" or "test"
"""
self.mode = mode
self.root_dir = root_dir
# TODO: get image and mask paths
self.rgb_dir = os.path.join(self.root_dir, "rgb").replace("/","\\")
self.mask_dir = os.path.join(self.root_dir, "masks").replace("/","\\")
self.image_paths = os.listdir(self.rgb_dir)
self.mask_paths = os.listdir(self.mask_dir)
# TODO: set image transforms - these transforms will be applied to pre-process the data before passing it through the model
self.transform = None # TO-DO
print("[INFO] Dataset mode:", mode)
print(
"[INFO] Number of images in the {} dataset : {}".format(mode, len(self.image_paths))
)
def __len__(self):
"""Return the length of the dataset."""
return len(self.image_paths)
def __getitem__(self, idx: int):
"""Get an item from the dataset."""
# TODO: load image and gt mask (unless when in test mode), apply transforms if necessary
'''
if self.mode == "test":
mask_path = os.path.join (self.mask_dir, self.image_paths[idx])
mask = read_image(mask_path, ImageReadMode = ImageReadMode.UNCHANGED)
return mask
'''
mask_path = os.path.join (self.mask_dir, self.mask_paths[idx])
mask = read_image(mask_path)
img_path = os.path.join(self.rgb_dir, self.image_paths[idx])
image = read_image(img_path)
return image, mask