Exemple #1
0
    def __init__(self):
        import sparseconvnet as scn
        super(SCNet, self).__init__()

        self.input_tensor = scn.InputLayer(dimension=2,
                                           spatial_size=(3600, 3600))
        self.elu = scn.ELU()
        self.conv1 = scn.SubmanifoldConvolution(dimension=2,
                                                nIn=1,
                                                nOut=10,
                                                filter_size=5,
                                                bias=False)
        self.conv2 = scn.SubmanifoldConvolution(dimension=2,
                                                nIn=10,
                                                nOut=64,
                                                filter_size=5,
                                                bias=False)
        self.conv3 = scn.SubmanifoldConvolution(dimension=2,
                                                nIn=64,
                                                nOut=128,
                                                filter_size=5,
                                                bias=False)
        self.conv4 = scn.SubmanifoldConvolution(dimension=2,
                                                nIn=128,
                                                nOut=256,
                                                filter_size=5,
                                                bias=False)
        self.maxp = scn.MaxPooling(dimension=2, pool_size=5, pool_stride=5)
        self.maxp2 = scn.MaxPooling(dimension=2, pool_size=4, pool_stride=4)

        N = 256
        self.sparse_to_dense = scn.SparseToDense(dimension=2, nPlanes=N)
        self.fc1 = nn.Linear(N * 9 * 9, 32)
        self.fc2 = nn.Linear(32, 5)
Exemple #2
0
 def __init__(self):
     nn.Module.__init__(self)
     self.stage1 = scn.Sequential().add(
         scn.ValidConvolution(3, 1, 16, 3, False))
     self.stage1.add(scn.MaxPooling(3, 2, 2))
     res(self.stage1, 3, 16, 64)
     self.stage1.add(scn.MaxPooling(3, 2, 2))
     self.stage2 = UNet6(3, nClasses)
     self.densePred = scn.SparseToDense(3, nClasses)
Exemple #3
0
 def __init__(self, sgc_config):
     nn.Module.__init__(self)
     self.stage1 = scn.Sequential().add(
        scn.ValidConvolution(3, 1, 16, 3, False))
     self.stage1_2 = scn.MaxPooling(3, 2, 2)
     self.stage2 = scn.Sequential()
     res(self.stage2, 3, 16, 64)
     self.stage2_2 = scn.MaxPooling(3, 2, 2)
     self.stage3 = UNet6(3, nClasses, sgc_config=sgc_config)
     self.densePred = scn.SparseToDense(3, nClasses)
     self.sgc_config = sgc_config
Exemple #4
0
 def __init__(self, n_classes):
     nn.Module.__init__(self)
     self.n_classes = n_classes
     self.sparseModel = scn.Sequential(
         # 255x255
         scn.SubmanifoldConvolution(
             2, 2, 16, 3, False),  # dimension, nIn, nOut, filter_size, bias
         scn.MaxPooling(2, 3, 2),  # dimension, pool_size, pool_stride
         # 127x127
         scn.SparseResNet(
             2,
             16,
             [  # dimension, nInputPlanes, layers
                 ['b', 16, 2, 1],  # 63x63  # blockType, n, reps, stride
                 ['b', 32, 2, 2],  # 63x63
                 ['b', 48, 2, 2],  # 31x31
                 ['b', 96, 2, 2],  # 15x15 
                 ['b', 144, 2, 2],  # 7x7
                 ['b', 192, 2, 2]
             ]),  # 3x3
         scn.Convolution(
             2, 192, 256, 3, 1, False
         ),  # 1x1 # dimension, nIn, nOut, filter_size, filter_stride, bias
         scn.BatchNormReLU(256))  # dimension, nPlanes
     self.sparse_to_dense = scn.SparseToDense(2, 256)
     #self.spatial_size= self.sc.input_spatial_size(torch.LongTensor([1, 1]))
     self.spatial_size = self.sparseModel.input_spatial_size(
         torch.LongTensor([1, 1]))
     self.inputLayer = scn.InputLayer(2, self.spatial_size,
                                      2)  # dimension, spatial_size, mode
     self.linear = nn.Linear(256, self.n_classes)
     print(self.spatial_size)
    def test_fb_maxpool(self):
        dimension = 2
        pool_size = 3
        pool_stride = 2

        batch_input = np.asarray([[0, 0, 0, 0, 0],
                                  [0, 1, 0, 2, 0],
                                  [0, 0, 0, 2, 0],
                                  [0, 0, 0, 2, 8],
                                  [0, 0, 0, 0, 0]]).astype(float)
        indices = batch_input.copy().nonzero()
        locations = np.concatenate([indices[0][:, None], indices[1][:, None]], axis=1)
        batch_input = batch_input[:, :, np.newaxis].copy()

        spatial_size = torch.LongTensor(batch_input.shape[:dimension])
        select_indices = tuple(locations.T)
        features = batch_input[select_indices]

        input_layer = scn.InputLayer(dimension, spatial_size, mode=3)
        # Facebook implementation applies only valid padding
        max_pool_layer = scn.MaxPooling(dimension, pool_size, pool_stride)

        output_layer = scn.SparseToDense(dimension=dimension, nPlanes=1)

        x = input_layer([torch.LongTensor(locations), torch.FloatTensor(features)])
        x = max_pool_layer(x)
        x = output_layer(x)

        correct_output = np.asarray([[1, 2],
                                     [0, 8]]).astype(float)
        self.assertListEqual(correct_output.tolist(), torch.squeeze(x).data.cpu().tolist())
Exemple #6
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential(
         scn.SubmanifoldConvolution(
             3, 1, 64, 7,
             False),  # sscn(dimension, nIn, nOut, filter_size, bias)
         scn.BatchNormReLU(64),
         scn.MaxPooling(3, 3,
                        2),  # MaxPooling(dimension, pool_size, pool_stride)
         scn.SparseResNet(
             3,
             64,
             [  # SpraseResNet(dimension, nInputPlanes, layers=[])
                 ['b', 64, 2, 1],  # [block_type, nOut, rep, stride]
                 ['b', 64, 2, 1],
                 ['b', 128, 2, 2],
                 ['b', 128, 2, 2],
                 ['b', 256, 2, 2],
                 ['b', 256, 2, 2],
                 ['b', 512, 2, 2],
                 ['b', 512, 2, 2]
             ]),
         scn.SparseToDense(3, 256))
     self.avgpool = nn.AdaptiveAvgPool3d((1, 1, 1))
     # self.spatial_size= self.sparseModel.input_spatial_size(torch.LongTensor([1, 1]))
     self.spatial_size = torch.LongTensor([101, 101, 101])
     self.inputLayer = scn.InputLayer(3, self.spatial_size, mode=3)
 def __init__(self, flags):
     torch.nn.Module.__init__(self)
     import sparseconvnet as scn
     self._flags = flags
     dimension = self._flags.DATA_DIM
     num_class = self._flags.NUM_CLASS
     image_size = self._flags.SPATIAL_SIZE
     num_filter = self._flags.BASE_NUM_FILTERS
     assert image_size == 128
     net = scn.Sequential()
     net.add(scn.InputLayer(dimension, image_size, mode=3))
     net.add(scn.SubmanifoldConvolution(dimension, 1, num_filter, 3, False))
     net.add(scn.MaxPooling(dimension, 2, 2))
     net.add(
         SparseResNet(dimension, num_filter,
                      [[num_filter * 1, 2, 1], [num_filter * 2, 2, 2],
                       [num_filter * 4, 2, 2], [num_filter * 8, 2, 2]]))
     net.add(
         scn.Convolution(dimension, num_filter * 8, num_filter * 16, 3, 1,
                         False))
     net.add(scn.BatchNormReLU(num_filter * 16))
     net.add(scn.SparseToDense(dimension, num_filter * 16))
     net.add(torch.nn.AvgPool3d(6))
     self._net = net
     self.linear = torch.nn.Linear(num_filter * 16, num_class)
Exemple #8
0
    def __init__(self, full_scale=127, use_normal=False):
        nn.Module.__init__(self)

        dimension = 3
        m = 32  # 16 or 32
        residual_blocks = True  #True or False
        block_reps = 2  #Conv block repetition factor: 1 or 2

        blocks = [['b', m * k, 2, 2] for k in [1, 2, 3, 4, 5]]
        self.num_final_channels = m * len(blocks)

        self.sparseModel = scn.Sequential().add(
            scn.InputLayer(dimension, full_scale, mode=4)).add(
                scn.SubmanifoldConvolution(
                    dimension, 3 + 3 * int(use_normal), m, 3,
                    False)).add(scn.MaxPooling(dimension, 3, 2)).add(
                        scn.SparseResNet(dimension, m, blocks)).add(
                            scn.BatchNormReLU(self.num_final_channels)).add(
                                scn.SparseToDense(dimension,
                                                  self.num_final_channels))

        self.num_labels = 20
        self.label_encoder = nn.Sequential(nn.Linear(self.num_labels, 64),
                                           nn.ReLU())
        #self.pred = nn.Linear(m * len(blocks), 1)
        self.pred = nn.Sequential(nn.Linear(self.num_final_channels + 64, 64),
                                  nn.ReLU(), nn.Linear(64, 1))
        return
Exemple #9
0
def createMaxLayers(dimension=2,
                    facebook_layer=True,
                    pool_size=3,
                    pool_stride=1,
                    padding_mode='valid'):
    """Creates max pooling layers"""
    asyn_max_layer = ascn_max.asynMaxPool(dimension=dimension,
                                          filter_size=pool_size,
                                          filter_stride=pool_stride,
                                          padding_mode=padding_mode)

    if facebook_layer:
        if padding_mode != 'valid':
            raise ValueError(
                'Expected padding mode valid for facebook implementation. Got mode: %s'
                % padding_mode)
        sparse_max_layer = scn.MaxPooling(dimension, pool_size, pool_stride)

        return sparse_max_layer, asyn_max_layer
    else:
        batch_asyn_max_layer = ascn_max.asynMaxPool(dimension=dimension,
                                                    filter_size=pool_size,
                                                    filter_stride=pool_stride,
                                                    padding_mode=padding_mode)
        return batch_asyn_max_layer, asyn_max_layer
Exemple #10
0
    def __init__(self,
                 dimension,
                 reps,
                 n_layers,
                 leakiness=0,
                 input_layer=None,
                 name='encoder',
                 device=None):
        super(Encoder, self).__init__()
        self.dimension = dimension
        self.reps = reps
        self.n_layers = n_layers
        self.leakiness = leakiness
        self.name = name
        self.device = device

        if input_layer != None:
            self.input_layer = scn.InputLayer(len(input_layer), input_layer)

        self.blocks = []
        self.block_names = {}
        n_in, n_out = 1, 1
        for i in range(len(n_layers)):
            block = scn.Sequential()
            # add reps Resnet blocks, where reps >= 1 and first block just ensures number of
            # input channels is correct
            for rep in range(reps):
                block.add(
                    resnet_block(dimension,
                                 n_in,
                                 n_out,
                                 1,
                                 leakiness,
                                 computation='submanifoldconvolution'))
                n_in = n_out
            n_out = n_layers[i][1]
            '''
            block.add(
                scn.BatchNormLeakyReLU(n_in, leakiness)
            )
            '''
            block.add(scn.LeakyReLU(leakiness))
            if len(n_layers[i]) == 2:
                block.add(scn.Convolution(dimension, n_in, n_out, 2, 2, False))
            elif len(n_layers[i]
                     ) == 3 and n_layers[i][2] == 'submanifoldconvolution':
                block.add(
                    scn.SubmanifoldConvolution(dimension, n_in, n_out, 2,
                                               False))
            elif len(n_layers[i]) == 3 and n_layers[i][2] == 'maxpool':
                block.add(scn.MaxPooling(dimension, 2, 2))
            elif len(n_layers[i]) == 3 and n_layers[i][2] == 'avgpool':
                block.add(scn.AveragePooling(dimension, 2, 2))
            block_name = get_block_name(name, dimension, reps, n_in, n_out,
                                        leakiness)
            n_in = n_out
            self.blocks.append(block)
            self.block_names[block_name] = len(self.blocks) - 1
        self.blocks = torch.nn.ModuleList(self.blocks)
Exemple #11
0
    def __init__(self):
        super(CNN, self).__init__()
        ###############################
        # Hardcoded settings
        ###############################
        self._dimension = 3
        reps = 2
        kernel_size = 2
        num_strides = 7
        init_num_features = 8
        nInputFeatures = 1
        spatial_size = 128  #padding the rest for 169 PMTs
        num_classes = 2  # good versus ghost

        nPlanes = [(2**i) * init_num_features for i in range(0, num_strides)
                   ]  # every layer double the number of features
        downsample = [kernel_size, 2]
        leakiness = 0

        #################################
        # Input layer
        #################################
        self.input = scn.Sequential().add(
            scn.InputLayer(self._dimension, spatial_size, mode=3)).add(
                scn.SubmanifoldConvolution(self._dimension, nInputFeatures,
                                           init_num_features, 3,
                                           False))  # Kernel size 3, no bias
        self.concat = scn.JoinTable()
        #################################
        # Encode layers
        #################################\
        self.encoding_conv = scn.Sequential()
        for i in range(num_strides):
            if i < 4:  #hardcoded
                self.encoding_conv.add(
                    scn.BatchNormLeakyReLU(
                        nPlanes[i], leakiness=leakiness)).add(
                            scn.Convolution(self._dimension, nPlanes[i],
                                            nPlanes[i + 1], downsample[0],
                                            downsample[1], False))
            elif i < num_strides - 1:
                self.encoding_conv.add(scn.MaxPooling(self._dimension, 2, 2))

        self.output = scn.Sequential().add(
            scn.SparseToDense(self._dimension, nPlanes[-1]))
        ###################################
        # Final linear layer
        ###################################
        self.deepest_layer_num_features = int(
            nPlanes[-1] * np.power(spatial_size / (2**(num_strides - 1)), 3.))
        self.classifier = torch.nn.Sequential(
            torch.nn.ReLU(),
            torch.nn.Linear(self.deepest_layer_num_features, 2),
        )
Exemple #12
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
         scn.SubmanifoldConvolution(2, 3, 8, 3, False)).add(
             scn.MaxPooling(2, 3, 2)).add(
                 scn.SparseResNet(2, 8, [['b', 8, 2, 1], [
                     'b', 16, 2, 2
                 ], ['b', 24, 2, 2], ['b', 32, 2, 2]])).add(
                     scn.Convolution(2, 32, 64, 5, 1,
                                     False)).add(scn.BatchNormReLU(64)).add(
                                         scn.SparseToDense(2, 64))
     self.linear = nn.Linear(64, 183)
Exemple #13
0
 def __init__(self, num_classes=5):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(scn.DenseToSparse(2)).add(
         scn.ValidConvolution(2, 3, 8, 2, False)).add(
             scn.MaxPooling(2, 4, 2)).add(
                 scn.SparseResNet(2, 8, [['b', 8, 3, 1], [
                     'b', 16, 2, 2
                 ], ['b', 24, 2, 2], ['b', 32, 2, 2]])).add(
                     scn.Convolution(2, 32, 64, 4, 1,
                                     False)).add(scn.BatchNormReLU(64)).add(
                                         scn.SparseToDense(2, 64))
     self.linear = nn.Linear(6400, num_classes)
Exemple #14
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential(
         scn.SubmanifoldConvolution(2, 3, 16, 3, False),
         scn.MaxPooling(2, 3, 2),
         scn.SparseResNet(2, 16, [['b', 16, 2, 1], ['b', 32, 2, 2],
                                  ['b', 48, 2, 2], ['b', 96, 2, 2]]),
         scn.Convolution(2, 96, 128, 3, 1, False), scn.BatchNormReLU(128),
         scn.SparseToDense(2, 128))
     self.spatial_size = self.sparseModel.input_spatial_size(
         torch.LongTensor([1, 1]))
     self.inputLayer = scn.InputLayer(2, self.spatial_size, 2)
     self.linear = nn.Linear(128, 3755)
    def __init__(self):
        nn.Module.__init__(self)
        self.sparseModel = scn.Sequential(
            scn.SubmanifoldConvolution(2, 200, 16, 3, True),
            scn.MaxPooling(2, 3, 2),
            #             scn.SparseResNet(2, 8, [
            #                         ['b', 8, 2, 1],
            #                         ['b', 16, 2, 2],
            #                         ['b', 24, 2, 2],
            #                         ['b', 32, 2, 2]]),
            scn.Convolution(2, 16, 32, 5, 1, False),
            scn.MaxPooling(2, 3, 2),

            #             scn.BatchNormReLU(12),
            scn.SparseToDense(2, 32))
        self.spatial_size = self.sparseModel.input_spatial_size(
            torch.LongTensor([1, 1]))
        #         print(self.spatial_size)
        #         self.spatial_size=1500#torch.IntTensor(1500)
        #         print(self.spatial_size)
        self.inputLayer = scn.InputLayer(1, self.spatial_size, 2)
        #         self.linear = nn.Linear(16, 8)

        self.linear = nn.Linear(32, 2)
Exemple #16
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(dimension, (nvox, nvox, nvoxz), mode=3)).add(
             scn.SubmanifoldConvolution(
                 dimension, nPlanes, 4, 3,
                 False)).add(scn.MaxPooling(dimension, 3, 3)).add(
                     scn.SparseResNet(dimension, 4, [[
                         'b', 8, 2, 1
                     ], ['b', 16, 2, 1], ['b', 24, 2, 1]])).add(
                         #                        ['b', 32, 2, 1]])).add(
                         scn.Convolution(
                             dimension, 24, 32, 5, 1,
                             False)).add(scn.BatchNormReLU(32)).add(
                                 scn.SparseToDense(dimension, 32))
     #        self.spatial_size = self.sparseModel.input_spatial_size(torch.LongTensor([1, 1]))
     self.linear = nn.Linear(int(32 * 46 * 46 * 96), 32)
     self.linear2 = nn.Linear(32, global_Nclass)
Exemple #17
0
def get_down_maxpooling(
        num_dims, sparse, input_channels, output_channels=None, *, stride=2):
    if isinstance(stride, int):
        stride = np.full(num_dims, stride)
    else:
        assert len(stride) == num_dims
    stride_tuple = tuple(stride)

    assert output_channels is None or output_channels == input_channels

    if sparse:
        layer = scn.MaxPooling(
            num_dims, pool_size=stride_tuple, pool_stride=stride_tuple)
    else:
        maxpool_class = get_dense_maxpool_class(num_dims)
        layer = maxpool_class(
            kernel_size=stride_tuple, stride=stride_tuple, padding=0)

    return sparse, stride, input_channels, layer
Exemple #18
0
    def __init__(self):
        nn.Module.__init__(self)
        self.sparseModel = scn.Sequential(
            scn.SubmanifoldConvolution(2, 1, 8, 3, False),
            scn.MaxPooling(2, 3, 2),
            scn.SparseResNet(2, 8, [['b', 8, 2, 1], ['b', 16, 2, 2],
                                    ['b', 24, 2, 2], ['b', 32, 2, 2]]),
            scn.Convolution(2, 32, 64, 5, 1, False), scn.BatchNormReLU(64),
            scn.SparseToDense(2, 64))
        # self.spatial_size= self.sparseModel.input_spatial_size(torch.LongTensor([1, 1]))
        self.spatial_size = torch.LongTensor([28, 28])
        self.inputLayer = scn.InputLayer(2, self.spatial_size, 2)
        self.linear = nn.Linear(64, 183)

        self.sscn1 = scn.SubmanifoldConvolution(2, 1, 32, 3, False)
        self.sscn2 = scn.SubmanifoldConvolution(2, 32, 64, 3, False)
        self.dropout1 = nn.Dropout2d(0.25)
        self.dropout2 = nn.Dropout2d(0.5)
        # self.fc1 = nn.Linear(9216, 128)
        self.fc1 = nn.Linear(12544, 128)
        self.fc2 = nn.Linear(128, 10)
Exemple #19
0
 def __init__(self, dimension=3, device='cuda'):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential(
         scn.SubmanifoldConvolution(dimension,
                                    nIn=1,
                                    nOut=8,
                                    filter_size=3,
                                    bias=False),
         scn.MaxPooling(dimension, pool_size=3, pool_stride=2),
         scn.SparseResNet(dimension, 8, [['b', 8, 2, 1], ['b', 16, 2, 2],
                                         ['b', 24, 2, 2], ['b', 32, 2, 2]]),
         scn.Convolution(dimension,
                         nIn=32,
                         nOut=64,
                         filter_size=5,
                         filter_stride=1,
                         bias=False), scn.BatchNormReLU(64),
         scn.SparseToDense(dimension, 64)).to(device)
     self.spatial_size = self.sparseModel.input_spatial_size(
         torch.LongTensor([1] * dimension))
     self.inputLayer = scn.InputLayer(dimension, self.spatial_size, mode=4)
     self.linear = nn.Linear(64, 1).to(device)
Exemple #20
0
def MaxPool2D(pool_size, pool_stride=None, dimension=2, nFeaturesToDrop=0):
    if pool_stride is None: pool_stride = pool_size
    return scn.MaxPooling(dimension, pool_size, pool_stride, nFeaturesToDrop)
Exemple #21
0
    def __init__(self, in_channel, n_classes, batchnorm=True, droput=0.0):
        self.in_channel = in_channel
        self.n_classes = n_classes
        super(UNet3D, self).__init__()

        self.conv1_1 = self.encoder(in_channel,
                                    32,
                                    bias=False,
                                    batchnorm=batchnorm)
        self.conv1_2 = self.encoder(32, 64, bias=False, batchnorm=batchnorm)
        self.pool1 = scn.MaxPooling(3, 2, 2)

        self.conv2_1 = self.encoder(64, 64, bias=False, batchnorm=batchnorm)
        self.conv2_2 = self.encoder(64, 128, bias=False, batchnorm=batchnorm)
        self.pool2 = scn.MaxPooling(3, 2, 2)

        self.conv3_1 = self.encoder(128,
                                    128,
                                    bias=False,
                                    filter_stride=1,
                                    filter_size=3,
                                    batchnorm=batchnorm)
        self.conv3_2 = self.encoder(128,
                                    256,
                                    bias=False,
                                    filter_stride=1,
                                    filter_size=3,
                                    batchnorm=batchnorm)
        self.pool3 = scn.MaxPooling(3, 2, 2)

        self.conv4_1 = self.encoder(256, 256, bias=False, batchnorm=batchnorm)
        self.conv4_2 = self.encoder(256, 512, bias=False, batchnorm=batchnorm)

        self.up5_1 = self.decoder(512,
                                  512,
                                  filter_size=2,
                                  filter_stride=2,
                                  bias=False)
        self.up5_2 = scn.JoinTable()
        self.conv5_1 = self.encoder(256 + 512,
                                    256,
                                    bias=False,
                                    batchnorm=batchnorm)
        self.conv5_2 = self.encoder(256, 256, bias=False, batchnorm=batchnorm)

        self.up6_1 = self.decoder(256,
                                  256,
                                  filter_size=2,
                                  filter_stride=2,
                                  bias=False)
        self.up6_2 = scn.JoinTable()
        self.conv6_1 = self.encoder(128 + 256, 128, bias=False)
        self.conv6_2 = self.encoder(128, 128, bias=False)

        self.up7_1 = self.decoder(128,
                                  128,
                                  filter_size=2,
                                  filter_stride=2,
                                  bias=False)
        self.up7_2 = scn.JoinTable()
        self.conv7_1 = self.encoder(64 + 128,
                                    64,
                                    bias=False,
                                    batchnorm=batchnorm)
        self.conv7_2 = self.encoder(64, 64, bias=False, batchnorm=batchnorm)

        self.conv8 = self.encoder(64,
                                  n_classes,
                                  filter_size=1,
                                  bias=False,
                                  batchnorm=False)
        self.act = scn.Sigmoid()

        self.log_level = 0
def SparseVggNet(dimension, nInputPlanes, layers):
    """
    VGG style nets
    Use submanifold convolutions
    Also implements 'Plus'-augmented nets
    """
    nPlanes = nInputPlanes
    m = scn.Sequential()
    for x in layers:
        if x == 'MP':
            m.add(scn.MaxPooling(dimension, 3, 2))
        elif x[0] == 'MP':
            m.add(scn.MaxPooling(dimension, x[1], x[2]))
        elif x == 'C3/2':
            m.add(scn.Convolution(dimension, nPlanes, nPlanes, 3, 2, False))
            m.add(scn.BatchNormReLU(nPlanes))
        elif x[0] == 'C3/2':
            m.add(scn.Convolution(dimension, nPlanes, x[1], 3, 2, False))
            nPlanes = x[1]
            m.add(scn.BatchNormReLU(nPlanes))
        elif x[0] == 'C' and len(x) == 2:
            m.add(
                scn.SubmanifoldConvolution(dimension, nPlanes, x[1], 3, False))
            nPlanes = x[1]
            m.add(scn.BatchNormReLU(nPlanes))
        elif x[0] == 'C' and len(x) == 3:
            m.add(scn.ConcatTable().add(
                scn.SubmanifoldConvolution(
                    dimension, nPlanes, x[1], 3,
                    False)).add(scn.Sequential().add(
                        scn.Convolution(
                            dimension, nPlanes, x[2], 3, 2,
                            False)).add(scn.BatchNormReLU(x[2])).add(
                                scn.SubmanifoldConvolution(
                                    dimension, x[2], x[2], 3,
                                    False)).add(scn.BatchNormReLU(x[2])).add(
                                        scn.Deconvolution(
                                            dimension, x[2], x[2], 3, 2,
                                            False)))).add(scn.JoinTable())
            nPlanes = x[1] + x[2]
            m.add(scn.BatchNormReLU(nPlanes))
        elif x[0] == 'C' and len(x) == 4:
            m.add(scn.ConcatTable().add(
                scn.SubmanifoldConvolution(
                    dimension, nPlanes, x[1], 3,
                    False)).add(scn.Sequential().add(
                        scn.Convolution(
                            dimension, nPlanes, x[2], 3, 2,
                            False)).add(scn.BatchNormReLU(x[2])).add(
                                scn.SubmanifoldConvolution(
                                    dimension, x[2], x[2], 3,
                                    False)).add(scn.BatchNormReLU(x[2])).add(
                                        scn.Deconvolution(
                                            dimension, x[2], x[2], 3, 2,
                                            False))).
                  add(scn.Sequential().add(
                      scn.Convolution(
                          dimension, nPlanes, x[3],
                          3, 2, False)).add(scn.BatchNormReLU(x[3])).add(
                              scn.SubmanifoldConvolution(
                                  dimension, x[3], x[3], 3,
                                  False)).add(scn.BatchNormReLU(x[3])).add(
                                      scn.Convolution(dimension, x[3], x[3], 3,
                                                      2, False)).
                      add(scn.BatchNormReLU(x[3])).add(
                          scn.SubmanifoldConvolution(
                              dimension, x[3], x[3],
                              3, False)).add(scn.BatchNormReLU(x[3])).add(
                                  scn.Deconvolution(
                                      dimension, x[3], x[3], 3, 2,
                                      False)).add(scn.BatchNormReLU(x[3])).add(
                                          scn.SubmanifoldConvolution(
                                              dimension, x[3], x[3], 3,
                                              False)).add(
                                                  scn.BatchNormReLU(x[3])).add(
                                                      scn.Deconvolution(
                                                          dimension, x[3],
                                                          x[3], 3, 2,
                                                          False)))).add(
                                                              scn.JoinTable())
            nPlanes = x[1] + x[2] + x[3]
            m.add(scn.BatchNormReLU(nPlanes))
        elif x[0] == 'C' and len(x) == 5:
            m.add(scn.ConcatTable().add(
                scn.SubmanifoldConvolution(
                    dimension, nPlanes, x[1], 3,
                    False)).add(scn.Sequential().add(
                        scn.Convolution(
                            dimension, nPlanes, x[2], 3, 2,
                            False)).add(scn.BatchNormReLU(x[2])).add(
                                scn.SubmanifoldConvolution(
                                    dimension, x[2], x[2], 3,
                                    False)).add(scn.BatchNormReLU(x[2])).add(
                                        scn.Deconvolution(
                                            dimension, x[2], x[2], 3, 2,
                                            False))).
                  add(scn.Sequential().add(
                      scn.Convolution(
                          dimension, nPlanes, x[3],
                          3, 2, False)).add(scn.BatchNormReLU(x[3])).add(
                              scn.SubmanifoldConvolution(
                                  dimension, x[3], x[3], 3,
                                  False)).add(scn.BatchNormReLU(x[3])).add(
                                      scn.Convolution(dimension, x[3], x[3], 3,
                                                      2, False)).
                      add(scn.BatchNormReLU(x[3])).add(
                          scn.SubmanifoldConvolution(
                              dimension, x[3], x[3],
                              3, False)).add(scn.BatchNormReLU(x[3])).add(
                                  scn.Deconvolution(
                                      dimension, x[3], x[3], 3, 2,
                                      False)).add(scn.BatchNormReLU(x[3])).add(
                                          scn.SubmanifoldConvolution(
                                              dimension, x[3], x[3], 3,
                                              False)).add(
                                                  scn.BatchNormReLU(x[3])).add(
                                                      scn.Deconvolution(
                                                          dimension, x[3],
                                                          x[3], 3, 2, False))).
                  add(scn.Sequential().add(
                      scn.Convolution(
                          dimension, nPlanes,
                          x[4], 3, 2,
                          False)).add(scn.BatchNormReLU(x[4])).add(
                              scn.SubmanifoldConvolution(
                                  dimension, x[4], x[4], 3,
                                  False)).add(scn.BatchNormReLU(x[4])).add(
                                      scn.Convolution(
                                          dimension, x[4], x[4], 3,
                                          2, False)).add(
                                              scn.BatchNormReLU(x[4])).add(
                                                  scn.SubmanifoldConvolution(
                                                      dimension, x[4], x[4], 3,
                                                      False)).add(
                                                          scn.BatchNormReLU(
                                                              x[4])).
                      add(scn.Convolution(
                          dimension, x[4], x[4], 3, 2,
                          False)).add(scn.BatchNormReLU(x[4])).add(
                              scn.SubmanifoldConvolution(
                                  dimension, x[4], x[4], 3,
                                  False)).add(scn.BatchNormReLU(x[4])).add(
                                      scn.Deconvolution(
                                          dimension, x[4], x[4], 3,
                                          2, False)).add(
                                              scn.BatchNormReLU(x[4])).add(
                                                  scn.SubmanifoldConvolution(
                                                      dimension, x[4], x[4], 3,
                                                      False)).
                      add(scn.BatchNormReLU(x[4])).add(
                          scn.Deconvolution(
                              dimension, x[4], x[4], 3,
                              2, False)).add(scn.BatchNormReLU(x[4])).add(
                                  scn.SubmanifoldConvolution(
                                      dimension, x[4], x[4], 3,
                                      False)).add(scn.BatchNormReLU(x[4])).add(
                                          scn.Deconvolution(
                                              dimension, x[4], x[4], 3, 2,
                                              False)))).add(scn.JoinTable())
            nPlanes = x[1] + x[2] + x[3] + x[4]
            m.add(scn.BatchNormReLU(nPlanes))
    return m
Exemple #23
0
 def __init__(self, dimension=3, pool_size=2, pool_stride=2):
     super().__init__()
     self.pooling = scn.MaxPooling(dimension, pool_size, pool_stride)
     self.unpooling = scn.UnPooling(dimension, pool_size, pool_stride)
    def __init__(self,
                 spatial_size,
                 init_conv_nplanes,
                 init_conv_kernel,
                 kernel_sizes,
                 stride_sizes,
                 basic_num,
                 nlinear=32,
                 nclasses=2,
                 dim=3,
                 start_planes=1,
                 momentum=0.99):
        '''
        Parameters
        ----------
        spatial_size : tuple
            The spatial size of the input layer. Size of the tuple is also the dimension.
        init_conv_nplaness : int
            Number of planes we want after the initial SubmanifoldConvolution, that is, to begin downsampling.
        init_conv_kernel : int
            Kernel for the first convolutional layer.
        kernel_sizes : list
            Size of the kernels applied in each layer or block. Its length also indicates the level depth of the net.
            Last element corresponds just to the kernel of the basic block in the bottom of the net.
        stride_sizes : list
            Sizes of the kernels applied in each layer or block. Its length also indicates the level depth of the net.
            Last element corresponds just to the kernel of the basic block in the bottom of the net.
        basic_num : int
            Number of times a basic residual block is passed in each level.
        nclasses : int, optional
            Number of output classes for predictions. The default is 2.
        dim : int, optional
            Number of dimensions of the input. The default is 3.
        start_planes : int, optional
            Number of planes that enter the net. The default is 1.
        momentum : float, optional
            Momentum for BatchNormalization layer. The default is 0.99.
        '''
        torch.nn.Module.__init__(self)

        self.basic_num = basic_num
        self.level_depth = len(kernel_sizes)

        self.inp = scn.InputLayer(dim, spatial_size)
        self.convBN = ConvBNBlock(start_planes,
                                  init_conv_nplanes,
                                  init_conv_kernel,
                                  momentum=momentum)
        inplanes = init_conv_nplanes

        self.downsample = torch.nn.ModuleList([])
        self.basic = torch.nn.ModuleList(
            [torch.nn.ModuleList([]) for i in range(self.level_depth - 1)])
        self.bottom = torch.nn.ModuleList([])
        out_size = (calculate_output_dimension(spatial_size, kernel_sizes,
                                               stride_sizes))
        for i in range(self.level_depth - 1):
            for j in range(basic_num):
                self.basic[i].append(
                    ResidualBlock_basic(inplanes,
                                        kernel_sizes[i],
                                        momentum=momentum))

            self.downsample.append(
                ResidualBlock_downsample(inplanes,
                                         kernel_sizes[i],
                                         stride_sizes[i],
                                         momentum=momentum))

            inplanes = inplanes * 2

        for j in range(basic_num):
            self.bottom.append(
                ResidualBlock_basic(inplanes,
                                    kernel_sizes[-1],
                                    momentum=momentum))

        self.max = scn.MaxPooling(dim, out_size, 1)
        self.sparse = scn.SparseToDense(dim, inplanes)
        self.linear1 = torch.nn.Linear(inplanes, nlinear)
        self.linear2 = torch.nn.Linear(nlinear, nclasses)
        self.activation = torch.nn.ReLU()