TomographicImaging / CIL

A versatile python framework for tomographic imaging
https://tomographicimaging.github.io/CIL/
Apache License 2.0
97 stars 45 forks source link

Sqlite Callback #1988

Open manchester-jhellier opened 1 week ago

manchester-jhellier commented 1 week ago

Description

A callback which lets us evaluate user-defined functions on an Algorithm and save solution state under user-defined conditions. Evaluations of functions and locations of saved solution artifacts are stored in an sqlite database.

Example Usage

Not quite sure what to put for this. Is there a minimal CIL problem you like to test with? I can write some tests based on that if you'd like.

MargaretDuff commented 1 week ago

Hey @manchester-jhellier - thank-you for this - it looks really interesting!

A demonstration example could be something on the spheres data (which is small and should be quick to run) e.g.

from cil.utilities import dataexample
from cil.processors import TransmissionAbsorptionConverter, Slicer
from cil.utilities.quality_measures import psnr
import numpy as np
from cil.plugins.tigre import ProjectionOperator
from cil.optimisation.functions import LeastSquares, L2NormSquared, TotalVariation,
from cil.optimisation.algorithms import FISTA 

ground_truth = dataexample.SIMULATED_SPHERE_VOLUME.get()

data = dataexample.SIMULATED_CONE_BEAM_DATA.get()
data = data.get_slice(vertical='centre')
ground_truth = ground_truth.get_slice(vertical='centre')
absorption = TransmissionAbsorptionConverter()(data)
absorption = Slicer(roi={'angle':(0, -1, 5)})(absorption)

ig = ground_truth.geometry

alpha1=0.1
alpha2=0.1

A = ProjectionOperator(image_geometry=ig, 
                       acquisition_geometry=absorption.geometry)
F = LeastSquares(A = A, b = absorption)+alpha1*L2NormSquared()
G = alpha2*TotalVariation(lower=0)

algo_tv = FISTA(initial = ig.allocate(), f = F, g = G)
algo_tv.run(100, callbacks=[callbacks.ProgressCallback()])

Here there are 2 regularisation parameters - for the TV and Tikhonov terms and so you could try a range of values of these, saving the PSNR of the reconstructed image compared with the ground truth alongside a path to saved image? Example code could go in this folder https://github.com/TomographicImaging/CIL-Demos/tree/main/misc or it would fit really well as a CIL showcase (I can help polish this!) https://github.com/TomographicImaging/CIL-User-Showcase.

R.e. unit tests, they need to be much less computationally costly and are more about testing each "unit" of the code e.g. that the initialise works with all the right defaults etc. We can help write these later!

manchester-jhellier commented 1 week ago

Cool! That looks like nice little problem; I'll see if I can't grab all three components of the objective and store them, should show you what I mean. The callbacks will be a bit solver-specific, hence my issue about subobjective evaluation on demand. I'll try and make you a demonstration soon.

MargaretDuff commented 1 week ago

Thanks @manchester-jhellier!