-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
108 lines (85 loc) · 3.12 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import torch
import torch.nn as nn
import torch.optim as optim
class NonLinear(nn.Module):
def __init__(self, input_channels, output_channels):
super(NonLinear, self).__init__()
self.input_channels = input_channels
self.output_channels = output_channels
self.main = nn.Sequential(
nn.Linear(self.input_channels, self.output_channels),
nn.ReLU(inplace=True),
nn.BatchNorm1d(self.output_channels))
def forward(self, input_data):
return self.main(input_data)
class MaxPool(nn.Module):
def __init__(self, num_channels, num_points):
super(MaxPool, self).__init__()
self.num_channels = num_channels
self.num_points = num_points
self.main = nn.MaxPool1d(self.num_points)
def forward(self, input_data):
out = input_data.view(-1, self.num_channels, self.num_points)
out = self.main(out)
out = out.view(-1, self.num_channels)
return out
class InputTNet(nn.Module):
def __init__(self, num_points):
super(InputTNet, self).__init__()
self.num_points = num_points
self.main = nn.Sequential(
NonLinear(3, 64),
NonLinear(64, 128),
NonLinear(128, 1024),
MaxPool(1024, self.num_points),
NonLinear(1024, 512),
NonLinear(512, 256),
nn.Linear(256, 9)
)
# shape of input_data is (batchsize x num_points, channel)
def forward(self, input_data):
matrix = self.main(input_data).view(-1, 3, 3)
out = torch.matmul(input_data.view(-1, self.num_points, 3), matrix)
out = out.view(-1, 3)
return out
class FeatureTNet(nn.Module):
def __init__(self, num_points):
super(FeatureTNet, self).__init__()
self.num_points = num_points
self.main = nn.Sequential(
NonLinear(64, 64),
NonLinear(64, 128),
NonLinear(128, 1024),
MaxPool(1024, self.num_points),
NonLinear(1024, 512),
NonLinear(512, 256),
nn.Linear(256, 4096)
)
# shape of input_data is (batchsize x num_points, channel)
def forward(self, input_data):
matrix = self.main(input_data).view(-1, 64, 64)
out = torch.matmul(input_data.view(-1, self.num_points, 64), matrix)
out = out.view(-1, 64)
return out
class PointNet(nn.Module):
def __init__(self, num_points, num_labels):
super(PointNet, self).__init__()
self.num_points = num_points
self.num_labels = num_labels
self.main = nn.Sequential(
InputTNet(self.num_points),
NonLinear(3, 64),
NonLinear(64, 64),
FeatureTNet(self.num_points),
NonLinear(64, 64),
NonLinear(64, 128),
NonLinear(128, 1024),
MaxPool(1024, self.num_points),
NonLinear(1024, 512),
nn.Dropout(p = 0.3),
NonLinear(512, 256),
nn.Dropout(p = 0.3),
NonLinear(256, self.num_labels),
)
def forward(self, input_data):
return self.main(input_data)