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