File size: 18,015 Bytes
0ca2a11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Optional, Sequence, Tuple, Union
import torch
from torch import nn
from . import convnext
from monai.networks.blocks import UpSample
from monai.networks.layers.factories import Conv
from monai.networks.layers.utils import get_act_layer
from monai.networks.nets import EfficientNetBNFeatures
from monai.networks.nets.basic_unet import UpCat
from monai.utils import InterpolateMode
__all__ = ["FlexibleUNet"]
encoder_feature_channel = {
"efficientnet-b0": (16, 24, 40, 112, 320),
"efficientnet-b1": (16, 24, 40, 112, 320),
"efficientnet-b2": (16, 24, 48, 120, 352),
"efficientnet-b3": (24, 32, 48, 136, 384),
"efficientnet-b4": (24, 32, 56, 160, 448),
"efficientnet-b5": (24, 40, 64, 176, 512),
"efficientnet-b6": (32, 40, 72, 200, 576),
"efficientnet-b7": (32, 48, 80, 224, 640),
"efficientnet-b8": (32, 56, 88, 248, 704),
"efficientnet-l2": (72, 104, 176, 480, 1376),
"convnext_small": (96, 192, 384, 768),
"convnext_base": (128, 256, 512, 1024),
"van_b2": (64, 128, 320, 512),
"van_b1": (64, 128, 320, 512),
}
def _get_encoder_channels_by_backbone(backbone: str, in_channels: int = 3) -> tuple:
"""
Get the encoder output channels by given backbone name.
Args:
backbone: name of backbone to generate features, can be from [efficientnet-b0, ..., efficientnet-b7].
in_channels: channel of input tensor, default to 3.
Returns:
A tuple of output feature map channels' length .
"""
encoder_channel_tuple = encoder_feature_channel[backbone]
encoder_channel_list = [in_channels] + list(encoder_channel_tuple)
encoder_channel = tuple(encoder_channel_list)
return encoder_channel
class UNetDecoder(nn.Module):
"""
UNet Decoder.
This class refers to `segmentation_models.pytorch
<https://github.com/qubvel/segmentation_models.pytorch>`_.
Args:
spatial_dims: number of spatial dimensions.
encoder_channels: number of output channels for all feature maps in encoder.
`len(encoder_channels)` should be no less than 2.
decoder_channels: number of output channels for all feature maps in decoder.
`len(decoder_channels)` should equal to `len(encoder_channels) - 1`.
act: activation type and arguments.
norm: feature normalization type and arguments.
dropout: dropout ratio.
bias: whether to have a bias term in convolution blocks in this decoder.
upsample: upsampling mode, available options are
``"deconv"``, ``"pixelshuffle"``, ``"nontrainable"``.
pre_conv: a conv block applied before upsampling.
Only used in the "nontrainable" or "pixelshuffle" mode.
interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
Only used in the "nontrainable" mode.
align_corners: set the align_corners parameter for upsample. Defaults to True.
Only used in the "nontrainable" mode.
is_pad: whether to pad upsampling features to fit the encoder spatial dims.
"""
def __init__(
self,
spatial_dims: int,
encoder_channels: Sequence[int],
decoder_channels: Sequence[int],
act: Union[str, tuple],
norm: Union[str, tuple],
dropout: Union[float, tuple],
bias: bool,
upsample: str,
pre_conv: Optional[str],
interp_mode: str,
align_corners: Optional[bool],
is_pad: bool,
):
super().__init__()
if len(encoder_channels) < 2:
raise ValueError("the length of `encoder_channels` should be no less than 2.")
if len(decoder_channels) != len(encoder_channels) - 1:
raise ValueError("`len(decoder_channels)` should equal to `len(encoder_channels) - 1`.")
in_channels = [encoder_channels[-1]] + list(decoder_channels[:-1])
skip_channels = list(encoder_channels[1:-1][::-1]) + [0]
halves = [True] * (len(skip_channels) - 1)
halves.append(False)
blocks = []
for in_chn, skip_chn, out_chn, halve in zip(in_channels, skip_channels, decoder_channels, halves):
blocks.append(
UpCat(
spatial_dims=spatial_dims,
in_chns=in_chn,
cat_chns=skip_chn,
out_chns=out_chn,
act=act,
norm=norm,
dropout=dropout,
bias=bias,
upsample=upsample,
pre_conv=pre_conv,
interp_mode=interp_mode,
align_corners=align_corners,
halves=halve,
is_pad=is_pad,
)
)
self.blocks = nn.ModuleList(blocks)
def forward(self, features: List[torch.Tensor], skip_connect: int = 3):
skips = features[:-1][::-1]
features = features[1:][::-1]
x = features[0]
for i, block in enumerate(self.blocks):
if i < skip_connect:
skip = skips[i]
else:
skip = None
x = block(x, skip)
return x
class SegmentationHead(nn.Sequential):
"""
Segmentation head.
This class refers to `segmentation_models.pytorch
<https://github.com/qubvel/segmentation_models.pytorch>`_.
Args:
spatial_dims: number of spatial dimensions.
in_channels: number of input channels for the block.
out_channels: number of output channels for the block.
kernel_size: kernel size for the conv layer.
act: activation type and arguments.
scale_factor: multiplier for spatial size. Has to match input size if it is a tuple.
"""
def __init__(
self,
spatial_dims: int,
in_channels: int,
out_channels: int,
kernel_size: int = 3,
act: Optional[Union[Tuple, str]] = None,
scale_factor: float = 1.0,
):
conv_layer = Conv[Conv.CONV, spatial_dims](
in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, padding=kernel_size // 2
)
up_layer: nn.Module = nn.Identity()
# if scale_factor > 1.0:
# up_layer = UpSample(
# in_channels=out_channels,
# spatial_dims=spatial_dims,
# scale_factor=scale_factor,
# mode="deconv",
# pre_conv=None,
# interp_mode=InterpolateMode.LINEAR,
# )
if scale_factor > 1.0:
up_layer = UpSample(
spatial_dims=spatial_dims,
scale_factor=scale_factor,
mode="nontrainable",
pre_conv=None,
interp_mode=InterpolateMode.LINEAR,
)
if act is not None:
act_layer = get_act_layer(act)
else:
act_layer = nn.Identity()
super().__init__(conv_layer, up_layer, act_layer)
class FlexibleUNetConvext(nn.Module):
"""
A flexible implementation of UNet-like encoder-decoder architecture.
"""
def __init__(
self,
in_channels: int,
out_channels: int,
backbone: str,
pretrained: bool = False,
decoder_channels: Tuple = (1024, 512, 256, 128),
spatial_dims: int = 2,
norm: Union[str, tuple] = ("batch", {"eps": 1e-3, "momentum": 0.1}),
act: Union[str, tuple] = ("relu", {"inplace": True}),
dropout: Union[float, tuple] = 0.0,
decoder_bias: bool = False,
upsample: str = "nontrainable",
interp_mode: str = "nearest",
is_pad: bool = True,
) -> None:
"""
A flexible implement of UNet, in which the backbone/encoder can be replaced with
any efficient network. Currently the input must have a 2 or 3 spatial dimension
and the spatial size of each dimension must be a multiple of 32 if is pad parameter
is False
Args:
in_channels: number of input channels.
out_channels: number of output channels.
backbone: name of backbones to initialize, only support efficientnet right now,
can be from [efficientnet-b0,..., efficientnet-b8, efficientnet-l2].
pretrained: whether to initialize pretrained ImageNet weights, only available
for spatial_dims=2 and batch norm is used, default to False.
decoder_channels: number of output channels for all feature maps in decoder.
`len(decoder_channels)` should equal to `len(encoder_channels) - 1`,default
to (256, 128, 64, 32, 16).
spatial_dims: number of spatial dimensions, default to 2.
norm: normalization type and arguments, default to ("batch", {"eps": 1e-3,
"momentum": 0.1}).
act: activation type and arguments, default to ("relu", {"inplace": True}).
dropout: dropout ratio, default to 0.0.
decoder_bias: whether to have a bias term in decoder's convolution blocks.
upsample: upsampling mode, available options are``"deconv"``, ``"pixelshuffle"``,
``"nontrainable"``.
interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
Only used in the "nontrainable" mode.
is_pad: whether to pad upsampling features to fit features from encoder. Default to True.
If this parameter is set to "True", the spatial dim of network input can be arbitary
size, which is not supported by TensorRT. Otherwise, it must be a multiple of 32.
"""
super().__init__()
if backbone not in encoder_feature_channel:
raise ValueError(f"invalid model_name {backbone} found, must be one of {encoder_feature_channel.keys()}.")
if spatial_dims not in (2, 3):
raise ValueError("spatial_dims can only be 2 or 3.")
adv_prop = "ap" in backbone
self.backbone = backbone
self.spatial_dims = spatial_dims
model_name = backbone
encoder_channels = _get_encoder_channels_by_backbone(backbone, in_channels)
self.encoder = convnext.convnext_small(pretrained=True,in_22k=True)
# self.encoder = VAN(embed_dims=[64, 128, 320, 512],
# depths=[3, 3, 12, 3],
# init_cfg=dict(type='Pretrained', checkpoint='pretrained/van_b2.pth'),
# norm_cfg=dict(type='BN', requires_grad=True)
# )
# self.encoder = VAN(embed_dims=[64, 128, 320, 512],
# depths=[2, 2, 4, 2],
# init_cfg=dict(type='Pretrained', checkpoint='pretrained/van_b1.pth'),
# norm_cfg=dict(type='BN', requires_grad=True)
# )
# self.encoder.init_weights()
self.decoder = UNetDecoder(
spatial_dims=spatial_dims,
encoder_channels=encoder_channels,
decoder_channels=decoder_channels,
act=act,
norm=norm,
dropout=dropout,
bias=decoder_bias,
upsample=upsample,
interp_mode=interp_mode,
pre_conv=None,
align_corners=None,
is_pad=is_pad,
)
self.dist_head = SegmentationHead(
spatial_dims=spatial_dims,
in_channels=decoder_channels[-1],
out_channels=64,
kernel_size=1,
act='relu',
scale_factor = 2,
)
self.prob_head = SegmentationHead(
spatial_dims=spatial_dims,
in_channels=decoder_channels[-1],
out_channels=1,
kernel_size=1,
act='sigmoid',
scale_factor = 2,
)
def forward(self, inputs: torch.Tensor):
"""
Do a typical encoder-decoder-header inference.
Args:
inputs: input should have spatially N dimensions ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``,
N is defined by `dimensions`.
Returns:
A torch Tensor of "raw" predictions in shape ``(Batch, out_channels, dim_0[, dim_1, ..., dim_N])``.
"""
x = inputs
enc_out = self.encoder(x)
decoder_out = self.decoder(enc_out)
dist = self.dist_head(decoder_out)
prob = self.prob_head(decoder_out)
return dist,prob
class FlexibleUNet_hv(nn.Module):
"""
A flexible implementation of UNet-like encoder-decoder architecture.
"""
def __init__(
self,
in_channels: int,
out_channels: int,
backbone: str,
pretrained: bool = False,
decoder_channels: Tuple = (1024, 512, 256, 128),
spatial_dims: int = 2,
norm: Union[str, tuple] = ("batch", {"eps": 1e-3, "momentum": 0.1}),
act: Union[str, tuple] = ("relu", {"inplace": True}),
dropout: Union[float, tuple] = 0.0,
decoder_bias: bool = False,
upsample: str = "nontrainable",
interp_mode: str = "nearest",
is_pad: bool = True,
n_rays: int = 32,
prob_out_channels: int = 1,
) -> None:
"""
A flexible implement of UNet, in which the backbone/encoder can be replaced with
any efficient network. Currently the input must have a 2 or 3 spatial dimension
and the spatial size of each dimension must be a multiple of 32 if is pad parameter
is False
Args:
in_channels: number of input channels.
out_channels: number of output channels.
backbone: name of backbones to initialize, only support efficientnet right now,
can be from [efficientnet-b0,..., efficientnet-b8, efficientnet-l2].
pretrained: whether to initialize pretrained ImageNet weights, only available
for spatial_dims=2 and batch norm is used, default to False.
decoder_channels: number of output channels for all feature maps in decoder.
`len(decoder_channels)` should equal to `len(encoder_channels) - 1`,default
to (256, 128, 64, 32, 16).
spatial_dims: number of spatial dimensions, default to 2.
norm: normalization type and arguments, default to ("batch", {"eps": 1e-3,
"momentum": 0.1}).
act: activation type and arguments, default to ("relu", {"inplace": True}).
dropout: dropout ratio, default to 0.0.
decoder_bias: whether to have a bias term in decoder's convolution blocks.
upsample: upsampling mode, available options are``"deconv"``, ``"pixelshuffle"``,
``"nontrainable"``.
interp_mode: {``"nearest"``, ``"linear"``, ``"bilinear"``, ``"bicubic"``, ``"trilinear"``}
Only used in the "nontrainable" mode.
is_pad: whether to pad upsampling features to fit features from encoder. Default to True.
If this parameter is set to "True", the spatial dim of network input can be arbitary
size, which is not supported by TensorRT. Otherwise, it must be a multiple of 32.
"""
super().__init__()
if backbone not in encoder_feature_channel:
raise ValueError(f"invalid model_name {backbone} found, must be one of {encoder_feature_channel.keys()}.")
if spatial_dims not in (2, 3):
raise ValueError("spatial_dims can only be 2 or 3.")
adv_prop = "ap" in backbone
self.backbone = backbone
self.spatial_dims = spatial_dims
model_name = backbone
encoder_channels = _get_encoder_channels_by_backbone(backbone, in_channels)
self.encoder = convnext.convnext_small(pretrained=True,in_22k=True)
self.decoder = UNetDecoder(
spatial_dims=spatial_dims,
encoder_channels=encoder_channels,
decoder_channels=decoder_channels,
act=act,
norm=norm,
dropout=dropout,
bias=decoder_bias,
upsample=upsample,
interp_mode=interp_mode,
pre_conv=None,
align_corners=None,
is_pad=is_pad,
)
self.dist_head = SegmentationHead(
spatial_dims=spatial_dims,
in_channels=decoder_channels[-1],
out_channels=n_rays,
kernel_size=1,
act=None,
scale_factor = 2,
)
self.prob_head = SegmentationHead(
spatial_dims=spatial_dims,
in_channels=decoder_channels[-1],
out_channels=prob_out_channels,
kernel_size=1,
act='sigmoid',
scale_factor = 2,
)
def forward(self, inputs: torch.Tensor):
"""
Do a typical encoder-decoder-header inference.
Args:
inputs: input should have spatially N dimensions ``(Batch, in_channels, dim_0[, dim_1, ..., dim_N])``,
N is defined by `dimensions`.
Returns:
A torch Tensor of "raw" predictions in shape ``(Batch, out_channels, dim_0[, dim_1, ..., dim_N])``.
"""
x = inputs
enc_out = self.encoder(x)
decoder_out = self.decoder(enc_out)
dist = self.dist_head(decoder_out)
prob = self.prob_head(decoder_out)
return dist,prob |