resnet_general_block

tfsnippet.layers.resnet_general_block(*args, **kwargs)

A general implementation of ResNet block.

The architecture of this ResNet implementation follows the work “Wide residual networks” (Zagoruyko & Komodakis, 2016). It basically does the following things:

shortcut = input
if strides != 1 or in_channels != out_channels or use_shortcut_conv:
    shortcut = shortcut_conv_fn(
        input=shortcut,
        out_channels=out_channels,
        kernel_size=shortcut_kernel_size,
        strides=strides,
        scope='shortcut'
    )

residual = input
residual = conv_fn(
    input=activation_fn(normalizer_fn(residual)),
    out_channels=in_channels if resize_at_exit else out_channels,
    kernel_size=kernel_size,
    strides=strides,
    scope='conv_0'
)
residual = after_conv_0(residual)
residual = dropout_fn(residual)
residual = conv_fn(
    input=activation_fn(normalizer_fn(residual)),
    out_channels=out_channels,
    kernel_size=kernel_size,
    strides=strides,
    scope='conv_1'
)
residual = after_conv_1(residual)

output = shortcut + residual
Parameters:
  • conv_fn – The convolution function for “conv_0” and “conv_1” convolutional layers. It must accept the following named arguments:
  • name (str) – Default name of the variable scope. Will be uniquified. If not specified, generate one according to the class name.
  • scope (str) –

    The name of the variable scope.

    • input
    • out_channels
    • kernel_size
    • strides
    • channels_last
    • use_bias
    • scope

    Also, it must accept the named arguments specified in kwargs.

  • input (Tensor) – The input tensor.
  • in_channels (int) – The channel numbers of the tensor.
  • out_channels (int) – The channel numbers of the output.
  • kernel_size (int or tuple[int]) – Kernel size over spatial dimensions, for “conv_0” and “conv_1” convolutional layers.
  • strides (int or tuple[int]) – Strides over spatial dimensions, for “conv_0”, “conv_1” and “shortcut” convolutional layers.
  • channels_last (bool) – Whether or not the channel axis is the last axis in input? (i.e., the data format is “NHWC”)
  • use_shortcut_conv (True or None) – If True, force to apply a linear convolution transformation on the shortcut path. If None (by default), only use shortcut if necessary.
  • shortcut_conv_fn – The convolution function for the “shortcut” convolutional layer. It should accept same named arguments as conv_fn. If not specified, use conv_fn.
  • shortcut_kernel_size (int or tuple[int]) – Kernel size over spatial dimensions, for the “shortcut” convolutional layer.
  • resize_at_exit (bool) – If True, resize the spatial dimensions at the “conv_1” convolutional layer. If False, resize at the “conv_0” convolutional layer. (see above)
  • after_conv_0 – The function to apply on the output of “conv_0” layer.
  • after_conv_1 – The function to apply on the output of “conv_1” layer.
  • activation_fn – The activation function.
  • normalizer_fn – The normalizer function.
  • dropout_fn – The dropout function.
  • gated (bool) – Whether or not to use gate on the output of “conv_1”? conv_1_output = activation_fn(conv_1_output) * sigmoid(gate).
  • gate_sigmoid_bias (Tensor) – The bias added to gate before applying the sigmoid activation.
  • use_bias (bool or None) – Whether or not to use bias in “conv_0” and “conv_1”? If True, will always use bias. If None, will use bias only if normalizer_fn is not given. If False, will never use bias. Default is None.
  • **kwargs – Other named arguments passed to “conv_0”, “conv_1” and “shortcut” convolutional layers.
Returns:

The output tensor.

Return type:

tf.Tensor