-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
pointnet2_classification.py
109 lines (84 loc) · 3.47 KB
/
pointnet2_classification.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
109
import os.path as osp
import torch
import torch.nn.functional as F
import torch_geometric.transforms as T
from torch_geometric.datasets import ModelNet
from torch_geometric.loader import DataLoader
from torch_geometric.nn import MLP, PointNetConv, fps, global_max_pool, radius
from torch_geometric.typing import WITH_TORCH_CLUSTER
if not WITH_TORCH_CLUSTER:
quit("This example requires 'torch-cluster'")
class SAModule(torch.nn.Module):
def __init__(self, ratio, r, nn):
super().__init__()
self.ratio = ratio
self.r = r
self.conv = PointNetConv(nn, add_self_loops=False)
def forward(self, x, pos, batch):
idx = fps(pos, batch, ratio=self.ratio)
row, col = radius(pos, pos[idx], self.r, batch, batch[idx],
max_num_neighbors=64)
edge_index = torch.stack([col, row], dim=0)
x_dst = None if x is None else x[idx]
x = self.conv((x, x_dst), (pos, pos[idx]), edge_index)
pos, batch = pos[idx], batch[idx]
return x, pos, batch
class GlobalSAModule(torch.nn.Module):
def __init__(self, nn):
super().__init__()
self.nn = nn
def forward(self, x, pos, batch):
x = self.nn(torch.cat([x, pos], dim=1))
x = global_max_pool(x, batch)
pos = pos.new_zeros((x.size(0), 3))
batch = torch.arange(x.size(0), device=batch.device)
return x, pos, batch
class Net(torch.nn.Module):
def __init__(self):
super().__init__()
# Input channels account for both `pos` and node features.
self.sa1_module = SAModule(0.5, 0.2, MLP([3, 64, 64, 128]))
self.sa2_module = SAModule(0.25, 0.4, MLP([128 + 3, 128, 128, 256]))
self.sa3_module = GlobalSAModule(MLP([256 + 3, 256, 512, 1024]))
self.mlp = MLP([1024, 512, 256, 10], dropout=0.5, norm=None)
def forward(self, data):
sa0_out = (data.x, data.pos, data.batch)
sa1_out = self.sa1_module(*sa0_out)
sa2_out = self.sa2_module(*sa1_out)
sa3_out = self.sa3_module(*sa2_out)
x, pos, batch = sa3_out
return self.mlp(x).log_softmax(dim=-1)
def train(epoch):
model.train()
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
loss = F.nll_loss(model(data), data.y)
loss.backward()
optimizer.step()
def test(loader):
model.eval()
correct = 0
for data in loader:
data = data.to(device)
with torch.no_grad():
pred = model(data).max(1)[1]
correct += pred.eq(data.y).sum().item()
return correct / len(loader.dataset)
if __name__ == '__main__':
path = osp.join(osp.dirname(osp.realpath(__file__)), '..',
'data/ModelNet10')
pre_transform, transform = T.NormalizeScale(), T.SamplePoints(1024)
train_dataset = ModelNet(path, '10', True, transform, pre_transform)
test_dataset = ModelNet(path, '10', False, transform, pre_transform)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True,
num_workers=6)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False,
num_workers=6)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for epoch in range(1, 201):
train(epoch)
test_acc = test(test_loader)
print(f'Epoch: {epoch:03d}, Test: {test_acc:.4f}')