-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_old.py
158 lines (116 loc) · 5.87 KB
/
dataset_old.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import torch
from torch.utils.data import Dataset
import SimpleITK as sitk
import numpy as np
import torch.nn as nn
import matplotlib.pyplot as plt
from monai.transforms import ScaleIntensity
# from monai.transforms import SpatialPadd
class PelvisDataset(Dataset):
def __init__(self, root_dir, input_dim=(224, 224, 224), nlabels=4, transform=None):
super(PelvisDataset, self).__init__()
self.root_dir = root_dir
self.input_dim = input_dim
self.transform = transform
self.nlabels = nlabels
# define the path for images and their labels
self.images_path = self.root_dir + '/images/'
self.labels_path = self.root_dir + '/landmarks/'
# sort the list of images and labels so that they match
self.images = sorted(os.listdir(self.images_path))
self.labels = sorted(os.listdir(self.labels_path))
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
# Select the item of interest
img_name = self.images[idx]
label_name = self.labels[idx]
# print(img_name)
# read the images and labels and convert them to numpy arrays
image = sitk.ReadImage(self.images_path + img_name)
label = sitk.ReadImage(self.labels_path + label_name)
# Transform them if specified
if self.transform:
image, label = self.transform((image, label))
image = sitk.GetArrayFromImage(image)
label = sitk.GetArrayFromImage(label)
# image = (image_array - np.min(image_array)) / (np.max(image_array) - np.min(image_array))
# fig, axes = plt.subplots(1, 2, figsize=(10, 5))
# axes[1].imshow(image_array[22,:,:],cmap="gray")
# axes[1].set_title("before norm")
# axes[0].imshow(image[22,:,:],cmap="gray")
# axes[0].set_title("afternorm")
# print('image size', image.shape)
# print("label size", label.shape)
# Apply the transform to your image
# label_t = np.transpose(label, (2,1,0) )
# axes[1].imshow(label_t[22,:,:],cmap="gray")
# axes[1].set_title("label")
# axes[0].imshow(image[22,:,:],cmap="gray")
# axes[0].set_title("image")
label_img = np.copy(label)
# Establecer todos los valores distintos de 0 a 1
label_img[abs(label_img) > 1e-3] = 1
label_img[abs(label_img) <= 1e-3] = 0
# plt.imshow(label_img[22,:,:])
# plt.show()
# image = padding_layer(image)
# label = padding_layer(label_t)
# print('label resize', label_t.shape)
# Normalize the image
# image = (image - np.mean(image)) / np.std(image)
# Convert label to one-hot encoding
# label = label_t.astype(int)
# plt.imshow(label[22,:,:],cmap ="gray")
# plt.show()
# label[label >= self.nlabels] = 0
# label_one_hot = torch.nn.functional.one_hot(torch.from_numpy(label),
# self.nlabels + 1).float()
label_one_hot = torch.from_numpy(label_img).float().unsqueeze(0)
# change the order so that it is CxWxHxD instead of WxHxDxC
# label_tensor = label_one_hot.permute(3, 0, 1, 2)
label_tensor = label_one_hot[0:1,:,:,:]
# Convert image to PyTorch tensor so that dimensions are CxWxHxD
image_tensor = torch.from_numpy(image).float().unsqueeze(0)
# padding_layer = nn.ConstantPad3d(padding=(31, 31, 54, 54, 75, 76), value=0)
padding_layer = nn.ConstantPad3d(padding=(7, 7, 6, 6, 4, 3), value=0)
image_tensor = padding_layer(image_tensor)
label_tensor = padding_layer(label_tensor)
# print("label tensor", label_tensor.shape)
# print("image tensor", image_tensor.shape)
return image_tensor, label_tensor, img_name, label_name
# def plotitem(self, idx, slc=None):
# # Select the item of interest
# img_name = self.images[idx]
# label_name = self.labels[idx]
# # Read the images and labels
# image = sitk.ReadImage(self.images_path + img_name)
# image = np.array(sitk.GetArrayFromImage(image))
# label = sitk.ReadImage(self.labels_path + label_name)
# label = np.array(sitk.GetArrayFromImage(label))
# # Transform them if specified
# if self.transform:
# sample = (image, label)
# sample = self.transform(sample)
# image, label = sample
# # Select the middle slice if not specified
# if slc is None:
# slc = image.shape[2] // 2
# image = sitk.GetImageFromArray(image)
# label = sitk.GetImageFromArray(label)
# fig, axs = plt.subplots(2, 3, figsize=(10, 8))
# axs[0, 0].imshow(sitk.GetArrayViewFromImage(image)[:, :, slc], cmap=plt.cm.Greys_r)
# axs[0, 0].set_title(f'Sagittal plane image {img_name.strip(".nii.gz")}')
# axs[0, 1].imshow(sitk.GetArrayViewFromImage(image)[:, slc, :], cmap=plt.cm.Greys_r)
# axs[0, 1].set_title(f'Coronal Plane {img_name.strip(".nii.gz")}')
# axs[0, 2].imshow(sitk.GetArrayViewFromImage(image)[slc, :, :], cmap=plt.cm.Greys_r)
# axs[0, 2].set_title(f'Axial plane {img_name.strip(".nii.gz")}')
# axs[1, 0].imshow(sitk.GetArrayViewFromImage(label)[:, :, slc], cmap=plt.cm.Greys_r)
# axs[1, 0].set_title(f'Sagittal plane label {label_name.strip(".nii.gz")}')
# axs[1, 1].imshow(sitk.GetArrayViewFromImage(label)[:, slc, :], cmap=plt.cm.Greys_r)
# axs[1, 1].set_title(f'Coronal Plane {label_name.strip(".nii.gz")}')
# axs[1, 2].imshow(sitk.GetArrayViewFromImage(label)[slc, :, :], cmap=plt.cm.Greys_r)
# axs[1, 2].set_title(f'Axial plane {label_name.strip(".nii.gz")}')
# plt.tight_layout()
# plt.show()