Spaces:
Running
Running
| # Copyright (c) EPFL VILAB. | |
| # All rights reserved. | |
| # This source code is licensed under the license found in the | |
| # LICENSE file in the root directory of this source tree. | |
| # -------------------------------------------------------- | |
| # Based on timm, DPT and ConvNeXt code bases | |
| # https://github.com/rwightman/pytorch-image-models/tree/master/timm | |
| # https://github.com/isl-org/DPT | |
| # https://github.com/facebookresearch/ConvNeXt | |
| # -------------------------------------------------------- | |
| import torch | |
| import torch.nn as nn | |
| from .multimae_utils import DropPath | |
| class ConvNeXtBlock(nn.Module): | |
| r"""ConvNeXt Block. There are two equivalent implementations: | |
| (1) DwConv -> LayerNorm (channels_first) -> 1x1 Conv -> GELU -> 1x1 Conv; all in (N, C, H, W) | |
| (2) DwConv -> Permute to (N, H, W, C); LayerNorm (channels_last) -> Linear -> GELU -> Linear; Permute back | |
| We use (2) as we find it slightly faster in PyTorch | |
| Args: | |
| dim (int): Number of input channels. | |
| drop_path: Stochastic depth rate. Default: 0.0 | |
| layer_scale_init_value (float): Init value for Layer Scale. Default: 0 (disabled for isotropic ConvNeXt). | |
| Code from: https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py | |
| """ | |
| def __init__(self, dim, drop_path=0., layer_scale_init_value=0.): | |
| super().__init__() | |
| self.dwconv = nn.Conv2d(dim, dim, kernel_size=7, padding=3, groups=dim) # depthwise conv | |
| self.norm = nn.LayerNorm(dim, eps=1e-6) | |
| self.pwconv1 = nn.Linear(dim, 4 * dim) # pointwise/1x1 convs, implemented with linear layers | |
| self.act = nn.GELU() | |
| self.pwconv2 = nn.Linear(4 * dim, dim) | |
| self.gamma = nn.Parameter(layer_scale_init_value * torch.ones((dim)), | |
| requires_grad=True) if layer_scale_init_value > 0 else None | |
| self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() | |
| def forward(self, x): | |
| input = x | |
| x = self.dwconv(x) | |
| x = x.permute(0, 2, 3, 1) # (N, C, H, W) -> (N, H, W, C) | |
| x = self.norm(x) | |
| x = self.pwconv1(x) | |
| x = self.act(x) | |
| x = self.pwconv2(x) | |
| if self.gamma is not None: | |
| x = self.gamma * x | |
| x = x.permute(0, 3, 1, 2) # (N, H, W, C) -> (N, C, H, W) | |
| x = input + self.drop_path(x) | |
| return x | |
| class ResidualConvUnit_custom(nn.Module): | |
| """Residual convolution module.""" | |
| def __init__(self, features, activation, bn): | |
| """Init. | |
| Args: | |
| features (int): number of features | |
| """ | |
| super().__init__() | |
| self.bn = bn | |
| self.groups = 1 | |
| self.conv1 = nn.Conv2d( | |
| features, | |
| features, | |
| kernel_size=3, | |
| stride=1, | |
| padding=1, | |
| bias=not self.bn, | |
| groups=self.groups, | |
| ) | |
| self.conv2 = nn.Conv2d( | |
| features, | |
| features, | |
| kernel_size=3, | |
| stride=1, | |
| padding=1, | |
| bias=not self.bn, | |
| groups=self.groups, | |
| ) | |
| if self.bn == True: | |
| self.bn1 = nn.BatchNorm2d(features) | |
| self.bn2 = nn.BatchNorm2d(features) | |
| self.activation = activation | |
| self.skip_add = nn.quantized.FloatFunctional() | |
| def forward(self, x): | |
| """Forward pass. | |
| Args: | |
| x (tensor): input | |
| Returns: | |
| tensor: output | |
| """ | |
| out = self.activation(x) | |
| out = self.conv1(out) | |
| if self.bn == True: | |
| out = self.bn1(out) | |
| out = self.activation(out) | |
| out = self.conv2(out) | |
| if self.bn == True: | |
| out = self.bn2(out) | |
| if self.groups > 1: | |
| out = self.conv_merge(out) | |
| return self.skip_add.add(out, x) | |
| def make_scratch(in_shape, out_shape, groups=1, expand=False): | |
| scratch = nn.Module() | |
| out_shape1 = out_shape | |
| out_shape2 = out_shape | |
| out_shape3 = out_shape | |
| out_shape4 = out_shape | |
| if expand == True: | |
| out_shape1 = out_shape | |
| out_shape2 = out_shape * 2 | |
| out_shape3 = out_shape * 4 | |
| out_shape4 = out_shape * 8 | |
| scratch.layer1_rn = nn.Conv2d( | |
| in_shape[0], | |
| out_shape1, | |
| kernel_size=3, | |
| stride=1, | |
| padding=1, | |
| bias=False, | |
| groups=groups, | |
| ) | |
| scratch.layer2_rn = nn.Conv2d( | |
| in_shape[1], | |
| out_shape2, | |
| kernel_size=3, | |
| stride=1, | |
| padding=1, | |
| bias=False, | |
| groups=groups, | |
| ) | |
| scratch.layer3_rn = nn.Conv2d( | |
| in_shape[2], | |
| out_shape3, | |
| kernel_size=3, | |
| stride=1, | |
| padding=1, | |
| bias=False, | |
| groups=groups, | |
| ) | |
| scratch.layer4_rn = nn.Conv2d( | |
| in_shape[3], | |
| out_shape4, | |
| kernel_size=3, | |
| stride=1, | |
| padding=1, | |
| bias=False, | |
| groups=groups, | |
| ) | |
| scratch.layer_rn = nn.ModuleList([ | |
| scratch.layer1_rn, | |
| scratch.layer2_rn, | |
| scratch.layer3_rn, | |
| scratch.layer4_rn, | |
| ]) | |
| return scratch | |
| class FeatureFusionBlock_custom(nn.Module): | |
| """Feature fusion block.""" | |
| def __init__( | |
| self, | |
| features, | |
| activation, | |
| deconv=False, | |
| bn=False, | |
| expand=False, | |
| align_corners=True, | |
| ): | |
| """Init. | |
| Args: | |
| features (int): number of features | |
| """ | |
| super(FeatureFusionBlock_custom, self).__init__() | |
| self.deconv = deconv | |
| self.align_corners = align_corners | |
| self.groups = 1 | |
| self.expand = expand | |
| out_features = features | |
| if self.expand == True: | |
| out_features = features // 2 | |
| self.out_conv = nn.Conv2d( | |
| features, | |
| out_features, | |
| kernel_size=1, | |
| stride=1, | |
| padding=0, | |
| bias=True, | |
| groups=1, | |
| ) | |
| self.resConfUnit1 = ResidualConvUnit_custom(features, activation, bn) | |
| self.resConfUnit2 = ResidualConvUnit_custom(features, activation, bn) | |
| self.skip_add = nn.quantized.FloatFunctional() | |
| def forward(self, *xs): | |
| """Forward pass. | |
| Returns: | |
| tensor: output | |
| """ | |
| output = xs[0] | |
| if len(xs) == 2: | |
| res = self.resConfUnit1(xs[1]) | |
| output = self.skip_add.add(output, res) | |
| # output += res | |
| output = self.resConfUnit2(output) | |
| output = nn.functional.interpolate( | |
| output, scale_factor=2, mode="bilinear", align_corners=self.align_corners | |
| ) | |
| output = self.out_conv(output) | |
| return output | |
| def make_fusion_block(features, use_bn): | |
| return FeatureFusionBlock_custom( | |
| features, | |
| nn.ReLU(False), | |
| deconv=False, | |
| bn=use_bn, | |
| expand=False, | |
| align_corners=True, | |
| ) | |
| class Interpolate(nn.Module): | |
| """Interpolation module.""" | |
| def __init__(self, scale_factor, mode, align_corners=False): | |
| """Init. | |
| Args: | |
| scale_factor (float): scaling | |
| mode (str): interpolation mode | |
| """ | |
| super(Interpolate, self).__init__() | |
| self.interp = nn.functional.interpolate | |
| self.scale_factor = scale_factor | |
| self.mode = mode | |
| self.align_corners = align_corners | |
| def forward(self, x): | |
| """Forward pass. | |
| Args: | |
| x (tensor): input | |
| Returns: | |
| tensor: interpolated data | |
| """ | |
| x = self.interp( | |
| x, | |
| scale_factor=self.scale_factor, | |
| mode=self.mode, | |
| align_corners=self.align_corners, | |
| ) | |
| return x | |