Repository: Project-MONAI/MONAI
File: monai/networks/nets/resnet.py (Python)

Code before:
                logger.info(f"{filename} not available for resnet{resnet_depth}")
                filename = f"{medicalnet_huggingface_files_basename}{resnet_depth}.pth"
                logger.info(f"Trying with {filename}")
                pretrained_path = hf_hub_download(
                    repo_id=f"{medicalnet_huggingface_repo_basename}{resnet_depth}", filename=filename
                )
            else:
                raise EntryNotFoundError(
                    f"{filename} not found on {medicalnet_huggingface_repo_basename}{resnet_depth}"
                ) from None
        checkpoint = torch.load(pretrained_path, map_location=torch.device(device))
    else:
        raise NotImplementedError("Supported resnet_depth are: [10, 18, 34, 50, 101, 152, 200]")
    logger.info(f"{filename} downloaded")
    return checkpoint.get("state_dict")


def get_medicalnet_pretrained_resnet_args(resnet_depth: int):
    """
    Return correct shortcut_type and bias_downsample
    for pretrained MedicalNet weights according to resnet depth.
    """
    # After testing
    # False: 10, 50, 101, 152, 200
    # Any: 18, 34
    bias_downsample = True if resnet_depth in [18, 34] else False  # 18, 10, 34
    shortcut_type = "A" if resnet_depth in [18, 34] else "B"
    return bias_downsample, shortcut_type


def _load_state_dict(model: nn.Module, model_name: str, datasets23: bool = True) -> None:
    search_res = re.search(r"resnet(\d+)", model_name)
    if search_res:
        resnet_depth = int(search_res.group(1))
        datasets23 = model_name.endswith("_23datasets")
    else:
        raise ValueError("model_name argument should contain resnet depth. Example: resnet18 or resnet18_23datasets.")

    model_state_dict = get_pretrained_resnet_medicalnet(resnet_depth, device="cpu", datasets23=datasets23)
    model_state_dict = {key.replace("module.", ""): value for key, value in model_state_dict.items()}
    model.load_state_dict(model_state_dict)


Review comment (suggestion):
```suggestion
    bias_downsample = resnet_depth in (18, 34)
```

Slightly simpler, I don't think the comment was accurate anyway given the values being checked.

Code after:
                logger.info(f"{filename} not available for resnet{resnet_depth}")
                filename = f"{medicalnet_huggingface_files_basename}{resnet_depth}.pth"
                logger.info(f"Trying with {filename}")
                pretrained_path = hf_hub_download(
                    repo_id=f"{medicalnet_huggingface_repo_basename}{resnet_depth}", filename=filename
                )
            else:
                raise EntryNotFoundError(
                    f"{filename} not found on {medicalnet_huggingface_repo_basename}{resnet_depth}"
                ) from None
        checkpoint = torch.load(pretrained_path, map_location=torch.device(device))
    else:
        raise NotImplementedError("Supported resnet_depth are: [10, 18, 34, 50, 101, 152, 200]")
    logger.info(f"{filename} downloaded")
    return checkpoint.get("state_dict")


def get_medicalnet_pretrained_resnet_args(resnet_depth: int):
    """
    Return correct shortcut_type and bias_downsample
    for pretrained MedicalNet weights according to resnet depth.
    """
    # After testing
    # False: 10, 50, 101, 152, 200
    # Any: 18, 34
    bias_downsample = resnet_depth in (18, 34)
    shortcut_type = "A" if resnet_depth in [18, 34] else "B"
    return bias_downsample, shortcut_type


def _load_state_dict(model: nn.Module, model_name: str, datasets23: bool = True) -> None:
    search_res = re.search(r"resnet(\d+)", model_name)
    if search_res:
        resnet_depth = int(search_res.group(1))
        datasets23 = model_name.endswith("_23datasets")
    else:
        raise ValueError("model_name argument should contain resnet depth. Example: resnet18 or resnet18_23datasets.")

    model_state_dict = get_pretrained_resnet_medicalnet(resnet_depth, device="cpu", datasets23=datasets23)
    model_state_dict = {key.replace("module.", ""): value for key, value in model_state_dict.items()}
    model.load_state_dict(model_state_dict)