-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
173 lines (117 loc) · 5.29 KB
/
data_loader.py
File metadata and controls
173 lines (117 loc) · 5.29 KB
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
import torch
from utils import *
import numpy as np
import os
import imageio
import torch.nn.functional as F
from skimage.transform import iradon, radon
class CT_dataset(torch.utils.data.Dataset):
def __init__(self, directory, network = 'glimpse'):
self.directory = directory
self.name_list = sorted(os.listdir(self.directory))
self.network = network
def __len__(self):
return len(self.name_list)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
file_name = self.name_list[idx]
file = np.load(os.path.join(self.directory,file_name))
image = file['image']
image = torch.tensor(image, dtype = torch.float32)
if self.network == 'unet':
fbp = file['fbp']
fbp = torch.tensor(fbp, dtype = torch.float32)[None,...]
return image[None,...], fbp
elif self.network == 'iradon':
sinogram = file['sinogram']
sinogram = torch.tensor(sinogram, dtype = torch.float32)
return image[None,...], sinogram
else:
sinogram = file['sinogram']
sinogram = torch.tensor(sinogram, dtype = torch.float32)
image = image.reshape(-1, 1)
return image, sinogram
class CT_odl(torch.utils.data.Dataset):
def __init__(self, directory):
self.directory = directory
self.name_list = sorted(os.listdir(self.directory))
def __len__(self):
return len(self.name_list)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
file_name = self.name_list[idx]
data = np.load(os.path.join(self.directory,file_name))
image = data['image']
sinogram = data['sinogram']
fbp = data['fbp']
image = torch.tensor(image, dtype = torch.float32)[None,...]
fbp = torch.tensor(fbp, dtype = torch.float32)[None,...]
sinogram = torch.tensor(sinogram, dtype = torch.float32)[None,...]
# print(image.shape, fbp.shape, sinogram.shape)
################ Remove it later
# image = F.interpolate(image[None,...], size = image.shape[1]* (256//128),
# mode = 'bilinear',
# antialias= True,
# align_corners= True)[0]
# fbp = F.interpolate(fbp[None,...], size = fbp.shape[1]* (256//128),
# mode = 'bilinear',
# antialias= True,
# align_corners= True)[0]
# sinogram = F.interpolate(sinogram[None,...], size = (sinogram.shape[1],363),
# mode = 'bilinear',
# antialias= True,
# align_corners= True)[0]
# print(image.shape, fbp.shape, sinogram.shape)
return image, sinogram, fbp
class CT_images(torch.utils.data.Dataset):
def __init__(self, directory, image_size, theta_actual, theta_init,
noise_snr = 30, unet = False, subset = 'train'):
self.directory = directory
self.name_list = sorted(os.listdir(self.directory))
self.image_size = image_size
self.unet = unet
self.theta_actual = theta_actual
self.theta_init = theta_init
self.noise_snr = noise_snr
self.subset = subset
def __len__(self):
return len(self.name_list)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
np.random.seed(idx + len(self.name_list))
file_name = self.name_list[idx]
if self.subset == 'ood':
image = imageio.imread(os.path.join(self.directory,file_name))
image = (image/255.0)
elif self.subset == 'div2k':
image = imageio.imread(os.path.join(self.directory,file_name))
image = (image/255.0)
# print(image.shape)
image = np.mean(image, axis = 3)
else:
image = np.load(os.path.join(self.directory,file_name))
image = torch.tensor(image, dtype = torch.float32)[None,None]
image = F.interpolate(image, size = self.image_size,
mode = 'bilinear',
antialias= True,
align_corners= True)[0,0].cpu().detach().numpy()
sinogram = radon(image, theta=self.theta_actual, circle= False)
noise_sigma = 10**(-self.noise_snr/20.0)*np.sqrt(np.mean(np.sum(
np.square(np.reshape(sinogram, (1 , -1))) , -1)))
noise = np.random.normal(loc = 0,
scale = noise_sigma,
size = np.shape(sinogram))/np.sqrt(np.prod(np.shape(sinogram)))
sinogram += noise
image = torch.tensor(image, dtype = torch.float32)
# print(image.shape, sinogram.shape)
if self.unet:
fbp = iradon(sinogram, theta= self.theta_init, circle= False)
fbp = torch.tensor(fbp, dtype = torch.float32)[None,...]
return image[None,...], fbp
else:
sinogram = torch.tensor(sinogram, dtype = torch.float32)
image = image.reshape(-1, 1)
return image, sinogram