Skip to content

[AI] Add neural restore lighttable module for AI denoise and upscale#20523

Open
andriiryzhkov wants to merge 1 commit intodarktable-org:masterfrom
andriiryzhkov:neural-restore
Open

[AI] Add neural restore lighttable module for AI denoise and upscale#20523
andriiryzhkov wants to merge 1 commit intodarktable-org:masterfrom
andriiryzhkov:neural-restore

Conversation

@andriiryzhkov
Copy link
Contributor

This is a third and improved part of the original PR #20322.

Summary

  • Add a new lighttable/darkroom utility module (neural_restore) that provides AI-based image restoration using ONNX backend models
  • Supports two operations via a tabbed notebook UI: denoise (e.g. NIND UNet) and upscale (e.g. BSRGAN at 2x/4x)
  • Includes an interactive split before/after preview with draggable divider, on-demand generation (click to start), and a detail recovery slider that uses wavelet (DWT) decomposition to recover fine texture lost during denoising
  • Batch processing runs as a background job with progress reporting, cancellation support, tiled inference with overlap to avoid seam artifacts, and memory-aware tile size selection
  • Output is written as 32-bit float TIFF and automatically imported into the library, grouped with the source image
  • Register denoise-nind and upscale-bsrgan models in the AI model registry (ai_models.json)

Details

  • Built only when USE_AI=ON (cmake option, default OFF)
  • Preview thread uses atomic sequence counter for cancellation and is joined before cleanup to avoid use-after-free
  • Pixel pipeline: linear Rec.709 → sRGB before inference, sRGB → linear after; planar NCHW layout for ONNX models
  • Detail recovery: extracts luminance residual (original − denoised), applies per-band wavelet thresholding to separate noise from texture, blends filtered detail back at user-controlled strength
denoise upscale

Fixes: #19310

@esq4
Copy link
Contributor

esq4 commented Mar 14, 2026

Excellent! I was looking forward to it :)
But one question.
Output is written as 32-bit float TIFF and automatically imported into the library, grouped with the source image
That's very hard on the disk. Have you considered integrating it into the export like in CommReteris/nind-denoise?
With this implementation, the intermediate TIFF can be created in a temporary directory (I even placed it on tmpfs on my Linux) and deleted immediately after the export is complete.

@wpferguson
Copy link
Member

How do these models fit in with the "acceptable" model conversation?

If we merge a module that requires a model that's not "acceptable", then darktable is "endorsing"/requiring the non acceptable model.

@andriiryzhkov
Copy link
Contributor Author

@wpferguson:

How do these models fit in with the "acceptable" model conversation?

These models easily meet criteria for open-source AI. Please, see details here:

@andriiryzhkov
Copy link
Contributor Author

@esq4: Good point about disk usage. The current approach (import as grouped image) is intentional — it gives you a denoised/upscaled source that you can further develop in darkroom and compare via grouping. This is conceptually different from export, which is a final output step.

More flexibility would definitely be helpful here. What I think can be added as extra parameters:

  • Choice between 16-bit and 32-bit float TIFF (halves the size when full precision isn't needed)
  • Option to auto-import into the library or not
  • Maybe, configurable output directory (so you can point it to tmpfs or a fast scratch disk)

As for export-time denoising (like nind-denoise) — that's a different use case but a valid one. It could be added to the export module down the road as a complementary feature.

@wpferguson
Copy link
Member

BSRGAN only meets open source tooling and in the limitations it says Training datasets Flickr2K/WED/OST do not have explicit open-source licenses

If I look at some of the other models (SAM) they require data destruction in 3 months. Does that mean I have to destroy my edits? It also says no commercial use, so if I sell one of my images am I in violation?

I'm sorry, but this is a minefield. Somehow we need to decide a quick way to determine if a model is acceptable.

Do we use the OSAID, and if so what MOF? It seems Class I, Open Science, is fine. However Class II, Open Tooling, seems to come with lots of limitations/questions. If we decide to use Class II, how are we going to communicate the limitations to the users? Don't expect them to read, we already know how well that works.

@victoryforce
Copy link
Collaborator

If I look at some of the other models (SAM) they require data destruction in 3 months.

You are referring to clause 7 of SA-1B DATASET RESEARCH LICENSE. But that is a requirement for the training dataset, not the model. You can use that dataset for your research, for training your own model, but you can't keep it indefinitely. What's the problem?

Does that mean I have to destroy my edits?

Absolutely not. This is a conclusion from your statement above that is not true.

It also says no commercial use, so if I sell one of my images am I in violation?

Also no. The user is using the model and not the training dataset.

I'm sorry, but this is a minefield.

I'm also sorry, but this is NOT a minefield. Wanna see photos of what a minefield actually is? :)

Somehow we need to decide a quick way to determine if a model is acceptable.

Too vague. What is "acceptable"? Why should we decide what is acceptable for users and not the users themselves?

@KarlMagnusLarsson
Copy link

KarlMagnusLarsson commented Mar 15, 2026

Thank you for this PR. It works for me. The denoise and upscale functions work. I use the nind and bsrgan models from preferences -> AI after downloading them.

If I test this PR on top of git master branch I get fallback to CPU and can not activate NVIDIA CUDA. The denose and, even more so, upscale, are very heavy using CPU and I would say that a GPU is essential (not so for AI masks).

Workaround:
If I test this PR + PR #20522 (comment) (Enable GPU for ONNX runtime on GNU/Linux) then I can run NVIDIA CUDA and GPU. (Linux Debian, NVIDIA QUADRO RTX 4000 8GB, driver 550.163.01, CUDA 12.4).

I found a couple of preliminary issues:

  • The generated 32 bit floating point TIFF (deniosed, upscaled) do not contain a color profile. The normal darktable export module allows for picking color profile and and a rendering intent, EDIT: which is then stored in output file. I can se in details that we have: Pixel pipeline: linear Rec.709 → sRGB before inference, sRGB → linear after; planar NCHW layout for ONNX models. EDIT2: I can use the darktable export module and select linear Rec.709, if I want to (convert to) and embed linear rec 709 in output result file.
  • The EXIF data is not carried over from the source into the TIFF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI Denoising

5 participants