src/AlgorithmPET.jl

Fri, 19 Apr 2024 16:34:59 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 19 Apr 2024 16:34:59 -0500
changeset 10
68fe515ea38f
parent 8
e4ad8f7ce671
permissions
-rw-r--r--

README fine-tuning

8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
1 ####################################################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
2 # Predictive online PDPS for optical flow with known velocity field
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
3 ####################################################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
4
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
5 __precompile__()
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
6
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
7 module AlgorithmPET
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
8
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
9 identifier = "pet_known_orig"
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
10
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
11 using Printf
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
12
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
13 using AlgTools.Util
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
14 import AlgTools.Iterate
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
15 using ImageTools.Gradient
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
16 using ImageTools.Translate
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
17
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
18 using ..Radon
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
19 using ImageTransformations
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
20 using Images, CoordinateTransformations, Rotations, OffsetArrays
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
21 using ImageCore, Interpolations
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
22
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
23 using ..OpticalFlow: ImageSize,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
24 Image,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
25 petpdflow!
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
26
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
27 #########################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
28 # Iterate initialisation
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
29 #########################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
30
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
31
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
32
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
33 function init_rest(x::Image)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
34 imdim=size(x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
35
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
36 y = zeros(2, imdim...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
37 Δx = copy(x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
38 Δy = copy(y)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
39 x̄ = copy(x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
40 radonx = copy(x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
41
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
42 return x, y, Δx, Δy, x̄, radonx
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
43 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
44
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
45 function init_iterates(xinit::Image)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
46 return init_rest(copy(xinit))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
47 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
48
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
49 function init_iterates(dim::ImageSize)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
50 return init_rest(zeros(dim...))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
51 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
52
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
53 #########################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
54 # PETscan related
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
55 #########################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
56 function petvalue(x, b, c)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
57 tmp = similar(b)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
58 radon!(tmp, x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
59 return sum(@. tmp - b*log(tmp+c))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
60 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
61
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
62 function petgrad!(res, x, b, c, S)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
63 tmp = similar(b)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
64 radon!(tmp, x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
65 @. tmp = S .- b/(tmp+c)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
66 backproject!(res, S.*tmp)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
67 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
68
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
69 function proj_nonneg!(y)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
70 @inbounds @simd for i=1:length(y)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
71 if y[i] < 0
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
72 y[i] = 0
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
73 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
74 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
75 return y
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
76 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
77
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
78
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
79
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
80 ############
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
81 # Algorithm
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
82 ############
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
83
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
84 function step_lengths(params, γ, R_K²)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
85 ρ̃₀, τ₀, σ₀, σ̃₀ = params.ρ̃₀, params.τ₀, params.σ₀, params.σ̃₀
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
86 δ = params.δ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
87 ρ = isdefined(params, :phantom_ρ) ? params.phantom_ρ : params.ρ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
88 Λ = params.Λ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
89 Θ = params.dual_flow ? Λ : 1
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
90
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
91 τ = τ₀/γ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
92 @assert(1+γ*τ ≥ Λ)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
93 σ = σ₀*1/(τ*R_K²)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
94 #σ = σ₀*min(1/(τ*R_K²), 1/max(0, τ*R_K²/((1+γ*τ-Λ)*(1-δ))-ρ))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
95 q = δ*(1+σ*ρ)/Θ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
96 if 1 ≥ q
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
97 σ̃ = σ̃₀*σ/q
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
98 #ρ̃ = ρ̃₀*max(0, ((Θ*σ)/(2*δ*σ̃^2*(1+σ*ρ))+1/(2σ)-1/σ̃))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
99 ρ̃ = max(0, (1-q)/(2*σ))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
100 else
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
101 σ̃ = σ̃₀*σ/(q*(1-√(1-1/q)))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
102 ρ̃ = 0
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
103 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
104
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
105 #println("Step length parameters: τ=$(τ), σ=$(σ), σ̃=$(σ̃), ρ̃=$(ρ̃)")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
106
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
107 return τ, σ, σ̃, ρ̃
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
108 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
109
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
110 function solve( :: Type{DisplacementT};
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
111 dim :: ImageSize,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
112 iterate = AlgTools.simple_iterate,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
113 params::NamedTuple) where DisplacementT
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
114
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
115 ################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
116 # Extract and set up parameters
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
117 ################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
118
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
119 α, ρ = params.α, params.ρ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
120 R_K² = ∇₂_norm₂₂_est²
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
121 γ = 1
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
122 # τ, σ, σ̃, ρ̃ = step_lengths(params, γ, R_K²)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
123 λ = params.λ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
124 ω = 1
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
125 c = params.c*ones(params.radondims...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
126
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
127 ρ̃₀, τ₀, σ₀, σ̃₀ = params.ρ̃₀, params.τ₀, params.σ₀, params.σ̃₀
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
128
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
129 # Update step length parameters
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
130 L = 300.0
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
131 τ = τ₀/L
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
132 σ = σ₀*(1-τ₀)/(R_K²*τ)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
133 println("Step length parameters: L=$(round(L, digits=4)), τ=$(round(τ, digits=4)), σ=$(round(σ, digits=4))")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
134
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
135
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
136
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
137 δ = params.δ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
138 ρ = isdefined(params, :phantom_ρ) ? params.phantom_ρ : params.ρ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
139 Λ = params.Λ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
140 Θ = params.dual_flow ? Λ : 1
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
141
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
142 q = δ*(1+σ*ρ)/Θ
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
143 if 1 ≥ q
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
144 σ̃ = σ̃₀*σ/q
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
145 #ρ̃ = ρ̃₀*max(0, ((Θ*σ)/(2*δ*σ̃^2*(1+σ*ρ))+1/(2σ)-1/σ̃))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
146 ρ̃ = max(0, (1-q)/(2*σ))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
147 else
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
148 σ̃ = σ̃₀*σ/(q*(1-√(1-1/q)))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
149 ρ̃ = 0
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
150 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
151
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
152 ######################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
153 # Initialise iterates
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
154 ######################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
155
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
156 x, y, Δx, Δy, x̄, r∇ = init_iterates(dim)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
157
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
158 # L = 1.0
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
159 # oldpetgradx = zeros(size(x)...)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
160 # petgradx = zeros(size(x))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
161 # oldx = ones(size(x))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
162
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
163 ####################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
164 # Run the algorithm
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
165 ####################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
166 # THIS IS THE step function inside iterate_visualise
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
167 v = iterate(params) do verbose :: Function,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
168 b :: Image, # noisy_sinogram
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
169 v_known :: DisplacementT,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
170 theta_known :: DisplacementT,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
171 b_true :: Image,
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
172 S :: Image
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
173
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
174 ##################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
175 # Update the step length parameter
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
176 ##################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
177 # τ = τ₀/L
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
178 # σ = σ₀*(1-τ₀)/(R_K²*τ)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
179 # println("Step length parameters: L=$(round(L, digits=4)), τ=$(round(τ, digits=4)), σ=$(round(σ, digits=4))")
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
180
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
181
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
182 ###################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
183 # Prediction steps
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
184 ###################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
185
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
186 petpdflow!(x, Δx, y, Δy, v_known, theta_known, params.dual_flow) # Old algorithm
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
187 #pdflow!(x, Δx, y, Δy, v_known, theta_known, params.dual_flow, 1e-2,1e-2) # Rotation
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
188 #pdflow!(x, Δx, y, Δy, v_known, theta_known, params.dual_flow, 1e-2) # Adhoc
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
189 #@. oldx = x
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
190
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
191 if params.prox_predict
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
192 ∇₂!(Δy, x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
193 @. y = (y + σ̃*Δy)/(1 + σ̃*(ρ̃+ρ/α))
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
194 #@. cc = y + 1000000*σ̃*Δy
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
195 #@. y = (y + σ̃*Δy)/(1 + σ̃*(ρ̃+ρ/α)) + (1 - 1/(1 + ρ̃*σ̃))*cc
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
196 proj_norm₂₁ball!(y, α)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
197 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
198
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
199
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
200 ############
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
201 # PDPS step
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
202 ############
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
203
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
204 ∇₂ᵀ!(Δx, y) # primal step:
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
205 @. x̄ = x # | save old x for over-relax
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
206 petgrad!(r∇, x, b, c, S) # | Calculate gradient of fidelity term
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
207
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
208 @. x = x-(τ*λ)*r∇-τ*Δx # |
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
209 proj_nonneg!(x) # | non-negativity constaint prox
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
210 @. x̄ = (1+ω)*x - ω*x̄ # over-relax: x̄ = 2x-x_old
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
211 ∇₂!(Δy, x̄) # dual step:
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
212 @. y = y + σ*Δy # |
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
213 proj_norm₂₁ball!(y, α) # | prox
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
214
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
215 ##########################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
216 # Compute for the local Lipschitz constant
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
217 ##########################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
218 # petgrad!(petgradx, x, b, c, S)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
219 # petgrad!(oldpetgradx, oldx, b, c, S)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
220 # if norm₂(x-oldx)>1e-12
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
221 # L = max(0.9*norm₂(petgradx - oldpetgradx)/norm₂(x-oldx),L)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
222 # end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
223
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
224 ################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
225 # Give function value if needed
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
226 ################################
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
227
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
228 v = verbose() do
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
229 ∇₂!(Δy, x)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
230 value = λ*petvalue(x, b, c) + params.α*norm₂₁(Δy)
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
231 value, x, [NaN, NaN], nothing
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
232 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
233
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
234 v
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
235 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
236
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
237 return x, y, v
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
238 end
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
239
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
240 end # Module
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
241
e4ad8f7ce671 Added PET and updated README
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
242

mercurial