-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_function.py
74 lines (53 loc) · 2.33 KB
/
data_function.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
import torch
import os
import torchio as tio
from glob import glob
from preprocess import CustomTransform
from preprocess import Resample
import numpy as np
from os.path import join
class MedData_finetune(torch.utils.data.Dataset):
def __init__(self, images_dir, points_dir,patch_size):
queue_length = 24
samples_per_volume = 2
self.images = sorted(glob(os.path.join(images_dir, "volume*.nii.gz")))
self.images = [self.images[0]] #-15
self.subjects = []
self.query_points = []
self.occupancy = []
self.noise =[0, 0.3, 5, 10]
for img in self.images:
file_num = os.path.basename(img).split('.')[0].split('-')[-1]
for noise in self.noise:
p=np.load(join(points_dir , f'{file_num}_boundary_{str(noise)}_samples.npz'))
self.query_points.append(p['points'])
self.occupancy.append(p['occupancy'])
self.query_points = np.concatenate(self.query_points, axis= 0)
self.occupancy = np.concatenate(self.occupancy , axis= 0 )
subject = tio.Subject(
image = tio.ScalarImage(img),
points = self.query_points,
occupancy = self.occupancy
)
self.subjects.append(subject)
# self.transforms = self.transform()
self.training_set = tio.SubjectsDataset(self.subjects, transform=None)
self.queue_dataset = tio.Queue(
self.training_set,
queue_length,
samples_per_volume,
tio.UniformSampler(patch_size),
num_workers= 2,
)
class MedData_val(torch.utils.data.Dataset):
def __init__(self, images_dir, labels_dir):
self.images = sorted(glob(os.path.join(images_dir, "volume*.nii.gz")))[-15:] #-15
self.labels = sorted(glob(os.path.join(labels_dir, 'segmentation*.nii.gz')))[-15:]
self.subjects = []
for (img, lab) in zip(self.images, self.labels):
subject = tio.Subject(
image=tio.ScalarImage(img),
label = tio.LabelMap(lab),
)
self.subjects.append(subject)
self.val_set = tio.SubjectsDataset(self.subjects, transform=None)