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