Draft PR Add Python demo for smoothed TV inpainting#4152
Draft PR Add Python demo for smoothed TV inpainting#4152jb2178-star wants to merge 7 commits intoFEniCS:mainfrom
Conversation
|
Added plotting with matplotlib for comparison of fields (u_true, mask, f, u, and difference). Matplotlib was chosen here for clarity in comparing multiple scalar fields. PyVista could be used for alternative visualization if preferred. Additionally added diagnostics for data fidelity, TV seminorm and reconstruction error. |
|
Hi all, I believe this PR is now ready for review. I have added clearer and more detailed documentation to better align with the style used in dolfinx. I can also include a more complete derivation of the weak form if that would be helpful. I have also added the nonlinear solver metrics such as:
As well as reconstruction metrics:
I have also improved the visualization by including both global error and the hole only error plots. This example demonstrates how to use dolfinx for nonlinear variational problems that may arise in image processing. In particular it demonstrates:
This highlights dolfinx's ability to handle nonlinear, non-quadratic variational problems in a clear and concise way. Thanks to @mscroggs for linking this PR to the relevant issue, and to @jorgensd for reviewing the original issue and providing feedback. If there are any further suggestions or improvements, please let me know. Otherwise I believe this is now a complete documented inpainting demo ready for review. |
|
The CI lint checks are picking up some ruff warnings. You can fix many of these by running |
|
@jb2178-star I've tried to do a major refactoring of the text (to make sure the docs render) and simplify the text a bit. |
|
Hi @jorgensd , I really appreciate you for refactoring , simplifying the text and taking the time to review this. It has been a really valuable learning experience for me, as this is my first time contributing to an open source project. I initially wrote F with the intention to show the weak form explicitly. However I understand that using ufl.derivative to obtain the weak form directly is cleaner and more concise, and I'm happy to refactor it if you think that would be preferable. |
Summary
This PR adds a minimal Python demo for for variational image inpainting with smoothed total variation regularization on a synthetic image with an irregular interior mask, as this demonstrates the use of FEM over Finite Difference methods.
Starting with a variational inpainting model with the spirit of Chan, Shen we have J(u)= data fit + regularization term, we use the TV term from Rudin, Osher, Fatemi :
And make it smooth:
and the data fitted term, from standard masked least squares (m=1 on known, m=0 on missing data):
Due to the fact$|\nabla u|$ is not differentiable when $\nabla u=0$ , and its hard to use newton's method, so we introduce a small $\varepsilon$ term.
When applying Euler-Lagrange to minimize$J(u)$ we get:
And with our test function$v$ we can arrive at the weak form:
this current demo adds a unit-square mesh generated with DOLFINx, a synthetic scalar image, an irregular mask, a nonlinear variational formulation in UFL, a Jacobian generated with ufl.derivative and a nonlinear solve using dolfinx.fem.petsc.NonlinearProblem
Notes
This is intended as a rough draft of the demo. I do intend to include plotting/diagnostics and more advanced geometry in a follow-up revision if that makes sense. Additionally more defined comments and documentation.