-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataClass.py
134 lines (99 loc) · 3.82 KB
/
DataClass.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
class MarioData(Dataset):
def __init__(self, ID_list, data_frame, data_path, transform=None):
self.new_Frame = data_frame[data_frame['id_patient'].isin(ID_list)]
self.data_path = data_path
self.transform = transform
def __len__(self):
return self.new_Frame.shape[0]
def __getitem__(self, item):
row = self.new_Frame.iloc[item]
im_ti = row['image_at_ti']
im_ti_1 = row['image_at_ti+1']
gt_label = row['label']
im_ti = np.load(os.path.join(self.data_path, f"{os.path.splitext(im_ti)[0]}.npy"))
im_ti_1 = np.load(os.path.join(self.data_path, f"{os.path.splitext(im_ti_1)[0]}.npy"))
label = torch.zeros(4)
if self.transform is not None:
im_ti = self.transform(im_ti)
im_ti_1 = self.transform(im_ti_1)
label[int(gt_label)] = 1
return im_ti, im_ti_1, label
class MarioDataTask2(Dataset):
def __init__(self, ID_list, DataFrame, DataPath, transform=None):
self.new_Frame = DataFrame[DataFrame['id_patient'].isin(ID_list)]
self.DataPath = DataPath
self.transform = transform
def __len__(self):
return self.new_Frame.shape[0]
def __getitem__(self, item):
row = self.new_Frame.iloc[item]
img = row['image']
gt_label = row['label']
img = np.load(os.path.join(self.DataPath, f"{os.path.splitext(img)[0]}.npy"))
label = torch.zeros(3)
if self.transform is not None:
img = self.transform(img)
label[int(gt_label)] = 1
return img, label
class MarioValData(Dataset):
def __init__(self, DataFrame, DataPath, transform=None):
self.new_Frame = DataFrame
self.DataPath = DataPath
self.transform = transform
def __len__(self):
return self.new_Frame.shape[0]
def __getitem__(self, item):
row = self.new_Frame.iloc[item]
im_ti = row['image_at_ti']
im_ti_1 = row['image_at_ti+1']
case = row['case']
im_ti = np.load(os.path.join(self.DataPath, f"{os.path.splitext(im_ti)[0]}.npy"))
im_ti_1 = np.load(os.path.join(self.DataPath, f"{os.path.splitext(im_ti_1)[0]}.npy"))
size1 = im_ti.size
size2 = im_ti_1.size
if self.transform is not None:
im_ti = self.transform(im_ti)
im_ti_1 = self.transform(im_ti_1)
return im_ti, im_ti_1, case
class MarioValDataTask2(Dataset):
def __init__(self, DataFrame, DataPath, transform=None):
self.new_Frame = DataFrame
self.DataPath = DataPath
self.transform = transform
def __len__(self):
return self.new_Frame.shape[0]
def __getitem__(self, item):
row = self.new_Frame.iloc[item]
image = row['image']
case = row['case']
image = np.load(os.path.join(self.DataPath, f"{os.path.splitext(image)[0]}.npy"))
if self.transform is not None:
image = self.transform(image)
return image, case
class mAEData(torch.utils.data.Dataset):
def __init__(self, cases, frame, data_path, transform=None):
self.cases = cases
self.frame = frame
self.data_path = data_path
self.transform = transform
def __len__(self):
return len(self.cases)
def __getitem__(self, idx):
img_id = self.frame['File Path'][idx]
img_path = os.path.join(self.data_path, f"{img_id}")
try:
img = Image.open(img_path)
except FileNotFoundError:
print(f"File not found: {img_path}")
raise
# Ensure image is in RGB format
if img.mode != 'L':
img = img.convert('L')
if self.transform:
img = self.transform(img)
return img