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())
예제 #2
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
예제 #3
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)
예제 #4
0
 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)
예제 #5
0
 def __init__(self, nf_in, nf, input_sparsetensor, return_sparsetensor,
              max_data_size):
     nn.Module.__init__(self)
     data_dim = 3
     self.nf_in = nf_in
     self.nf = nf
     self.input_sparsetensor = input_sparsetensor
     self.return_sparsetensor = return_sparsetensor
     self.max_data_size = max_data_size
     if not self.input_sparsetensor:
         self.p0 = scn.InputLayer(data_dim, self.max_data_size, mode=0)
     self.p1 = scn.SubmanifoldConvolution(data_dim,
                                          nf_in,
                                          nf,
                                          filter_size=FSIZE0,
                                          bias=False)
     self.p2 = scn.Sequential()
     self.p2.add(scn.ConcatTable().add(scn.Identity()).add(
         scn.Sequential().add(scn.BatchNormReLU(nf)).add(
             scn.SubmanifoldConvolution(
                 data_dim, nf, nf, FSIZE0,
                 False)).add(scn.BatchNormReLU(nf)).add(
                     scn.SubmanifoldConvolution(data_dim, nf, nf, FSIZE0,
                                                False)))).add(
                                                    scn.AddTable())
     self.p2.add(scn.BatchNormReLU(nf))
     # downsample space by factor of 2
     self.p3 = scn.Sequential().add(
         scn.Convolution(data_dim, nf, nf, FSIZE1, 2, False))
     self.p3.add(scn.BatchNormReLU(nf))
     if not self.return_sparsetensor:
         self.p4 = scn.SparseToDense(data_dim, nf)
예제 #6
0
def get_sparse_to_dense(
        num_dims, sparse, input_channels, output_channels=None):
    assert sparse
    assert input_channels == output_channels or output_channels is None
    stride = np.full(num_dims, 1)
    layer = scn.SparseToDense(num_dims, input_channels)
    return False, stride, input_channels, layer
예제 #7
0
    def test_fb_sparse_convolution1d(self):
        dense_input = np.array([[[0, 0, 0, 1], [0, 0, 2, 4], [1, 0, 0, 0]]])
        kernel = np.array([[[0, -1, 2], [1, 1, -1], [0, 0, 0]]])
        locations_x, locations_y = np.squeeze(dense_input).nonzero()
        features = dense_input[0, locations_x, locations_y]
        spatial_size = torch.LongTensor([3, 4])
        input_layer = scn.InputLayer(2, spatial_size, mode=3)
        sparse_conv_layer = scn.SubmanifoldConvolution(dimension=2,
                                                       nIn=1,
                                                       nOut=1,
                                                       filter_size=3,
                                                       bias=False)
        sparse_conv_layer.weight.data = torch.squeeze(
            torch.tensor(kernel.flatten(), dtype=torch.float32))[:, None, None,
                                                                 None]
        output_layer = scn.SparseToDense(dimension=2, nPlanes=1)

        x = input_layer([
            torch.LongTensor(
                np.concatenate((locations_x[:, None], locations_y[:, None]),
                               axis=-1)),
            torch.FloatTensor(features)[:, None]
        ])
        x = sparse_conv_layer(x)
        x = output_layer(x)

        dense_output = np.array([[[0, 0, 0, 1], [0, 0, 0, 5], [1, 0, 0, 0]]])

        self.assertListEqual(dense_output.flatten().squeeze().tolist(),
                             torch.squeeze(x.flatten()).data.cpu().tolist())
예제 #8
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)
예제 #9
0
    def __init__(self, nr_classes):
        super(FBSparseVGGTest, self).__init__()
        self.sparseModel = scn.SparseVggNet(
            2,
            nInputPlanes=2,
            layers=[['C', 16], ['C', 16], 'MP', ['C', 32], ['C', 32], 'MP',
                    ['C', 64], ['C', 64], 'MP', ['C', 128], ['C', 128], 'MP',
                    ['C', 256], ['C', 256], 'MP', ['C', 512]]).add(
                        scn.Convolution(2,
                                        512,
                                        256,
                                        3,
                                        filter_stride=2,
                                        bias=False)).add(
                                            scn.BatchNormReLU(256)).add(
                                                scn.SparseToDense(2, 256))

        cnn_spatial_output_size = [2, 3]
        self.spatial_size = self.sparseModel.input_spatial_size(
            torch.LongTensor(cnn_spatial_output_size))
        self.inputLayer = scn.InputLayer(dimension=2,
                                         spatial_size=self.spatial_size,
                                         mode=2)
        self.linear_input_features = cnn_spatial_output_size[
            0] * cnn_spatial_output_size[1] * 256
        self.linear = nn.Linear(self.linear_input_features, nr_classes)
예제 #10
0
 def __init__(self, dimension, nPlanes,full_scale = 32,topk = 5,r = 0.5):
     super(DistMatchLayer_v4, self).__init__()
     self.dimension = dimension
     self.s2d = scn.SparseToDense(dimension, nPlanes)
     self.full_scale = full_scale
     self.topk = topk
     self.r = r
예제 #11
0
    def _make_transpose(self, transblock, planes, blocks, stride=1):

        upsample = None
        if stride != 1:
            upsample = scn.Sequential(
                scn.SparseToDense(2,self.inplanes * transblock.expansion),
                nn.ConvTranspose2d(self.inplanes * transblock.expansion, planes,
                                  kernel_size=2, stride=stride, padding=0, bias=False),
                scn.DenseToSparse(2),
                scn.BatchNormalization(planes)                
            )            
        elif self.inplanes * transblock.expansion != planes:
            upsample = scn.Sequential(
                scn.NetworkInNetwork(self.inplanes * transblock.expansion, planes, False),
                scn.BatchNormalization(planes)
            )

        layers = []
        
        for i in range(1, blocks):
            layers.append(transblock(self.inplanes, self.inplanes * transblock.expansion))

        layers.append(transblock(self.inplanes, planes, stride, upsample))
        self.inplanes = planes // transblock.expansion

        return scn.Sequential(*layers)
예제 #12
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)
예제 #13
0
    def evaluate_child(self, child, hyperparameters):
        '''Calculate validation signal for child using hyperparameters
        '''
        loss_fn = torch.nn.MSELoss()
        sparsifier = scn.InputLayer(len(self.input_space_size),
                                    self.input_space_size)
        densifier = scn.SparseToDense(len(self.latent_space_size),
                                      self.latent_space_channels)

        # move to gpu for eval
        child.to(self.device)

        total_loss = 0
        for (inputs, targets) in self.val:
            inputs = [[torch.from_numpy(t).to(self.device) for t in j]
                      for j in inputs]
            primary, secondary = inputs[0], inputs[1]
            primary[1] = primary[1].float().reshape(-1, 1)
            secondary[1] = secondary[1].float().reshape(-1, 1)

            predictions = child(primary, secondary)
            predictions = densifier(predictions)

            targets = [torch.from_numpy(t).to(self.device) for t in targets]
            targets[1] = targets[1].float().reshape(-1, 1)
            targets = sparsifier(targets)
            targets = densifier(targets)

            loss = loss_fn(predictions, targets)
            total_loss += loss.item()

        # take off gpu when finished
        child.to('cpu')
        return total_loss / len(self.val)
예제 #14
0
    def __init__(self,
                 output_shape,
                 use_norm=True,
                 num_input_features=128,
                 num_filters_down1=[64],
                 num_filters_down2=[64, 64],
                 name='SparseMiddleExtractor'):
        super(SparseMiddleExtractor, self).__init__()
        self.name = name
        if use_norm:
            BatchNorm1d = change_default_args(
                eps=1e-3, momentum=0.01)(nn.BatchNorm1d)
            Linear = change_default_args(bias=False)(nn.Linear)
        else:
            BatchNorm1d = Empty
            Linear = change_default_args(bias=True)(nn.Linear)
        sparse_shape = np.array(output_shape[1:4]) + [1, 0, 0]
        # sparse_shape[0] = 11
        print(sparse_shape)
        self.scn_input = scn.InputLayer(3, sparse_shape.tolist())
        self.voxel_output_shape = output_shape
        middle_layers = []

        num_filters = [num_input_features] + num_filters_down1
        # num_filters = [64] + num_filters_down1
        filters_pairs_d1 = [[num_filters[i], num_filters[i + 1]]
                            for i in range(len(num_filters) - 1)]

        for i, o in filters_pairs_d1:
            middle_layers.append(scn.SubmanifoldConvolution(3, i, o, 3, False))
            middle_layers.append(scn.BatchNormReLU(o, eps=1e-3, momentum=0.99))
        middle_layers.append(
            scn.Convolution(
                3,
                num_filters[-1],
                num_filters[-1], (3, 1, 1), (2, 1, 1),
                bias=False))
        middle_layers.append(
            scn.BatchNormReLU(num_filters[-1], eps=1e-3, momentum=0.99))
        # assert len(num_filters_down2) > 0
        if len(num_filters_down1) == 0:
            num_filters = [num_filters[-1]] + num_filters_down2
        else:
            num_filters = [num_filters_down1[-1]] + num_filters_down2
        filters_pairs_d2 = [[num_filters[i], num_filters[i + 1]]
                            for i in range(len(num_filters) - 1)]
        for i, o in filters_pairs_d2:
            middle_layers.append(scn.SubmanifoldConvolution(3, i, o, 3, False))
            middle_layers.append(scn.BatchNormReLU(o, eps=1e-3, momentum=0.99))
        middle_layers.append(
            scn.Convolution(
                3,
                num_filters[-1],
                num_filters[-1], (3, 1, 1), (2, 1, 1),
                bias=False))
        middle_layers.append(
            scn.BatchNormReLU(num_filters[-1], eps=1e-3, momentum=0.99))
        middle_layers.append(scn.SparseToDense(3, num_filters[-1]))
        self.middle_conv = Sequential(*middle_layers)
예제 #15
0
    def __init__(self, args):
        torch.nn.Module.__init__(self)
        # All of the parameters are controlled via the flags module

        # Create the sparse input tensor:
        self.input_tensor = scn.InputLayer(dimension=3, spatial_size=(512))

        # Here, define the layers we will need in the forward path:

        # The convolutional layers, which can be shared or not across planes,
        # are defined below

        # We apply an initial convolution, to each plane, to get n_inital_filters

        self.initial_convolution = scn.SubmanifoldConvolution(
            dimension=3,
            nIn=1,
            nOut=args.n_initial_filters,
            filter_size=5,
            bias=args.use_bias)

        n_filters = args.n_initial_filters
        # Next, build out the convolution steps

        self.convolutional_layers = torch.nn.ModuleList()
        for layer in range(args.network_depth):

            self.convolutional_layers.append(
                SparseBlockSeries(inplanes=n_filters,
                                  n_blocks=args.res_blocks_per_layer,
                                  bias=args.use_bias,
                                  batch_norm=args.batch_norm,
                                  residual=True))
            out_filters = 2 * n_filters

            self.convolutional_layers.append(
                SparseConvolutionDownsample(inplanes=n_filters,
                                            outplanes=out_filters,
                                            bias=args.use_bias,
                                            batch_norm=args.batch_norm))
            # outplanes = n_filters + args.n_initial_filters))
            n_filters = out_filters

        # Here, take the final output and convert to a dense tensor:

        self.final_layer = SparseBlockSeries(
            inplanes=n_filters,
            n_blocks=args.res_blocks_per_layer,
            bias=args.use_bias,
            batch_norm=args.batch_norm,
            residual=True)

        self.bottleneck = scn.SubmanifoldConvolution(dimension=3,
                                                     nIn=n_filters,
                                                     nOut=2,
                                                     filter_size=3,
                                                     bias=args.use_bias)

        self.sparse_to_dense = scn.SparseToDense(dimension=3, nPlanes=2)
예제 #16
0
 def get_occupied_grids(self, inputs):
     dense_inputs = scn.InputLayer(2, [256, 5760])(inputs)
     dense_inputs = scn.SparseToDense(2, 2)(dense_inputs)
     dense_inputs = dense_inputs.sum(dim=1)
     gridified = torch.nn.MaxPool2d(self.pool_size)(dense_inputs)
     gridified = torch.gt(gridified, 0.).float()
     # logger.info('gridified = %s %s',gridified.size(),gridified.sum())
     return gridified
예제 #17
0
    def __init__(self, output_shape):
        torch.nn.Module.__init__(self)
        # All of the parameters are controlled via the flags module

        # Create the sparse input tensor:
        self.input_tensor = scn.InputLayer(dimension=3, spatial_size=(512))

        # Here, define the layers we will need in the forward path:

        # The convolutional layers, which can be shared or not across planes,
        # are defined below

        # We apply an initial convolution, to each plane, to get n_inital_filters

        self.initial_convolution = scn.SubmanifoldConvolution(
            dimension=3,
            nIn=1,
            nOut=FLAGS.N_INITIAL_FILTERS,
            filter_size=5,
            bias=FLAGS.USE_BIAS)

        n_filters = FLAGS.N_INITIAL_FILTERS
        # Next, build out the convolution steps

        self.convolutional_layers = []
        for layer in range(FLAGS.NETWORK_DEPTH):

            self.convolutional_layers.append(
                SparseBlockSeries(n_filters,
                                  FLAGS.RES_BLOCKS_PER_LAYER,
                                  residual=True))
            out_filters = filter_increase(n_filters)

            self.convolutional_layers.append(
                SparseConvolutionDownsample(inplanes=n_filters,
                                            outplanes=out_filters))
            # outplanes = n_filters + FLAGS.N_INITIAL_FILTERS))
            n_filters = out_filters

            self.add_module("conv_{}".format(layer),
                            self.convolutional_layers[-2])
            self.add_module("down_{}".format(layer),
                            self.convolutional_layers[-1])

        # Here, take the final output and convert to a dense tensor:

        self.final_layer = SparseBlockSeries(
            inplanes=n_filters,
            n_blocks=FLAGS.RES_BLOCKS_PER_LAYER,
            residual=True)

        self.bottleneck = scn.SubmanifoldConvolution(dimension=3,
                                                     nIn=n_filters,
                                                     nOut=2,
                                                     filter_size=3,
                                                     bias=FLAGS.USE_BIAS)

        self.sparse_to_dense = scn.SparseToDense(dimension=3, nPlanes=2)
예제 #18
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)
예제 #19
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.SparseVggNet(
         2, 3,
         [['C', 16], ['C', 16], 'MP', ['C', 32], ['C', 32], 'MP', ['C', 48],
          ['C', 48], 'MP', ['C', 64], ['C', 64], 'MP', ['C', 96], ['C', 96]
          ]).add(scn.Convolution(2, 96, 128, 3, 2, False)).add(
              scn.BatchNormReLU(128)).add(scn.SparseToDense(2, 128))
     self.linear = nn.Linear(128, 3755)
예제 #20
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.SparseVggNet(2, 3, [
         ['C', 8, ], ['C', 8], 'MP',
         ['C', 16], ['C', 16], 'MP',
         ['C', 16 + 8], ['C', 16 + 8], 'MP',
         ['C', 24 + 8], ['C', 24 + 8], 'MP']
     ).add(scn.Convolution(2, 32, 64, 5, 1, False)
           ).add(scn.BatchNormReLU(64)
                 ).add(scn.SparseToDense(2, 64))
     self.linear = nn.Linear(64, 183)
예제 #21
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),
        )
예제 #22
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
예제 #23
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.SparseVggNet(
         2, 3,
         [['C', 16], ['C', 16], 'MP', ['C', 32], ['C', 32], 'MP', ['C', 48],
          ['C', 48], 'MP', ['C', 64], ['C', 64], 'MP', ['C', 96], ['C', 96]
          ]).add(scn.Convolution(2, 96, 128, 3, 2, False)).add(
              scn.BatchNormReLU(128)).add(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)
예제 #24
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)
예제 #25
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential(
         scn.SubmanifoldConvolution(2, 3, 16, 3, False),
         scn.BatchNormReLU(16),
         scn.SparseResNet(
             2, 16, [['b', 16, 3, 1], ['b', 32, 3, 2], ['b', 64, 3, 2]]),
         scn.AveragePooling(2, 8, 8), scn.SparseToDense(2, 64))
     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(64, 10)
예제 #26
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)
예제 #27
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)
예제 #28
0
파일: ResNet.py 프로젝트: HaiwangYu/nue-cc
 def __init__(self, dimension=3, device='cuda'):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
         scn.SparseVggNet(
             dimension, 1,
             [['C', 8], ['C', 8], ['MP', 3, 2], ['C', 16], ['C', 16],
              ['MP', 3, 2], ['C', 24], ['C', 24], ['MP', 3, 2]])).add(
                  scn.SubmanifoldConvolution(
                      dimension, 24, 32, 3,
                      False)).add(scn.BatchNormReLU(32)).add(
                          scn.SparseToDense(dimension, 32)).to(device)
     self.spatial_size = self.sparseModel.input_spatial_size(
         torch.LongTensor([1] * dimension))
     self.inputLayer = scn.InputLayer(dimension, self.spatial_size)
예제 #29
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential(
         scn.SparseVggNet(
             2, 3, [[
                 'C',
                 8,
             ], ['C', 8], 'MP', ['C', 16], ['C', 16], 'MP', ['C', 16, 8],
                    ['C', 16, 8], 'MP', ['C', 24, 8], ['C', 24, 8], 'MP']),
         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.inputLayer = scn.InputLayer(2, self.spatial_size, 2)
     self.linear = nn.Linear(64, 183)
예제 #30
0
 def forward(self, x):
     pdb.set_trace()
     x = self.inputLayer(x)
     x = self.sscn1(x)
     x = scn.ReLU()(x)
     x = self.sscn2(x)
     x = scn.SparseToDense(2, 64)(x)
     x = F.relu(x)
     x = F.max_pool2d(x, 2)
     x = self.dropout1(x)
     x = torch.flatten(x, 1)
     x = self.fc1(x)
     x = F.relu(x)
     x = self.dropout2(x)
     x = self.fc2(x)
     output = F.log_softmax(x, dim=1)
     return output