Example #1
0
    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)
 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)
Example #3
0
 def __init__(self,
              dimension=3,
              size=1536,
              nFeatures=16,
              depth=5,
              nClasses=1):
     super(UResNet, self).__init__()
     self.dimension = dimension
     self.size = size
     self.nFeatures = nFeatures
     self.depth = depth
     self.nClasses = nClasses
     reps = 2  # Conv block repetition factor
     kernel_size = 2  # Use input_spatial_size method for other values?
     m = nFeatures  # Unet number of features
     nPlanes = [i * m for i in range(1, depth + 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 = 6
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(dimension, 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, nClasses)
     self.linear = torch.nn.Sequential(torch.nn.Linear(m, m // 2),
                                       torch.nn.ReLU(0.1),
                                       torch.nn.Linear(m // 2, nClasses))
Example #4
0
    def __init__(self, cfg):
        import sparseconvnet as scn
        super(UResNet, self).__init__()
        self._model_config = cfg['modules']['uresnet']

        self._dimension = self._model_config.get('data_dim', 3)
        num_strides = self._model_config.get('num_strides', 5)
        spatial_size = self._model_config.get('spatial_size', 512)
        num_classes = self._model_config.get('num_classes', 5)
        m = self._model_config.get('filters', 16)  # Unet number of features
        nInputFeatures = self._model_config.get('features', 1)

        reps = 2  # Conv block repetition factor
        kernel_size = 2  # Use input_spatial_size method for other values?
        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

        self.sparseModel = scn.Sequential().add(
            scn.InputLayer(self._dimension, spatial_size, mode=3)).add(
                scn.SubmanifoldConvolution(
                    self._dimension, nInputFeatures, m, 3,
                    False)).add(  # Kernel size 3, no bias
                        scn.UNet(self._dimension,
                                 reps,
                                 nPlanes,
                                 residual_blocks=True,
                                 downsample=[kernel_size, 2])
                    ).add(  # downsample = [filter size, filter stride]
                        scn.BatchNormReLU(m)).add(
                            scn.OutputLayer(self._dimension))
        self.linear = torch.nn.Linear(m, num_classes)
    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(
                        scn.UNet(dimension,
                                 reps,
                                 nPlanes,
                                 residual_blocks=False,
                                 downsample=[kernel_size, 2])
                    ).add(  # downsample = [filter size, filter stride]
                        scn.BatchNormReLU(m)).add(scn.OutputLayer(dimension))

        self.linear = nn.Linear(m, num_classes)
Example #6
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
        scn.InputLayer(dimension, spatialSize, mode=3)).add(
        scn.SubmanifoldConvolution(dimension, 1, m, 3, False)).add(
        scn.UNet(dimension, reps, nPlanes, residual_blocks=False, downsample=[2,2])).add(
        scn.BatchNormReLU(m)).add(
        scn.OutputLayer(dimension))
     self.linear = nn.Linear(m, classes_total)
Example #7
0
 def __init__(self,  dimension = 3, device = 'cuda', spatialSize = 4096, nIn = 3, nOut = 2, reps = REPS, nPlanes = NPLANES):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(dimension, torch.LongTensor([spatialSize]*3), mode=3)).add(
         scn.SubmanifoldConvolution(dimension, nIn, m, 3, False)).add(
         scn.UNet(dimension, reps, nPlanes, residual_blocks=False, downsample=[2,2])).add(
         scn.BatchNormReLU(m)).add(
         scn.OutputLayer(dimension)).to(device)
     self.inputLayer = scn.InputLayer(dimension, torch.LongTensor([spatialSize]*3), mode=3)
     self.linear = nn.Linear(m, nOut).to(device)
Example #8
0
 def __init__(self):
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(data.dimension, data.full_scale, mode=4)).add(
             scn.SubmanifoldConvolution(
                 data.dimension, 3, m, 3, False)).add(
                     scn.UNet(data.dimension, block_reps,
                              [m, 2 * m, 3 * m, 4 * m, 5 * m, 6 * m, 7 * m],
                              residual_blocks)).add(
                                  scn.BatchNormReLU(m)).add(
                                      scn.OutputLayer(data.dimension))
     self.linear = nn.Linear(m, 20)
Example #9
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)
Example #10
0
 def __init__(self, Feature_channel, num_class):
     nn.Module.__init__(self)
     dimension = 3
     self.compress_fn = nn.Linear(Feature_channel, dimension)
     self.bn = nn.BatchNorm1d(3)
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(dimension, full_scale, mode=4)).add(
             scn.SubmanifoldConvolution(dimension, 3, m, 3, False)).add(
                 scn.UNet(dimension, block_reps,
                          [m, 2 * m, 3 * m, 4 * m, 5 * m, 6 * m, 7 * m],
                          residual_blocks)).add(scn.BatchNormReLU(m)).add(
                              scn.OutputLayer(dimension))
     self.linear = nn.Linear(m, num_class)
Example #11
0
 def __init__(self, config):
     super().__init__()
     self.config = config
     m = config['Segmentation']['m']
     input_dim = 4 if config['Segmentation']['use_coords'] else 1
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(3, config['Segmentation']['full_scale'][1], mode=4)
     ).add(scn.SubmanifoldConvolution(3, input_dim, m, 3, False)).add(
         scn.UNet(dimension=3,
                  reps=config['Segmentation']['block_reps'],
                  nPlanes=[m, 2 * m, 3 * m, 4 * m, 5 * m, 6 * m, 7 * m],
                  residual_blocks=config['Segmentation']['block_residual'],
                  groups=config['Segmentation']['seg_groups'])).add(
                      scn.BatchNormReLU(m)).add(scn.OutputLayer(3))
     self.linear = nn.Linear(m, self.config['DATA']['classes_seg'])
     if self.config['Completion']['interaction']:
         self.shape_embedding = conv_base.Conv1d(
             m, m, kernel_size=1, bn=True, activation=nn.LeakyReLU(0.2))
Example #12
0
    def __init__(self,
                 in_channels,
                 m=16,  # number of unet features (multiplied in each layer)
                 block_reps=1,  # depth
                 residual_blocks=False,  # ResNet style basic blocks
                 full_scale=4096,
                 num_planes=7
                 ):
        super(UNetSCN, self).__init__()

        self.in_channels = in_channels
        self.out_channels = m
        n_planes = [(n + 1) * m for n in range(num_planes)]

        self.sparseModel = scn.Sequential().add(
            scn.InputLayer(DIMENSION, full_scale, mode=4)).add(
            scn.SubmanifoldConvolution(DIMENSION, in_channels, m, 3, False)).add(
            scn.UNet(DIMENSION, block_reps, n_planes, residual_blocks)).add(
            scn.BatchNormReLU(m)).add(
            scn.OutputLayer(DIMENSION))
Example #13
0
 def __init__(self, size):
     self.dimension = 3
     self.reps = 1  #Conv block repetition factor
     self.m = 32  #Unet number of features
     self.nPlanes = [m, 2 * m, 3 * m, 4 * m,
                     5 * m]  #UNet number of features per level
     nn.Module.__init__(self)
     self.sparseModel = scn.Sequential().add(
         scn.InputLayer(
             self.dimension, torch.LongTensor([size] * 3), mode=3)).add(
                 scn.SubmanifoldConvolution(
                     self.dimension, 1, self.m, 3, False)).add(
                         scn.UNet(self.dimension,
                                  self.reps,
                                  self.nPlanes,
                                  residual_blocks=False,
                                  downsample=[2, 2])).add(
                                      scn.BatchNormReLU(self.m)).add(
                                          scn.OutputLayer(self.dimension))
     self.linear = nn.Linear(m, 2)
Example #14
0
    def __init__(self, options):
        nn.Module.__init__(self)

        self.options = options

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

        self.outputs = []

        def hook(module, input, output):
            self.outputs.append(output)
            return

        self.sparseModel = scn.Sequential().add(
            scn.InputLayer(dimension, options.inputScale, mode=4)).add(
                scn.SubmanifoldConvolution(
                    dimension, 3 + 3 * int('normal' in self.options.suffix), m,
                    3, False)).add(
                        scn.UNet(dimension, block_reps,
                                 [m, 2 * m, 3 * m, 4 * m, 5 * m, 6 * m, 7 * m],
                                 residual_blocks)).add(scn.BatchNormReLU(m))
        #print(self.sparseModel[2])
        #exit(1)

        if options.numScales >= 2:
            #list(self.sparseModel[2][1].children())[1][2][3].register_forward_hook(hook)
            list(self.sparseModel[2]
                 [4].children())[1][3].register_forward_hook(hook)
            pass
        if options.numScales >= 3:
            list(list(self.sparseModel[2][4].children())[1][2]
                 [4].children())[1][3].register_forward_hook(hook)
            pass
        if options.numScales >= 4:
            list(
                list(
                    list(
                        self.sparseModel[2][4].children())[1][2][4].children())
                [1][2][4].children())[1][3].register_forward_hook(hook)
            pass
        if options.numScales >= 5:
            list(
                list(
                    list(
                        list(self.sparseModel[2][4].children())[1][2]
                        [4].children())[1][2][4].children())[1][2]
                [4].children())[1][3].register_forward_hook(hook)
            pass
        if options.numScales >= 6:
            list(
                list(
                    list(
                        list(
                            list(self.sparseModel[2][4].children())[1][2]
                            [4].children())[1][2][4].children())[1][2]
                    [4].children())[1][2]
                [4].children())[1][3].register_forward_hook(hook)
            pass

        # list(list(list(list(list(self.sparseModel[2][1].children())[1][2][1].children())[1][2][1].children())[1][2][1].children())[1][2][1].children())[1][2][3].register_forward_hook(hook)
        # list(list(list(list(self.sparseModel[2][1].children())[1][2][1].children())[1][2][1].children())[1][2][1].children())[1][2][3].register_forward_hook(hook)
        # list(list(list(self.sparseModel[2][1].children())[1][2][1].children())[1][2][1].children())[1][2][3].register_forward_hook(hook)
        # list(list(self.sparseModel[2][1].children())[1][2][1].children())[1][2][3].register_forward_hook(hook)
        # list(self.sparseModel[2][1].children())[1][2][3].register_forward_hook(hook)

        self.sparsify = scn.Sparsify(dimension)
        self.output_layer = scn.OutputLayer(dimension)

        self.linear = nn.Linear(m, 20)

        self.neighbor_linear_0 = nn.Linear(
            m, 6 + 7 * min(self.options.numCrossScales,
                           max(self.options.numScales - 1, 0)))
        self.neighbor_linear_1 = nn.Linear(
            m * 2, 6 + 7 * min(self.options.numCrossScales,
                               max(self.options.numScales - 2, 0)))
        self.neighbor_linear_2 = nn.Linear(
            m * 3, 6 + 7 * min(self.options.numCrossScales,
                               max(self.options.numScales - 3, 0)))
        self.neighbor_linear_3 = nn.Linear(
            m * 4, 6 + 7 * min(self.options.numCrossScales,
                               max(self.options.numScales - 4, 0)))
        self.neighbor_linear_4 = nn.Linear(
            m * 5, 6 + 7 * min(self.options.numCrossScales,
                               max(self.options.numScales - 5, 0)))
        self.neighbor_linear_5 = nn.Linear(
            m * 6, 6 + 7 * min(self.options.numCrossScales,
                               max(self.options.numScales - 6, 0)))

        # self.neighbor_linear_0 = nn.Linear(m, 6 + 7 * 5)
        # self.neighbor_linear_1 = nn.Linear(m * 2, 6 + 7 * 4)
        # self.neighbor_linear_2 = nn.Linear(m * 3, 6 + 7 * 3)
        # self.neighbor_linear_3 = nn.Linear(m * 4, 6 + 7 * 2)
        # self.neighbor_linear_4 = nn.Linear(m * 5, 6 + 7 * 1)
        # self.neighbor_linear_5 = nn.Linear(m * 6, 6 + 7 * 0)

        self.unpool_1 = scn.UnPooling(dimension, 2, 2)
        self.unpool_2 = scn.UnPooling(dimension, 4, 4)
        self.unpool_3 = scn.UnPooling(dimension, 8, 8)
        self.unpool_4 = scn.UnPooling(dimension, 16, 16)
        self.unpool_5 = scn.UnPooling(dimension, 32, 32)
        return
Example #15
0
    def __init__(self,
                 ndimensions=2,
                 inputshape=(1024, 3456),
                 input_nfeatures=1,
                 stem_nfeatures=16,
                 features_per_layer=16,
                 classifier_nfeatures=[32, 32],
                 leakiness=0.01,
                 ninput_planes=3,
                 use_unet=False,
                 nresnet_blocks=10):
        """
        parameters
        -----------
        ndimensions [int]    number of spatial dimensions of input data, default=2
        inputshape  [tuple of int]  size of input tensor/image in (num of tick pixels, num of wire pixels), default=(1024,3456)
        input_nfeatures [int] number of features in the input tensor, default=1 (the image charge)
        stem_nfeatures [int] number of features in the stem layers, also controls num of features in unet layers, default=16
        features_per_layer [int] number of channels in the resnet layers that follow unet layers, default=16
        classifier_nfeatures [tuple of int] number of channels per hidden layer of larmatch classification network, default=[32,32]
        leakiness [float] leakiness of LeakyRelu activiation layers, default=0.01
        ninput_planes [int] number of input image planes, default=3
        use_unet [bool] if true, use UNet layers, default=True
        nresnet_blocks [int] number of resnet blocks if not using unet, DEPRECATED, default=10
        """
        super(LArMatch, self).__init__()

        self.use_unet = use_unet

        # INPUT LAYERS: converts torch tensor into scn.SparseMatrix
        self.ninput_planes = ninput_planes
        self.source_inputlayer = scn.InputLayer(ndimensions,
                                                inputshape,
                                                mode=0)
        self.target1_inputlayer = scn.InputLayer(ndimensions,
                                                 inputshape,
                                                 mode=0)
        self.target2_inputlayer = scn.InputLayer(ndimensions,
                                                 inputshape,
                                                 mode=0)

        # STEM
        self.stem = scn.Sequential()
        self.stem.add(
            scn.SubmanifoldConvolution(ndimensions, input_nfeatures,
                                       stem_nfeatures, 3, False))
        self.stem.add(
            scn.BatchNormLeakyReLU(stem_nfeatures, leakiness=leakiness))
        if self.use_unet:
            self.stem.add(
                scn.SubmanifoldConvolution(ndimensions, stem_nfeatures,
                                           stem_nfeatures, 3, False))
            self.stem.add(
                scn.BatchNormLeakyReLU(stem_nfeatures, leakiness=leakiness))
            self.stem.add(
                scn.SubmanifoldConvolution(ndimensions, stem_nfeatures,
                                           stem_nfeatures, 3, False))

        # UNET BLOCK
        if self.use_unet:
            self.unet_layers = scn.UNet(
                2,
                2, [
                    stem_nfeatures, stem_nfeatures * 2, stem_nfeatures * 3,
                    stem_nfeatures * 4, stem_nfeatures * 5
                ],
                residual_blocks=True,
                downsample=[2, 2])

        # RESNET BLOCK
        if not self.use_unet:
            self.resnet_layers = create_resnet_layer(nresnet_blocks,
                                                     stem_nfeatures,
                                                     features_per_layer,
                                                     leakiness=leakiness)
        else:
            self.resnet_layers = create_resnet_layer(1,
                                                     stem_nfeatures,
                                                     features_per_layer,
                                                     leakiness=leakiness)
            self.resnet_layers.add(
                scn.BatchNormLeakyReLU(stem_nfeatures, leakiness=leakiness))

        # OUTPUT FEATURES
        self.nfeatures = features_per_layer
        self.feature_layer = scn.SubmanifoldConvolution(
            ndimensions, features_per_layer, self.nfeatures, 1, True)

        # from there, we move back into a tensor
        self.source_outlayer = scn.OutputLayer(ndimensions)
        self.target1_outlayer = scn.OutputLayer(ndimensions)
        self.target2_outlayer = scn.OutputLayer(ndimensions)

        # CLASSIFER: MATCH/NO-MATCH
        classifier_layers = OrderedDict()
        classifier_layers["class0conv"] = torch.nn.Conv1d(
            self.ninput_planes * features_per_layer, classifier_nfeatures[0],
            1)
        #classifier_layers["class0bn"]   = torch.nn.BatchNorm1d
        classifier_layers["class0relu"] = torch.nn.ReLU()
        for ilayer, nfeats in enumerate(classifier_nfeatures[1:]):
            classifier_layers["class%dconv" % (ilayer + 1)] = torch.nn.Conv1d(
                nfeats, nfeats, 1)
            #classifier_layers["class0bn"]   = torch.nn.BatchNorm1d
            classifier_layers["class%drelu" % (ilayer + 1)] = torch.nn.ReLU()
        classifier_layers["classout"] = torch.nn.Conv1d(nfeats, 1, 1)
        #classifier_layers["sigmoid"]  = torch.nn.Sigmoid()
        self.classifier = torch.nn.Sequential(classifier_layers)