Ejemplo n.º 1
0
    def __init__(self, option, model_type, dataset, modules):
        super(PointGroup, self).__init__(option)
        backbone_options = option.get("backbone", {"architecture": "unet"})
        self.Backbone = Minkowski(
            backbone_options.architecture,
            input_nc=dataset.feature_dimension,
            num_layers=4,
            config=backbone_options.config,
        )
        self.BackboneHead = Seq().append(FastBatchNorm1d(self.Backbone.output_nc)).append(torch.nn.ReLU())

        self._scorer_is_encoder = option.scorer.architecture == "encoder"
        self._activate_scorer = option.scorer.activate
        self.Scorer = Minkowski(
            option.scorer.architecture, input_nc=self.Backbone.output_nc, num_layers=option.scorer.depth
        )
        self.ScorerHead = Seq().append(torch.nn.Linear(self.Scorer.output_nc, 1)).append(torch.nn.Sigmoid())

        self.Offset = Seq().append(MLP([self.Backbone.output_nc, self.Backbone.output_nc], bias=False))
        self.Offset.append(torch.nn.Linear(self.Backbone.output_nc, 3))

        self.Semantic = (
            Seq()
            .append(MLP([self.Backbone.output_nc, self.Backbone.output_nc], bias=False))
            .append(torch.nn.Linear(self.Backbone.output_nc, dataset.num_classes))
            .append(torch.nn.LogSoftmax())
        )
        self.loss_names = ["loss", "offset_norm_loss", "offset_dir_loss", "semantic_loss", "score_loss"]
        stuff_classes = dataset.stuff_classes
        if is_list(stuff_classes):
            stuff_classes = torch.Tensor(stuff_classes).long()
        self._stuff_classes = torch.cat([torch.tensor([IGNORE_LABEL]), stuff_classes])
Ejemplo n.º 2
0
    def __init__(self, input_nc, output_nc, convolution, reduction=4):
        super().__init__()

        self.block = (Seq().append(
            snn.Conv3d(
                input_nc, output_nc // reduction, kernel_size=1,
                stride=1)).append(snn.BatchNorm(
                    output_nc // reduction)).append(snn.ReLU()).append(
                        convolution(
                            output_nc // reduction,
                            output_nc // reduction,
                            kernel_size=3,
                            stride=1,
                        )).append(snn.BatchNorm(
                            output_nc // reduction)).append(snn.ReLU()).append(
                                snn.Conv3d(
                                    output_nc // reduction,
                                    output_nc,
                                    kernel_size=1,
                                )).append(snn.BatchNorm(output_nc)).append(
                                    snn.ReLU()))

        if input_nc != output_nc:
            self.downsample = (Seq().append(
                convolution(input_nc, output_nc, kernel_size=1,
                            stride=1)).append(snn.BatchNorm(output_nc)))
        else:
            self.downsample = None
Ejemplo n.º 3
0
    def __init__(self, option, model_type, dataset, modules):
        super(PointGroup, self).__init__(option)
        self.Backbone = Minkowski("unet",
                                  input_nc=dataset.feature_dimension,
                                  num_layers=4)

        self._scorer_is_encoder = option.scorer.architecture == "encoder"
        self.Scorer = Minkowski(option.scorer.architecture,
                                input_nc=self.Backbone.output_nc,
                                num_layers=2)
        self.ScorerHead = Seq().append(
            torch.nn.Linear(self.Scorer.output_nc,
                            1)).append(torch.nn.Sigmoid())

        self.Offset = Seq().append(
            MLP([self.Backbone.output_nc, self.Backbone.output_nc],
                bias=False))
        self.Offset.append(torch.nn.Linear(self.Backbone.output_nc, 3))

        self.Semantic = (Seq().append(
            torch.nn.Linear(self.Backbone.output_nc,
                            dataset.num_classes)).append(
                                torch.nn.LogSoftmax()))
        self.loss_names = [
            "loss", "offset_norm_loss", "offset_dir_loss", "semantic_loss",
            "score_loss"
        ]
        self._stuff_classes = torch.cat(
            [torch.tensor([IGNORE_LABEL]), dataset.stuff_classes])
Ejemplo n.º 4
0
    def __init__(
        self,
        down_conv_nn=[],
        kernel_size=2,
        dilation=1,
        stride=2,
        N=1,
        block="ResBlock",
        **kwargs,
    ):
        block = getattr(_res_blocks, block)
        super().__init__()
        if stride > 1:
            conv1_output = down_conv_nn[0]
        else:
            conv1_output = down_conv_nn[1]

        conv = getattr(snn, self.CONVOLUTION)
        self.conv_in = (Seq().append(
            conv(
                in_channels=down_conv_nn[0],
                out_channels=conv1_output,
                kernel_size=kernel_size,
                stride=stride,
                dilation=dilation,
            )).append(snn.BatchNorm(conv1_output)).append(snn.ReLU()))

        if N > 0:
            self.blocks = Seq()
            for _ in range(N):
                self.blocks.append(block(conv1_output, down_conv_nn[1], conv))
                conv1_output = down_conv_nn[1]
        else:
            self.blocks = None
Ejemplo n.º 5
0
    def __init__(self, option, model_type, dataset, modules):
        super(PointGroup, self).__init__(option)
        backbone_options = option.get("backbone", {"architecture": "unet"})
        self.Backbone = Minkowski(
            backbone_options.get("architecture", "unet"),
            input_nc=dataset.feature_dimension,
            num_layers=4,
            config=backbone_options.get("config", {}),
        )

        self._scorer_type = option.get("scorer_type", "encoder")
        cluster_voxel_size = option.get("cluster_voxel_size", 0.05)
        if cluster_voxel_size:
            self._voxelizer = GridSampling3D(cluster_voxel_size,
                                             quantize_coords=True,
                                             mode="mean")
        else:
            self._voxelizer = None
        self.ScorerUnet = Minkowski("unet",
                                    input_nc=self.Backbone.output_nc,
                                    num_layers=4,
                                    config=option.scorer_unet)
        self.ScorerEncoder = Minkowski("encoder",
                                       input_nc=self.Backbone.output_nc,
                                       num_layers=4,
                                       config=option.scorer_encoder)
        self.ScorerMLP = MLP([
            self.Backbone.output_nc, self.Backbone.output_nc,
            self.ScorerUnet.output_nc
        ])
        self.ScorerHead = Seq().append(
            torch.nn.Linear(self.ScorerUnet.output_nc,
                            1)).append(torch.nn.Sigmoid())

        self.Offset = Seq().append(
            MLP([self.Backbone.output_nc, self.Backbone.output_nc],
                bias=False))
        self.Offset.append(torch.nn.Linear(self.Backbone.output_nc, 3))

        self.Semantic = (Seq().append(
            MLP([self.Backbone.output_nc, self.Backbone.output_nc],
                bias=False)).append(
                    torch.nn.Linear(self.Backbone.output_nc,
                                    dataset.num_classes)).append(
                                        torch.nn.LogSoftmax(dim=-1)))
        self.loss_names = [
            "loss", "offset_norm_loss", "offset_dir_loss", "semantic_loss",
            "score_loss"
        ]
        stuff_classes = dataset.stuff_classes
        if is_list(stuff_classes):
            stuff_classes = torch.Tensor(stuff_classes).long()
        self._stuff_classes = torch.cat(
            [torch.tensor([IGNORE_LABEL]), stuff_classes])
Ejemplo n.º 6
0
    def __init__(self,
                 input_nc,
                 output_nc,
                 convolution,
                 dimension=3,
                 reduction=4):
        self.block = (Seq().append(
            convolution(
                in_channels=input_nc,
                out_channels=output_nc // reduction,
                kernel_size=1,
                stride=1,
                dilation=1,
                bias=False,
                dimension=dimension,
            )).append(ME.MinkowskiBatchNorm(output_nc // reduction)).append(
                ME.MinkowskiReLU()).append(
                    convolution(
                        output_nc // reduction,
                        output_nc // reduction,
                        kernel_size=3,
                        stride=1,
                        dilation=1,
                        bias=False,
                        dimension=dimension,
                    )).append(ME.MinkowskiBatchNorm(
                        output_nc //
                        reduction)).append(ME.MinkowskiReLU()).append(
                            convolution(
                                output_nc // reduction,
                                output_nc,
                                kernel_size=1,
                                stride=1,
                                dilation=1,
                                bias=False,
                                dimension=dimension,
                            )).append(ME.MinkowskiBatchNorm(output_nc)).append(
                                ME.MinkowskiReLU()))

        if input_nc != output_nc:
            self.downsample = (Seq().append(
                convolution(
                    in_channels=input_nc,
                    out_channels=output_nc,
                    kernel_size=1,
                    stride=1,
                    dilation=1,
                    bias=False,
                    dimension=dimension,
                )).append(ME.MinkowskiBatchNorm(output_nc)))
        else:
            self.downsample = None
Ejemplo n.º 7
0
    def __init__(self, option, model_type, dataset, modules):
        UnwrappedUnetBasedModel.__init__(self, option, model_type, dataset, modules)
        self.mode = option.loss_mode
        self.normalize_feature = option.normalize_feature
        self.loss_names = ["loss_reg", "loss"]
        self.metric_loss_module, self.miner_module = BaseModel.get_metric_loss_and_miner(
            getattr(option, "metric_loss", None), getattr(option, "miner", None)
        )
        # Last Layer

        if option.mlp_cls is not None:
            last_mlp_opt = option.mlp_cls
            in_feat = last_mlp_opt.nn[0]
            self.FC_layer = Seq()
            for i in range(1, len(last_mlp_opt.nn)):
                self.FC_layer.append(
                    str(i),
                    Sequential(
                        *[
                            Linear(in_feat, last_mlp_opt.nn[i], bias=False),
                            FastBatchNorm1d(last_mlp_opt.nn[i], momentum=last_mlp_opt.bn_momentum),
                            LeakyReLU(0.2),
                        ]
                    ),
                )
                in_feat = last_mlp_opt.nn[i]

            if last_mlp_opt.dropout:
                self.FC_layer.append(Dropout(p=last_mlp_opt.dropout))

            self.FC_layer.append(Linear(in_feat, in_feat, bias=False))
        else:
            self.FC_layer = torch.nn.Identity()
Ejemplo n.º 8
0
 def __init__(self, option, model_type, dataset, modules):
     # Last Layer
     BaseMS_SparseConv3d.__init__(self, option, model_type, dataset,
                                  modules)
     option_unet = option.option_unet
     num_scales = option_unet.num_scales
     self.unet = nn.ModuleList()
     for i in range(num_scales):
         module = UnetMSparseConv3d(
             option_unet.backbone,
             input_nc=option_unet.input_nc,
             grid_size=option_unet.grid_size[i],
             pointnet_nn=option_unet.pointnet_nn,
             post_mlp_nn=option_unet.post_mlp_nn,
             pre_mlp_nn=option_unet.pre_mlp_nn,
             add_pos=option_unet.add_pos,
             add_pre_x=option_unet.add_pre_x,
             aggr=option_unet.aggr,
             backend=option.backend,
         )
         self.unet.add_module(name=str(i), module=module)
     # Last MLP layer
     assert option.mlp_cls is not None
     last_mlp_opt = option.mlp_cls
     self.FC_layer = Seq()
     for i in range(1, len(last_mlp_opt.nn)):
         self.FC_layer.append(
             Sequential(*[
                 Linear(
                     last_mlp_opt.nn[i -
                                     1], last_mlp_opt.nn[i], bias=False),
                 FastBatchNorm1d(last_mlp_opt.nn[i],
                                 momentum=last_mlp_opt.bn_momentum),
                 LeakyReLU(0.2),
             ]))
Ejemplo n.º 9
0
    def __init__(self,
                 down_conv_nn=[],
                 kernel_size=2,
                 dilation=1,
                 dimension=3,
                 stride=2,
                 N=1,
                 block="ResBlock",
                 **kwargs):
        block = getattr(_res_blocks, block)
        ME.MinkowskiNetwork.__init__(self, dimension)
        if stride > 1:
            conv1_output = down_conv_nn[0]
        else:
            conv1_output = down_conv_nn[1]

        self.conv_in = (Seq().append(
            self.CONVOLUTION(
                in_channels=down_conv_nn[0],
                out_channels=conv1_output,
                kernel_size=kernel_size,
                stride=stride,
                dilation=dilation,
                bias=False,
                dimension=dimension,
            )).append(ME.MinkowskiBatchNorm(conv1_output)).append(
                ME.MinkowskiReLU()))

        if N > 0:
            self.blocks = Seq()
            for _ in range(N):
                self.blocks.append(
                    block(conv1_output,
                          down_conv_nn[1],
                          self.CONVOLUTION,
                          dimension=dimension))
                conv1_output = down_conv_nn[1]
        else:
            self.blocks = None
Ejemplo n.º 10
0
    def __init__(self, option, model_type, dataset, modules):
        BaseMS_SparseConv3d.__init__(self, option, model_type, dataset,
                                     modules)
        option_unet = option.option_unet
        self.grid_size = option_unet.grid_size
        self.unet = UnetMSparseConv3d(
            option_unet.backbone,
            input_nc=option_unet.input_nc,
            pointnet_nn=option_unet.pointnet_nn,
            post_mlp_nn=option_unet.post_mlp_nn,
            pre_mlp_nn=option_unet.pre_mlp_nn,
            add_pos=option_unet.add_pos,
            add_pre_x=option_unet.add_pre_x,
            aggr=option_unet.aggr,
            backend=option.backend,
        )
        assert option.mlp_cls is not None
        last_mlp_opt = option.mlp_cls
        self.FC_layer = Seq()
        for i in range(1, len(last_mlp_opt.nn)):
            self.FC_layer.append(
                Sequential(*[
                    Linear(
                        last_mlp_opt.nn[i -
                                        1], last_mlp_opt.nn[i], bias=False),
                    FastBatchNorm1d(last_mlp_opt.nn[i],
                                    momentum=last_mlp_opt.bn_momentum),
                    LeakyReLU(0.2),
                ]))

        # Intermediate loss
        if option.intermediate_loss is not None:
            int_loss_option = option.intermediate_loss
            self.int_metric_loss, _ = FragmentBaseModel.get_metric_loss_and_miner(
                getattr(int_loss_option, "metric_loss", None),
                getattr(int_loss_option, "miner", None))
            self.int_weights = int_loss_option.weights
            for i in range(len(int_loss_option.weights)):
                self.loss_names += ["loss_intermediate_loss_{}".format(i)]
        else:
            self.int_metric_loss = None
Ejemplo n.º 11
0
    def __init__(self, option, model_type, dataset, modules):
        BaseModel.__init__(self, option)
        option_unet = option.option_unet
        self.normalize_feature = option.normalize_feature
        self.grid_size = option_unet.grid_size
        self.unet = UnetMSparseConv3d(
            option_unet.backbone,
            input_nc=option_unet.input_nc,
            pointnet_nn=option_unet.pointnet_nn,
            post_mlp_nn=option_unet.post_mlp_nn,
            pre_mlp_nn=option_unet.pre_mlp_nn,
            add_pos=option_unet.add_pos,
            add_pre_x=option_unet.add_pre_x,
            aggr=option_unet.aggr,
            backend=option.backend,
        )
        if option.mlp_cls is not None:
            last_mlp_opt = option.mlp_cls

            self.FC_layer = Seq()
            for i in range(1, len(last_mlp_opt.nn)):
                self.FC_layer.append(
                    nn.Sequential(*[
                        nn.Linear(last_mlp_opt.nn[i - 1],
                                  last_mlp_opt.nn[i],
                                  bias=False),
                        FastBatchNorm1d(last_mlp_opt.nn[i],
                                        momentum=last_mlp_opt.bn_momentum),
                        nn.LeakyReLU(0.2),
                    ]))
            if last_mlp_opt.dropout:
                self.FC_layer.append(nn.Dropout(p=last_mlp_opt.dropout))
        else:
            self.FC_layer = torch.nn.Identity()
        self.head = nn.Sequential(
            nn.Linear(option.output_nc, dataset.num_classes))
        self.loss_names = ["loss_seg"]
Ejemplo n.º 12
0
    def __init__(self, option, model_type, dataset, modules):
        super(VoteNet2, self).__init__(option)
        self._dataset = dataset
        # 1 - CREATE BACKBONE MODEL
        input_nc = dataset.feature_dimension
        backbone_option = option.backbone
        backbone_cls = getattr(models, backbone_option.model_type)
        backbone_extr_options = backbone_option.get("extra_options", {})
        self.backbone_model = backbone_cls(architecture="unet",
                                           input_nc=input_nc,
                                           num_layers=4,
                                           config=backbone_option.config,
                                           **backbone_extr_options)
        self._kpconv_backbone = backbone_cls.__name__ == "KPConv"
        self.is_dense_format = self.conv_type == "DENSE"
        dropout = option.get("dropout", None)
        if dropout is not None:
            self.dropout = torch.nn.Dropout(dropout)
        else:
            self.dropout = None

        # 2 - SEGMENTATION HEAD
        semantic_supervision = option.get("semantic_supervision", False)
        if semantic_supervision:
            self.Semantic = (Seq().append(
                MLP([
                    self.backbone_model.output_nc,
                    self.backbone_model.output_nc
                ],
                    bias=False)).append(
                        torch.nn.Linear(self.backbone_model.output_nc,
                                        dataset.num_classes)).append(
                                            torch.nn.LogSoftmax()))
        else:
            self.Semantic = None

        # 3 - CREATE VOTING MODEL
        voting_option = option.voting
        self._num_seeds = voting_option.num_points_to_sample
        voting_cls = getattr(votenet_module, voting_option.module_name)
        self.voting_module = voting_cls(
            vote_factor=voting_option.vote_factor,
            seed_feature_dim=self.backbone_model.output_nc)

        # 4 - CREATE PROPOSAL MODULE
        proposal_option = option.proposal
        proposal_option.vote_aggregation.down_conv_nn = [[
            self.backbone_model.output_nc + 3,
            self.backbone_model.output_nc,
            self.backbone_model.output_nc,
        ]]
        proposal_cls = getattr(votenet_module, proposal_option.module_name)
        self.proposal_cls_module = proposal_cls(
            num_class=proposal_option.num_class,
            vote_aggregation_config=proposal_option.vote_aggregation,
            num_heading_bin=proposal_option.num_heading_bin,
            mean_size_arr=dataset.mean_size_arr,
            num_proposal=proposal_option.num_proposal,
            sampling=proposal_option.sampling,
        )

        # Loss params
        self.loss_params = option.loss_params
        self.loss_params.num_heading_bin = proposal_option.num_heading_bin
        self.loss_params.mean_size_arr = dataset.mean_size_arr.tolist()

        self.losses_has_been_added = False
        self.loss_names = []