-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
executable file
·95 lines (81 loc) · 3.58 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
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
import torch
from torch.utils.data import Dataset
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
import cv2
import pandas as pd
import os
from torchvision.io import read_image
import model_params as params
from pathlib import Path
from modules.utils import set_seeds
class CCBMDataset(Dataset):
def __init__(self, annotations_file, dir_path, masks_dir, extension, transform=None, target_transform=None):
"""Initializes a custom dataset given a CSV file.
:param annotations_file: The CSV file containing images and labels.
:param dir_path: The path directory of the images.
:param masks_dir: The path directory of the segmentation masks.
:param extension: The file extension of the images (jpg, png).
:param transform: A PyTorch Transform to be applied to the images.
:param target_transform: A PyTorch Transform to be applied to the labels
"""
self.img_labels = pd.read_csv(annotations_file)
self.dir_path = dir_path
self.masks_dir = masks_dir
self.extension = extension
self.transform = transform
self.target_transform = target_transform
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
# Get image path
img_path = os.path.join(self.dir_path, self.img_labels.iloc[idx, 0])
# Read image
if self.extension == "":
#image = read_image(img_path)
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if self.img_labels.iloc[idx, 0].find("/") != -1:
mask = cv2.imread(
os.path.join(self.masks_dir, self.img_labels.iloc[idx, 0][4:-4] + ".png"), cv2.IMREAD_UNCHANGED
)
else:
mask = cv2.imread(
os.path.join(self.masks_dir, self.img_labels.iloc[idx, 0][:-4] + ".png"), cv2.IMREAD_UNCHANGED
)
else:
#image = read_image(img_path + "." + self.extension)
image = cv2.imread(img_path + "." + self.extension)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if self.img_labels.iloc[idx, 0].find("/") != -1:
mask = cv2.imread(
os.path.join(self.masks_dir, self.img_labels.iloc[idx, 0][4:] + ".png"), cv2.IMREAD_UNCHANGED
)
else:
mask = cv2.imread(
os.path.join(self.masks_dir, self.img_labels.iloc[idx, 0] + ".png"), cv2.IMREAD_UNCHANGED
)
# Get image mask
#img_mask = read_image(os.path.join(self.masks_dir, self.img_labels.iloc[idx, 0] + ".png"))
# Get respective label
label = self.img_labels.iloc[idx, 1]
# Get Indicator Vectors
indicator_vectors_dict = np.load(params.INDICATOR_VECTORS, allow_pickle=True).item()
indicator_vectors_dict = {k.replace('.jpg', '').replace('.JPG', ''): v for k, v in indicator_vectors_dict.items()}
filename = Path(img_path).stem
indicator_vector = indicator_vectors_dict[filename]
# Apply transformations
if self.transform:
transformed = self.transform(image=image, mask=mask)
image = transformed["image"]
mask = transformed["mask"]
if self.target_transform:
label = self.target_transform(label)
return {
'image': image,
'label': label,
'ind_vec': indicator_vector,
'mask': mask,
'img_path': img_path
}