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