Skip to content

Add SAM-style segmentation architecture#21492

Open
AyedaOk wants to merge 3 commits into
darktable-org:masterfrom
AyedaOk:ai-sam3-arch
Open

Add SAM-style segmentation architecture#21492
AyedaOk wants to merge 3 commits into
darktable-org:masterfrom
AyedaOk:ai-sam3-arch

Conversation

@AyedaOk

@AyedaOk AyedaOk commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

This PR Add support for the SAM3 segmentation models ("arch": "sam3"). I know darktable can't ship SAM3 because of licences, but someone could get the model on its own if it is supported.

SAM3 uses the existing SAM-style encoder/decoder flow but with different dimensions:

  • 1008x1008 encoder input
  • 288x288 mask feedback size

The SAM2 path remains unchanged at 1024x1024 input and 256x256 mask size. SegNext behavior is unchanged.

For SAM3 encoder loading, graph optimizations are disabled to avoid ONNX Runtime session creation issues.

To test it, you can request access to SAM3 and download sam3.pt (https://huggingface.co/facebook/sam3). Place it alongside this script and run it with uv run export_sam3_native.py . This will create mask-object-sam3.dtmodel you can import in darktable.

Disclaimer: Co-created with Codex.

@wpferguson

Copy link
Copy Markdown
Member

I know darktable can't ship SAM3 because of licenses

So a PR that enables SAM3 specifically, if merged, says that darktable is endorsing running SAM3, which it's not.


If you want to make a generic (non model specific) way to add ANY model to darktable, then that would be acceptable. If the user wanted to use SAM3 darktable isn't going to stand in the way, but neither is it going to endorse it.


An example:

In the lua-scripts there is contrib/gimp.lua,, which lets a user export an image and edit it with GIMP. Since it's open source, anyone could take that script and change it to launch photoshop instead of GIMP and that's OK. But, if they want that script merged into the lua-scripts, it won't happen because the script is promoting the use of commercial software.

However, if someone makes a script that allows you to open an image and edit it with any executable that you specify, such as contrib/ext_editor.lua, then that's fine. It's up to the user what editor they use and darktable isn't endorsing anything.

@AyedaOk

AyedaOk commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

If you want to make a generic (non model specific) way to add ANY model to darktable, then that would be acceptable. If the user wanted to use SAM3 darktable isn't going to stand in the way, but neither is it going to endorse it.

I understand and will try to make a generic arch. Perhaps if put the dimensions in the config.json or something like that.

@AyedaOk AyedaOk changed the title Add SAM3 segmentation architecture Add SAM-style segmentation architecture Jul 5, 2026
@AyedaOk

AyedaOk commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

The last commit remove the SAM3 and add a generic "SAM-style" architecture. The input and mask size can be defined in the config.json.

{
  "arch": "sam",
  "attributes": {
    "input_size": 1008,
    "prev_mask_size": 288,
    "encoder_optimization": "disabled"
  }
}

To test it, you can download sam3.pt from the Hugging Face and run this script to create the .dtmodel.

@TurboGit

TurboGit commented Jul 6, 2026

Copy link
Copy Markdown
Member

Have you looked at the following page to ensure this meets our policy about AI:

https://github.com/darktable-org/darktable/wiki/AI-Model-Integration-Policy

@andriiryzhkov andriiryzhkov self-requested a review July 6, 2026 09:20
@andriiryzhkov andriiryzhkov added the scope: AI features AI features related issues and PR label Jul 6, 2026
@andriiryzhkov

Copy link
Copy Markdown
Collaborator

SAM3 model does not meet requirements of our AI policy. First of all in terms of license - it is not compatible with GPL-3 and not a free model. So SAM3 model can not be added to out official model repository.

However, adding attributes support to the segmentation.c is reasonable, so that manual installation of model package from unsupported source will be possible.

I will review this PR a bit later. Need some time for this.

@AyedaOk

AyedaOk commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Have you looked at the following page to ensure this meets our policy about AI:

I did and I know SAM3 don't meet your policy. It uses a custome license called the SAM licience. It is permissive, but not open sourced.

My goal was not to have darktable distribute SAM3 in a repositorry. I understand this will never happen. I wanted to create the arch so people could manually add SAM3 they get from a third-party. But like Bill pointed out adding a SAM3 arch means that darktable endorse that model and it's not acceptable.

That's why 2c042d2 only add attributes support. We set the dimensions in the config.json, which allows darktable to accept any "SAM-style" segmentation because we can define the parameters within the configuration file.

I will review this PR a bit later. Need some time for this.

Thanks and no rush. I'm not sure I'm allowed to upload SAM3, so I put a script to convert the .pt into a .dtmodel. You can get the .pt from the HF.

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

Labels

scope: AI features AI features related issues and PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants