From f063cc2cb0f1984e36737e31b010077bc396c6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 9 Aug 2025 14:34:18 +0800 Subject: [PATCH 01/17] Fix: add ensure_channel_first in utils and integrate into DiceHelper (refs #8366) --- issue38366/bin/Activate.ps1 | 247 +++++++++++++++++++++++++++++++++++ issue38366/bin/activate | 69 ++++++++++ issue38366/bin/activate.csh | 26 ++++ issue38366/bin/activate.fish | 69 ++++++++++ issue38366/bin/pip | 8 ++ issue38366/bin/pip3 | 8 ++ issue38366/bin/pip3.11 | 8 ++ issue38366/bin/python | 1 + issue38366/bin/python3 | 1 + issue38366/bin/python3.11 | 1 + issue38366/pyvenv.cfg | 5 + monai/metrics/meandice.py | 9 +- 12 files changed, 451 insertions(+), 1 deletion(-) create mode 100644 issue38366/bin/Activate.ps1 create mode 100644 issue38366/bin/activate create mode 100644 issue38366/bin/activate.csh create mode 100644 issue38366/bin/activate.fish create mode 100755 issue38366/bin/pip create mode 100755 issue38366/bin/pip3 create mode 100755 issue38366/bin/pip3.11 create mode 120000 issue38366/bin/python create mode 120000 issue38366/bin/python3 create mode 120000 issue38366/bin/python3.11 create mode 100644 issue38366/pyvenv.cfg diff --git a/issue38366/bin/Activate.ps1 b/issue38366/bin/Activate.ps1 new file mode 100644 index 0000000000..b49d77ba44 --- /dev/null +++ b/issue38366/bin/Activate.ps1 @@ -0,0 +1,247 @@ +<# +.Synopsis +Activate a Python virtual environment for the current PowerShell session. + +.Description +Pushes the python executable for a virtual environment to the front of the +$Env:PATH environment variable and sets the prompt to signify that you are +in a Python virtual environment. Makes use of the command line switches as +well as the `pyvenv.cfg` file values present in the virtual environment. + +.Parameter VenvDir +Path to the directory that contains the virtual environment to activate. The +default value for this is the parent of the directory that the Activate.ps1 +script is located within. + +.Parameter Prompt +The prompt prefix to display when this virtual environment is activated. By +default, this prompt is the name of the virtual environment folder (VenvDir) +surrounded by parentheses and followed by a single space (ie. '(.venv) '). + +.Example +Activate.ps1 +Activates the Python virtual environment that contains the Activate.ps1 script. + +.Example +Activate.ps1 -Verbose +Activates the Python virtual environment that contains the Activate.ps1 script, +and shows extra information about the activation as it executes. + +.Example +Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv +Activates the Python virtual environment located in the specified location. + +.Example +Activate.ps1 -Prompt "MyPython" +Activates the Python virtual environment that contains the Activate.ps1 script, +and prefixes the current prompt with the specified string (surrounded in +parentheses) while the virtual environment is active. + +.Notes +On Windows, it may be required to enable this Activate.ps1 script by setting the +execution policy for the user. You can do this by issuing the following PowerShell +command: + +PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser + +For more information on Execution Policies: +https://go.microsoft.com/fwlink/?LinkID=135170 + +#> +Param( + [Parameter(Mandatory = $false)] + [String] + $VenvDir, + [Parameter(Mandatory = $false)] + [String] + $Prompt +) + +<# Function declarations --------------------------------------------------- #> + +<# +.Synopsis +Remove all shell session elements added by the Activate script, including the +addition of the virtual environment's Python executable from the beginning of +the PATH variable. + +.Parameter NonDestructive +If present, do not remove this function from the global namespace for the +session. + +#> +function global:deactivate ([switch]$NonDestructive) { + # Revert to original values + + # The prior prompt: + if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { + Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt + Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT + } + + # The prior PYTHONHOME: + if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { + Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME + Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME + } + + # The prior PATH: + if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { + Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH + Remove-Item -Path Env:_OLD_VIRTUAL_PATH + } + + # Just remove the VIRTUAL_ENV altogether: + if (Test-Path -Path Env:VIRTUAL_ENV) { + Remove-Item -Path env:VIRTUAL_ENV + } + + # Just remove VIRTUAL_ENV_PROMPT altogether. + if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { + Remove-Item -Path env:VIRTUAL_ENV_PROMPT + } + + # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: + if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { + Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force + } + + # Leave deactivate function in the global namespace if requested: + if (-not $NonDestructive) { + Remove-Item -Path function:deactivate + } +} + +<# +.Description +Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the +given folder, and returns them in a map. + +For each line in the pyvenv.cfg file, if that line can be parsed into exactly +two strings separated by `=` (with any amount of whitespace surrounding the =) +then it is considered a `key = value` line. The left hand string is the key, +the right hand is the value. + +If the value starts with a `'` or a `"` then the first and last character is +stripped from the value before being captured. + +.Parameter ConfigDir +Path to the directory that contains the `pyvenv.cfg` file. +#> +function Get-PyVenvConfig( + [String] + $ConfigDir +) { + Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" + + # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). + $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue + + # An empty map will be returned if no config file is found. + $pyvenvConfig = @{ } + + if ($pyvenvConfigPath) { + + Write-Verbose "File exists, parse `key = value` lines" + $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath + + $pyvenvConfigContent | ForEach-Object { + $keyval = $PSItem -split "\s*=\s*", 2 + if ($keyval[0] -and $keyval[1]) { + $val = $keyval[1] + + # Remove extraneous quotations around a string value. + if ("'""".Contains($val.Substring(0, 1))) { + $val = $val.Substring(1, $val.Length - 2) + } + + $pyvenvConfig[$keyval[0]] = $val + Write-Verbose "Adding Key: '$($keyval[0])'='$val'" + } + } + } + return $pyvenvConfig +} + + +<# Begin Activate script --------------------------------------------------- #> + +# Determine the containing directory of this script +$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition +$VenvExecDir = Get-Item -Path $VenvExecPath + +Write-Verbose "Activation script is located in path: '$VenvExecPath'" +Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" +Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" + +# Set values required in priority: CmdLine, ConfigFile, Default +# First, get the location of the virtual environment, it might not be +# VenvExecDir if specified on the command line. +if ($VenvDir) { + Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" +} +else { + Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." + $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") + Write-Verbose "VenvDir=$VenvDir" +} + +# Next, read the `pyvenv.cfg` file to determine any required value such +# as `prompt`. +$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir + +# Next, set the prompt from the command line, or the config file, or +# just use the name of the virtual environment folder. +if ($Prompt) { + Write-Verbose "Prompt specified as argument, using '$Prompt'" +} +else { + Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" + if ($pyvenvCfg -and $pyvenvCfg['prompt']) { + Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" + $Prompt = $pyvenvCfg['prompt']; + } + else { + Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" + Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" + $Prompt = Split-Path -Path $venvDir -Leaf + } +} + +Write-Verbose "Prompt = '$Prompt'" +Write-Verbose "VenvDir='$VenvDir'" + +# Deactivate any currently active virtual environment, but leave the +# deactivate function in place. +deactivate -nondestructive + +# Now set the environment variable VIRTUAL_ENV, used by many tools to determine +# that there is an activated venv. +$env:VIRTUAL_ENV = $VenvDir + +if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { + + Write-Verbose "Setting prompt to '$Prompt'" + + # Set the prompt to include the env name + # Make sure _OLD_VIRTUAL_PROMPT is global + function global:_OLD_VIRTUAL_PROMPT { "" } + Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT + New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt + + function global:prompt { + Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " + _OLD_VIRTUAL_PROMPT + } + $env:VIRTUAL_ENV_PROMPT = $Prompt +} + +# Clear PYTHONHOME +if (Test-Path -Path Env:PYTHONHOME) { + Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME + Remove-Item -Path Env:PYTHONHOME +} + +# Add the venv to the PATH +Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH +$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/issue38366/bin/activate b/issue38366/bin/activate new file mode 100644 index 0000000000..ca7120d641 --- /dev/null +++ b/issue38366/bin/activate @@ -0,0 +1,69 @@ +# This file must be used with "source bin/activate" *from bash* +# you cannot run it directly + +deactivate () { + # reset old environment variables + if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then + PATH="${_OLD_VIRTUAL_PATH:-}" + export PATH + unset _OLD_VIRTUAL_PATH + fi + if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then + PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" + export PYTHONHOME + unset _OLD_VIRTUAL_PYTHONHOME + fi + + # This should detect bash and zsh, which have a hash command that must + # be called to get it to forget past commands. Without forgetting + # past commands the $PATH changes we made may not be respected + if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then + hash -r 2> /dev/null + fi + + if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then + PS1="${_OLD_VIRTUAL_PS1:-}" + export PS1 + unset _OLD_VIRTUAL_PS1 + fi + + unset VIRTUAL_ENV + unset VIRTUAL_ENV_PROMPT + if [ ! "${1:-}" = "nondestructive" ] ; then + # Self destruct! + unset -f deactivate + fi +} + +# unset irrelevant variables +deactivate nondestructive + +VIRTUAL_ENV="/Users/linminyou/Projects/MONAI/issue38366" +export VIRTUAL_ENV + +_OLD_VIRTUAL_PATH="$PATH" +PATH="$VIRTUAL_ENV/bin:$PATH" +export PATH + +# unset PYTHONHOME if set +# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) +# could use `if (set -u; : $PYTHONHOME) ;` in bash +if [ -n "${PYTHONHOME:-}" ] ; then + _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" + unset PYTHONHOME +fi + +if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then + _OLD_VIRTUAL_PS1="${PS1:-}" + PS1="(issue38366) ${PS1:-}" + export PS1 + VIRTUAL_ENV_PROMPT="(issue38366) " + export VIRTUAL_ENV_PROMPT +fi + +# This should detect bash and zsh, which have a hash command that must +# be called to get it to forget past commands. Without forgetting +# past commands the $PATH changes we made may not be respected +if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then + hash -r 2> /dev/null +fi diff --git a/issue38366/bin/activate.csh b/issue38366/bin/activate.csh new file mode 100644 index 0000000000..37b77be26d --- /dev/null +++ b/issue38366/bin/activate.csh @@ -0,0 +1,26 @@ +# This file must be used with "source bin/activate.csh" *from csh*. +# You cannot run it directly. +# Created by Davide Di Blasi . +# Ported to Python 3.3 venv by Andrew Svetlov + +alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate' + +# Unset irrelevant variables. +deactivate nondestructive + +setenv VIRTUAL_ENV "/Users/linminyou/Projects/MONAI/issue38366" + +set _OLD_VIRTUAL_PATH="$PATH" +setenv PATH "$VIRTUAL_ENV/bin:$PATH" + + +set _OLD_VIRTUAL_PROMPT="$prompt" + +if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then + set prompt = "(issue38366) $prompt" + setenv VIRTUAL_ENV_PROMPT "(issue38366) " +endif + +alias pydoc python -m pydoc + +rehash diff --git a/issue38366/bin/activate.fish b/issue38366/bin/activate.fish new file mode 100644 index 0000000000..cea8ecad26 --- /dev/null +++ b/issue38366/bin/activate.fish @@ -0,0 +1,69 @@ +# This file must be used with "source /bin/activate.fish" *from fish* +# (https://fishshell.com/); you cannot run it directly. + +function deactivate -d "Exit virtual environment and return to normal shell environment" + # reset old environment variables + if test -n "$_OLD_VIRTUAL_PATH" + set -gx PATH $_OLD_VIRTUAL_PATH + set -e _OLD_VIRTUAL_PATH + end + if test -n "$_OLD_VIRTUAL_PYTHONHOME" + set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME + set -e _OLD_VIRTUAL_PYTHONHOME + end + + if test -n "$_OLD_FISH_PROMPT_OVERRIDE" + set -e _OLD_FISH_PROMPT_OVERRIDE + # prevents error when using nested fish instances (Issue #93858) + if functions -q _old_fish_prompt + functions -e fish_prompt + functions -c _old_fish_prompt fish_prompt + functions -e _old_fish_prompt + end + end + + set -e VIRTUAL_ENV + set -e VIRTUAL_ENV_PROMPT + if test "$argv[1]" != "nondestructive" + # Self-destruct! + functions -e deactivate + end +end + +# Unset irrelevant variables. +deactivate nondestructive + +set -gx VIRTUAL_ENV "/Users/linminyou/Projects/MONAI/issue38366" + +set -gx _OLD_VIRTUAL_PATH $PATH +set -gx PATH "$VIRTUAL_ENV/bin" $PATH + +# Unset PYTHONHOME if set. +if set -q PYTHONHOME + set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME + set -e PYTHONHOME +end + +if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" + # fish uses a function instead of an env var to generate the prompt. + + # Save the current fish_prompt function as the function _old_fish_prompt. + functions -c fish_prompt _old_fish_prompt + + # With the original prompt function renamed, we can override with our own. + function fish_prompt + # Save the return status of the last command. + set -l old_status $status + + # Output the venv prompt; color taken from the blue of the Python logo. + printf "%s%s%s" (set_color 4B8BBE) "(issue38366) " (set_color normal) + + # Restore the return status of the previous command. + echo "exit $old_status" | . + # Output the original/"old" prompt. + _old_fish_prompt + end + + set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" + set -gx VIRTUAL_ENV_PROMPT "(issue38366) " +end diff --git a/issue38366/bin/pip b/issue38366/bin/pip new file mode 100755 index 0000000000..dbf05001bc --- /dev/null +++ b/issue38366/bin/pip @@ -0,0 +1,8 @@ +#!/Users/linminyou/Projects/MONAI/issue38366/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/issue38366/bin/pip3 b/issue38366/bin/pip3 new file mode 100755 index 0000000000..dbf05001bc --- /dev/null +++ b/issue38366/bin/pip3 @@ -0,0 +1,8 @@ +#!/Users/linminyou/Projects/MONAI/issue38366/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/issue38366/bin/pip3.11 b/issue38366/bin/pip3.11 new file mode 100755 index 0000000000..dbf05001bc --- /dev/null +++ b/issue38366/bin/pip3.11 @@ -0,0 +1,8 @@ +#!/Users/linminyou/Projects/MONAI/issue38366/bin/python3.11 +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/issue38366/bin/python b/issue38366/bin/python new file mode 120000 index 0000000000..6e7f3c7dd2 --- /dev/null +++ b/issue38366/bin/python @@ -0,0 +1 @@ +python3.11 \ No newline at end of file diff --git a/issue38366/bin/python3 b/issue38366/bin/python3 new file mode 120000 index 0000000000..6e7f3c7dd2 --- /dev/null +++ b/issue38366/bin/python3 @@ -0,0 +1 @@ +python3.11 \ No newline at end of file diff --git a/issue38366/bin/python3.11 b/issue38366/bin/python3.11 new file mode 120000 index 0000000000..e8dc9bc4c9 --- /dev/null +++ b/issue38366/bin/python3.11 @@ -0,0 +1 @@ +/Users/linminyou/anaconda3/bin/python3.11 \ No newline at end of file diff --git a/issue38366/pyvenv.cfg b/issue38366/pyvenv.cfg new file mode 100644 index 0000000000..6d0bd1b4b1 --- /dev/null +++ b/issue38366/pyvenv.cfg @@ -0,0 +1,5 @@ +home = /Users/linminyou/anaconda3/bin +include-system-site-packages = false +version = 3.11.4 +executable = /Users/linminyou/anaconda3/bin/python3.11 +command = /Users/linminyou/venvs/monai-dev/bin/python3 -m venv /Users/linminyou/Projects/MONAI/issue38366 diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index 0802cc3364..f9e29cea88 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -15,6 +15,8 @@ from monai.metrics.utils import do_metric_reduction from monai.utils import MetricReduction, deprecated_arg +from monai.metrics.utils import ensure_channel_first + from .metric import CumulativeIterationMetric @@ -122,6 +124,7 @@ def __init__( ignore_empty=self.ignore_empty, num_classes=self.num_classes, ) + def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor: # type: ignore[override] """ @@ -296,7 +299,7 @@ def compute_channel(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor if denorm <= 0: return torch.tensor(1.0, device=y_o.device) return torch.tensor(0.0, device=y_o.device) - + def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: """ Compute the metric for the given prediction and ground truth. @@ -306,6 +309,10 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl the number of channels is inferred from ``y_pred.shape[1]`` when ``num_classes is None``. y: ground truth with shape (batch_size, num_classes or 1, spatial_dims...). """ + y_pred, _ = ensure_channel_first(y_pred) + if y.ndim == y_pred.ndim and (y.shape[-1] == y_pred.shape[1] or y.shape[-1] == 1): + y, _ = ensure_channel_first(y) + _apply_argmax, _threshold = self.apply_argmax, self.threshold if self.num_classes is None: n_pred_ch = y_pred.shape[1] # y_pred is in one-hot format or multi-channel scores From dc721d4ca53fa2b6be12b332fcd2f8773d3fcfbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 9 Aug 2025 14:36:09 +0800 Subject: [PATCH 02/17] chore: remove local venv directory from repo --- .gitignore | 1 + issue38366/bin/Activate.ps1 | 247 ----------------------------------- issue38366/bin/activate | 69 ---------- issue38366/bin/activate.csh | 26 ---- issue38366/bin/activate.fish | 69 ---------- issue38366/bin/pip | 8 -- issue38366/bin/pip3 | 8 -- issue38366/bin/pip3.11 | 8 -- issue38366/bin/python | 1 - issue38366/bin/python3 | 1 - issue38366/bin/python3.11 | 1 - issue38366/pyvenv.cfg | 5 - 12 files changed, 1 insertion(+), 443 deletions(-) delete mode 100644 issue38366/bin/Activate.ps1 delete mode 100644 issue38366/bin/activate delete mode 100644 issue38366/bin/activate.csh delete mode 100644 issue38366/bin/activate.fish delete mode 100755 issue38366/bin/pip delete mode 100755 issue38366/bin/pip3 delete mode 100755 issue38366/bin/pip3.11 delete mode 120000 issue38366/bin/python delete mode 120000 issue38366/bin/python3 delete mode 120000 issue38366/bin/python3.11 delete mode 100644 issue38366/pyvenv.cfg diff --git a/.gitignore b/.gitignore index 76c6ab0d12..99cc678859 100644 --- a/.gitignore +++ b/.gitignore @@ -165,3 +165,4 @@ runs *.pth *zarr/* +issue38366/ diff --git a/issue38366/bin/Activate.ps1 b/issue38366/bin/Activate.ps1 deleted file mode 100644 index b49d77ba44..0000000000 --- a/issue38366/bin/Activate.ps1 +++ /dev/null @@ -1,247 +0,0 @@ -<# -.Synopsis -Activate a Python virtual environment for the current PowerShell session. - -.Description -Pushes the python executable for a virtual environment to the front of the -$Env:PATH environment variable and sets the prompt to signify that you are -in a Python virtual environment. Makes use of the command line switches as -well as the `pyvenv.cfg` file values present in the virtual environment. - -.Parameter VenvDir -Path to the directory that contains the virtual environment to activate. The -default value for this is the parent of the directory that the Activate.ps1 -script is located within. - -.Parameter Prompt -The prompt prefix to display when this virtual environment is activated. By -default, this prompt is the name of the virtual environment folder (VenvDir) -surrounded by parentheses and followed by a single space (ie. '(.venv) '). - -.Example -Activate.ps1 -Activates the Python virtual environment that contains the Activate.ps1 script. - -.Example -Activate.ps1 -Verbose -Activates the Python virtual environment that contains the Activate.ps1 script, -and shows extra information about the activation as it executes. - -.Example -Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv -Activates the Python virtual environment located in the specified location. - -.Example -Activate.ps1 -Prompt "MyPython" -Activates the Python virtual environment that contains the Activate.ps1 script, -and prefixes the current prompt with the specified string (surrounded in -parentheses) while the virtual environment is active. - -.Notes -On Windows, it may be required to enable this Activate.ps1 script by setting the -execution policy for the user. You can do this by issuing the following PowerShell -command: - -PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser - -For more information on Execution Policies: -https://go.microsoft.com/fwlink/?LinkID=135170 - -#> -Param( - [Parameter(Mandatory = $false)] - [String] - $VenvDir, - [Parameter(Mandatory = $false)] - [String] - $Prompt -) - -<# Function declarations --------------------------------------------------- #> - -<# -.Synopsis -Remove all shell session elements added by the Activate script, including the -addition of the virtual environment's Python executable from the beginning of -the PATH variable. - -.Parameter NonDestructive -If present, do not remove this function from the global namespace for the -session. - -#> -function global:deactivate ([switch]$NonDestructive) { - # Revert to original values - - # The prior prompt: - if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { - Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt - Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT - } - - # The prior PYTHONHOME: - if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { - Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME - Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME - } - - # The prior PATH: - if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { - Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH - Remove-Item -Path Env:_OLD_VIRTUAL_PATH - } - - # Just remove the VIRTUAL_ENV altogether: - if (Test-Path -Path Env:VIRTUAL_ENV) { - Remove-Item -Path env:VIRTUAL_ENV - } - - # Just remove VIRTUAL_ENV_PROMPT altogether. - if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { - Remove-Item -Path env:VIRTUAL_ENV_PROMPT - } - - # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: - if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { - Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force - } - - # Leave deactivate function in the global namespace if requested: - if (-not $NonDestructive) { - Remove-Item -Path function:deactivate - } -} - -<# -.Description -Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the -given folder, and returns them in a map. - -For each line in the pyvenv.cfg file, if that line can be parsed into exactly -two strings separated by `=` (with any amount of whitespace surrounding the =) -then it is considered a `key = value` line. The left hand string is the key, -the right hand is the value. - -If the value starts with a `'` or a `"` then the first and last character is -stripped from the value before being captured. - -.Parameter ConfigDir -Path to the directory that contains the `pyvenv.cfg` file. -#> -function Get-PyVenvConfig( - [String] - $ConfigDir -) { - Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" - - # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). - $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue - - # An empty map will be returned if no config file is found. - $pyvenvConfig = @{ } - - if ($pyvenvConfigPath) { - - Write-Verbose "File exists, parse `key = value` lines" - $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath - - $pyvenvConfigContent | ForEach-Object { - $keyval = $PSItem -split "\s*=\s*", 2 - if ($keyval[0] -and $keyval[1]) { - $val = $keyval[1] - - # Remove extraneous quotations around a string value. - if ("'""".Contains($val.Substring(0, 1))) { - $val = $val.Substring(1, $val.Length - 2) - } - - $pyvenvConfig[$keyval[0]] = $val - Write-Verbose "Adding Key: '$($keyval[0])'='$val'" - } - } - } - return $pyvenvConfig -} - - -<# Begin Activate script --------------------------------------------------- #> - -# Determine the containing directory of this script -$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition -$VenvExecDir = Get-Item -Path $VenvExecPath - -Write-Verbose "Activation script is located in path: '$VenvExecPath'" -Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" -Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" - -# Set values required in priority: CmdLine, ConfigFile, Default -# First, get the location of the virtual environment, it might not be -# VenvExecDir if specified on the command line. -if ($VenvDir) { - Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" -} -else { - Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." - $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") - Write-Verbose "VenvDir=$VenvDir" -} - -# Next, read the `pyvenv.cfg` file to determine any required value such -# as `prompt`. -$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir - -# Next, set the prompt from the command line, or the config file, or -# just use the name of the virtual environment folder. -if ($Prompt) { - Write-Verbose "Prompt specified as argument, using '$Prompt'" -} -else { - Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" - if ($pyvenvCfg -and $pyvenvCfg['prompt']) { - Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" - $Prompt = $pyvenvCfg['prompt']; - } - else { - Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" - Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" - $Prompt = Split-Path -Path $venvDir -Leaf - } -} - -Write-Verbose "Prompt = '$Prompt'" -Write-Verbose "VenvDir='$VenvDir'" - -# Deactivate any currently active virtual environment, but leave the -# deactivate function in place. -deactivate -nondestructive - -# Now set the environment variable VIRTUAL_ENV, used by many tools to determine -# that there is an activated venv. -$env:VIRTUAL_ENV = $VenvDir - -if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { - - Write-Verbose "Setting prompt to '$Prompt'" - - # Set the prompt to include the env name - # Make sure _OLD_VIRTUAL_PROMPT is global - function global:_OLD_VIRTUAL_PROMPT { "" } - Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT - New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt - - function global:prompt { - Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " - _OLD_VIRTUAL_PROMPT - } - $env:VIRTUAL_ENV_PROMPT = $Prompt -} - -# Clear PYTHONHOME -if (Test-Path -Path Env:PYTHONHOME) { - Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME - Remove-Item -Path Env:PYTHONHOME -} - -# Add the venv to the PATH -Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH -$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/issue38366/bin/activate b/issue38366/bin/activate deleted file mode 100644 index ca7120d641..0000000000 --- a/issue38366/bin/activate +++ /dev/null @@ -1,69 +0,0 @@ -# This file must be used with "source bin/activate" *from bash* -# you cannot run it directly - -deactivate () { - # reset old environment variables - if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then - PATH="${_OLD_VIRTUAL_PATH:-}" - export PATH - unset _OLD_VIRTUAL_PATH - fi - if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then - PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" - export PYTHONHOME - unset _OLD_VIRTUAL_PYTHONHOME - fi - - # This should detect bash and zsh, which have a hash command that must - # be called to get it to forget past commands. Without forgetting - # past commands the $PATH changes we made may not be respected - if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then - hash -r 2> /dev/null - fi - - if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then - PS1="${_OLD_VIRTUAL_PS1:-}" - export PS1 - unset _OLD_VIRTUAL_PS1 - fi - - unset VIRTUAL_ENV - unset VIRTUAL_ENV_PROMPT - if [ ! "${1:-}" = "nondestructive" ] ; then - # Self destruct! - unset -f deactivate - fi -} - -# unset irrelevant variables -deactivate nondestructive - -VIRTUAL_ENV="/Users/linminyou/Projects/MONAI/issue38366" -export VIRTUAL_ENV - -_OLD_VIRTUAL_PATH="$PATH" -PATH="$VIRTUAL_ENV/bin:$PATH" -export PATH - -# unset PYTHONHOME if set -# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) -# could use `if (set -u; : $PYTHONHOME) ;` in bash -if [ -n "${PYTHONHOME:-}" ] ; then - _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" - unset PYTHONHOME -fi - -if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then - _OLD_VIRTUAL_PS1="${PS1:-}" - PS1="(issue38366) ${PS1:-}" - export PS1 - VIRTUAL_ENV_PROMPT="(issue38366) " - export VIRTUAL_ENV_PROMPT -fi - -# This should detect bash and zsh, which have a hash command that must -# be called to get it to forget past commands. Without forgetting -# past commands the $PATH changes we made may not be respected -if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then - hash -r 2> /dev/null -fi diff --git a/issue38366/bin/activate.csh b/issue38366/bin/activate.csh deleted file mode 100644 index 37b77be26d..0000000000 --- a/issue38366/bin/activate.csh +++ /dev/null @@ -1,26 +0,0 @@ -# This file must be used with "source bin/activate.csh" *from csh*. -# You cannot run it directly. -# Created by Davide Di Blasi . -# Ported to Python 3.3 venv by Andrew Svetlov - -alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate' - -# Unset irrelevant variables. -deactivate nondestructive - -setenv VIRTUAL_ENV "/Users/linminyou/Projects/MONAI/issue38366" - -set _OLD_VIRTUAL_PATH="$PATH" -setenv PATH "$VIRTUAL_ENV/bin:$PATH" - - -set _OLD_VIRTUAL_PROMPT="$prompt" - -if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then - set prompt = "(issue38366) $prompt" - setenv VIRTUAL_ENV_PROMPT "(issue38366) " -endif - -alias pydoc python -m pydoc - -rehash diff --git a/issue38366/bin/activate.fish b/issue38366/bin/activate.fish deleted file mode 100644 index cea8ecad26..0000000000 --- a/issue38366/bin/activate.fish +++ /dev/null @@ -1,69 +0,0 @@ -# This file must be used with "source /bin/activate.fish" *from fish* -# (https://fishshell.com/); you cannot run it directly. - -function deactivate -d "Exit virtual environment and return to normal shell environment" - # reset old environment variables - if test -n "$_OLD_VIRTUAL_PATH" - set -gx PATH $_OLD_VIRTUAL_PATH - set -e _OLD_VIRTUAL_PATH - end - if test -n "$_OLD_VIRTUAL_PYTHONHOME" - set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME - set -e _OLD_VIRTUAL_PYTHONHOME - end - - if test -n "$_OLD_FISH_PROMPT_OVERRIDE" - set -e _OLD_FISH_PROMPT_OVERRIDE - # prevents error when using nested fish instances (Issue #93858) - if functions -q _old_fish_prompt - functions -e fish_prompt - functions -c _old_fish_prompt fish_prompt - functions -e _old_fish_prompt - end - end - - set -e VIRTUAL_ENV - set -e VIRTUAL_ENV_PROMPT - if test "$argv[1]" != "nondestructive" - # Self-destruct! - functions -e deactivate - end -end - -# Unset irrelevant variables. -deactivate nondestructive - -set -gx VIRTUAL_ENV "/Users/linminyou/Projects/MONAI/issue38366" - -set -gx _OLD_VIRTUAL_PATH $PATH -set -gx PATH "$VIRTUAL_ENV/bin" $PATH - -# Unset PYTHONHOME if set. -if set -q PYTHONHOME - set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME - set -e PYTHONHOME -end - -if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" - # fish uses a function instead of an env var to generate the prompt. - - # Save the current fish_prompt function as the function _old_fish_prompt. - functions -c fish_prompt _old_fish_prompt - - # With the original prompt function renamed, we can override with our own. - function fish_prompt - # Save the return status of the last command. - set -l old_status $status - - # Output the venv prompt; color taken from the blue of the Python logo. - printf "%s%s%s" (set_color 4B8BBE) "(issue38366) " (set_color normal) - - # Restore the return status of the previous command. - echo "exit $old_status" | . - # Output the original/"old" prompt. - _old_fish_prompt - end - - set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" - set -gx VIRTUAL_ENV_PROMPT "(issue38366) " -end diff --git a/issue38366/bin/pip b/issue38366/bin/pip deleted file mode 100755 index dbf05001bc..0000000000 --- a/issue38366/bin/pip +++ /dev/null @@ -1,8 +0,0 @@ -#!/Users/linminyou/Projects/MONAI/issue38366/bin/python3.11 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/issue38366/bin/pip3 b/issue38366/bin/pip3 deleted file mode 100755 index dbf05001bc..0000000000 --- a/issue38366/bin/pip3 +++ /dev/null @@ -1,8 +0,0 @@ -#!/Users/linminyou/Projects/MONAI/issue38366/bin/python3.11 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/issue38366/bin/pip3.11 b/issue38366/bin/pip3.11 deleted file mode 100755 index dbf05001bc..0000000000 --- a/issue38366/bin/pip3.11 +++ /dev/null @@ -1,8 +0,0 @@ -#!/Users/linminyou/Projects/MONAI/issue38366/bin/python3.11 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/issue38366/bin/python b/issue38366/bin/python deleted file mode 120000 index 6e7f3c7dd2..0000000000 --- a/issue38366/bin/python +++ /dev/null @@ -1 +0,0 @@ -python3.11 \ No newline at end of file diff --git a/issue38366/bin/python3 b/issue38366/bin/python3 deleted file mode 120000 index 6e7f3c7dd2..0000000000 --- a/issue38366/bin/python3 +++ /dev/null @@ -1 +0,0 @@ -python3.11 \ No newline at end of file diff --git a/issue38366/bin/python3.11 b/issue38366/bin/python3.11 deleted file mode 120000 index e8dc9bc4c9..0000000000 --- a/issue38366/bin/python3.11 +++ /dev/null @@ -1 +0,0 @@ -/Users/linminyou/anaconda3/bin/python3.11 \ No newline at end of file diff --git a/issue38366/pyvenv.cfg b/issue38366/pyvenv.cfg deleted file mode 100644 index 6d0bd1b4b1..0000000000 --- a/issue38366/pyvenv.cfg +++ /dev/null @@ -1,5 +0,0 @@ -home = /Users/linminyou/anaconda3/bin -include-system-site-packages = false -version = 3.11.4 -executable = /Users/linminyou/anaconda3/bin/python3.11 -command = /Users/linminyou/venvs/monai-dev/bin/python3 -m venv /Users/linminyou/Projects/MONAI/issue38366 From 99f7e303ca428d69947ea287b53e563891676e65 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 9 Aug 2025 06:40:03 +0000 Subject: [PATCH 03/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/metrics/meandice.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index f9e29cea88..fa71b8850f 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -124,7 +124,7 @@ def __init__( ignore_empty=self.ignore_empty, num_classes=self.num_classes, ) - + def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor: # type: ignore[override] """ @@ -299,7 +299,7 @@ def compute_channel(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor if denorm <= 0: return torch.tensor(1.0, device=y_o.device) return torch.tensor(0.0, device=y_o.device) - + def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: """ Compute the metric for the given prediction and ground truth. From 95c58a097b91d2728cab4f0c98ab95df37bba291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 9 Aug 2025 14:52:32 +0800 Subject: [PATCH 04/17] Fix: robust channel-last detection for y using num_classes (refs #8366) --- monai/metrics/meandice.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index fa71b8850f..f7446a9a4c 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -310,8 +310,11 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl y: ground truth with shape (batch_size, num_classes or 1, spatial_dims...). """ y_pred, _ = ensure_channel_first(y_pred) - if y.ndim == y_pred.ndim and (y.shape[-1] == y_pred.shape[1] or y.shape[-1] == 1): + + n_ch = self.num_classes or y_pred.shape[1] + if y.ndim == y_pred.ndim and y.shape[-1] in (1, n_ch): y, _ = ensure_channel_first(y) + _apply_argmax, _threshold = self.apply_argmax, self.threshold if self.num_classes is None: From ca32652cce6054402d284a0c18b54773c8a71f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 9 Aug 2025 14:59:09 +0800 Subject: [PATCH 05/17] Fix: robust channel-last detection for y using num_classes (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/inferers/utils.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index 766486a807..bbb387c3b1 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -38,7 +38,39 @@ __all__ = ["sliding_window_inference"] +def ensure_channel_first(x: torch.Tensor, spatial_ndim: Optional[int] = None) -> Tuple[torch.Tensor, int]: + """ + 將張量標準化為 channel-first(N,C,spatial...)。 + 回傳 (可能已轉換的張量, 原本 channel 維度:1 表示本來就在 dim=1;-1 表示本來在最後一維)。 + 支援常見情況: + - [N, C, *spatial] -> 原樣返回 + - [N, *spatial, C] -> 移動最後一維到 dim=1 + 其他模糊情況則丟出 ValueError,避免悄悄算錯。 + """ + if not isinstance(x, torch.Tensor): + raise TypeError(f"expect torch.Tensor, got {type(x)}") + if x.ndim < 3: + raise ValueError(f"expect >=3 dims (N,C,spatial...), got shape={tuple(x.shape)}") + + # 若未指定,估個常見的 2D/3D 空間維度數,僅用於錯誤訊息與判斷參考 + if spatial_ndim is None: + spatial_ndim = max(2, min(3, x.ndim - 2)) + + # 簡單啟發式:C 通常不會太大(<=512) + c_first_ok = x.shape[1] <= 512 + c_last_ok = x.shape[-1] <= 512 + + # 優先保留 channel-first + if c_first_ok and x.ndim >= 2 + spatial_ndim: + return x, 1 + if c_last_ok: + return x.movedim(-1, 1), -1 + + raise ValueError( + f"cannot infer channel dim for shape={tuple(x.shape)}; " + f"expected [N,C,spatial...] or [N,spatial...,C] (spatial_ndim≈{spatial_ndim})" + ) def sliding_window_inference( inputs: torch.Tensor | MetaTensor, roi_size: Sequence[int] | int, From 7bb99263b8db4b52187e25c8d018685f0e4fa2be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 9 Aug 2025 07:07:20 +0000 Subject: [PATCH 06/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/metrics/meandice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index f7446a9a4c..08adad9ed4 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -314,7 +314,7 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl n_ch = self.num_classes or y_pred.shape[1] if y.ndim == y_pred.ndim and y.shape[-1] in (1, n_ch): y, _ = ensure_channel_first(y) - + _apply_argmax, _threshold = self.apply_argmax, self.threshold if self.num_classes is None: From 488e1040b4c2429d64007e424bb0da15b6e00175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 16 Aug 2025 16:03:33 +0800 Subject: [PATCH 07/17] Fix: correct import, English docstring, safer channel heuristic; robust y channel-last detection (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/inferers/utils.py | 54 ++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index bbb387c3b1..afb0e80779 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -40,37 +40,47 @@ def ensure_channel_first(x: torch.Tensor, spatial_ndim: Optional[int] = None) -> Tuple[torch.Tensor, int]: """ - 將張量標準化為 channel-first(N,C,spatial...)。 - 回傳 (可能已轉換的張量, 原本 channel 維度:1 表示本來就在 dim=1;-1 表示本來在最後一維)。 + Normalize a tensor to channel-first layout (N, C, spatial...). - 支援常見情況: - - [N, C, *spatial] -> 原樣返回 - - [N, *spatial, C] -> 移動最後一維到 dim=1 - 其他模糊情況則丟出 ValueError,避免悄悄算錯。 - """ - if not isinstance(x, torch.Tensor): - raise TypeError(f"expect torch.Tensor, got {type(x)}") - if x.ndim < 3: - raise ValueError(f"expect >=3 dims (N,C,spatial...), got shape={tuple(x.shape)}") + Args: + x: Tensor with shape (N, C, spatial...) or (N, spatial..., C). + spatial_ndim: Number of spatial dimensions. If None, inferred as x.ndim - 2. + + Returns: + A tuple (x_cf, orig_channel_dim): + - x_cf: the tensor in channel-first layout. + - orig_channel_dim: 1 if input was already channel-first; -1 if the channel was last. + + Raises: + TypeError: if x is not a torch.Tensor. + ValueError: if x.ndim < 3 or the channel dimension cannot be inferred unambiguously. - # 若未指定,估個常見的 2D/3D 空間維度數,僅用於錯誤訊息與判斷參考 + Notes: + Uses a small-channel heuristic (<=32) typical for segmentation/classification. When ambiguous, + prefers preserving the input layout or raises ValueError to avoid silent errors. + """ + + if spatial_ndim is None: - spatial_ndim = max(2, min(3, x.ndim - 2)) + spatial_ndim = x.ndim - 2 - # 簡單啟發式:C 通常不會太大(<=512) - c_first_ok = x.shape[1] <= 512 - c_last_ok = x.shape[-1] <= 512 + threshold = 32 + s1, sl = int(x.shape[1]), int(x.shape[-1]) - # 優先保留 channel-first - if c_first_ok and x.ndim >= 2 + spatial_ndim: + if s1 <= threshold and sl > threshold: return x, 1 - if c_last_ok: + if sl <= threshold and s1 > threshold: return x.movedim(-1, 1), -1 + if s1 <= threshold and sl <= threshold: + return x, 1 + raise ValueError( - f"cannot infer channel dim for shape={tuple(x.shape)}; " - f"expected [N,C,spatial...] or [N,spatial...,C] (spatial_ndim≈{spatial_ndim})" - ) + f"cannot infer channel dim for shape={tuple(x.shape)}; expected [N,C,spatial...] or [N,spatial...,C]; " + f"both dim1={s1} and dim-1={sl} look like spatial dims" + ) + + def sliding_window_inference( inputs: torch.Tensor | MetaTensor, roi_size: Sequence[int] | int, From 2f9254fb862531cca208b1a6e9c5e4c108fe873b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 16 Aug 2025 16:05:09 +0800 Subject: [PATCH 08/17] Fix: correct import, English docstring, safer channel heuristic; robust y channel-last detection (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/metrics/meandice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index 08adad9ed4..5d33d9bd01 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -15,7 +15,7 @@ from monai.metrics.utils import do_metric_reduction from monai.utils import MetricReduction, deprecated_arg -from monai.metrics.utils import ensure_channel_first +from monai.inferers.utils import ensure_channel_first from .metric import CumulativeIterationMetric From 69b96e25730ff1445905595cd98d60a151568921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sat, 16 Aug 2025 16:10:29 +0800 Subject: [PATCH 09/17] Refactor: English docstring and safer channel heuristic in ensure_channel_first (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/inferers/utils.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index afb0e80779..ad9dee1e93 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -59,27 +59,37 @@ def ensure_channel_first(x: torch.Tensor, spatial_ndim: Optional[int] = None) -> Uses a small-channel heuristic (<=32) typical for segmentation/classification. When ambiguous, prefers preserving the input layout or raises ValueError to avoid silent errors. """ - - + if not isinstance(x, torch.Tensor): + raise TypeError(f"Expected torch.Tensor, got {type(x)}") + if x.ndim < 3: + raise ValueError(f"Expected >=3 dims (N,C,spatial...), got shape={tuple(x.shape)}") + + # Infer spatial dims if not provided (handles 1D/2D/3D uniformly). if spatial_ndim is None: - spatial_ndim = x.ndim - 2 + spatial_ndim = x.ndim - 2 # not directly used for logic; informative only - threshold = 32 - s1, sl = int(x.shape[1]), int(x.shape[-1]) + # Heuristic: channels are usually small (e.g., <=32) in segmentation/classification. + threshold = 32 + s1 = int(x.shape[1]) # candidate channel at dim=1 (N, C, ...) + sl = int(x.shape[-1]) # candidate channel at last dim (..., C) + # Unambiguous cases first. if s1 <= threshold and sl > threshold: + # Looks like NCHW/D already. return x, 1 if sl <= threshold and s1 > threshold: + # Looks like NHWC/D: move last dim to channel dim. return x.movedim(-1, 1), -1 + # Ambiguous: both sides small (or both large). Prefer preserving to avoid silent mis-reordering. if s1 <= threshold and sl <= threshold: return x, 1 raise ValueError( f"cannot infer channel dim for shape={tuple(x.shape)}; expected [N,C,spatial...] or [N,spatial...,C]; " f"both dim1={s1} and dim-1={sl} look like spatial dims" - ) - + ) + def sliding_window_inference( inputs: torch.Tensor | MetaTensor, From 10809873804f3edab2ef39a1abb9986f4922d9c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 16 Aug 2025 08:14:06 +0000 Subject: [PATCH 10/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/inferers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index ad9dee1e93..038d50de55 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -90,7 +90,7 @@ def ensure_channel_first(x: torch.Tensor, spatial_ndim: Optional[int] = None) -> f"both dim1={s1} and dim-1={sl} look like spatial dims" ) - + def sliding_window_inference( inputs: torch.Tensor | MetaTensor, roi_size: Sequence[int] | int, From a360570e10caccf1c1ed59171df468f4c7900618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sun, 24 Aug 2025 16:08:54 +0800 Subject: [PATCH 11/17] Fix: update ensure_channel_first and DiceHelper channel-last handling (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/inferers/utils.py | 49 +++++++++++++++++++++++++-------------- monai/metrics/meandice.py | 11 ++++++--- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index 038d50de55..e3a8c8eff2 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -38,13 +38,24 @@ __all__ = ["sliding_window_inference"] -def ensure_channel_first(x: torch.Tensor, spatial_ndim: Optional[int] = None) -> Tuple[torch.Tensor, int]: +dfrom typing import Optional, Tuple +import torch + +def ensure_channel_first( + x: torch.Tensor, + spatial_ndim: Optional[int] = None, + channel_hint: Optional[int] = None, + threshold: int = 32, +) -> Tuple[torch.Tensor, int]: """ Normalize a tensor to channel-first layout (N, C, spatial...). Args: x: Tensor with shape (N, C, spatial...) or (N, spatial..., C). spatial_ndim: Number of spatial dimensions. If None, inferred as x.ndim - 2. + channel_hint: If provided, the expected channel size (e.g., num_classes). When present, + we prioritize matching this size at either dim=1 (channel-first) or dim=-1 (channel-last). + threshold: Heuristic upper bound for typical channel counts to disambiguate layouts. Returns: A tuple (x_cf, orig_channel_dim): @@ -56,41 +67,45 @@ def ensure_channel_first(x: torch.Tensor, spatial_ndim: Optional[int] = None) -> ValueError: if x.ndim < 3 or the channel dimension cannot be inferred unambiguously. Notes: - Uses a small-channel heuristic (<=32) typical for segmentation/classification. When ambiguous, - prefers preserving the input layout or raises ValueError to avoid silent errors. + 1. When channel_hint is provided, it is used as a strong signal to decide layout. + 2. Otherwise, uses a heuristic where channels are usually small (<= threshold). + 3. In ambiguous cases (both candidate dims small or both large), the input layout + is preserved (assumed channel-first) to avoid silent mis-reordering. """ if not isinstance(x, torch.Tensor): raise TypeError(f"Expected torch.Tensor, got {type(x)}") if x.ndim < 3: raise ValueError(f"Expected >=3 dims (N,C,spatial...), got shape={tuple(x.shape)}") - # Infer spatial dims if not provided (handles 1D/2D/3D uniformly). if spatial_ndim is None: - spatial_ndim = x.ndim - 2 # not directly used for logic; informative only + spatial_ndim = x.ndim - 2 # informative only - # Heuristic: channels are usually small (e.g., <=32) in segmentation/classification. - threshold = 32 - s1 = int(x.shape[1]) # candidate channel at dim=1 (N, C, ...) - sl = int(x.shape[-1]) # candidate channel at last dim (..., C) + s1 = int(x.shape[1]) # candidate channel at dim=1 + sl = int(x.shape[-1]) # candidate channel at dim=-1 - # Unambiguous cases first. + # 1) Strong signal: use channel_hint if provided + if channel_hint is not None: + if s1 == channel_hint and sl != channel_hint: + return x, 1 + if sl == channel_hint and s1 != channel_hint: + return x.movedim(-1, 1), -1 + # if both match or both mismatch, fall back to heuristic + + # 2) Heuristic: channels are usually small if s1 <= threshold and sl > threshold: - # Looks like NCHW/D already. return x, 1 if sl <= threshold and s1 > threshold: - # Looks like NHWC/D: move last dim to channel dim. return x.movedim(-1, 1), -1 - # Ambiguous: both sides small (or both large). Prefer preserving to avoid silent mis-reordering. - if s1 <= threshold and sl <= threshold: + # 3) Ambiguous: both sides small OR both sides large → preserve as channel-first + if (s1 <= threshold and sl <= threshold) or (s1 > threshold and sl > threshold): return x, 1 + # 4) Should not reach here under normal cases raise ValueError( - f"cannot infer channel dim for shape={tuple(x.shape)}; expected [N,C,spatial...] or [N,spatial...,C]; " - f"both dim1={s1} and dim-1={sl} look like spatial dims" + f"cannot infer channel dim for shape={tuple(x.shape)}; expected [N,C,spatial...] or [N,spatial...,C]" ) - def sliding_window_inference( inputs: torch.Tensor | MetaTensor, roi_size: Sequence[int] | int, diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index 5d33d9bd01..113504f325 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -309,11 +309,16 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl the number of channels is inferred from ``y_pred.shape[1]`` when ``num_classes is None``. y: ground truth with shape (batch_size, num_classes or 1, spatial_dims...). """ - y_pred, _ = ensure_channel_first(y_pred) - + # --- Normalize layout to channel-first (N, C, spatial...) --- n_ch = self.num_classes or y_pred.shape[1] + + # Always normalize y_pred with hint + y_pred, _ = ensure_channel_first(y_pred, channel_hint=n_ch) + + # Normalize y if it looks like channel-last (last dim = 1 or n_ch) if y.ndim == y_pred.ndim and y.shape[-1] in (1, n_ch): - y, _ = ensure_channel_first(y) + y, _ = ensure_channel_first(y, channel_hint=n_ch) + _apply_argmax, _threshold = self.apply_argmax, self.threshold From 51eeb6545cb3450fafaf81c92ccbeb70d11723ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sun, 24 Aug 2025 16:14:01 +0800 Subject: [PATCH 12/17] chore: ignore local venv directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 99cc678859..cda9ebc15a 100644 --- a/.gitignore +++ b/.gitignore @@ -166,3 +166,5 @@ runs *zarr/* issue38366/ + +issue8366/ From 6d52c3c7c538294e5b056a9abf69337739d82ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Sun, 24 Aug 2025 19:24:13 +0800 Subject: [PATCH 13/17] Fix: cleanup utils import/export and robust channel-last handling in DiceHelper (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/inferers/utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index e3a8c8eff2..9592bf2006 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -38,9 +38,6 @@ __all__ = ["sliding_window_inference"] -dfrom typing import Optional, Tuple -import torch - def ensure_channel_first( x: torch.Tensor, spatial_ndim: Optional[int] = None, @@ -105,6 +102,8 @@ def ensure_channel_first( raise ValueError( f"cannot infer channel dim for shape={tuple(x.shape)}; expected [N,C,spatial...] or [N,spatial...,C]" ) + + def sliding_window_inference( inputs: torch.Tensor | MetaTensor, From 12a34b774ffe54bcd8e76f78d855456f5c20be79 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Aug 2025 11:25:53 +0000 Subject: [PATCH 14/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/inferers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index 9592bf2006..2c7c453448 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -102,7 +102,7 @@ def ensure_channel_first( raise ValueError( f"cannot infer channel dim for shape={tuple(x.shape)}; expected [N,C,spatial...] or [N,spatial...,C]" ) - + def sliding_window_inference( From adcbfdb97e76e057c6094227b7352da59148cf9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Mon, 25 Aug 2025 00:15:31 +0800 Subject: [PATCH 15/17] WIP: save local changes before rebase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/inferers/utils.py | 4 ++-- monai/metrics/meandice.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index 2c7c453448..7929817bf9 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -36,14 +36,14 @@ tqdm, _ = optional_import("tqdm", name="tqdm") _nearest_mode = "nearest-exact" -__all__ = ["sliding_window_inference"] +__all__ = ["ensure_channel_first","sliding_window_inference"] def ensure_channel_first( x: torch.Tensor, spatial_ndim: Optional[int] = None, channel_hint: Optional[int] = None, threshold: int = 32, -) -> Tuple[torch.Tensor, int]: +) -> tuple[torch.Tensor, int]: """ Normalize a tensor to channel-first layout (N, C, spatial...). diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index 113504f325..ab52a1506e 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -309,18 +309,22 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl the number of channels is inferred from ``y_pred.shape[1]`` when ``num_classes is None``. y: ground truth with shape (batch_size, num_classes or 1, spatial_dims...). """ + + # --- Normalize layout to channel-first (N, C, spatial...) --- - n_ch = self.num_classes or y_pred.shape[1] + # Prefer a strong signal when available. + if self.num_classes is not None: + y_pred, _ = ensure_channel_first(y_pred, channel_hint=self.num_classes) + else: + y_pred, _ = ensure_channel_first(y_pred) - # Always normalize y_pred with hint - y_pred, _ = ensure_channel_first(y_pred, channel_hint=n_ch) + # Infer channels after normalization (or use provided). + n_ch = self.num_classes or y_pred.shape[1] - # Normalize y if it looks like channel-last (last dim = 1 or n_ch) + # Normalize y if it plausibly is channel-last. if y.ndim == y_pred.ndim and y.shape[-1] in (1, n_ch): y, _ = ensure_channel_first(y, channel_hint=n_ch) - - _apply_argmax, _threshold = self.apply_argmax, self.threshold if self.num_classes is None: n_pred_ch = y_pred.shape[1] # y_pred is in one-hot format or multi-channel scores From 0fea0702f4ffa9f34112f7a4311752994b26c082 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Aug 2025 16:16:08 +0000 Subject: [PATCH 16/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/metrics/meandice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index ab52a1506e..a420dbab45 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -309,7 +309,7 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl the number of channels is inferred from ``y_pred.shape[1]`` when ``num_classes is None``. y: ground truth with shape (batch_size, num_classes or 1, spatial_dims...). """ - + # --- Normalize layout to channel-first (N, C, spatial...) --- # Prefer a strong signal when available. From c52922cc432ba05cbe04f26e9d6b8a556ec9069e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=97=BB=E4=BD=91?= Date: Mon, 25 Aug 2025 14:51:42 +0800 Subject: [PATCH 17/17] Fix: add y-informed fallback when num_classes is None to robustly normalize NHWC with large C (refs #8366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 林旻佑 --- monai/metrics/meandice.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py index a420dbab45..24426c16c7 100644 --- a/monai/metrics/meandice.py +++ b/monai/metrics/meandice.py @@ -314,9 +314,15 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor) -> torch.Tensor | tupl # --- Normalize layout to channel-first (N, C, spatial...) --- # Prefer a strong signal when available. if self.num_classes is not None: - y_pred, _ = ensure_channel_first(y_pred, channel_hint=self.num_classes) + y_pred, _ = ensure_channel_first(y_pred, channel_hint=self.num_classes) else: + # First pass: heuristic only. y_pred, _ = ensure_channel_first(y_pred) + # Fallback: if implausible vs y's layout, retry with a hint from y's last dim. + if y.ndim == y_pred.ndim: + plausible = {1, y.shape[1], y.shape[-1]} + if y_pred.shape[1] not in plausible: + y_pred, _ = ensure_channel_first(y_pred, channel_hint=int(y.shape[-1])) # Infer channels after normalization (or use provided). n_ch = self.num_classes or y_pred.shape[1]