Repository navigation
Add pretrained_backbone parameter to ConvNeXtUNet to enable pretrained convnext as backbone #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ def __init__( | |
| decoder_up_block: Literal['pixelshuffle', 'convt'] = 'pixelshuffle', | ||
| decoder_compute_block: Literal['convnext', 'conv2d'] = 'convnext', | ||
| act_type: ActivationType = 'sigmoid', | ||
| pretrained_backbone: bool = False, | ||
| _num_units: Union[List[int], int] = 2, | ||
| _pixel_shuffle_preserve_channels: bool = False, | ||
| ): | ||
|
|
@@ -61,6 +62,8 @@ def __init__( | |
| :param decoder_compute_block: Type of computation block to use in the | ||
| decoder. Can be 'convnext' for Conv2DConvNeXtBlock or 'conv2d' for | ||
| Conv2DNormActBlock. Default is 'convnext'. | ||
| :param pretrained_backbone: Whether to use a pretrained ConvNeXtV2_tiny backbone. | ||
| Default is False. | ||
| :param act_type: Type of activation function to use in the output layer. | ||
| Default is 'sigmoid'. | ||
| :param _num_units: Number of computation units in each stage. | ||
|
|
@@ -80,7 +83,7 @@ def __init__( | |
| convnextv2_model = timm.create_model( | ||
| "convnextv2_tiny", | ||
| features_only=True, | ||
| pretrained=False | ||
| pretrained=pretrained_backbone | ||
| ) | ||
| # replace the first convolutional layer to work with (N, in_channels, H, W) | ||
| # images | ||
|
|
@@ -93,6 +96,7 @@ def __init__( | |
| stride=1, | ||
| padding=0 | ||
| ) | ||
| self._pretrained_backbone = pretrained_backbone | ||
| depth = len(convnextv2_model.feature_info.channels()) | ||
|
|
||
| self.encoder = convnextv2_model | ||
|
Comment on lines
62
to
102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '35,180p' src/virtual_stain_flow/models/unext.py
rg -n 'apply\(|reset_parameters|init_weights|initialize|trunc_normal_|kaiming_|xavier_' src/virtual_stain_flow/modelsRepository: WayScience/virtual_stain_flow Length of output: 7441 🏁 Script executed: #!/bin/bash
printf '%s\n' '--- unext imports/class and related symbols ---'
sed -n '1,45p' src/virtual_stain_flow/models/unext.py
rg -n 'class (ConvNeXtUNet|Base|.*Model)|super\(\).__init__|def __init__|self\.encoder|apply\(|reset_parameters|init_weights|initialize|state_dict|load_state_dict' src/virtual_stain_flow/ -g '*.py' | head -240
printf '%s\n' '--- dependency declarations for timm/torch ---'
rg -n -i '(^|[ =])timm|(^|[ =])torch|dependencies|requires-python' pyproject.toml setup.py setup.cfg requirements*.txt environment*.yml environment*.yaml 2>/dev/null || true
printf '%s\n' '--- decoder and relevant block construction ---'
sed -n '1,130p' src/virtual_stain_flow/models/decoder.py
sed -n '1,125p' src/virtual_stain_flow/models/encoder.pyRepository: WayScience/virtual_stain_flow Length of output: 25205 🏁 Script executed: #!/bin/bash
printf '%s\n' '--- base model initialization ---'
sed -n '1,165p' src/virtual_stain_flow/models/model.py
printf '%s\n' '--- pretrained backbone reachability ---'
rg -n -C 3 'pretrained_backbone|ConvNeXtUNet' . -g '*.py' -g '*.yaml' -g '*.yml' -g '*.json' -g '*.toml' -g '*.md' -g '*.ini' | head -240
printf '%s\n' '--- timm and torch version declarations ---'
sed -n '1,55p' pyproject.toml
find . -maxdepth 3 -type d \( -name timm -o -name torch -o -name site-packages \) -printRepository: WayScience/virtual_stain_flow Length of output: 19070 Preserve the pretrained stem when 🤖 Prompt for AI Agents |
||
|
|
@@ -197,6 +201,7 @@ def to_config(self) -> Dict[str, Any]: | |
| "decoder_up_block": self._decoder_up_block, | ||
| "decoder_compute_block": self._decoder_compute_block, | ||
| "act_type": self._act_type, | ||
| "pretrained_backbone": self._pretrained_backbone, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win Keep serialized pretrained metadata consistent.
🤖 Prompt for AI Agents |
||
| "_num_units": self._num_units_cfg, | ||
| "_pixel_shuffle_preserve_channels": self._pixel_shuffle_preserve_channels, | ||
| }, | ||
|
|
@@ -213,5 +218,8 @@ def from_config(cls, config: Dict[str, Any]) -> "ConvNeXtUNet": | |
| if "_pixel_shuffle_preserve_channels" not in init_cfg: | ||
| # For backward compatibility with configs that don't have this key | ||
| init_cfg["_pixel_shuffle_preserve_channels"] = True | ||
| if "pretrained_backbone" not in init_cfg: | ||
| # For backward compatibility with configs that don't have this key | ||
| init_cfg["pretrained_backbone"] = False | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice addition also |
||
|
|
||
| return cls(**init_cfg) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: WayScience/virtual_stain_flow
Length of output: 3991
🏁 Script executed:
Repository: WayScience/virtual_stain_flow
Length of output: 49759
Preserve existing positional argument binding.
Callers that pass
_num_unitspositionally now bind that value topretrained_backbone. A seventh positional value binds to_num_units, while_pixel_shuffle_preserve_channelskeeps its default. This can enable pretrained loading or configure the decoder with the wrong unit count.Move the new option after the existing parameters and make it keyword-only.
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents