-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
175 lines (137 loc) · 6.72 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import pytorch_lightning as pl
from pytorch_lightning import LightningDataModule, LightningModule, Trainer
from torchmetrics.image import StructuralSimilarityIndexMeasure
from torchmetrics.image import PeakSignalNoiseRatio
import odl
import numpy as np
from odl.contrib import torch as odl_torch
class RegularizationBlock(nn.Module):
def __init__(self, in_channels=1, out_channels=1,kernel_size=5):
super(RegularizationBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 48, kernel_size=kernel_size, padding='same')
nn.init.normal_(self.conv1.weight, mean=0.0, std=0.01)
self.conv2 = nn.Conv2d(48, 48, kernel_size=kernel_size, padding='same')
nn.init.normal_(self.conv1.weight, mean=0.0, std=0.01)
self.conv3 = nn.Conv2d(48, out_channels, kernel_size=kernel_size, padding='same')
nn.init.normal_(self.conv1.weight, mean=0.0, std=0.01)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = self.conv3(x)
return x
class GradientFunction(nn.Module):
def __init__(self):
super(GradientFunction, self).__init__()
self.regularitation_term = RegularizationBlock()
self.alpha = torch.nn.Parameter(torch.tensor(0.1))
def forward(self,x_t, y, forward_module, backward_module):
data_fidelity_term = forward_module(x_t) - y
bp_data_fidelity = backward_module(data_fidelity_term)
reg_value = self.regularitation_term(x_t)
gradient = self.alpha * bp_data_fidelity + reg_value
return gradient
class LEARN_pl(pl.LightningModule):
def __init__(self, n_iterations,num_view):
super(LEARN_pl, self).__init__()
self.save_hyperparameters()
self.gradient_list = nn.ModuleList([GradientFunction() for _ in range(n_iterations)])
self.initial_lr = 1e-4
self.final_lr = 1e-5
self.num_iter = n_iterations
self.ssim = StructuralSimilarityIndexMeasure()
self.psnr = PeakSignalNoiseRatio()
#self.rmse = RootMeanSquaredErrorUsingSlidingWindow()
#radon_curr, fbp_curr = radon(num_view=num_view)
radon_curr, fbp_curr = self.radon_transform(num_view=num_view)
self.forward_module = radon_curr
self.backward_module = fbp_curr
self.grid = None
def forward(self,x_t,y):
x_t = x_t
for i in range(self.num_iter):
x_t = x_t - self.gradient_list[i](x_t, y, self.forward_module, self.backward_module)
return x_t
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr= self.initial_lr)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=5, eta_min=self.final_lr)
return (
{ "optimizer":optimizer,
"lr_scheduler": {
"scheduler": scheduler,
},
} )
def training_step(self, train_batch, batch_idx):
phantom, fbp_u, sino_noisy = train_batch
x_t = fbp_u
y = sino_noisy
initial = torch.rand(y.shape[0],1,256, 256).cuda()
x_reconstructed = self.forward(x_t, y)
loss = nn.functional.mse_loss(phantom, x_reconstructed)
#self.log('train_loss', loss)
self.logger.experiment.add_scalars('loss', {'train': loss},self.global_step)
return loss
def validation_step(self, val_batch, batch_idx):
phantom, fbp_u, sino_noisy = val_batch
x_t = fbp_u
y = sino_noisy
initial = torch.rand(y.shape[0],1,256, 256).cuda()
x_reconstructed = self.forward(x_t, y)
loss = nn.functional.mse_loss(phantom, x_reconstructed)
ssim_p = self.ssim(x_reconstructed, phantom)
psnr_p = self.psnr(x_reconstructed, phantom)
rmse_p = self.rmse(x_reconstructed, phantom)
#self.log('val_loss', loss)
self.logger.experiment.add_scalars('loss', {'validation': loss},self.global_step)
self.log('val_ssim', ssim_p)
self.log('val_psnr', psnr_p)
self.log('val_rmse', rmse_p)
self.grid = torchvision.utils.make_grid(x_reconstructed)
def test_step(self, batch, batch_idx):
phantom, fbp_u, sino_noisy = batch
x_t = fbp_u
y = sino_noisy
initial = torch.rand(y.shape[0],1,256, 256).cuda()
x_reconstructed = self.forward(x_t, y)
loss = nn.functional.mse_loss(phantom, x_reconstructed)
ssim_p = self.ssim(x_reconstructed, phantom)
psnr_p = self.psnr(x_reconstructed, phantom)
rmse_p = self.rmse(x_reconstructed, phantom)
test_out = {
"SSIM": ssim_p,
"PSNR": psnr_p,
"RMSE": rmse_p
}
self.log('test_loss', loss)
self.log('test_ssim', ssim_p)
self.log('test_psnr', psnr_p)
self.log('test_rmse', rmse_p)
return test_out
def on_validation_epoch_end(self):
self.logger.experiment.add_image("generated_images", self.grid, self.current_epoch,)
def radon_transform(self, num_view=64, start_ang=0, end_ang=2*np.pi, num_detectors=800):
# the function is used to generate fp, bp, fbp functions
# the physical parameters is set as MetaInvNet and EPNet
xx=200
space=odl.uniform_discr([-xx, -xx], [xx, xx], [256,256], dtype='float32')
angles=np.array(num_view).astype(int)
angle_partition=odl.uniform_partition(start_ang, end_ang, angles)
detector_partition=odl.uniform_partition(-480, 480, num_detectors)
geometry=odl.tomo.FanBeamGeometry(angle_partition, detector_partition, src_radius=600, det_radius=290)
#geometry=odl.tomo.geometry.conebeam.FanBeamGeometry(angle_partition, detector_partition, src_radius=600, det_radius=290)
operator=odl.tomo.RayTransform(space, geometry, impl='astra_cuda')
#op_norm=odl.operator.power_method_opnorm(operator)
#op_norm=torch.from_numpy(np.array(op_norm*2*np.pi)).double().cuda()
op_layer=odl_torch.operator.OperatorModule(operator)
#op_layer_adjoint=odl_torch.operator.OperatorModule(operator.adjoint)
fbp=odl.tomo.fbp_op(operator, filter_type='Ram-Lak', frequency_scaling=0.9)*np.sqrt(2)
op_layer_fbp=odl_torch.operator.OperatorModule(fbp)
return op_layer, op_layer_fbp
def on_validation_epoch_end(self):
tag = f"generated_images_epoch_{self.current_epoch}"
self.logger.experiment.add_image(tag, self.grid, self.current_epoch)
def rmse(self,y_true, y_pred):
return torch.sqrt(torch.mean((y_true - y_pred) ** 2))