Exemple #1
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride=1,
                 padding=1,
                 wscale=1.0):
        super(P4MTransitionBlock2D, self).__init__()

        # Import the group structure
        import importlib
        group_name = 'E2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        e2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        self.h_grid = e2_layers.H.grid_global(8)  # p4

        # Parameters of the model
        eps = 2e-5
        bias = False
        # Layers
        self.bn = nn.BatchNorm3d(num_features=in_channels, eps=eps)
        self.conv = e2_layers.ConvGG(N_in=in_channels,
                                     N_out=out_channels,
                                     kernel_size=1,
                                     h_grid=self.h_grid,
                                     input_h_grid=self.h_grid,
                                     stride=stride,
                                     padding=padding,
                                     wscale=wscale)
        self.pooling = e2_layers.average_pooling_Rn
Exemple #2
0
    def __init__(self, N_h_in, N_in, ratio=1, group='SE2'):
        super(fChannelAttentionGG, self).__init__()
        self.N_in = N_in
        self.ratio = ratio
        self.N_h_in = N_h_in
        self.N_h = N_h_in
        self.weight_fc1 = torch.nn.Parameter(
            torch.rand(self.N_in // ratio, self.N_in, self.N_h_in))
        self.weight_fc2 = torch.nn.Parameter(
            torch.rand(self.N_in, self.N_in // ratio, self.N_h_in))

        # group instantiation
        self.action = self._left_action_of_h_grid_se2
        if group == 'E2':  # TODO: Move to a more meaningful place.
            import importlib
            group = importlib.import_module('attgconv.group.' + group)
            import attgconv
            e2_layers = attgconv.layers(
                group
            )  # The layers is instantiated with the group structure as input
            # Create H grid for p4m group
            n_grid = 8
            self.h_grid = e2_layers.H.grid_global(n_grid)
            self.action = self._left_action_on_grid_e2

        # Initialize weights
        self.reset_parameters()
Exemple #3
0
    def __init__(self, in_channels, out_channels, kernel_size=3, fiber_map='id', stride=1, padding=1, wscale=1.0):
        super(P4MResBlock2D, self).__init__()

        # Asserts
        assert kernel_size % 2 == 1
        if not padding == (kernel_size - 1) // 2:
            raise NotImplementedError()

        # Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'E2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        e2_layers = attgconv.layers(group)  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        self.h_grid = e2_layers.H.grid_global(8)  # 2*p4

        # Parameters of the model
        eps = 2e-5

        if stride != 1:
            self.really_equivariant = True
            self.pooling = e2_layers.max_pooling_Rn
        else:
            self.really_equivariant = False

        self.bn1 = nn.BatchNorm3d(num_features=in_channels, eps=eps)
        self.c1 = e2_layers.ConvGG(N_in=in_channels , N_out=out_channels, kernel_size=kernel_size, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=stride, padding=padding, wscale=wscale)
        if self.really_equivariant:
            self.c1 = e2_layers.ConvGG(N_in=in_channels, N_out=out_channels, kernel_size=kernel_size, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1, padding=padding, wscale=wscale)

        self.bn2 = nn.BatchNorm3d(num_features=out_channels, eps=eps)
        self.c2 = e2_layers.ConvGG(N_in=out_channels, N_out=out_channels, kernel_size=kernel_size, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1     , padding=padding, wscale=wscale)

        if fiber_map == 'id':
            if not in_channels == out_channels:
                raise ValueError('fiber_map cannot be identity when channel dimension is changed.')
            self.fiber_map = nn.Sequential() # Identity
        elif fiber_map == 'zero_pad':
            raise NotImplementedError()
        elif fiber_map == 'linear':
            self.fiber_map = e2_layers.ConvGG(N_in=in_channels, N_out=out_channels, kernel_size=1, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=stride, padding=0, wscale=wscale)
            if self.really_equivariant:
                self.fiber_map = e2_layers.ConvGG(N_in=in_channels, N_out=out_channels, kernel_size=1, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1, padding=0, wscale=wscale)
        else:
            raise ValueError('Unknown fiber_map: ' + str(type))
Exemple #4
0
    def __init__(self, use_bias=False):
        super(RomHog_fA_P4CNN, self).__init__()

        #Parameters of the group
        # Import the group structure
        import importlib
        group_name = 'SE2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        se2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        n_grid = 4
        h_grid = se2_layers.H.grid_global(n_grid)
        # ----------------------
        # Parameters of the model
        p = 0.3
        stride = 1
        padding = 0
        kernel_size = 3
        N_channels = 10
        eps = 2e-5
        # ----------------------

        # --------------------------------------------------------
        # Store all parameters such that all (sub)models share it.
        self.group_name = group_name
        self.group = group
        self.layers = se2_layers
        self.n_grid = n_grid
        self.h_grid = h_grid
        self.p = p
        self.stride = stride
        self.padding = padding
        self.kernel_size = kernel_size
        self.N_channels = N_channels
        self.eps = eps

        from attgconv.RomHog_attention import fSpatialAttention as sp_RnG
        from attgconv.RomHog_attention import fSpatialAttentionGG

        sp_GG = functools.partial(fSpatialAttentionGG,
                                  group=group,
                                  input_h_grid=self.h_grid,
                                  stride=stride)

        self.c1 = se2_layers.fAttConvRnG(N_in=1,
                                         N_out=N_channels,
                                         kernel_size=kernel_size,
                                         h_grid=self.h_grid,
                                         stride=stride,
                                         padding=padding,
                                         spatial_attention=sp_RnG(
                                             group=group,
                                             h_grid=self.h_grid,
                                             N_in=1))
        self.c2 = se2_layers.fAttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            spatial_attention=sp_GG(N_in=N_channels))
        self.c3 = se2_layers.fAttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            spatial_attention=sp_GG(N_in=N_channels))
        self.c4 = se2_layers.fAttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            spatial_attention=sp_GG(N_in=N_channels))
        self.c5 = se2_layers.fAttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            spatial_attention=sp_GG(N_in=N_channels))
        self.c6 = se2_layers.fAttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            spatial_attention=sp_GG(N_in=N_channels))
        self.c7 = se2_layers.fAttConvGG(
            N_in=N_channels,
            N_out=10,
            kernel_size=4,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            spatial_attention=sp_GG(N_in=N_channels))
        # Dropout
        self.dp = nn.Dropout(p)
        # Batch Normalization
        self.bn1 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn2 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn3 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn4 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn5 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn6 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        # Max Pooling
        self.max_pooling = se2_layers.max_pooling_Rn
Exemple #5
0
    def __init__(self, num_blocks=5, n_channels=24):
        super(fA_P4MDenseNet, self).__init__()

        #Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'E2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        e2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        n_grid = 8
        self.h_grid = e2_layers.H.grid_global(n_grid)  # 2*p4

        # Parameters of the model
        padding = 0
        stride = 1
        kernel_size = 3
        eps = 2e-5
        bias = False
        grow_ch = n_channels

        # Save num_blocks
        self.num_blocks = num_blocks

        # Initialization parameters
        wscale = sqrt(
            2.)  # This makes the initialization equal to that of He et. al.

        # ----------------------
        # Parameters of attention
        # ch_ratio = 16
        sp_kernel_size = 7
        sp_padding = (sp_kernel_size // 2)
        # --------------------------------------------------------

        from attgconv.attention_layers import fChannelAttention as ch_RnG
        from attgconv.attention_layers import fChannelAttentionGG  # as ch_GG
        from attgconv.attention_layers import fSpatialAttention  # as sp_RnG
        from attgconv.attention_layers import fSpatialAttentionGG

        ch_GG = functools.partial(fChannelAttentionGG,
                                  N_h_in=n_grid,
                                  group=group_name)
        sp_RnG = functools.partial(fSpatialAttention, wscale=wscale)
        sp_GG = functools.partial(fSpatialAttentionGG,
                                  group=group,
                                  input_h_grid=self.h_grid,
                                  wscale=wscale)

        # First layer is a convolution
        self.c1 = e2_layers.fAttConvRnG(N_in=3,
                                        N_out=n_channels,
                                        kernel_size=kernel_size,
                                        h_grid=self.h_grid,
                                        stride=stride,
                                        padding=padding,
                                        wscale=wscale,
                                        channel_attention=ch_RnG(N_in=3,
                                                                 ratio=1),
                                        spatial_attention=sp_RnG(
                                            group=group,
                                            kernel_size=sp_kernel_size,
                                            h_grid=self.h_grid,
                                            dilation=4))

        # Add num_blocks - 1 DenseBlocks and TransitionBlocks
        block_layers = []
        trans_layers = []
        dilations = [4, 4, 3, 2]
        for i in range(num_blocks - 1):
            block_layers.append(
                fA_P4MDenseBlock2D(in_channels=n_channels,
                                   out_channels=grow_ch,
                                   kernel_size=kernel_size,
                                   stride=stride,
                                   padding=padding,
                                   wscale=wscale,
                                   dilation=dilations[i]))
            n_channels = n_channels + grow_ch
            if i == 3:
                dilations[i] = 1
            trans_layers.append(
                fA_P4MTransitionBlock2D(in_channels=n_channels,
                                        out_channels=n_channels,
                                        stride=stride,
                                        padding=padding,
                                        wscale=wscale,
                                        dilation=dilations[i]))
        # Add last layer to DenseBlock
        block_layers.append(
            fA_P4MDenseBlock2D(in_channels=n_channels,
                               out_channels=grow_ch,
                               kernel_size=kernel_size,
                               stride=stride,
                               padding=padding,
                               wscale=wscale,
                               sp_kernel_size=5))
        n_channels = n_channels + grow_ch
        # Add layers to self
        self.block_layers = nn.Sequential(*block_layers)
        self.trans_layers = nn.Sequential(*trans_layers)

        # Add BN for final DenseBlock
        self.bn_out = nn.BatchNorm3d(num_features=n_channels, eps=eps)
        # Reduce to 2 channels (IMPORTANT! THIS IS DIFFERENT TO VEELING ET. AL. (2018), They use only one output channel with sigmoid)
        self.c_out = e2_layers.fAttConvGG(
            N_in=n_channels,
            N_out=2,
            kernel_size=1,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            wscale=wscale,
            channel_attention=ch_GG(N_in=n_channels, ratio=n_channels // 2),
            spatial_attention=sp_GG(kernel_size=3))
        # Define last pooling layer
        self.pooling = e2_layers.average_pooling_Rn
Exemple #6
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride=1,
                 padding=1,
                 wscale=1.0,
                 sp_kernel_size=None,
                 dilation=1):
        super(fA_P4TransitionBlock2D, self).__init__()

        # Import the group structure
        import importlib
        group_name = 'SE2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        se2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        n_grid = 4
        self.h_grid = se2_layers.H.grid_global(n_grid)  # p4

        # Parameters of the model
        eps = 2e-5
        bias = False
        # ----------------------
        # Parameters of attention
        # ch_ratio = 16
        if sp_kernel_size is None:
            sp_kernel_size = 7
        sp_padding = (sp_kernel_size // 2)
        # --------------------------------------------------------

        from attgconv.attention_layers import fChannelAttentionGG  # as ch_GG
        from attgconv.attention_layers import fSpatialAttention  # as sp_RnG
        from attgconv.attention_layers import fSpatialAttentionGG

        ch_GG = functools.partial(fChannelAttentionGG,
                                  N_h_in=n_grid,
                                  group=group_name)
        sp_RnG = functools.partial(fSpatialAttention, wscale=wscale)
        sp_GG = functools.partial(fSpatialAttentionGG,
                                  group=group,
                                  input_h_grid=self.h_grid,
                                  wscale=wscale)

        # Layers
        self.bn = nn.BatchNorm3d(num_features=in_channels, eps=eps)
        self.conv = se2_layers.fAttConvGG(
            N_in=in_channels,
            N_out=out_channels,
            kernel_size=1,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            wscale=wscale,
            channel_attention=ch_GG(N_in=in_channels, ratio=in_channels // 2),
            spatial_attention=sp_GG(kernel_size=sp_kernel_size,
                                    dilation=dilation))
        self.pooling = se2_layers.average_pooling_Rn
Exemple #7
0
    def __init__(self, num_blocks=5, n_channels=24):
        super(P4MDenseNet, self).__init__()

        #Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'E2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        e2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        self.h_grid = e2_layers.H.grid_global(8)  # 2xp4

        # Parameters of the model
        padding = 0
        stride = 1
        kernel_size = 3
        eps = 2e-5
        bias = False
        grow_ch = n_channels

        # Save num_blocks
        self.num_blocks = num_blocks

        # Initialization parameters
        wscale = sqrt(
            2.)  # This makes the initialization equal to that of He et. al.

        # First layer is a convolution
        self.c1 = e2_layers.ConvRnG(N_in=3,
                                    N_out=n_channels,
                                    kernel_size=kernel_size,
                                    h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding,
                                    wscale=wscale)

        # Add num_blocks - 1 DenseBlocks and TransitionBlocks
        block_layers = []
        trans_layers = []
        for i in range(num_blocks - 1):
            block_layers.append(
                P4MDenseBlock2D(in_channels=n_channels,
                                out_channels=grow_ch,
                                kernel_size=kernel_size,
                                stride=stride,
                                padding=padding,
                                wscale=wscale))
            n_channels = n_channels + grow_ch
            trans_layers.append(
                P4MTransitionBlock2D(in_channels=n_channels,
                                     out_channels=n_channels,
                                     stride=stride,
                                     padding=padding,
                                     wscale=wscale))
        # Add last layer to DenseBlock
        block_layers.append(
            P4MDenseBlock2D(in_channels=n_channels,
                            out_channels=grow_ch,
                            kernel_size=kernel_size,
                            stride=stride,
                            padding=padding,
                            wscale=wscale))
        n_channels = n_channels + grow_ch
        # Add layers to self
        self.block_layers = nn.Sequential(*block_layers)
        self.trans_layers = nn.Sequential(*trans_layers)

        # Add BN for final DenseBlock
        self.bn_out = nn.BatchNorm3d(num_features=n_channels, eps=eps)
        # Reduce to 2 channels (IMPORTANT! THIS IS DIFFERENT TO VEELING ET. AL. (2018), They use only one output channel with sigmoid)
        self.c_out = e2_layers.ConvGG(N_in=n_channels,
                                      N_out=2,
                                      kernel_size=1,
                                      h_grid=self.h_grid,
                                      input_h_grid=self.h_grid,
                                      stride=stride,
                                      padding=padding,
                                      wscale=wscale)
        # Define last pooling layer
        self.pooling = e2_layers.average_pooling_Rn
Exemple #8
0
    def __init__(self, in_channels, out_channels, kernel_size=3, fiber_map='id', stride=1, padding=1, wscale=1.0):
        super(fA_P4MResBlock2D, self).__init__()

        # Asserts
        assert kernel_size % 2 == 1
        if not padding == (kernel_size - 1) // 2:
            raise NotImplementedError()

        # Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'E2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        e2_layers = attgconv.layers(group)  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        n_grid = 8
        self.h_grid = e2_layers.H.grid_global(n_grid)  # 2*p4
        # ----------------------
        # Parameters of the model
        eps = 2e-5
        # ----------------------
        # Parameters of attention
        #ch_ratio = 16
        sp_kernel_size = 7
        sp_padding = (sp_kernel_size // 2)
        # --------------------------------------------------------

        from attgconv.attention_layers import fChannelAttention as ch_RnG
        from attgconv.attention_layers import fChannelAttentionGG  # as ch_GG
        from attgconv.attention_layers import fSpatialAttention  # as sp_RnG
        from attgconv.attention_layers import fSpatialAttentionGG

        ch_GG = functools.partial(fChannelAttentionGG, N_h_in=n_grid, group=group_name)
        sp_RnG = functools.partial(fSpatialAttention, wscale=wscale)
        sp_GG = functools.partial(fSpatialAttentionGG, group=group, input_h_grid=self.h_grid, wscale=wscale)

        if stride != 1:
            self.really_equivariant = True
            self.pooling = e2_layers.max_pooling_Rn
        else:
            self.really_equivariant = False

        self.bn1 = nn.BatchNorm3d(num_features=in_channels, eps=eps)
        self.c1 = e2_layers.fAttConvGG(N_in=in_channels , N_out=out_channels, kernel_size=kernel_size, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=stride, padding=padding, wscale=wscale,
                                       channel_attention=ch_GG(N_in=in_channels, ratio=in_channels // 2),
                                       spatial_attention=sp_GG(kernel_size=sp_kernel_size)
                                       )
        if self.really_equivariant:
            self.c1 = e2_layers.fAttConvGG(N_in=in_channels, N_out=out_channels, kernel_size=kernel_size, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1, padding=padding, wscale=wscale,
                                           channel_attention=ch_GG(N_in=in_channels, ratio=in_channels // 2),
                                           spatial_attention=sp_GG(kernel_size=sp_kernel_size)
                                           )

        self.bn2 = nn.BatchNorm3d(num_features=out_channels, eps=eps)
        self.c2 = e2_layers.fAttConvGG(N_in=out_channels, N_out=out_channels, kernel_size=kernel_size, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1     , padding=padding, wscale=wscale,
                                       channel_attention=ch_GG(N_in=out_channels, ratio=out_channels // 2),
                                       spatial_attention=sp_GG(kernel_size=sp_kernel_size)
                                       )
        if fiber_map == 'id':
            if not in_channels == out_channels:
                raise ValueError('fiber_map cannot be identity when channel dimension is changed.')
            self.fiber_map = nn.Sequential() # Identity
        elif fiber_map == 'zero_pad':
            raise NotImplementedError()
        elif fiber_map == 'linear':
            self.fiber_map = e2_layers.fAttConvGG(N_in=in_channels, N_out=out_channels, kernel_size=1, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=stride, padding=0, wscale=wscale,
                                              channel_attention=ch_GG(N_in=in_channels, ratio=in_channels // 2),
                                              spatial_attention=sp_GG(kernel_size=sp_kernel_size)
                                              )
            if self.really_equivariant:
                self.fiber_map = e2_layers.fAttConvGG(N_in=in_channels, N_out=out_channels, kernel_size=1, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1, padding=0, wscale=wscale,
                                                      channel_attention=ch_GG(N_in=in_channels, ratio=in_channels // 2),
                                                      spatial_attention=sp_GG(kernel_size=sp_kernel_size)
                                                      )
        else:
            raise ValueError('Unknown fiber_map: ' + str(type))
Exemple #9
0
    def __init__(self, num_blocks=7, nc32=11, nc16=23, nc8=45):
        """
        :param num_blocks: the number of resnet blocks per stage. There are 3 stages, for feature map width 32, 16, 8.
        Total number of layers is 6 * num_blocks + 2
        :param nc32: the number of feature maps in the first stage (where feature maps are 32x32)
        :param nc16: the number of feature maps in the second stage (where feature maps are 16x16)
        :param nc8: the number of feature maps in the third stage (where feature maps are 8x8)
        """
        super(fA_P4MResNet, self).__init__()

        #Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'E2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        e2_layers = attgconv.layers(group)  # The layers is instantiated with the group structure as input
        # Create H grid for p4m group
        n_grid = 8
        h_grid = e2_layers.H.grid_global(n_grid)
        # ----------------------
        # Parameters of the model
        stride = 1
        padding = 1
        kernel_size = 3
        eps = 2e-5
        # --------------------------------------------------------
        # Store in self
        self.group_name = group_name
        self.group = group
        self.layers = e2_layers
        self.n_grid = n_grid
        self.h_grid = h_grid
        # ----------------------
        # Initialization parameters
        wscale = sqrt(2.)  # This makes the initialization equal to that of He et al.
        # ----------------------
        # Parameters of attention
        ch_ratio = 16
        sp_kernel_size = 7
        sp_padding = (sp_kernel_size // 2)

        from attgconv.attention_layers import fChannelAttention as ch_RnG
        from attgconv.attention_layers import fChannelAttentionGG  # as ch_GG
        from attgconv.attention_layers import fSpatialAttention  # as sp_RnG
        from attgconv.attention_layers import fSpatialAttentionGG

        ch_GG = functools.partial(fChannelAttentionGG, N_h_in=n_grid, group=group_name)
        sp_RnG = functools.partial(fSpatialAttention, wscale=wscale)
        sp_GG = functools.partial(fSpatialAttentionGG, group=group, input_h_grid=self.h_grid, wscale=wscale)

        # Pooling layer
        self.avg_pooling = e2_layers.average_pooling_Rn

        # The first layer is always a convolution.
        self.c1 = e2_layers.fAttConvRnG(N_in=3, N_out=nc32, kernel_size=kernel_size, h_grid=self.h_grid, stride=stride, padding=padding, wscale=wscale,
                                    channel_attention=ch_RnG(N_in=3, ratio=1),
                                    spatial_attention=sp_RnG(group=group, kernel_size=sp_kernel_size, h_grid=self.h_grid)
                                    )
        # Add num_blocks ResBlocks (2 * num_blocks layers) for the size 32x32 feature maps
        layers_nc32 = []
        for i in range(num_blocks):
            layers_nc32.append(fA_P4MResBlock2D(in_channels=nc32, out_channels=nc32, kernel_size=kernel_size, fiber_map='id', stride=stride, padding=padding, wscale=wscale))
        self.layers_nc32 = nn.Sequential(*layers_nc32)

        # Add num_blocks ResBlocks (2 * num_blocks layers) for the size 16x16 feature maps
        # The first convolution uses stride 2
        layers_nc16 = []
        for i in range(num_blocks):
            stride_block = 1 if i > 0 else 2
            fiber_map = 'id' if i > 0 else 'linear'
            nc_in = nc16 if i > 0 else nc32
            layers_nc16.append(fA_P4MResBlock2D(in_channels=nc_in, out_channels=nc16, kernel_size=kernel_size, fiber_map=fiber_map, stride=stride_block, padding=padding, wscale=wscale))
        self.layers_nc16 = nn.Sequential(*layers_nc16)

        # Add num_blocks ResBlocks (2 * num_blocks layers) for the size 8x8 feature maps
        # The first convolution uses stride 2
        layers_nc8 = []
        for i in range(num_blocks):
            stride_block = 1 if i > 0 else 2
            fiber_map = 'id' if i > 0 else 'linear'
            nc_in = nc8 if i > 0 else nc16
            layers_nc8.append(fA_P4MResBlock2D(in_channels=nc_in, out_channels=nc8, kernel_size=kernel_size, fiber_map=fiber_map, stride=stride_block, padding=padding,  wscale=wscale))
        self.layers_nc8 = nn.Sequential(*layers_nc8)

        # Add BN and final layer
        # We do ReLU and average pooling between BN and final layer,
        # but since these are stateless they don't require a Link.
        self.bn_out = nn.BatchNorm3d(num_features=nc8, eps=eps)
        self.c_out = e2_layers.fAttConvGG(N_in=nc8, N_out=10, kernel_size=1, h_grid=self.h_grid, input_h_grid=self.h_grid, stride=1, padding=0, wscale=wscale,
                                          channel_attention=ch_GG(N_in=nc8, ratio=nc8 // 2),
                                          spatial_attention=sp_GG(kernel_size=sp_kernel_size)
                                          )
Exemple #10
0
    def __init__(self, use_bias=False):
        super(P4CNN, self).__init__()

        #Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'SE2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        se2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        self.h_grid = se2_layers.H.grid_global(4)

        # Parameters of the model
        p = 0.3
        stride = 1
        padding = 0
        kernel_size = 3
        N_channels = 10
        eps = 2e-5

        # Conv Layers
        self.c1 = se2_layers.ConvRnG(N_in=1,
                                     N_out=N_channels,
                                     kernel_size=kernel_size,
                                     h_grid=self.h_grid,
                                     stride=stride,
                                     padding=padding)
        self.c2 = se2_layers.ConvGG(N_in=N_channels,
                                    N_out=N_channels,
                                    kernel_size=kernel_size,
                                    h_grid=self.h_grid,
                                    input_h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding)
        self.c3 = se2_layers.ConvGG(N_in=N_channels,
                                    N_out=N_channels,
                                    kernel_size=kernel_size,
                                    h_grid=self.h_grid,
                                    input_h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding)
        self.c4 = se2_layers.ConvGG(N_in=N_channels,
                                    N_out=N_channels,
                                    kernel_size=kernel_size,
                                    h_grid=self.h_grid,
                                    input_h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding)
        self.c5 = se2_layers.ConvGG(N_in=N_channels,
                                    N_out=N_channels,
                                    kernel_size=kernel_size,
                                    h_grid=self.h_grid,
                                    input_h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding)
        self.c6 = se2_layers.ConvGG(N_in=N_channels,
                                    N_out=N_channels,
                                    kernel_size=kernel_size,
                                    h_grid=self.h_grid,
                                    input_h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding)
        self.c7 = se2_layers.ConvGG(N_in=N_channels,
                                    N_out=10,
                                    kernel_size=4,
                                    h_grid=self.h_grid,
                                    input_h_grid=self.h_grid,
                                    stride=stride,
                                    padding=padding)
        # Dropout
        self.dp = nn.Dropout(p)
        # Batch Normalization
        self.bn1 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn2 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn3 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn4 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn5 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn6 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        # Max Pooling
        self.max_pooling = se2_layers.max_pooling_Rn
Exemple #11
0
    def __init__(self, use_bias=False, attention=False):
        super(A_Sp_P4CNN, self).__init__()

        #Parameters of the group

        # Import the group structure
        import importlib
        group_name = 'SE2'
        group = importlib.import_module('attgconv.group.' + group_name)
        # Import the gsplintes package and the layers
        import attgconv
        se2_layers = attgconv.layers(
            group
        )  # The layers is instantiated with the group structure as input
        # Create H grid for p4 group
        n_grid = 4
        self.h_grid = se2_layers.H.grid_global(n_grid)

        # Parameters of the model
        p = 0.3
        stride = 1
        padding = 0
        kernel_size = 3
        N_channels = 10
        eps = 2e-5

        # Parameters of attention
        ch_ratio = self.ch_ratio  #(N_channels // 2)    # Hidden layer consists of 2 neurons
        sp_kernel_size = self.sp_kernel_size

        from attgconv.attention_layers import ChannelAttention as ch_RnG
        from attgconv.attention_layers import ChannelAttentionGG  #as ch_GG
        from attgconv.attention_layers import SpatialAttention  #as sp_RnG
        from attgconv.attention_layers import SpatialAttentionGG

        ch_GG = functools.partial(ChannelAttentionGG,
                                  N_h=n_grid,
                                  N_h_in=n_grid)
        sp_RnG = functools.partial(SpatialAttention,
                                   group=group,
                                   h_grid=self.h_grid,
                                   stride=stride)
        sp_GG = functools.partial(SpatialAttentionGG,
                                  group=group,
                                  h_grid=self.h_grid,
                                  input_h_grid=self.h_grid,
                                  stride=stride)

        self.c1 = se2_layers.AttConvRnG(
            N_in=1,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_RnG(N_out=N_channels, N_in=1        , ratio=1),
            spatial_attention=sp_RnG(N_out=N_channels,
                                     N_in=1,
                                     kernel_size=sp_kernel_size))
        self.c2 = se2_layers.AttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_GG(N_out= N_channels, N_in=N_channels, ratio=ch_ratio),
            spatial_attention=sp_GG(N_out=N_channels,
                                    N_in=N_channels,
                                    kernel_size=sp_kernel_size))
        self.c3 = se2_layers.AttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_GG(N_out=N_channels, N_in=N_channels, ratio=ch_ratio),
            spatial_attention=sp_GG(N_out=N_channels,
                                    N_in=N_channels,
                                    kernel_size=sp_kernel_size))
        self.c4 = se2_layers.AttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_GG(N_out=N_channels, N_in=N_channels, ratio=ch_ratio),
            spatial_attention=sp_GG(N_out=N_channels,
                                    N_in=N_channels,
                                    kernel_size=sp_kernel_size))
        self.c5 = se2_layers.AttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_GG(N_out=N_channels, N_in=N_channels, ratio=ch_ratio),
            spatial_attention=sp_GG(N_out=N_channels,
                                    N_in=N_channels,
                                    kernel_size=sp_kernel_size))
        self.c6 = se2_layers.AttConvGG(
            N_in=N_channels,
            N_out=N_channels,
            kernel_size=kernel_size,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_GG(N_out=N_channels, N_in=N_channels, ratio=ch_ratio),
            spatial_attention=sp_GG(N_out=N_channels,
                                    N_in=N_channels,
                                    kernel_size=sp_kernel_size))
        self.c7 = se2_layers.AttConvGG(
            N_in=N_channels,
            N_out=10,
            kernel_size=4,
            h_grid=self.h_grid,
            input_h_grid=self.h_grid,
            stride=stride,
            padding=padding,
            #channel_attention=ch_GG(N_out=10        , N_in=N_channels, ratio=ch_ratio),
            spatial_attention=sp_GG(N_out=10,
                                    N_in=N_channels,
                                    kernel_size=sp_kernel_size))
        # Dropout
        self.dp = nn.Dropout(p)
        # Batch Normalization
        self.bn1 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn2 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn3 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn4 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn5 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        self.bn6 = nn.BatchNorm3d(num_features=N_channels, eps=eps)
        # Max Pooling
        self.max_pooling = se2_layers.max_pooling_Rn