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