mednet.models.segment.make_layers

Functions

conv_with_kaiming_uniform(in_channels, ...)

Convolution layer with kaiming uniform.

convtrans_with_kaiming_uniform(in_channels, ...)

Implement convtrans layer with kaiming uniform.

icnr(x[, scale, init])

ICNR init of x, with scale and init function.

ifnone(a, b)

Return a if a is not None, otherwise b.

Classes

PixelShuffleICNR(ni[, nf, scale])

Upsample by scale from ni filters to nf (default ni), using torch.nn.PixelShuffle, icnr init, and weight_norm.

UnetBlock(up_in_c, x_in_c[, pixel_shuffle, ...])

Unet block implementation.

UpsampleCropBlock(in_channels, out_channels, ...)

Combines Conv2d, ConvTransposed2d and Cropping.

mednet.models.segment.make_layers.conv_with_kaiming_uniform(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1)[source]

Convolution layer with kaiming uniform.

Parameters:
  • in_channels – Number of input channels.

  • out_channels – Number of output channels.

  • kernel_size – The kernel size.

  • stride – The stride.

  • padding – The padding.

  • dilation – The dilation.

Returns:

The convoluation layer.

mednet.models.segment.make_layers.convtrans_with_kaiming_uniform(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1)[source]

Implement convtrans layer with kaiming uniform.

Parameters:
  • in_channels – Number of input channels.

  • out_channels – Number of output channels.

  • kernel_size – The kernel size.

  • stride – The stride.

  • padding – The padding.

  • dilation – The dilation.

Returns:

The convtrans layer.

class mednet.models.segment.make_layers.UpsampleCropBlock(in_channels, out_channels, up_kernel_size, up_stride, up_padding, pixelshuffle=False)[source]

Bases: Module

Combines Conv2d, ConvTransposed2d and Cropping. Simulates the caffe2 crop layer in the forward function.

Used for DRIU and HED.

Parameters:
  • in_channels (int) – Number of channels of intermediate layer.

  • out_channels (int) – Number of output channels.

  • up_kernel_size (int) – Kernel size for transposed convolution.

  • up_stride (int) – Stride for transposed convolution.

  • up_padding (int) – Padding for transposed convolution.

  • pixelshuffle (bool) – If True, uses PixelShuffleICNR upsampling.

forward(x, input_res)[source]

Define 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.

mednet.models.segment.make_layers.ifnone(a, b)[source]

Return a if a is not None, otherwise b.

Parameters:
  • a – The first parameter.

  • b – The second parameter.

Returns:

The parameter a if it is not None, else b.

mednet.models.segment.make_layers.icnr(x, scale=2, init=<function kaiming_normal_>)[source]

ICNR init of x, with scale and init function.

https://docs.fast.ai/layers.html#PixelShuffleICNR.

Parameters:
  • x – Tensor.

  • scale – Scale of the upsample.

  • init – Function used to initialize.

class mednet.models.segment.make_layers.PixelShuffleICNR(ni, nf=None, scale=2)[source]

Bases: Module

Upsample by scale from ni filters to nf (default ni), using torch.nn.PixelShuffle, icnr init, and weight_norm.

https://docs.fast.ai/layers.html#PixelShuffleICNR.

Parameters:
  • ni (int) – Number of initial filters.

  • nf (int | None) – Number of final filters.

  • scale (int) – Scale of the upsample.

forward(x)[source]

Define 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.

class mednet.models.segment.make_layers.UnetBlock(up_in_c, x_in_c, pixel_shuffle=False, middle_block=False)[source]

Bases: Module

Unet block implementation.

Parameters:
  • up_in_c – Number of input channels.

  • x_in_c – Number of cat channels.

  • pixel_shuffle – If True, uses a PixelShuffleICNR layer for upsampling.

  • middle_block – If True, uses a middle block for VGG based U-Net.

forward(up_in, x_in)[source]

Define 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.