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