-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
75 lines (55 loc) · 2.42 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
from pathlib import Path
import torch
import numpy as np
import pandas as pd
import imgaug
from imgaug.augmentables.bbs import BoundingBox
class CardiacDataset(torch.utils.data.Dataset):
def __init__(self, path_to_labels_csv, patients, root_path, augs):
self.labels = pd.read_csv(path_to_labels_csv)
self.patients = np.load(patients)
self.root_path = Path(root_path)
self.augment = augs
def __len__(self):
"""
Returns the length of the dataset
"""
return len(self.patients)
def __getitem__(self, idx):
"""
Returns an image paired with bbox around the heart
"""
patient = self.patients[idx]
# Get data according to index
data = self.labels[self.labels["name"]==patient]
# Extract the patiendID (the filename)
patientId = data["name"].item()
# Get entries of given patient
# Extract coordinates
bbox = []
x_min = data["x0"].item()
bbox.append(x_min)
y_min = data["y0"].item()
bbox.append(y_min)
x_max = x_min + data["w"].item() # get xmax from width
bbox.append(x_max)
y_max = y_min + data["h"].item() # get ymax from height
bbox.append(y_max)
# Load file and convert to float32
file_path = self.root_path/str(patientId) # Create the path to the file
img = np.load(f"{file_path}.npy").astype(np.float32)
# Apply imgaug augmentations to image and bounding box
if self.augment:
bb = BoundingBox(x1=bbox[0], y1=bbox[1], x2=bbox[2], y2=bbox[3])
###################IMPORTANT###################
# Fix for https://discuss.pytorch.org/t/dataloader-workers-generate-the-same-random-augmentations/28830/2
random_seed = torch.randint(0, 1000000, (1,))[0].item()
imgaug.seed(random_seed)
#####################################################
img, aug_bbox = self.augment(image=img, bounding_boxes=bb)
bbox = aug_bbox[0][0], aug_bbox[0][1], aug_bbox[1][0], aug_bbox[1][1]
# Normalize the image according to the values computed in Preprocessing
img = (img - 0.494) / 0.252
img = torch.tensor(img).unsqueeze(0)
bbox = torch.tensor(bbox)
return img, bbox