src/AlgorithmZeroDual.jl

Sun, 21 Apr 2024 22:35:03 +0300

author
Neil Dizon <neil.dizon@helsinki.fi>
date
Sun, 21 Apr 2024 22:35:03 +0300
changeset 25
c9b06736a477
parent 20
7d3a75b875fa
permissions
-rw-r--r--

added fv_plot

20
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
1 ####################################################################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
2 # Predictive online PDPS for optical flow with known velocity field
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
3 ####################################################################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
4
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
5 __precompile__()
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
6
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
7 module AlgorithmZeroDual
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
8
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
9 identifier = "pdps_known_zerodual"
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
10
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
11 using Printf
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
12
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
13 using AlgTools.Util
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
14 import AlgTools.Iterate
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
15 using ImageTools.Gradient
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
16
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
17 using ..OpticalFlow: ImageSize,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
18 Image,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
19 pdflow!
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
20
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
21 #########################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
22 # Iterate initialisation
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
23 #########################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
24
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
25 function init_rest(x::Image)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
26 imdim=size(x)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
27
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
28 y = zeros(2, imdim...)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
29 Δx = copy(x)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
30 Δy = copy(y)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
31 x̄ = copy(x)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
32
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
33 return x, y, Δx, Δy, x̄
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
34 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
35
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
36 function init_iterates(xinit::Image)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
37 return init_rest(copy(xinit))
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
38 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
39
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
40 function init_iterates(dim::ImageSize)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
41 return init_rest(zeros(dim...))
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
42 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
43
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
44 ############
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
45 # Algorithm
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
46 ############
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
47
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
48 function solve( :: Type{DisplacementT};
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
49 dim :: ImageSize,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
50 iterate = AlgTools.simple_iterate,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
51 params::NamedTuple) where DisplacementT
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
52
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
53 ################################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
54 # Extract and set up parameters
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
55 ################################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
56
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
57 α, ρ = params.α, params.ρ
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
58 R_K² = ∇₂_norm₂₂_est²
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
59 γ = 1.0
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
60 Λ = params.Λ
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
61 τ₀, σ₀ = params.τ₀, params.σ₀
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
62
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
63 τ = τ₀/γ
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
64 @assert(1+γ*τ ≥ Λ)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
65 σ = σ₀*1/(τ*R_K²)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
66
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
67 println("Step length parameters: τ=$(τ), σ=$(σ)")
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
68
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
69 ######################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
70 # Initialise iterates
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
71 ######################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
72
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
73 x, y, Δx, Δy, x̄ = init_iterates(dim)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
74 init_data = (params.init == :data)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
75
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
76 ####################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
77 # Run the algorithm
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
78 ####################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
79
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
80 v = iterate(params) do verbose :: Function,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
81 b :: Image,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
82 v_known :: DisplacementT,
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
83 🚫unused_b_next :: Image
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
84
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
85 ##################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
86 # Prediction step
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
87 ##################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
88 if init_data
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
89 x .= b
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
90 init_data = false
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
91 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
92
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
93 pdflow!(x, Δx, y, Δy, v_known, false)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
94 y .= zeros(size(y)...)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
95
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
96 ############
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
97 # PDPS step
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
98 ############
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
99
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
100 ∇₂ᵀ!(Δx, y) # primal step:
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
101 @. x̄ = x # | save old x for over-relax
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
102 @. x = (x-τ*(Δx-b))/(1+τ) # | prox
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
103 @. x̄ = 2x - x̄ # over-relax
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
104 ∇₂!(Δy, x̄) # dual step: y
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
105 @. y = (y + σ*Δy)/(1 + σ*ρ/α) # |
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
106 proj_norm₂₁ball!(y, α) # | prox
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
107
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
108 ################################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
109 # Give function value if needed
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
110 ################################
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
111 v = verbose() do
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
112 ∇₂!(Δy, x)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
113 value = norm₂²(b-x)/2 + params.α*γnorm₂₁(Δy, params.ρ)
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
114 value, x, [NaN, NaN], nothing
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
115 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
116
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
117 v
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
118 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
119
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
120 return x, y, v
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
121 end
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
122
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
123 end # Module
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
124
7d3a75b875fa added zero dual predictor for denoising
Neil Dizon <neil.dizon@helsinki.fi>
parents:
diff changeset
125

mercurial