Thu, 18 Apr 2024 11:31:32 +0300
added seed restart
| 0 | 1 | ################## |
| 2 | # Our main module | |
| 3 | ################## | |
| 4 | ||
| 5 | __precompile__() | |
| 6 | ||
| 7 | module PredictPDPS | |
| 8 | ||
| 9 | ######################## | |
| 10 | # Load external modules | |
| 11 | ######################## | |
| 12 | ||
| 13 | using Printf | |
| 14 | using FileIO | |
| 15 | #using JLD2 | |
| 16 | using Setfield | |
|
2
be7cab83b14a
Update packages and manifest to Julia 1.7.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
17 | using ImageQualityIndexes: assess_psnr, assess_ssim |
| 0 | 18 | using DelimitedFiles |
| 19 | import GR | |
| 20 | ||
| 21 | using AlgTools.Util | |
| 22 | using AlgTools.StructTools | |
| 23 | using AlgTools.LinkedLists | |
| 24 | using AlgTools.Comms | |
| 25 | using ImageTools.Visualise: secs_ns, grayimg, do_visualise | |
| 26 | using ImageTools.ImFilter: gaussian | |
| 27 | ||
| 28 | ##################### | |
| 29 | # Load local modules | |
| 30 | ##################### | |
| 31 | ||
| 32 | include("OpticalFlow.jl") | |
| 33 | include("ImGenerate.jl") | |
| 34 | include("Algorithm.jl") | |
| 35 | include("AlgorithmBoth.jl") | |
| 36 | include("AlgorithmBothGreedyV.jl") | |
| 37 | include("AlgorithmBothCumul.jl") | |
| 38 | include("AlgorithmBothMulti.jl") | |
| 39 | include("AlgorithmBothNL.jl") | |
| 40 | include("AlgorithmFB.jl") | |
| 41 | include("AlgorithmFBDual.jl") | |
| 42 | ||
| 5 | 43 | # Additional |
| 44 | include("AlgorithmProximal.jl") | |
| 45 | include("AlgorithmGreedy.jl") | |
| 46 | include("AlgorithmRotation.jl") | |
| 47 | include("AlgorithmNoPrediction.jl") | |
| 48 | include("AlgorithmPrimalOnly.jl") | |
| 49 | include("AlgorithmDualScaling.jl") | |
| 50 | ||
| 0 | 51 | import .Algorithm, |
| 52 | .AlgorithmBoth, | |
| 53 | .AlgorithmBothGreedyV, | |
| 54 | .AlgorithmBothCumul, | |
| 55 | .AlgorithmBothMulti, | |
| 56 | .AlgorithmBothNL, | |
| 57 | .AlgorithmFB, | |
| 5 | 58 | .AlgorithmFBDual, |
| 59 | .AlgorithmProximal, | |
| 60 | .AlgorithmGreedy, | |
| 61 | .AlgorithmRotation, | |
| 62 | .AlgorithmNoPrediction, | |
| 63 | .AlgorithmPrimalOnly, | |
| 64 | .AlgorithmDualScaling | |
| 0 | 65 | |
| 66 | using .ImGenerate | |
| 67 | using .OpticalFlow: DisplacementFull, DisplacementConstant | |
| 68 | ||
| 69 | ############## | |
| 70 | # Our exports | |
| 71 | ############## | |
| 72 | ||
| 73 | export run_experiments, | |
| 74 | batchrun_article, | |
| 75 | demo_known1, | |
| 76 | demo_known2, | |
| 77 | demo_known3, | |
| 5 | 78 | demo_known4, |
| 79 | demo_known5, | |
| 80 | demo_known6, | |
| 0 | 81 | demo_unknown1, |
| 82 | demo_unknown2, | |
| 5 | 83 | demo_unknown3, |
| 84 | demo_predictor1, | |
| 85 | demo_predictor2, | |
| 86 | demo_predictor3, | |
| 87 | demo_predictor4, | |
| 88 | demo_predictor5, | |
| 89 | demo_predictor6, | |
| 90 | batchrun_predictors | |
| 0 | 91 | |
| 92 | ################################### | |
| 93 | # Parameterisation and experiments | |
| 94 | ################################### | |
| 95 | ||
| 96 | struct Experiment | |
| 97 | mod :: Module | |
| 98 | DisplacementT :: Type | |
| 99 | imgen :: ImGen | |
| 100 | params :: NamedTuple | |
| 101 | end | |
| 102 | ||
| 103 | function Base.show(io::IO, e::Experiment) | |
| 104 | displacementname(::Type{DisplacementFull}) = "DisplacementFull" | |
| 105 | displacementname(::Type{DisplacementConstant}) = "DisplacementConstant" | |
| 106 | print(io, " | |
| 107 | mod: $(e.mod) | |
| 108 | DisplacementT: $(displacementname(e.DisplacementT)) | |
| 109 | imgen: $(e.imgen.name) $(e.imgen.dim[1])×$(e.imgen.dim[2]) | |
| 110 | params: $(e.params ⬿ (kernel = "(not shown)",)) | |
| 111 | ") | |
| 112 | end | |
| 113 | ||
| 114 | const default_save_prefix="img/" | |
| 115 | ||
| 116 | const default_params = ( | |
| 117 | ρ = 0, | |
| 118 | verbose_iter = 100, | |
| 119 | maxiter = 10000, | |
| 120 | save_results = true, | |
| 121 | save_images = true, | |
| 122 | save_images_iters = Set([1, 2, 3, 5, | |
| 123 | 10, 25, 30, 50, | |
| 124 | 100, 250, 300, 500, | |
| 125 | 1000, 2500, 3000, 5000, | |
| 126 | 10000]), | |
| 127 | pixelwise_displacement=false, | |
| 128 | dual_flow = true, | |
| 129 | prox_predict = true, | |
| 130 | handle_interrupt = true, | |
| 131 | init = :zero, | |
| 132 | plot_movement = false, | |
| 133 | ) | |
| 134 | ||
| 135 | const square = imgen_square((200, 300)) | |
| 136 | const lighthouse = imgen_shake("lighthouse", (200, 300)) | |
| 137 | ||
| 138 | const p_known₀ = ( | |
| 139 | noise_level = 0.5, | |
| 5 | 140 | shake_noise_level = 0.025, |
| 141 | shake = 3.0, | |
| 142 | α = 1.0, | |
| 143 | ρ̃₀ = 1.0, | |
| 144 | σ̃₀ = 1.0, | |
| 0 | 145 | δ = 0.9, |
| 5 | 146 | σ₀ = 0.1, |
| 0 | 147 | τ₀ = 0.01, |
| 148 | ) | |
| 149 | ||
| 150 | const p_unknown₀ = ( | |
| 151 | noise_level = 0.3, | |
| 152 | shake_noise_level = 0.05, | |
| 153 | shake = 2, | |
| 154 | α = 0.2, | |
| 155 | ρ̃₀ = 1, | |
| 156 | σ̃₀ = 1, | |
| 157 | σ₀ = 1, | |
| 158 | δ = 0.9, | |
| 159 | λ = 1, | |
| 160 | θ = (300*200)*100^3, | |
| 161 | kernel = gaussian((3, 3), (11, 11)), | |
| 162 | timestep = 0.5, | |
| 163 | displacement_count = 100, | |
| 164 | τ₀ = 0.01, | |
| 165 | ) | |
| 166 | ||
| 167 | const experiments_pdps_known = ( | |
| 168 | Experiment(Algorithm, DisplacementConstant, lighthouse, | |
| 169 | p_known₀ ⬿ (phantom_ρ = 0,)), | |
| 170 | Experiment(Algorithm, DisplacementConstant, lighthouse, | |
| 171 | p_known₀ ⬿ (phantom_ρ = 100,)), | |
| 172 | Experiment(Algorithm, DisplacementConstant, square, | |
| 173 | p_known₀ ⬿ (phantom_ρ = 0,)) | |
| 174 | ) | |
| 175 | ||
| 176 | const experiments_pdps_unknown_multi = ( | |
| 177 | Experiment(AlgorithmBothMulti, DisplacementConstant, lighthouse, | |
| 178 | p_unknown₀ ⬿ (phantom_ρ = 0,)), | |
| 179 | Experiment(AlgorithmBothMulti, DisplacementConstant, lighthouse, | |
| 180 | p_unknown₀ ⬿ (phantom_ρ = 100,)), | |
| 181 | Experiment(AlgorithmBothMulti, DisplacementConstant, square, | |
| 182 | p_unknown₀ ⬿ (phantom_ρ = 0,)), | |
| 183 | ) | |
| 184 | ||
| 185 | const experiments_fb_known = ( | |
| 186 | Experiment(AlgorithmFB, DisplacementConstant, lighthouse, | |
| 187 | p_known₀ ⬿ (τ̃₀=0.9, fb_inner_iterations = 10)), | |
| 188 | ) | |
| 189 | ||
| 5 | 190 | const predictor_experiments_pdps_known = ( |
| 191 | Experiment(AlgorithmProximal, DisplacementConstant, lighthouse, | |
| 192 | p_known₀ ⬿ (phantom_ρ = 100,)), | |
| 193 | Experiment(AlgorithmRotation, DisplacementConstant, lighthouse, | |
| 194 | p_known₀), | |
| 195 | Experiment(AlgorithmGreedy, DisplacementConstant, lighthouse, | |
| 196 | p_known₀), | |
| 197 | Experiment(AlgorithmPrimalOnly, DisplacementConstant, lighthouse, | |
| 198 | p_known₀), | |
| 199 | Experiment(AlgorithmNoPrediction, DisplacementConstant, lighthouse, | |
| 200 | p_known₀), | |
| 201 | Experiment(AlgorithmDualScaling, DisplacementConstant, lighthouse, | |
| 202 | p_known₀), | |
| 203 | ) | |
| 204 | ||
| 205 | const predictor_experiments_all = Iterators.flatten(( | |
| 206 | predictor_experiments_pdps_known, | |
| 207 | )) | |
| 208 | ||
| 0 | 209 | const experiments_all = Iterators.flatten(( |
| 210 | experiments_pdps_known, | |
| 211 | experiments_pdps_unknown_multi, | |
| 212 | experiments_fb_known | |
| 213 | )) | |
| 214 | ||
| 5 | 215 | |
| 216 | ||
| 0 | 217 | ################ |
| 218 | # Log | |
| 219 | ################ | |
| 220 | ||
| 221 | struct LogEntry <: IterableStruct | |
| 222 | iter :: Int | |
| 223 | time :: Float64 | |
| 224 | function_value :: Float64 | |
| 5 | 225 | #v_cumul_true_y :: Float64 |
| 226 | #v_cumul_true_x :: Float64 | |
| 227 | #v_cumul_est_y :: Float64 | |
| 228 | #v_cumul_est_x :: Float64 | |
| 0 | 229 | psnr :: Float64 |
| 230 | ssim :: Float64 | |
| 5 | 231 | #psnr_data :: Float64 |
| 232 | #ssim_data :: Float64 | |
| 0 | 233 | end |
| 234 | ||
| 235 | struct LogEntryHiFi <: IterableStruct | |
| 236 | iter :: Int | |
| 237 | v_cumul_true_y :: Float64 | |
| 238 | v_cumul_true_x :: Float64 | |
| 239 | end | |
| 240 | ||
| 241 | ############### | |
| 242 | # Main routine | |
| 243 | ############### | |
| 244 | ||
| 245 | struct State | |
| 246 | vis :: Union{Channel,Bool,Nothing} | |
| 247 | start_time :: Union{Real,Nothing} | |
| 248 | wasted_time :: Real | |
| 249 | log :: LinkedList{LogEntry} | |
| 250 | log_hifi :: LinkedList{LogEntryHiFi} | |
| 251 | aborted :: Bool | |
| 252 | end | |
| 253 | ||
| 254 | function name(e::Experiment, p) | |
| 255 | ig = e.imgen | |
| 5 | 256 | # return "$(ig.name)_$(e.mod.identifier)_$(@sprintf "%x" hash(p))" |
| 257 | return "$(ig.name)_$(e.mod.identifier)_$(Int64(10000*p.σ₀))_$(Int64(10000*p.τ₀))" | |
| 0 | 258 | end |
| 259 | ||
| 260 | function write_tex(texfile, e_params) | |
| 261 | open(texfile, "w") do io | |
| 262 | wp = (n, v) -> println(io, "\\def\\EXPPARAM$(n){$(v)}") | |
| 263 | wf = (n, s) -> if isdefined(e_params, s) | |
| 264 | wp(n, getfield(e_params, s)) | |
| 265 | end | |
| 266 | wf("alpha", :α) | |
| 267 | wf("sigmazero", :σ₀) | |
| 268 | wf("tauzero", :τ₀) | |
| 269 | wf("tildetauzero", :τ̃₀) | |
| 270 | wf("delta", :δ) | |
| 271 | wf("lambda", :λ) | |
| 272 | wf("theta", :θ) | |
| 273 | wf("maxiter", :maxiter) | |
| 274 | wf("noiselevel", :noise_level) | |
| 275 | wf("shakenoiselevel", :shake_noise_level) | |
| 276 | wf("shake", :shake) | |
| 277 | wf("timestep", :timestep) | |
| 278 | wf("displacementcount", :displacementcount) | |
| 279 | wf("phantomrho", :phantom_ρ) | |
| 280 | if isdefined(e_params, :σ₀) | |
| 281 | wp("sigma", (e_params.σ₀ == 1 ? "" : "$(e_params.σ₀)") * "\\sigma_{\\max}") | |
| 282 | end | |
| 283 | end | |
| 5 | 284 | end |
| 0 | 285 | |
| 286 | function run_experiments(;visualise=true, | |
| 287 | recalculate=true, | |
| 288 | experiments, | |
| 289 | save_prefix=default_save_prefix, | |
| 290 | fullscreen=false, | |
| 291 | kwargs...) | |
| 292 | ||
| 293 | # Create visualisation | |
| 294 | if visualise | |
| 295 | rc = Channel(1) | |
| 5 | 296 | visproc = Threads.@spawn bg_visualise_enhanced(rc, fullscreen=fullscreen) |
| 0 | 297 | bind(rc, visproc) |
| 298 | vis = rc | |
| 299 | else | |
| 300 | vis = false | |
| 301 | end | |
| 302 | ||
| 303 | # Run all experiments | |
| 304 | for e ∈ experiments | |
| 305 | ||
| 306 | # Parameters for this experiment | |
| 307 | e_params = default_params ⬿ e.params ⬿ kwargs | |
| 308 | ename = name(e, e_params) | |
| 309 | e_params = e_params ⬿ (save_prefix = save_prefix * ename, | |
| 310 | dynrange = e.imgen.dynrange, | |
| 311 | Λ = e.imgen.Λ) | |
| 312 | ||
| 313 | if recalculate || !isfile(e_params.save_prefix * ".txt") | |
| 314 | println("Running experiment \"$(ename)\"") | |
| 315 | ||
| 316 | # Start data generation task | |
| 317 | datachannel = Channel{OnlineData{e.DisplacementT}}(2) | |
| 318 | gentask = Threads.@spawn e.imgen.f(e.DisplacementT, datachannel, e_params) | |
| 319 | bind(datachannel, gentask) | |
| 320 | ||
| 321 | # Run algorithm | |
| 322 | iterate = curry(iterate_visualise, datachannel, | |
| 323 | State(vis, nothing, 0.0, nothing, nothing, false)) | |
| 324 | ||
| 325 | x, y, st = e.mod.solve(e.DisplacementT; | |
| 326 | dim=e.imgen.dim, | |
| 327 | iterate=iterate, | |
| 328 | params=e_params) | |
| 329 | ||
| 330 | # Clear non-saveable things | |
| 331 | st = @set st.vis = nothing | |
| 332 | ||
| 333 | println("Wasted_time: $(st.wasted_time)s") | |
| 334 | ||
| 335 | if e_params.save_results | |
| 336 | println("Saving " * e_params.save_prefix * "(.txt,_hifi.txt,_params.tex)") | |
| 337 | ||
| 338 | perffile = e_params.save_prefix * ".txt" | |
| 339 | hififile = e_params.save_prefix * "_hifi.txt" | |
| 340 | texfile = e_params.save_prefix * "_params.tex" | |
| 341 | # datafile = e_params.save_prefix * ".jld2" | |
| 342 | ||
| 343 | write_log(perffile, st.log, "# params = $(e_params)\n") | |
| 344 | write_log(hififile, st.log_hifi, "# params = $(e_params)\n") | |
| 345 | write_tex(texfile, e_params) | |
| 346 | # @save datafile x y st params | |
| 347 | end | |
| 348 | ||
| 349 | close(datachannel) | |
| 350 | wait(gentask) | |
| 351 | ||
| 352 | if st.aborted | |
| 353 | break | |
| 354 | end | |
| 355 | else | |
| 356 | println("Skipping already computed experiment \"$(ename)\"") | |
| 357 | # texfile = e_params.save_prefix * "_params.tex" | |
| 358 | # write_tex(texfile, e_params) | |
| 359 | end | |
| 360 | end | |
| 361 | ||
| 362 | if visualise | |
| 363 | # Tell subprocess to finish, and wait | |
| 364 | put!(rc, nothing) | |
| 365 | close(rc) | |
| 366 | wait(visproc) | |
| 367 | end | |
| 368 | ||
| 369 | return | |
| 370 | end | |
| 371 | ||
| 372 | ####################### | |
| 373 | # Demos and batch runs | |
| 374 | ####################### | |
| 375 | ||
| 376 | function demo(experiment; kwargs...) | |
| 377 | run_experiments(;experiments=(experiment,), | |
| 378 | save_results=false, | |
| 379 | save_images=false, | |
| 380 | visualise=true, | |
| 381 | recalculate=true, | |
| 5 | 382 | verbose_iter=1, |
| 383 | fullscreen=true, | |
| 0 | 384 | kwargs...) |
| 385 | end | |
| 386 | ||
| 387 | demo_known1 = () -> demo(experiments_pdps_known[3]) | |
| 388 | demo_known2 = () -> demo(experiments_pdps_known[1]) | |
| 389 | demo_known3 = () -> demo(experiments_pdps_known[2]) | |
| 5 | 390 | |
| 391 | demo_predictor1 = () -> demo(predictor_experiments_pdps_known[1]) # Proximal | |
| 392 | demo_predictor2 = () -> demo(predictor_experiments_pdps_known[2]) # Rotation | |
| 393 | demo_predictor3 = () -> demo(predictor_experiments_pdps_known[3]) # Greedy | |
| 394 | demo_predictor4 = () -> demo(predictor_experiments_pdps_known[4]) # PrimalOnly | |
| 395 | demo_predictor5 = () -> demo(predictor_experiments_pdps_known[5]) # NoPrediction | |
| 396 | demo_predictor6 = () -> demo(predictor_experiments_pdps_known[6]) # DualScaling | |
| 397 | ||
| 0 | 398 | demo_unknown1 = () -> demo(experiments_pdps_unknown_multi[3], plot_movement=true) |
| 399 | demo_unknown2 = () -> demo(experiments_pdps_unknown_multi[1], plot_movement=true) | |
| 400 | demo_unknown3 = () -> demo(experiments_pdps_unknown_multi[2], plot_movement=true) | |
| 401 | ||
| 402 | function batchrun_article(kwargs...) | |
| 403 | run_experiments(;experiments=experiments_all, | |
| 404 | save_results=true, | |
| 405 | save_images=true, | |
| 406 | visualise=false, | |
| 407 | recalculate=false, | |
| 408 | kwargs...) | |
| 409 | end | |
| 410 | ||
| 5 | 411 | function batchrun_predictors(kwargs...) |
| 412 | run_experiments(;experiments=predictor_experiments_all, | |
| 413 | save_results=true, | |
| 414 | save_images=true, | |
| 415 | visualise=false, | |
| 416 | recalculate=false, | |
| 417 | kwargs...) | |
| 418 | end | |
| 419 | ||
| 0 | 420 | ###################################################### |
| 421 | # Iterator that does visualisation and log collection | |
| 422 | ###################################################### | |
| 423 | ||
| 424 | function iterate_visualise(datachannel::Channel{OnlineData{DisplacementT}}, | |
| 425 | st :: State, | |
| 426 | step :: Function, | |
| 427 | params :: NamedTuple) where DisplacementT | |
| 428 | try | |
| 429 | sc = nothing | |
| 430 | ||
| 431 | d = take!(datachannel) | |
| 432 | ||
| 433 | for iter=1:params.maxiter | |
| 434 | dnext = take!(datachannel) | |
| 435 | st = step(d.b_noisy, d.v, dnext.b_noisy) do calc_objective | |
| 436 | stn = st | |
| 437 | ||
| 438 | if isnothing(stn.start_time) | |
| 439 | # The Julia precompiler is a miserable joke, apparently not crossing module | |
| 440 | # boundaries, so only start timing after the first iteration. | |
| 441 | stn = @set stn.start_time=secs_ns() | |
| 442 | end | |
| 443 | ||
| 444 | verb = params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0 | |
| 445 | ||
| 446 | # Normalise movement to image dimensions so | |
| 447 | # our TikZ plotting code doesn't need to know | |
| 448 | # the image pixel size. | |
| 449 | sc = 1.0./maximum(size(d.b_true)) | |
| 450 | ||
| 451 | if verb || iter ≤ 20 || (iter ≤ 200 && mod(iter, 10) == 0) | |
| 452 | verb_start = secs_ns() | |
| 453 | tm = verb_start - stn.start_time - stn.wasted_time | |
| 454 | value, x, v, vhist = calc_objective() | |
| 455 | ||
| 456 | entry = LogEntry(iter, tm, value, | |
| 5 | 457 | #sc*d.v_cumul_true[1], |
| 458 | #sc*d.v_cumul_true[2], | |
| 459 | #sc*v[1], sc*v[2], | |
|
2
be7cab83b14a
Update packages and manifest to Julia 1.7.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
460 | assess_psnr(x, d.b_true), |
|
be7cab83b14a
Update packages and manifest to Julia 1.7.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
461 | assess_ssim(x, d.b_true), |
| 5 | 462 | #assess_psnr(d.b_noisy, d.b_true), |
| 463 | #assess_ssim(d.b_noisy, d.b_true) | |
| 464 | ) | |
| 0 | 465 | |
| 466 | # (**) Collect a singly-linked list of log to avoid array resizing | |
| 467 | # while iterating | |
| 468 | stn = @set stn.log=LinkedListEntry(entry, stn.log) | |
| 469 | ||
| 470 | if !isnothing(vhist) | |
| 471 | vhist=vhist.*sc | |
| 472 | end | |
| 473 | ||
| 474 | if verb | |
| 475 | @printf("%d/%d J=%f, PSNR=%f, SSIM=%f, avg. FPS=%f\n", | |
| 476 | iter, params.maxiter, value, entry.psnr, | |
| 477 | entry.ssim, entry.iter/entry.time) | |
| 478 | if isa(stn.vis, Channel) | |
| 479 | put_onlylatest!(stn.vis, ((d.b_noisy, x), | |
| 480 | params.plot_movement, | |
| 481 | stn.log, vhist)) | |
| 482 | ||
| 483 | end | |
| 484 | end | |
| 485 | ||
| 486 | if params.save_images && (!haskey(params, :save_images_iters) | |
| 487 | || iter ∈ params.save_images_iters) | |
| 488 | fn = (t, ext) -> "$(params.save_prefix)_$(t)_frame$(iter).$(ext)" | |
| 6 | 489 | # save(File(format"PNG", fn("true", "png")), grayimg(d.b_true)) |
| 490 | # save(File(format"PNG", fn("data", "png")), grayimg(d.b_noisy)) | |
| 0 | 491 | save(File(format"PNG", fn("reco", "png")), grayimg(x)) |
| 492 | if !isnothing(vhist) | |
| 493 | open(fn("movement", "txt"), "w") do io | |
| 494 | writedlm(io, ["est_y" "est_x"]) | |
| 495 | writedlm(io, vhist) | |
| 496 | end | |
| 497 | end | |
| 498 | end | |
| 499 | ||
| 500 | stn = @set stn.wasted_time += (secs_ns() - verb_start) | |
| 501 | ||
| 502 | return stn | |
| 503 | end | |
| 504 | ||
| 505 | hifientry = LogEntryHiFi(iter, sc*d.v_cumul_true[1], sc*d.v_cumul_true[2]) | |
| 506 | st = @set st.log_hifi=LinkedListEntry(hifientry, st.log_hifi) | |
| 507 | ||
| 508 | return st | |
| 509 | end | |
| 510 | d=dnext | |
| 511 | end | |
| 512 | catch ex | |
| 513 | if params.handle_interrupt && isa(ex, InterruptException) | |
| 514 | # If SIGINT is received (user pressed ^C), terminate computations, | |
| 515 | # returning current status. Effectively, we do not call `step()` again, | |
| 516 | # ending the iterations, but letting the algorithm finish up. | |
| 517 | # Assuming (**) above occurs atomically, `st.log` should be valid, but | |
| 518 | # any results returned by the algorithm itself may be partial, as for | |
| 519 | # reasons of efficiency we do *not* store results of an iteration until | |
| 520 | # the next iteration is finished. | |
| 521 | printstyled("\rUser interrupt—finishing up.\n", bold=true, color=202) | |
| 522 | st = @set st.aborted = true | |
| 523 | else | |
| 524 | rethrow(ex) | |
| 525 | end | |
| 526 | end | |
| 527 | ||
| 528 | return st | |
| 529 | end | |
| 530 | ||
| 5 | 531 | function bg_visualise_enhanced(rc; fullscreen=false) |
| 0 | 532 | process_channel(rc) do d |
| 533 | imgs, plot_movement, log, vhist = d | |
| 5 | 534 | do_visualise(imgs, refresh=false, fullscreen=fullscreen) |
| 0 | 535 | # Overlay movement |
| 536 | GR.settextcolorind(5) | |
| 537 | GR.setcharheight(0.015) | |
| 538 | GR.settextpath(GR.TEXT_PATH_RIGHT) | |
| 539 | tx, ty = GR.wctondc(0, 1) | |
| 540 | GR.text(tx, ty, @sprintf "FPS %.1f, SSIM %.2f, PSNR %.1f" (log.value.iter/log.value.time) log.value.ssim log.value.psnr) | |
| 541 | if plot_movement | |
| 542 | sc=1.0 | |
| 543 | p=unfold_linked_list(log) | |
| 544 | x=map(e -> 1.5+sc*e.v_cumul_true_x, p) | |
| 545 | y=map(e -> 0.5+sc*e.v_cumul_true_y, p) | |
| 546 | GR.setlinewidth(2) | |
| 547 | GR.setlinecolorind(2) | |
| 548 | GR.polyline(x, y) | |
| 549 | x=map(e -> 1.5+sc*e.v_cumul_est_x, p) | |
| 550 | y=map(e -> 0.5+sc*e.v_cumul_est_y, p) | |
| 551 | GR.setlinecolorind(3) | |
| 552 | GR.polyline(x, y) | |
| 553 | if vhist != nothing | |
| 554 | GR.setlinecolorind(4) | |
| 555 | x=map(v -> 1.5+sc*v, vhist[:,2]) | |
| 556 | y=map(v -> 0.5+sc*v, vhist[:,1]) | |
| 557 | GR.polyline(x, y) | |
| 558 | end | |
| 559 | end | |
| 560 | GR.updatews() | |
| 561 | end | |
| 562 | end | |
| 563 | ||
| 564 | ############### | |
| 565 | # Precompiling | |
| 566 | ############### | |
| 567 | ||
| 568 | # precompile(Tuple{typeof(GR.drawimage), Float64, Float64, Float64, Float64, Int64, Int64, Array{UInt32, 2}}) | |
| 569 | # precompile(Tuple{Type{Plots.Plot{T} where T<:RecipesBase.AbstractBackend}, Plots.GRBackend, Int64, Base.Dict{Symbol, Any}, Base.Dict{Symbol, Any}, Array{Plots.Series, 1}, Nothing, Array{Plots.Subplot{T} where T<:RecipesBase.AbstractBackend, 1}, Base.Dict{Any, Plots.Subplot{T} where T<:RecipesBase.AbstractBackend}, Plots.EmptyLayout, Array{Plots.Subplot{T} where T<:RecipesBase.AbstractBackend, 1}, Bool}) | |
| 570 | # precompile(Tuple{typeof(Plots._plot!), Plots.Plot{Plots.GRBackend}, Base.Dict{Symbol, Any}, Tuple{Array{ColorTypes.Gray{Float64}, 2}}}) | |
| 571 | ||
| 572 | end # Module |