src/AlgorithmNew.jl

Thu, 25 Apr 2024 14:54:34 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 25 Apr 2024 14:54:34 -0500
changeset 42
ddcf355f0b2d
parent 36
e4a8f662a1ac
child 66
dc69a0d234ae
permissions
-rw-r--r--

Oops

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

mercurial