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