コード例 #1
0
def residual_block(m, a, b, leakiness=0.01, dimensions=2):
    """
    append to a sequence module:
    produce output of [identity,3x3+3x3] then add together

    inputs
    ------
    m [scn.Sequential module] network to add layers to
    a [int]: number of input channels
    b [int]: number of output channels
    leakiness [float]: leakiness of ReLU activations
    dimensions [int]: dimensions of input sparse tensor

    modifies
    --------
    m: adds layers
    """
    m.add(scn.ConcatTable().add(scn.Identity(
    ) if a == b else scn.NetworkInNetwork(a, b, False)).add(
        scn.Sequential().add(scn.BatchNormLeakyReLU(
            a, leakiness=leakiness)).add(
                scn.SubmanifoldConvolution(dimensions, a, b, 3, False)).add(
                    scn.BatchNormLeakyReLU(b, leakiness=leakiness)).add(
                        scn.SubmanifoldConvolution(
                            dimensions, b, b, 3, False)))).add(scn.AddTable())
コード例 #2
0
 def block(self, n_in, n_out):
     m = scn.Sequential()
     if self.residual_blocks:  # ResNet style blocks
         m.add(scn.ConcatTable().add(
             scn.Identity() if n_in ==
             n_out else scn.NetworkInNetwork(n_in, n_out, False)).add(
                 scn.Sequential().add(
                     scn.BatchNormLeakyReLU(
                         n_in, leakiness=self.leakiness)).add(
                             scn.SubmanifoldConvolution(
                                 self.dimension, n_in, n_out, 3,
                                 False)).add(
                                     scn.BatchNormLeakyReLU(
                                         n_out,
                                         leakiness=self.leakiness)).add(
                                             scn.SubmanifoldConvolution(
                                                 self.dimension, n_out,
                                                 n_out, 3, False))))
         m.add(scn.AddTable())
     else:  # VGG style blocks
         m.add(scn.BatchNormLeakyReLU(n_in, leakiness=self.leakiness))
         m.add(
             scn.SubmanifoldConvolution(self.dimension, n_in, n_out, 3,
                                        False))
     return m
コード例 #3
0
    def __init__(self, inplanes, outplanes, batch_norm, leaky_relu):
        nn.Module.__init__(self)

        self.batch_norm = batch_norm
        self.leaky_relu = leaky_relu

        self.conv1 = scn.SubmanifoldConvolution(dimension=3,
            nIn         = inplanes,
            nOut        = outplanes,
            filter_size = 3,
            bias=False)

        if self.batch_norm:
            if self.leaky_relu: self.bn1 = scn.BatchNormLeakyReLU(outplanes)
            else:                self.bn1 = scn.BatchNormReLU(outplanes)

        self.conv2 = scn.SubmanifoldConvolution(dimension=3,
            nIn         = outplanes,
            nOut        = outplanes,
            filter_size = 3,
            bias        = False)

        if self.batch_norm:
            self.bn2 = scn.BatchNormalization(outplanes)

        self.residual = scn.Identity()

        if self.leaky_relu: self.relu = scn.LeakyReLU()
        else:                self.relu = scn.ReLU()

        self.add = scn.AddTable()
コード例 #4
0
    def __init__(self, inplanes, outplanes, nplanes=1):
        nn.Module.__init__(self)
        
        
        self.conv1 = scn.SubmanifoldConvolution(dimension=3, 
            nIn         = inplanes, 
            nOut        = outplanes, 
            filter_size = [nplanes,3,3], 
            bias=False)
        

        # if FLAGS.BATCH_NORM:
        self.bn1 = scn.BatchNormReLU(outplanes)

        self.conv2 = scn.SubmanifoldConvolution(dimension=3, 
            nIn         = outplanes,
            nOut        = outplanes,
            filter_size = [nplanes,3,3],
            bias        = False)

        # if FLAGS.BATCH_NORM:
        self.bn2 = scn.BatchNormalization(outplanes)

        self.residual = scn.Identity()
        self.relu = scn.ReLU()

        self.add = scn.AddTable()
コード例 #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 __init__(self, *, inplanes, outplanes, nplanes=1, params):
        nn.Module.__init__(self)

        self.conv1 = scn.SubmanifoldConvolution(dimension=3,
                                                nIn=inplanes,
                                                nOut=outplanes,
                                                filter_size=[nplanes, 3, 3],
                                                bias=params.use_bias)

        self.do_batch_norm = False
        if params.batch_norm:
            self.do_batch_norm = True
            self.bn1 = scn.BatchNormReLU(outplanes)

        self.conv2 = scn.SubmanifoldConvolution(dimension=3,
                                                nIn=outplanes,
                                                nOut=outplanes,
                                                filter_size=[nplanes, 3, 3],
                                                bias=False)

        if params.batch_norm:
            self.bn2 = scn.BatchNormalization(outplanes)

        self.residual = scn.Identity()
        self.relu = scn.ReLU()

        self.add = scn.AddTable()
コード例 #7
0
ファイル: FE_own.py プロジェクト: pancho111203/tDBN
    def block(self,
              m,
              a,
              b,
              dimension=3,
              residual_blocks=False,
              leakiness=0,
              kernel_size=3,
              use_batch_norm=True):  # default using residual_block
        if use_batch_norm:
            Activation = lambda channels: scn.BatchNormLeakyReLU(
                channels, leakiness=leakiness)
        else:
            Activation = lambda channels: scn.LeakyReLU(leakiness)

        if residual_blocks:  #ResNet style blocks
            m.add(scn.ConcatTable().add(scn.Identity(
            ) if a == b else scn.NetworkInNetwork(a, b, False)).add(
                scn.Sequential().add(Activation(a)).add(
                    scn.SubmanifoldConvolution(dimension, a, b, kernel_size,
                                               False)).add(Activation(b)).add(
                                                   scn.SubmanifoldConvolution(
                                                       dimension, b, b,
                                                       kernel_size,
                                                       False)))).add(
                                                           scn.AddTable())
        else:  #VGG style blocks
            m.add(scn.Sequential().add(Activation(a)).add(
                scn.SubmanifoldConvolution(dimension, a, b, kernel_size,
                                           False)))
コード例 #8
0
ファイル: fpn_net.py プロジェクト: zhupan007/Detection_3D
 def block(m, a, b):
     if residual_blocks:  #ResNet style blocks
         m.add(scn.ConcatTable().add(scn.Identity(
         ) if a == b else scn.NetworkInNetwork(a, b, False)).add(
             scn.Sequential().add(
                 scn.BatchNormLeakyReLU(
                     a,
                     momentum=bn_momentum,
                     leakiness=leakiness,
                     track_running_stats=track_running_stats)
             ).add(scn.SubmanifoldConvolution(
                 dimension, a, b, 3, False)).add(
                     scn.BatchNormLeakyReLU(
                         b,
                         momentum=bn_momentum,
                         leakiness=leakiness,
                         track_running_stats=track_running_stats)).add(
                             scn.SubmanifoldConvolution(
                                 dimension, b, b, 3,
                                 False)))).add(scn.AddTable())
     else:  #VGG style blocks
         m.add(scn.Sequential().add(
             scn.BatchNormLeakyReLU(
                 a,
                 momentum=bn_momentum,
                 leakiness=leakiness,
                 track_running_stats=track_running_stats)).add(
                     scn.SubmanifoldConvolution(dimension, a, b, 3,
                                                False)))
     operation = {'kernel': [1, 1, 1], 'stride': [1, 1, 1]}
     return operation
コード例 #9
0
    def __init__(self, inplanes, outplanes, bias, batch_norm):
        nn.Module.__init__(self)

        self.conv1 = scn.SubmanifoldConvolution(dimension=3,
                                                nIn=inplanes,
                                                nOut=outplanes,
                                                filter_size=3,
                                                bias=bias)

        if batch_norm:
            self.activation1 = scn.BatchNormReLU(outplanes, momentum=0.5)
        else:
            self.activation1 = scn.ReLU()

        self.conv2 = scn.SubmanifoldConvolution(dimension=3,
                                                nIn=outplanes,
                                                nOut=outplanes,
                                                filter_size=3,
                                                bias=bias)

        if batch_norm:
            self.activation2 = scn.BatchNormReLU(outplanes, momentum=0.5)
        else:
            self.activation2 = scn.ReLU()

        self.residual = scn.Identity()

        self.add = scn.AddTable()
コード例 #10
0
ファイル: layer_utils.py プロジェクト: LArbys/ublarcvserver
def residual_block(m, ninputchs, noutputchs, leakiness=0.01, dimensions=2):
    """
    Residual Modulae Block

    intention is to append to a sequence module (m)
    produce output of [identity,3x3+3x3] then add together

    inputs
    ------
    m [scn.Sequential module] network to add layers to
    ninputchs   [int]: number of input channels
    noutputchs  [int]: number of output channels
    leakiness [float]: leakiness of ReLU activations
    dimensions  [int]: dimensions of input sparse tensor

    modifies
    --------
    m: adds layers
    """
    inoutsame = ninputchs == noutputchs
    m.add(scn.ConcatTable().add(
        scn.Identity() if inoutsame else scn.
        NetworkInNetwork(ninputchs, noutputchs, False)).add(
            scn.Sequential().add(
                scn.BatchNormLeakyReLU(ninputchs, leakiness=leakiness)).add(
                    scn.SubmanifoldConvolution(
                        dimensions, ninputchs, noutputchs, 3, False)).add(
                            scn.BatchNormLeakyReLU(
                                noutputchs, leakiness=leakiness)).add(
                                    scn.SubmanifoldConvolution(
                                        dimensions, noutputchs, noutputchs, 3,
                                        False)))).add(scn.AddTable())
コード例 #11
0
 def block(self, nPlanes, n, reps, stride):
     m = scn.Sequential()
     for rep in range(reps):
         if rep == 0:
             m.add(scn.BatchNormReLU(nPlanes))
             m.add(scn.ConcatTable().add(self.residual(
                 nPlanes, n, stride)).add(scn.Sequential().add(
                     scn.SubmanifoldConvolution(self.dimension, nPlanes, n,
                                                3, False) if stride ==
                     1 else scn.Convolution(
                         self.dimension, nPlanes, n, 2, stride, False)).add(
                             scn.BatchNormReLU(n)).add(
                                 scn.SubmanifoldConvolution(
                                     self.dimension, n, n, 3, False))))
         else:
             m.add(scn.ConcatTable().add(scn.Sequential().add(
                 scn.BatchNormReLU(nPlanes)).add(
                     scn.SubmanifoldConvolution(
                         self.dimension, nPlanes, n, 3,
                         False)).add(scn.BatchNormReLU(n)).add(
                             scn.SubmanifoldConvolution(
                                 self.dimension, n, n, 3,
                                 False))).add(scn.Identity()))
         m.add(scn.AddTable())
         nPlanes = n
     return m
コード例 #12
0
ファイル: hvd_util.py プロジェクト: k-woodruff/bb2nu_rl_dnn
    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 __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)
コード例 #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, 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)
コード例 #16
0
ファイル: sparseinfill.py プロジェクト: NuTufts/sparse_infill
    def __init__(self, inputshape, reps, nin_features, nout_features, nplanes,
                 show_sizes):
        nn.Module.__init__(self)
        """
        inputs
        ------
        inputshape [list of int]: dimensions of the matrix or image
        reps [int]: number of residual modules per layer (for both encoder and decoder)
        nin_features [int]: number of features in the first convolutional layer
        nout_features [int]: number of features that feed into the regression layer
        nPlanes [int]: the depth of the U-Net
        show_sizes [bool]: if True, print sizes while running forward
        """
        self._mode = 0
        self._dimension = 2
        self._inputshape = inputshape
        if len(self._inputshape) != self._dimension:
            raise ValueError(
                "expected inputshape to contain size of 2 dimensions only." +
                "given %d values" % (len(self._inputshape)))
        self._reps = reps
        self._nin_features = nin_features
        self._nout_features = nout_features
        self._nplanes = [
            nin_features, 2 * nin_features, 3 * nin_features, 4 * nin_features,
            5 * nin_features
        ]
        self._show_sizes = show_sizes

        self.sparseModel = scn.Sequential().add(
            scn.InputLayer(
                self._dimension, self._inputshape, mode=self._mode)).add(
                    scn.SubmanifoldConvolution(
                        self._dimension, 1, self._nin_features, 3, False)).add(
                            scn.UNet(
                                self._dimension,
                                self._reps,
                                self._nplanes,
                                residual_blocks=True,
                                downsample=[2, 2])).add(
                                    scn.BatchNormReLU(self._nin_features)).add(
                                        scn.OutputLayer(self._dimension))

        self.input = scn.InputLayer(self._dimension,
                                    self._inputshape,
                                    mode=self._mode)
        self.conv1 = scn.SubmanifoldConvolution(self._dimension, 1,
                                                self._nin_features, 3, False)
        self.unet = scn.UNet(self._dimension,
                             self._reps,
                             self._nplanes,
                             residual_blocks=True,
                             downsample=[2, 2])
        self.batchnorm = scn.BatchNormReLU(self._nin_features)
        self.output = scn.OutputLayer(self._dimension)
        self.conv2 = scn.SubmanifoldConvolution(self._dimension,
                                                self._nin_features, 1, 3,
                                                False)
コード例 #17
0
def res(m, dimension, a, b):
    m.add(scn.ConcatTable()
          .add(scn.Identity() if a == b else scn.NetworkInNetwork(a, b, False))
          .add(scn.Sequential()
               .add(scn.BatchNormReLU(a))
               .add(scn.SubmanifoldConvolution(dimension, a, b, 3, False))
               .add(scn.BatchNormReLU(b))
               .add(scn.SubmanifoldConvolution(dimension, b, b, 3, False))))\
     .add(scn.AddTable())
コード例 #18
0
 def __init__(self, inplanes, kernel, dim=3):
     torch.nn.Module.__init__(self)
     self.bnr1 = scn.BatchNormReLU(inplanes)
     self.subconv1 = scn.SubmanifoldConvolution(dim, inplanes, inplanes,
                                                kernel, 0)
     self.bnr2 = scn.BatchNormReLU(inplanes)
     self.subconv2 = scn.SubmanifoldConvolution(dim, inplanes, inplanes,
                                                kernel, 0)
     self.add = scn.AddTable()
コード例 #19
0
 def block(m, a, b):  # ResNet style blocks
     m.add(scn.ConcatTable()
           .add(scn.Identity() if a == b else scn.NetworkInNetwork(a, b, False))
           .add(scn.Sequential()
             .add(scn.BatchNormLeakyReLU(a, leakiness=leakiness))
             .add(scn.SubmanifoldConvolution(self._dimension, a, b, 3, False))
             .add(scn.BatchNormLeakyReLU(b, leakiness=leakiness))
             .add(scn.SubmanifoldConvolution(self._dimension, b, b, 3, False)))
      ).add(scn.AddTable())
コード例 #20
0
    def __init__(self, class_num, full_scale, m=16, dimension=3):
        nn.Module.__init__(self)
        self.dimension = dimension
        self.input = scn.InputLayer(dimension, full_scale, mode=4)
        self.input_s = scn.InputLayer(dimension, full_scale, mode=4)
        self.down_in = scn.SubmanifoldConvolution(dimension, 1, m, 3, False)
        self.down_in_s = scn.SubmanifoldConvolution(dimension, 1, m, 3, False)
        self.main_block1 = self.block(m, m, 2, 1)
        self.main_block2 = self.block(m, 2 * m, 1, 2)
        self.main_block3 = self.block(2 * m, 3 * m, 1, 2)
        self.main_block4 = self.block(3 * m, 4 * m, 1, 2)
        self.main_block5 = self.block(4 * m, 5 * m, 1, 2)
        self.main_block6 = self.block(5 * m, 6 * m, 1, 2)
        self.main_block7 = self.block(6 * m, 7 * m, 2, 2)
        self.main_block8 = self.block(7 * m, 7 * m, 2, 1)

        self.support_block1 = self.block(m, m, 2, 1)
        self.support_block2 = self.block(m, 2 * m, 1, 2)
        self.support_block3 = self.block(2 * m, 3 * m, 1, 2)
        self.support_block4 = self.block(3 * m, 4 * m, 1, 2)
        self.support_block5 = self.block(4 * m, 5 * m, 1, 2)
        self.support_block6 = self.block(5 * m, 6 * m, 1, 2)
        self.support_block7 = self.block(6 * m, 7 * m, 1, 2)
        self.support_block8 = self.block(7 * m, 7 * m, 1, 1)

        self.support_block2_tune = self.guide_tune(dimension, 2 * m, 2 * m, 1,
                                                   False)
        self.support_block2_out = GlobalMeanAttentionPooling(dimension)
        self.support_block3_tune = self.guide_tune(dimension, 4 * m, 4 * m, 1,
                                                   False)
        self.support_block3_out = GlobalMeanAttentionPooling(dimension)
        self.support_block4_tune = self.guide_tune(dimension, 7 * m, 7 * m, 1,
                                                   False)
        self.support_block4_out = GlobalMeanAttentionPooling(dimension)

        self.global_add2 = GlobalMaskLayer(dimension)
        self.global_add3 = GlobalMaskLayer(dimension)
        self.global_add4 = GlobalMaskLayer(dimension)

        self.spatial_pick = DistMatchLayer_v4(dimension, 7 * m, topk=3)
        # self.join_sub = scn.JoinTable()
        self.tune_sub = self.guide_tune(dimension, 14 * m, 7 * m, 1, False)

        self.deconv7 = self.decoder(7 * m, 6 * m)
        self.join6 = scn.JoinTable()
        self.deconv6 = self.decoder(12 * m, 5 * m)
        self.deconv5 = self.decoder(5 * m, 4 * m)
        self.join4 = scn.JoinTable()
        self.deconv4 = self.decoder(8 * m, 3 * m)
        self.deconv3 = self.decoder(3 * m, 2 * m)
        self.join2 = scn.JoinTable()
        self.deconv2 = self.decoder(4 * m, 2 * m)
        # self.deconv1 = self.decoder(2 * m, m)

        self.output = scn.OutputLayer(dimension)
        self.linear = nn.Linear(2 * m, class_num)
コード例 #21
0
def SparseResNet(dimension, nInputPlanes, layers):
    import sparseconvnet as scn
    """
    pre-activated ResNet
    e.g. layers = {{'basic',16,2,1},{'basic',32,2}}
    """
    nPlanes = nInputPlanes
    m = scn.Sequential()

    def residual(nIn, nOut, stride):
        if stride > 1:
            return scn.Convolution(dimension, nIn, nOut, 2, stride, False)
        elif nIn != nOut:
            return scn.NetworkInNetwork(nIn, nOut, False)
        else:
            return scn.Identity()

    for n, reps, stride in layers:
        for rep in range(reps):
            if rep == 0:
                m.add(scn.BatchNormReLU(nPlanes))
                tab = scn.ConcatTable()
                tab_seq = scn.Sequential()
                if stride == 1:
                    tab_seq.add(
                        scn.SubmanifoldConvolution(dimension, nPlanes, n, 3,
                                                   False))
                else:
                    tab_seq.add(
                        scn.Convolution(dimension, nPlanes, n, 2, stride,
                                        False))
                tab_seq.add(scn.BatchNormReLU(n))
                tab_seq.add(
                    scn.SubmanifoldConvolution(dimension, n, n, 3, False))
                tab.add(tab_seq)
                tab.add(residual(nPlanes, n, stride))
                m.add(tab)
            else:
                tab = scn.ConcatTable()
                tab_seq = scn.Sequential()
                tab_seq.add(scn.BatchNormReLU(nPlanes))
                tab_seq.add(
                    scn.SubmanifoldConvolution(dimension, nPlanes, n, 3,
                                               False))
                tab_seq.add(scn.BatchNormReLU(n))
                tab_seq.add(
                    scn.SubmanifoldConvolution(dimension, n, n, 3, False))
                tab.add(tab_seq)
                tab.add(scn.Identity())
                m.add(tab)
            nPlanes = n
            m.add(scn.AddTable())
    m.add(scn.BatchNormReLU(nPlanes))
    return m
コード例 #22
0
 def f(m, a, b):
     m.add(scn.ConcatTable().add(scn.Identity(
     ) if a == b else scn.NetworkInNetwork(a, b, self.allow_bias)).add(
         scn.Sequential().add(norm_layer(
             a, leakiness=self.leakiness)).add(
                 scn.SubmanifoldConvolution(
                     self.dimension, a, b, 3, self.allow_bias)).add(
                         norm_layer(b, leakiness=self.leakiness)).add(
                             scn.SubmanifoldConvolution(
                                 self.dimension, b, b, 3,
                                 self.allow_bias)))).add(scn.AddTable())
     return m
コード例 #23
0
    def __init__(self, inputshape, reps, nfeatures, nplanes, noutput_classes):
        nn.Module.__init__(self)

        # set parameters
        self.dimensions = 2  # not playing with 3D for now

        # input shape: LongTensor, tuple, or list. Handled by InputLayer
        # size of each spatial dimesion
        self.inputshape = inputshape
        if len(self.inputshape) != self.dimensions:
            raise ValueError(
                "expected inputshape to contain size of 2 dimensions only. given %d values"
                % (len(self.inputshape)))

        # mode variable: how to deal with repeated data
        self.mode = 0

        # nfeatures
        self.nfeatures = nfeatures

        # plane structure
        self.nPlanes = [self.nfeatures * (n + 1) for n in xrange(nplanes)]

        # repetitions (per plane)
        self.reps = reps

        # output classes
        self.noutput_classes = noutput_classes

        # model:
        # input
        # stem
        # unet
        # linear to nclasses
        self.sparseModel = scn.Sequential().add(
            scn.InputLayer(
                self.dimensions, self.inputshape, mode=self.mode)).add(
                    scn.SubmanifoldConvolution(
                        self.dimensions, 1, self.nfeatures, 3, False)).add(
                            scn.UNet(
                                self.dimensions,
                                self.reps,
                                self.nPlanes,
                                residual_blocks=True,
                                downsample=[
                                    2, 2
                                ])).add(scn.BatchNormReLU(self.nfeatures)).add(
                                    scn.SubmanifoldConvolution(
                                        self.dimensions, self.nfeatures,
                                        self.noutput_classes, 1, False)).add(
                                            scn.OutputLayer(self.dimensions))
        self.softmax = torch.nn.Softmax(dim=1)
コード例 #24
0
 def block(m, a, b):
     if residual_blocks:  #ResNet style blocks
         m.add(scn.ConcatTable().add(scn.Identity(
         ) if a == b else scn.NetworkInNetwork(a, b, False)).add(
             scn.Sequential().add(scn.BatchNormReLU(a)).add(
                 scn.SubmanifoldConvolution(dimension, a, b, 3, False)).add(
                     scn.BatchNormReLU(b)).add(
                         scn.SubmanifoldConvolution(dimension, b, b, 3,
                                                    False)))).add(
                                                        scn.AddTable())
     else:  #VGG style blocks
         m.add(scn.Sequential().add(scn.BatchNormReLU(a)).add(
             scn.SubmanifoldConvolution(dimension, a, b, 3, False)))
コード例 #25
0
 def foo(m,np):
     for _ in range(reps):
         if residual: #ResNet style blocks
             m.add(scn.ConcatTable()
                   .add(scn.Identity())
                   .add(scn.Sequential()
                     .add(scn.BatchNormLeakyReLU(np,leakiness=leakiness))
                     .add(scn.SubmanifoldConvolution(dimension, np, np, 3, False))
                     .add(scn.BatchNormLeakyReLU(np,leakiness=leakiness))
                     .add(scn.SubmanifoldConvolution(dimension, np, np, 3, False)))
              ).add(scn.AddTable())
         else: #VGG style blocks
             m.add(scn.BatchNormLeakyReLU(np,leakiness=leakiness)
             ).add(scn.SubmanifoldConvolution(dimension, np, np, 3, False))
コード例 #26
0
 def block(self, m, a, b, dimension=3, residual_blocks=False, leakiness=0):  # default using residual_block
     if residual_blocks: #ResNet style blocks
         m.add(scn.ConcatTable()
               .add(scn.Identity() if a == b else scn.NetworkInNetwork(a, b, False))
               .add(scn.Sequential()
                 .add(scn.BatchNormLeakyReLU(a,leakiness=leakiness))
                 .add(scn.SubmanifoldConvolution(dimension, a, b, 3, False))
                 .add(scn.BatchNormLeakyReLU(b,leakiness=leakiness))
                 .add(scn.SubmanifoldConvolution(dimension, b, b, 3, False)))
          ).add(scn.AddTable())
     else: #VGG style blocks
         m.add(scn.Sequential()
              .add(scn.BatchNormLeakyReLU(a,leakiness=leakiness))
              .add(scn.SubmanifoldConvolution(dimension, a, b, 3, False)))
コード例 #27
0
ファイル: instance.py プロジェクト: xuhuahaoren/MASC
    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
コード例 #28
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)
コード例 #29
0
 def __init__(self,
              is_3d,
              num_strides=3,
              base_num_outputs=16,
              num_classes=3,
              spatialSize=192):
     nn.Module.__init__(self)
     dimension = 3 if is_3d else 2
     reps = 2  # Conv block repetition factor
     kernel_size = 2  # Use input_spatial_size method for other values?
     m = base_num_outputs  # Unet number of features
     nPlanes = [i * m for i in range(1, num_strides + 1)
                ]  # UNet number of features per level
     # nPlanes = [(2**i) * m for i in range(1, num_strides+1)]  # UNet number of features per level
     nInputFeatures = 1
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(dimension, spatialSize, mode=3)).add(
             scn.SubmanifoldConvolution(
                 dimension, nInputFeatures, m, 3,
                 False)).add(  # Kernel size 3, no bias
                     UNet(dimension,
                          reps,
                          nPlanes,
                          residual_blocks=True,
                          downsample=[kernel_size, 2])
                 ).add(  # downsample = [filter size, filter stride]
                     scn.BatchNormReLU(m)).add(scn.OutputLayer(dimension))
     self.linear = nn.Linear(m, num_classes)
コード例 #30
0
 def __init__(self, flags):
     import sparseconvnet as scn
     super(UResNet, self).__init__()
     self._flags = flags
     dimension = flags.DATA_DIM
     reps = 2  # Conv block repetition factor
     kernel_size = 2  # Use input_spatial_size method for other values?
     m = flags.URESNET_FILTERS  # Unet number of features
     nPlanes = [i * m for i in range(1, flags.URESNET_NUM_STRIDES + 1)
                ]  # UNet number of features per level
     # nPlanes = [(2**i) * m for i in range(1, num_strides+1)]  # UNet number of features per level
     nInputFeatures = 1
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(dimension, flags.SPATIAL_SIZE, mode=3)).add(
             scn.SubmanifoldConvolution(
                 dimension, nInputFeatures, m, 3,
                 False)).add(  # Kernel size 3, no bias
                     scn.UNet(dimension,
                              reps,
                              nPlanes,
                              residual_blocks=True,
                              downsample=[kernel_size, 2])
                 ).add(  # downsample = [filter size, filter stride]
                     scn.BatchNormReLU(m)).add(scn.OutputLayer(dimension))
     self.linear = torch.nn.Linear(m, flags.NUM_CLASS)