ITADN

Improve documentation for loading pretrained weights with timm universal (tu-) encoders

#1267Opentayden 创建于 2026-02-11
T
taydencommented
First off, thanks for maintaining this excellent library — it's a huge time-saver for me and the segmentation community! I'd like to suggest improving the documentation around how pretrained weights are loaded for `tu-` prefixed (timm universal) encoders. The current behavior works, but it took some source-diving to understand, and I think a few docs improvements could save others the same effort. ### Current Behavior For `tu-` encoders, `get_encoder()` in `encoders/__init__.py` converts `encoder_weights` to a boolean: ```python pretrained=weights is not None, ``` This means: - `encoder_weights="imagenet"` → `pretrained=True` - `encoder_weights="anything_at_all"` → `pretrained=True` - `encoder_weights=None` → `pretrained=False` The string value has no effect — it's purely a `None` check. This differs from built-in SMP encoders where `encoder_weights` selects a specific weight variant (e.g., `"imagenet"`, `"ssl"`, `"swsl"`). ### Why This Can Be Confusing 1. **Misleading parameter semantics**: Setting `encoder_weights="imagenet"` implies SMP is selecting ImageNet weights specifically, but timm models use their own naming scheme (e.g., `convnextv2_base.fcmae_ft_in22k_in1k`). The pretrained variant is determined entirely by the model name string, not `encoder_weights`. 2. **No validation or feedback**: Passing a meaningless string like `encoder_weights="foo"` silently loads whatever pretrained weights timm resolves for that model name. There's no warning that the value is ignored. 3. **Weight variant is embedded in the encoder name**: For timm models, the weights are selected by the model name suffix (e.g., `.fcmae_ft_in22k_in1k`), which is a completely different mechanism than the `encoder_weights` parameter used by built-in encoders. This isn't immediately obvious without reading the source. ### Suggestions These are just ideas — happy to open a PR if any of them seem worthwhile: - **Document the `tu-` encoder weight loading behavior** in the README and/or docstrings for `create_model()` and `get_encoder()`, explaining that `encoder_weights` acts as a boolean toggle and that the weight variant is selected via the encoder name. - **Consider accepting `encoder_weights=True`** as a valid value for `tu-` encoders to make the boolean nature explicit. - **Optionally emit a hint** if a `tu-` encoder is used with a string value, e.g.: ``` Note: For timm universal encoders, `encoder_weights` is used only as a boolean flag (not None = pretrained). The pretrained weight variant is determined by the encoder name. Got encoder_weights='imagenet'. ``` - **Add a section to the docs** showing how to use `tu-` encoders with specific pretrained weight variants, e.g.: ```python # Load convnextv2_base with FCMAE fine-tuned IN-22k → IN-1k weights model = smp.Unet( encoder_name="tu-convnextv2_base.fcmae_ft_in22k_in1k", encoder_weights="imagenet", # any non-None value enables pretrained loading ) ``` Thanks again for all the work on this library! ### Environment - segmentation-models-pytorch version: 0.5.0 - timm version: 1.0.24 - Python version: 3.13.9
1 条评论