src/AlgorithmBoth.jl

Thu, 25 Apr 2024 11:08:03 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 25 Apr 2024 11:08:03 -0500
changeset 33
e091766f556d
parent 0
a55e35d20336
permissions
-rw-r--r--

Oops, year in README

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 unknown 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 AlgorithmBoth
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_unknown_basic"
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 Gradient,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 DisplacementConstant,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 DisplacementFull,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 pdflow!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 pointwise_gradiprod_2d!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 pointwise_gradiprod_2dᵀ!,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 filter_hs
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 using ..Algorithm: step_lengths
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 #############
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 # Data types
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 #############
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 struct Primal{DisplacementT}
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 x :: Image
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 u :: DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 function Base.similar(x::Primal{DisplacementT}) where DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 return Primal{DisplacementT}(Base.similar(x.x), Base.similar(x.u))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 function Base.copy(x::Primal{DisplacementT}) where DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 return Primal{DisplacementT}(Base.copy(x.x), Base.copy(x.u))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 struct Dual
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 tv :: Gradient
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 flow :: Image
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 function Base.similar(y::Dual)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 return Dual(Base.similar(y.tv), Base.similar(y.flow))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 end
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 function Base.copy(y::Dual)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 return Dual(Base.copy(y.tv), Base.copy(y.flow))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 #########################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 # Iterate initialisation
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 #########################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 function init_primal(xinit::Image, ::Type{DisplacementConstant})
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 return Primal{DisplacementConstant}(xinit, zeros(2))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 function init_primal(xinit::Image, ::Type{DisplacementFull})
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 return Primal{DisplacementFull}(xinit, zeros(2, size(xinit)...))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 function init_rest(x::Primal{DisplacementT}) where DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 imdim=size(x.x)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 y = Dual(zeros(2, imdim...), zeros(imdim))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 Δx = copy(x)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 Δy = copy(y)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 x̄ = copy(x)
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 return x, y, Δx, Δy, x̄
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 end
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 function init_iterates( :: Type{DisplacementT},
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 xinit::Primal{DisplacementT}) where DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 return init_rest(copy(xinit))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 end
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 function init_iterates( :: Type{DisplacementT}, xinit::Image) where DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 return init_rest(init_primal(copy(xinit), DisplacementT))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 end
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 function init_iterates( :: Type{DisplacementT}, dim::ImageSize) where DisplacementT
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 return init_rest(init_primal(zeros(dim...), DisplacementT))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 end
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 ##############################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 # Weighting for different displacements types
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
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
99 norm²weight( :: Type{DisplacementConstant}, sz ) = prod(sz)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 norm²weight( :: Type{DisplacementFull}, sz ) = 1
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101
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 # Algorithm
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 ############
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 function solve( :: Type{DisplacementT};
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 dim :: ImageSize,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 iterate = AlgTools.simple_iterate,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 params::NamedTuple) where DisplacementT
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 ######################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 # Initialise iterates
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 ######################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 x, y, Δx, Δy, x̄ = init_iterates(DisplacementT, dim)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 init_data = (params.init == :data)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 # … for tracking cumulative movement
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 if DisplacementT == DisplacementConstant
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 ucumul = [0.0, 0.0]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 else
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 ucumul = [NaN, NaN]
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 #############################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 # Extract parameters and set up step lengths
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 #############################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 α, ρ, λ, θ, T = params.α, params.ρ, params.λ, params.θ, params.timestep
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130 R_K² = max(∇₂_norm₂₂_est², ∇₂_norm₂∞_est²*params.dynrange^2)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
131 γ = min(1, λ*norm²weight(DisplacementT, size(x.x)))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 τ, σ, σ̃, ρ̃ = step_lengths(params, γ, R_K²)
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 kernel = params.kernel
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 ####################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 # Run the algorithm
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138 ####################
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 b_next_filt=nothing
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
141
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 v = iterate(params) do verbose :: Function,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143 b :: Image,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144 🚫unused_v_known :: DisplacementT,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 b_next :: Image
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146
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 # Smooth data for Horn–Schunck term
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149 ####################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151 b_filt, b_next_filt = filter_hs(b, b_next, b_next_filt, kernel)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 ############################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 # Construct K for this step
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 ############################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 K! = (yʹ, xʹ) -> begin
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158 # Optical flow part: ⟨⟨u, ∇b_k⟩⟩.
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 # Use y.tv as temporary gradient storage.
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 pointwise_gradiprod_2d!(yʹ.flow, yʹ.tv, xʹ.u, b_filt)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 #@. yʹ.flow = -yʹ.flow
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 # TV part
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 ∇₂!(yʹ.tv, xʹ.x)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 Kᵀ! = (xʹ, yʹ) -> begin
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166 # Optical flow part: ∇b_k ⋅ y
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 pointwise_gradiprod_2dᵀ!(xʹ.u, yʹ.flow, b_filt)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 #@. xʹ.u = -xʹ.u
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 # TV part
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170 ∇₂ᵀ!(xʹ.x, yʹ.tv)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 ##################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 # Prediction step
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175 ##################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177 if init_data
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 x .= b
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 init_data = false
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 pdflow!(x.x, Δx.x, y.tv, Δy.tv, y.flow, Δy.flow, x.u, params.dual_flow)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
183
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 # Predict zero displacement
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185 x.u .= 0
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186 if params.prox_predict
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187 K!(Δy, x)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 @. y.tv = (y.tv + σ̃*Δy.tv)/(1 + σ̃*(ρ̃+ρ/α))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189 proj_norm₂₁ball!(y.tv, α)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
190 @. y.flow = (y.flow+σ̃*((b_next_filt-b_filt)/T+Δy.flow))/(1+σ̃*(ρ̃+1/θ))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 ############
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
194 # PDPS step
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
195 #
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 # NOTE: For DisplacementConstant, the x.u update is supposed to be with
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 # respect to the 𝟙^*𝟙 norm/inner product that makes the norm equivalent
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198 # to full-space norm when restricted to constant displacements. Since
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 # `OpticalFlow.pointwise_gradiprod_2dᵀ!` already uses this inner product,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200 # and the λ-weighted term in the problem is with respect to this norm,
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 # all the norm weights disappear in this update.
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202 ############
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204 Kᵀ!(Δx, y) # primal step:
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 @. x̄.x = x.x # | save old x for over-relax
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 @. x̄.u = x.u # |
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207 @. x.x = (x.x-τ*(Δx.x-b))/(1+τ) # | prox
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208 @. x.u = (x.u-τ*Δx.u)/(1+τ*λ) # |
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 @. x̄.x = 2x.x - x̄.x # over-relax
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 @. x̄.u = 2x.u - x̄.u # |
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 K!(Δy, x̄) # dual step: y
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 @. y.tv = (y.tv + σ*Δy.tv)/(1 + σ*ρ/α) # |
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
213 proj_norm₂₁ball!(y.tv, α) # | prox
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
214 @. y.flow = (y.flow+σ*((b_next_filt-b_filt)/T+Δy.flow))/(1+σ/θ)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216 if DisplacementT == DisplacementConstant
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 ucumul .+= x.u
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
219
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
220 ########################################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 # Give function value and cumulative movement if needed
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 ########################################################
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
223 v = verbose() do
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224 K!(Δy, x)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 value = (norm₂²(b-x.x)/2 + θ*norm₂²((b_next_filt-b_filt)./T+Δy.flow)
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226 + λ*norm₂²(x.u)/2 + α*γnorm₂₁(Δy.tv, ρ))
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
227
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
228 value, x.x, ucumul, nothing
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231 return v
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
233
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 return x, y, v
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235 end
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237 end # Module
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238
a55e35d20336 Initialise independent repo
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
239

mercurial