basicsr.archs.arch_util

class basicsr.archs.arch_util.DCNv2Pack(*args, **kwargs)[source]

Bases: ModulatedDeformConvPack

Modulated deformable conv for deformable alignment.

Different from the official DCNv2Pack, which generates offsets and masks from the preceding features, this DCNv2Pack takes another different features to generate offsets and masks.

Paper: Delving Deep into Deformable Alignment in Video Super-Resolution

forward(x, feat)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class basicsr.archs.arch_util.ResidualBlockNoBN(num_feat=64, res_scale=1, pytorch_init=False)[source]

Bases: Module

Residual block without BN.

Parameters:
  • num_feat (int) – Channel number of intermediate features. Default: 64.

  • res_scale (float) – Residual scale. Default: 1.

  • pytorch_init (bool) – If set to True, use pytorch default init, otherwise, use default_init_weights. Default: False.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class basicsr.archs.arch_util.Upsample(scale, num_feat)[source]

Bases: Sequential

Upsample module.

Parameters:
  • scale (int) – Scale factor. Supported scales: 2^n and 3.

  • num_feat (int) – Channel number of intermediate features.

basicsr.archs.arch_util.default_init_weights(module_list, scale=1, bias_fill=0, **kwargs)[source]

Initialize network weights.

Parameters:
  • module_list (list[nn.Module] | nn.Module) – Modules to be initialized.

  • scale (float) – Scale initialized weights, especially for residual blocks. Default: 1.

  • bias_fill (float) – The value to fill bias. Default: 0

  • kwargs (dict) – Other arguments for initialization function.

basicsr.archs.arch_util.flow_warp(x, flow, interp_mode='bilinear', padding_mode='zeros', align_corners=True)[source]

Warp an image or feature map with optical flow.

Parameters:
  • x (Tensor) – Tensor with size (n, c, h, w).

  • flow (Tensor) – Tensor with size (n, h, w, 2), normal value.

  • interp_mode (str) – ‘nearest’ or ‘bilinear’. Default: ‘bilinear’.

  • padding_mode (str) – ‘zeros’ or ‘border’ or ‘reflection’. Default: ‘zeros’.

  • align_corners (bool) – Before pytorch 1.3, the default value is align_corners=True. After pytorch 1.3, the default value is align_corners=False. Here, we use the True as default.

Returns:

Warped image or feature map.

Return type:

Tensor

basicsr.archs.arch_util.make_layer(basic_block, num_basic_block, **kwarg)[source]

Make layers by stacking the same blocks.

Parameters:
  • basic_block (nn.module) – nn.module class for basic block.

  • num_basic_block (int) – number of blocks.

Returns:

Stacked blocks in nn.Sequential.

Return type:

nn.Sequential

basicsr.archs.arch_util.modulated_deform_conv()
basicsr.archs.arch_util.pixel_unshuffle(x, scale)[source]

Pixel unshuffle.

Parameters:
  • x (Tensor) – Input feature with shape (b, c, hh, hw).

  • scale (int) – Downsample ratio.

Returns:

the pixel unshuffled feature.

Return type:

Tensor

basicsr.archs.arch_util.resize_flow(flow, size_type, sizes, interp_mode='bilinear', align_corners=False)[source]

Resize a flow according to ratio or shape.

Parameters:
  • flow (Tensor) – Precomputed flow. shape [N, 2, H, W].

  • size_type (str) – ‘ratio’ or ‘shape’.

  • sizes (list[int | float]) – the ratio for resizing or the final output shape. 1) The order of ratio should be [ratio_h, ratio_w]. For downsampling, the ratio should be smaller than 1.0 (i.e., ratio < 1.0). For upsampling, the ratio should be larger than 1.0 (i.e., ratio > 1.0). 2) The order of output_size should be [out_h, out_w].

  • interp_mode (str) – The mode of interpolation for resizing. Default: ‘bilinear’.

  • align_corners (bool) – Whether align corners. Default: False.

Returns:

Resized flow.

Return type:

Tensor

basicsr.archs.arch_util.to_1tuple(x)
basicsr.archs.arch_util.to_2tuple(x)
basicsr.archs.arch_util.to_3tuple(x)
basicsr.archs.arch_util.to_4tuple(x)
basicsr.archs.arch_util.to_ntuple(n)
basicsr.archs.arch_util.trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0)[source]

Fills the input Tensor with values drawn from a truncated normal distribution.

From: https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/layers/weight_init.py

The values are effectively drawn from the normal distribution \(\mathcal{N}(\text{mean}, \text{std}^2)\) with values outside \([a, b]\) redrawn until they are within the bounds. The method used for generating the random values works best when \(a \leq \text{mean} \leq b\).

Parameters:
  • tensor – an n-dimensional torch.Tensor

  • mean – the mean of the normal distribution

  • std – the standard deviation of the normal distribution

  • a – the minimum cutoff value

  • b – the maximum cutoff value

Examples

>>> w = torch.empty(3, 5)
>>> nn.init.trunc_normal_(w)