Exemplo n.º 1
0
    def __init__(self,
                 *,
                 npoint: int,
                 radii: List[float],
                 nsamples: List[int],
                 mlps: List[List[int]],
                 use_xyz: bool = True,
                 bias=True,
                 init=nn.init.kaiming_normal_,
                 first_layer=False,
                 relation_prior=1):
        super().__init__()
        assert len(radii) == len(nsamples) == len(mlps)
        self.npoint = npoint
        self.groupers = nn.ModuleList()
        self.mlps = nn.ModuleList()

        # initialize shared mapping functions
        C_in = (mlps[0][0] + 3) if use_xyz else mlps[0][0]
        C_out = mlps[0][1]
        """# my code:
        self.C_in_att = mlps[0][0]+3
        self.C_out_att = self.C_in_att if first_layer else math.floor(self.C_in_att/4)
        self.mlp_for_att1 = nn.Conv1d(in_channels = self.C_in_att, out_channels = self.C_out_att, 
                                    kernel_size = 1, stride = 1, bias = False)
        self.mlp_for_att2 = nn.Conv1d(in_channels = self.C_out_att, out_channels = 1, 
                                    kernel_size = 1, stride = 1, bias = False)
        nn.init.kaiming_normal_(self.mlp_for_att1.weight)
        nn.init.kaiming_normal_(self.mlp_for_att2.weight)
        self.first_layer = first_layer
        """

        if relation_prior == 0:
            in_channels = 1
        elif relation_prior == 1 or relation_prior == 2:
            in_channels = 10
        else:
            assert False, "relation_prior can only be 0, 1, 2."

        if first_layer:
            mapping_func1 = nn.Conv2d(in_channels=in_channels,
                                      out_channels=math.floor(C_out / 2),
                                      kernel_size=(1, 1),
                                      stride=(1, 1),
                                      bias=bias)
            mapping_func2 = nn.Conv2d(in_channels=math.floor(C_out / 2),
                                      out_channels=16,
                                      kernel_size=(1, 1),
                                      stride=(1, 1),
                                      bias=bias)
            xyz_raising = nn.Conv2d(in_channels=C_in,
                                    out_channels=16,
                                    kernel_size=(1, 1),
                                    stride=(1, 1),
                                    bias=bias)
            init(xyz_raising.weight)
            if bias:
                nn.init.constant_(xyz_raising.bias, 0)
        elif npoint is not None:
            mapping_func1 = nn.Conv2d(in_channels=in_channels,
                                      out_channels=math.floor(C_out / 4),
                                      kernel_size=(1, 1),
                                      stride=(1, 1),
                                      bias=bias)
            mapping_func2 = nn.Conv2d(in_channels=math.floor(C_out / 4),
                                      out_channels=C_in,
                                      kernel_size=(1, 1),
                                      stride=(1, 1),
                                      bias=bias)
        if npoint is not None:
            init(mapping_func1.weight)
            init(mapping_func2.weight)
            if bias:
                nn.init.constant_(mapping_func1.bias, 0)
                nn.init.constant_(mapping_func2.bias, 0)

            # channel raising mapping
            cr_mapping = nn.Conv1d(in_channels=C_in if not first_layer else 16,
                                   out_channels=C_out,
                                   kernel_size=1,
                                   stride=1,
                                   bias=bias)
            init(cr_mapping.weight)
            nn.init.constant_(cr_mapping.bias, 0)

        if first_layer:
            mapping = [mapping_func1, mapping_func2, cr_mapping, xyz_raising]
        elif npoint is not None:
            mapping = [mapping_func1, mapping_func2, cr_mapping]

        for i in range(len(radii)):
            radius = radii[i]
            nsample = nsamples[i]
            self.groupers.append(
                pointnet2_utils.QueryAndGroup(radius, nsample, use_xyz=use_xyz)
                if npoint is not None else pointnet2_utils.GroupAll(use_xyz))
            mlp_spec = mlps[i]
            if use_xyz:
                mlp_spec[0] += 3
            if npoint is not None:
                self.mlps.append(
                    pt_utils.SharedRSConv(mlp_spec,
                                          mapping=mapping,
                                          relation_prior=relation_prior,
                                          first_layer=first_layer))
            else:  # global convolutional pooling
                self.mlps.append(pt_utils.GloAvgConv(C_in=C_in, C_out=C_out))
Exemplo n.º 2
0
    def __init__(
            self,
            *,
            npoint: int,
            radii: List[float],
            nsamples: List[int],
            mlps: List[List[int]],
            use_xyz: bool = True,
            bias=True,
            init=nn.init.kaiming_normal,
            first_layer=False,
            last_layer=False,
            scale_num=1,
            rel_pose_mode="first"
    ):
        super().__init__()
        assert len(radii) == len(nsamples) == len(mlps)
        self.npoint = npoint
        self.groupers = nn.ModuleList()
        self.mlps = nn.ModuleList()
        
        # initialize shared mapping functions
        C_in = (mlps[0][0] + 3) if use_xyz else mlps[0][0]
        C_out = mlps[0][1]

        if first_layer:
            in_channels = 7
        else:
            in_channels = 10

        if first_layer:
            mapping_func1 = nn.Conv2d(in_channels=in_channels, out_channels=math.floor(C_out / 2), kernel_size=(1, 1),
                                      stride=(1, 1), bias=bias)
            mapping_func2 = nn.Conv2d(in_channels=math.floor(C_out / 2), out_channels=16, kernel_size=(1, 1),
                                  stride=(1, 1), bias=bias)
            xyz_raising = nn.Conv2d(in_channels=C_in, out_channels=16, kernel_size=(1, 1),
                                  stride=(1, 1), bias=bias)
            init(xyz_raising.weight)
            if bias:
                nn.init.constant(xyz_raising.bias, 0)
        elif npoint is not None:
            mapping_func1 = nn.Conv2d(in_channels=in_channels, out_channels=math.floor(C_out / 4), kernel_size=(1, 1),
                                      stride=(1, 1), bias=bias)
            mapping_func2 = nn.Conv2d(in_channels=math.floor(C_out / 4), out_channels=C_in, kernel_size=(1, 1),
                                  stride=(1, 1), bias=bias)

        if npoint is not None:
            init(mapping_func1.weight)
            init(mapping_func2.weight)
            if bias:
                nn.init.constant(mapping_func1.bias, 0)
                nn.init.constant(mapping_func2.bias, 0)    
                     
            # channel raising mapping
            cr_mapping = nn.Conv1d(in_channels=C_in if not first_layer else 16, out_channels=C_out, kernel_size=1,
                                      stride=1, bias=bias)
            init(cr_mapping.weight)
            nn.init.constant(cr_mapping.bias, 0)
        
        if first_layer:
            mapping = [mapping_func1, mapping_func2, cr_mapping, xyz_raising]
        elif npoint is not None:
            mapping = [mapping_func1, mapping_func2, cr_mapping]
        
        for i in range(len(radii)):
            radius = radii[i]
            nsample = nsamples[i]
            self.groupers.append(
                pointnet2_utils.QueryAndGroup(radius, nsample, use_xyz=use_xyz)
                if npoint is not None else pointnet2_utils.GroupAll(False) # modified
            )
            mlp_spec = mlps[i]
            if use_xyz:
                mlp_spec[0] += 3

            if npoint is not None:
                self.mlps.append(pt_utils.SharedRSConv(mlp_spec, mapping = mapping, first_layer = first_layer, last_layer=last_layer, scale_num=scale_num, rel_pose_mode=rel_pose_mode))
            else:
                # global pooling
                self.mlps.append(pt_utils.RSCNNGloAvgConv(C_in = C_in, C_out = C_out))