src/Algorithm.jl

Wed, 02 Dec 2020 12:54:58 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 02 Dec 2020 12:54:58 -0500
changeset 4
a64766c44642
parent 0
a55e35d20336
permissions
-rw-r--r--

windowsize

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

mercurial