Exemple #1
0
def test_conv2d_stack(
    img_height: int,
    img_width: int,
    layers: Optional[List[Dict]],
    num_layers: Optional[int],
    first_in_channels: Optional[int],
) -> None:
    conv2d_stack = Conv2DStack(
        img_height=img_height,
        img_width=img_width,
        layers=layers,
        num_layers=num_layers,
        first_in_channels=first_in_channels,
    )
    input_tensor = torch.rand(2, 3, img_height, img_width)
    output_tensor = conv2d_stack(input_tensor)
    assert output_tensor.shape[1:] == conv2d_stack.output_shape
Exemple #2
0
    def __init__(
            self,
            conv_layers=None,
            num_conv_layers=None,
            filter_size=3,
            num_filters=32,
            strides=(1, 1),
            padding='valid',
            dilation_rate=(1, 1),
            conv_use_bias=True,
            conv_weights_initializer='glorot_uniform',
            conv_bias_initializer='zeros',
            conv_weights_regularizer=None,
            conv_bias_regularizer=None,
            conv_activity_regularizer=None,
            # conv_weights_constraint=None,
            # conv_bias_constraint=None,
            conv_norm=None,
            conv_norm_params=None,
            conv_activation='relu',
            conv_dropout=0,
            pool_size=(2, 2),
            pool_strides=None,
            fc_layers=None,
            num_fc_layers=1,
            fc_size=128,
            fc_use_bias=True,
            fc_weights_initializer='glorot_uniform',
            fc_bias_initializer='zeros',
            fc_weights_regularizer=None,
            fc_bias_regularizer=None,
            fc_activity_regularizer=None,
            # fc_weights_constraint=None,
            # fc_bias_constraint=None,
            fc_norm=None,
            fc_norm_params=None,
            fc_activation='relu',
            fc_dropout=0,
            **kwargs):
        super(Stacked2DCNN, self).__init__()

        logger.debug(' {}'.format(self.name))

        logger.debug('  Conv2DStack')
        self.conv_stack_2d = Conv2DStack(
            layers=conv_layers,
            num_layers=num_conv_layers,
            default_num_filters=num_filters,
            default_filter_size=filter_size,
            default_strides=strides,
            default_padding=padding,
            default_dilation_rate=dilation_rate,
            default_use_bias=conv_use_bias,
            default_weights_initializer=conv_weights_initializer,
            defaultbias_initializer=conv_bias_initializer,
            default_weights_regularizer=conv_weights_regularizer,
            default_bias_regularizer=conv_bias_regularizer,
            default_activity_regularizer=conv_activity_regularizer,
            # default_weights_constraint=conv_weights_constraint,
            # default_bias_constraint=conv_bias_constraint,
            default_norm=conv_norm,
            default_norm_params=conv_norm_params,
            default_activation=conv_activation,
            default_dropout=conv_dropout,
            default_pool_size=pool_size,
            default_pool_strides=pool_strides,
        )

        logger.debug('  FCStacl')
        self.fc_stack = FCStack(
            layers=fc_layers,
            num_layers=num_fc_layers,
            default_fc_size=fc_size,
            default_use_bias=fc_use_bias,
            default_weights_initializer=fc_weights_initializer,
            default_bias_initializer=fc_bias_initializer,
            default_weights_regularizer=fc_weights_regularizer,
            default_bias_regularizer=fc_bias_regularizer,
            default_activity_regularizer=fc_activity_regularizer,
            # default_weights_constraint=fc_weights_constraint,
            # default_bias_constraint=fc_bias_constraint,
            default_norm=fc_norm,
            default_norm_params=fc_norm_params,
            default_activation=fc_activation,
            default_dropout=fc_dropout,
        )
Exemple #3
0
    def __init__(
        self,
        height: int,
        width: int,
        conv_layers: Optional[List[Dict]] = None,
        num_conv_layers: Optional[int] = None,
        num_channels: int = None,
        out_channels: int = 32,
        kernel_size: Union[int, Tuple[int]] = 3,
        stride: Union[int, Tuple[int]] = 1,
        padding: Union[int, Tuple[int], str] = "valid",
        dilation: Union[int, Tuple[int]] = 1,
        conv_use_bias: bool = True,
        padding_mode: str = "zeros",
        conv_norm: Optional[str] = None,
        conv_norm_params: Optional[Dict[str, Any]] = None,
        conv_activation: str = "relu",
        conv_dropout: int = 0,
        pool_function: str = "max",
        pool_kernel_size: Union[int, Tuple[int]] = 2,
        pool_stride: Union[int, Tuple[int]] = None,
        pool_padding: Union[int, Tuple[int]] = 0,
        pool_dilation: Union[int, Tuple[int]] = 1,
        groups: int = 1,
        fc_layers: Optional[List[Dict]] = None,
        num_fc_layers: Optional[int] = 1,
        output_size: int = 128,
        fc_use_bias: bool = True,
        fc_weights_initializer: str = "xavier_uniform",
        fc_bias_initializer: str = "zeros",
        fc_norm: Optional[str] = None,
        fc_norm_params: Optional[Dict[str, Any]] = None,
        fc_activation: str = "relu",
        fc_dropout: float = 0,
        **kwargs,
    ):
        super().__init__()

        logger.debug(f" {self.name}")

        # map parameter input feature config names to internal names
        img_height = height
        img_width = width
        first_in_channels = num_channels

        self._input_shape = (first_in_channels, img_height, img_width)

        if first_in_channels is None:
            raise ValueError("first_in_channels must not be None.")

        logger.debug("  Conv2DStack")
        self.conv_stack_2d = Conv2DStack(
            img_height=img_height,
            img_width=img_width,
            layers=conv_layers,
            num_layers=num_conv_layers,
            first_in_channels=first_in_channels,
            default_out_channels=out_channels,
            default_kernel_size=kernel_size,
            default_stride=stride,
            default_padding=padding,
            default_dilation=dilation,
            default_groups=groups,
            default_use_bias=conv_use_bias,
            default_padding_mode=padding_mode,
            default_norm=conv_norm,
            default_norm_params=conv_norm_params,
            default_activation=conv_activation,
            default_dropout=conv_dropout,
            default_pool_function=pool_function,
            default_pool_kernel_size=pool_kernel_size,
            default_pool_stride=pool_stride,
            default_pool_padding=pool_padding,
            default_pool_dilation=pool_dilation,
        )
        out_channels, img_height, img_width = self.conv_stack_2d.output_shape
        first_fc_layer_input_size = out_channels * img_height * img_width

        self.flatten = torch.nn.Flatten()

        logger.debug("  FCStack")
        self.fc_stack = FCStack(
            first_layer_input_size=first_fc_layer_input_size,
            layers=fc_layers,
            num_layers=num_fc_layers,
            default_output_size=output_size,
            default_use_bias=fc_use_bias,
            default_weights_initializer=fc_weights_initializer,
            default_bias_initializer=fc_bias_initializer,
            default_norm=fc_norm,
            default_norm_params=fc_norm_params,
            default_activation=fc_activation,
            default_dropout=fc_dropout,
        )