-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
224 lines (182 loc) · 8.95 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pandas as pd
import torchio as tio
import torch
import numpy as np
# Generate list of torchIO subjects from CSV file
def GenerateTIOSubjectsList(CSVFile):
df = pd.read_csv(CSVFile, sep=',')
File_list = df['Combined'].tolist()
TIOSubjects_list = []
for idx in range(len(File_list)):
TIOSubject = tio.Subject(
Combined = tio.ScalarImage(File_list[idx]),
)
TIOSubjects_list.append(TIOSubject)
return File_list, TIOSubjects_list
# split ROI into tiles (3x3, or 5x5)
# initial shape [bs=2000,1,45,45,120]
# new shape for 3x3: [bs=2000,9,15,15,120]
def roi_split(t_roi, tile_size, adjacent_tiles_dim):
# t_roi_shape = [B,C,H,W,D]
t_roi_shape = t_roi.shape
#print('\t t_roi_shape: ',t_roi_shape)
# Remove channel layer
a = torch.squeeze(t_roi)
# Reshape tensor [bs=2000,3,15,3,15,120]
b = a.reshape(t_roi_shape[0],adjacent_tiles_dim,tile_size,adjacent_tiles_dim,tile_size,-1)
# Swap axes [bs=2000,3,3,15,15,120]
c = b.swapaxes(2,3)
# Combine channels [bs=2000,3x3,15,15,120]
d = c.reshape(t_roi_shape[0],adjacent_tiles_dim*adjacent_tiles_dim,tile_size,tile_size,-1)
# Remove last dimension of size 1 when needed (D=1 for TargetDisparity)
e = torch.squeeze(d,axis=4)
return e
# Prepare data as multiple inputs to network (tensors)
# Perform data filtering if enabled, using Confidence and DispLMA maps
def prepare_data_withfiltering(t_input, nb_image_layers, nb_corr_layers, tile_size, adjacent_tiles_dim, is_filtering=0, confidence_threshold=0.0):
t_input_Corr = t_input[:,:,:,:,0:nb_corr_layers]
t_input_TargetDisp = t_input[:,:,:,:,-4]
t_GroundTruth = t_input[:,:,:,:,-3]
t_Confidence = t_input[:,:,:,:,-2]
t_DispLMA = t_input[:,:,:,:,-1]
# print('t_input.type: ', t_input.type())
# # torch.Size([2000, 1, 45, 45, 122])
# print('t_input.shape: ', t_input.shape)
# print('t_input_Corr.shape: ', t_input_Corr.shape)
# print('t_input_TargetDisp.shape: ', t_input_TargetDisp.shape)
# print('t_GroundTruth.shape: ', t_GroundTruth.shape)
# Generate tiles when needed
if (adjacent_tiles_dim == 1):
t_input_Corr_tiles = t_input_Corr
t_input_TargetDisp_tiles = t_input_TargetDisp
t_GroundTruth_tiles = t_GroundTruth
t_Confidence_tiles = t_Confidence
t_DispLMA_tiles = t_DispLMA
else:
# Split t_input into neighboring tiles
t_input_Corr_tiles = roi_split(t_input_Corr, tile_size, adjacent_tiles_dim)
# # torch.Size([2000, 9, 15, 15, 120])
# print('t_input_Corr_tiles.shape: ', t_input_Corr_tiles.shape)
t_input_TargetDisp_tiles = roi_split(t_input_TargetDisp, tile_size, adjacent_tiles_dim)
# print('t_input_TargetDisp_tiles.shape: ', t_input_TargetDisp_tiles.shape)
# # torch.Size([2000, 9, 15, 15])
t_GroundTruth_tiles = roi_split(t_GroundTruth, tile_size, adjacent_tiles_dim)
t_Confidence_tiles = roi_split(t_Confidence, tile_size, adjacent_tiles_dim)
t_DispLMA_tiles = roi_split(t_DispLMA, tile_size, adjacent_tiles_dim)
# Generate input_TargetDisp_tiles_real, t_GroundTruth_tiles_real, scaling back to 62x78 pixels
t_input_TargetDisp_tiles_real = t_input_TargetDisp_tiles[:,:,::tile_size,::tile_size]
t_GroundTruth_tiles_real = t_GroundTruth_tiles[:,:,::tile_size,::tile_size]
t_Confidence_tiles_real = t_Confidence_tiles[:,:,::tile_size,::tile_size]
t_DispLMA_tiles_real = t_DispLMA_tiles[:,:,::tile_size,::tile_size]
# # torch.Size([2000, 9, 1, 1])
# print('\nt_input_TargetDisp_tiles_real.shape: ', t_input_TargetDisp_tiles_real.shape)
# print('t_GroundTruth_tiles_real.shape: ', t_GroundTruth_tiles_real.shape)
# print('t_Confidence_tiles_real.shape: ', t_Confidence_tiles_real.shape)
# print('t_DispLMA_tiles_real.shape: ', t_DispLMA_tiles_real.shape)
# - - - - - - - -
# Data filtering
# print('Data filtering...')
if is_filtering:
t_DispLMA_tiles_real_Mask = ~torch.isnan(t_DispLMA_tiles_real)
t_Confidence_tiles_real_Mask = torch.where(t_Confidence_tiles_real >= confidence_threshold, 1, 0)
t_Mask = torch.logical_and(t_DispLMA_tiles_real_Mask, t_Confidence_tiles_real_Mask)
t_Mask = torch.squeeze(t_Mask)
if (adjacent_tiles_dim != 1):
t_Mask = torch.all(t_Mask, axis=1)
# print('t_Mask.shape: ', t_Mask.shape)
# print('t_Mask[:20]: ', t_Mask[:20,...])
t_input_Corr_tiles_filtered = t_input_Corr_tiles[t_Mask]
t_input_TargetDisp_tiles_real_filtered = t_input_TargetDisp_tiles_real[t_Mask]
t_GroundTruth_tiles_real_filtered = t_GroundTruth_tiles_real[t_Mask]
# print('t_input_Corr_tiles_filtered.shape: ', t_input_Corr_tiles_filtered.shape)
# print('t_input_TargetDisp_tiles_real_filtered.shape: ', t_input_TargetDisp_tiles_real_filtered.shape)
else:
t_input_Corr_tiles_filtered = t_input_Corr_tiles
t_input_TargetDisp_tiles_real_filtered = t_input_TargetDisp_tiles_real
t_GroundTruth_tiles_real_filtered = t_GroundTruth_tiles_real
# Define center tile for GroundTruth and TargetDisp maps
if (adjacent_tiles_dim == 3):
IndexCenterTile = 4
elif (adjacent_tiles_dim == 5):
IndexCenterTile = 12
else:
IndexCenterTile = 0
t_input_TargetDisp_real_filtered_center = t_input_TargetDisp_tiles_real_filtered[:,IndexCenterTile,...]
t_GroundTruth_real_filtered_center = t_GroundTruth_tiles_real_filtered[:,IndexCenterTile,...]
# print('t_GroundTruth_tiles.shape: ', t_GroundTruth_tiles.shape)
# print('t_GroundTruth_tiles_real.shape: ', t_GroundTruth_tiles_real.shape)
# print('t_GroundTruth_tiles_real_filtered.shape: ', t_GroundTruth_tiles_real_filtered.shape)
# print('t_GroundTruth_real_filtered_center.shape: ', t_GroundTruth_real_filtered_center.shape)
# print('t_input_TargetDisp_tiles.shape: ', t_input_TargetDisp_tiles.shape)
# print('t_input_TargetDisp_tiles_real.shape: ', t_input_TargetDisp_tiles_real.shape)
# print('t_input_TargetDisp_tiles_real_filtered.shape: ', t_input_TargetDisp_tiles_real_filtered.shape)
# print('t_input_TargetDisp_real_filtered_center.shape: ', t_input_TargetDisp_real_filtered_center.shape)
return t_input_Corr_tiles_filtered, t_input_TargetDisp_real_filtered_center, t_GroundTruth_real_filtered_center
# Initialize TorchIO GridSampler variables
# Generate patch_overlap based on adjacent_tiles_dim
def initialize_gridsampler_variables(nb_image_layers, tile_size, adjacent_tiles_dim, padding_mode=None):
# Define patch_size
patch_size = (adjacent_tiles_dim * tile_size, adjacent_tiles_dim * tile_size, nb_image_layers)
# Define padding_mode
#padding_mode = 'symmetric'
# Define patch_overlap
if (adjacent_tiles_dim == 1):
patch_overlap = (0,0,0)
elif (adjacent_tiles_dim == 3):
# patch_overlap = (30,30,0)
patch_overlap = (2*tile_size,2*tile_size,0)
elif (adjacent_tiles_dim == 5):
# patch_overlap = (60,60,0)
patch_overlap = (4*tile_size,4*tile_size,0)
else:
print("Error initialize_gridsampler_variables - adjacent_tiles_dim")
sys.exit()
# print('patch_size: ',patch_size)
# print('patch_overlap: ',patch_overlap)
# print('padding_mode: ',padding_mode)
padding_mode = padding_mode
return patch_size, patch_overlap, padding_mode
# Initialize TorchIO uniform Sampler variables
# patch_overlap = (0,0,0) # Not directly used
# patch overlap is generated by the random locations
def initialize_uniformsampler_variables(nb_image_layers, tile_size, adjacent_tiles_dim, padding_mode=None):
# Define patch_size
patch_size = (adjacent_tiles_dim * tile_size, adjacent_tiles_dim * tile_size, nb_image_layers)
# Define patch_overlap
patch_overlap = (0,0,0)
# Define padding_mode
#padding_mode = 'symmetric'
padding_mode = padding_mode
# print('patch_size: ',patch_size)
# print('patch_overlap: ',patch_overlap)
# print('padding_mode: ',padding_mode)
return patch_size, patch_overlap, padding_mode
# Generate TorchIO aggregator patch_location for prediction
# Example - Input patch location for Tiles 5x5 = [ 0, 0, 0, 75, 75, 122]
# Example - Output patch location for Tiles5x5 = [ 2, 2, 0, 3, 3, 1]
# - Use CenterTile location
# - Divide by TileSize
# - Depth = 1
def prediction_patch_location(input_location, tile_size, adjacent_tiles_dim):
if (adjacent_tiles_dim == 1):
output_location = input_location
elif (adjacent_tiles_dim == 3):
#CenterTile_Update = torch.tensor([15,15,0,-15,-15,0], dtype=torch.int64)
CenterTile_Update = torch.tensor([tile_size,tile_size,0,-tile_size,-tile_size,0], dtype=torch.int64)
output_location = input_location + CenterTile_Update[None,:]
elif (adjacent_tiles_dim == 5):
#CenterTile_Update = torch.tensor([30,30,0,-30,-30,0], dtype=torch.int64)
CenterTile_Update = torch.tensor([2*tile_size,2*tile_size,0,-2*tile_size,-2*tile_size,0], dtype=torch.int64)
output_location = input_location + CenterTile_Update[None,:]
else:
print("Error prediction_patch_location - adjacent_tiles_dim")
sys.exit()
# print('\t\t output_location shape: ', output_location.shape)
# print('\t\t output_location: ', output_location)
# Divide by tile_size
output_location = torch.div(output_location, tile_size, rounding_mode='floor')
# Update depth to 1 (from 3D volume to 2D image)
output_location[:,-1]=1
# print('\t\t output_location shape: ', output_location.shape)
# print('\t\t output_location: ', output_location)
return output_location