-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
63 lines (53 loc) · 2.1 KB
/
Copy pathutils.py
File metadata and controls
63 lines (53 loc) · 2.1 KB
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
import trimesh
from matplotlib import pyplot as plt
import MinkowskiEngine as ME
import torch
import torch.nn.functional as F
# display the mesh
def show_mesh(path):
mesh = trimesh.load(path)
return mesh.show()
# display the point cloud sampled from the mesh
def show_point_cloud(path, N = 2048):
mesh = trimesh.load(path)
points = mesh.sample(N)
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection="3d")
ax.scatter(points[:, 0], points[:, 1], points[:, 2])
ax.set_axis_off()
# display points of the point cloud
def display_points(points):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection="3d")
ax.scatter(points[:, 0], points[:, 1], points[:, 2])
ax.set_axis_off()
# Collating batches into Minkowski sparse tensor
def collate_function(batch_data):
coordinates, features, labels = [], [], []
for batch in batch_data:
coordinates.append(batch["coordinates"])
features.append(batch["features"])
labels.append(batch["labels"])
coordinates, features, labels = ME.utils.sparse_collate(coordinates, features, labels, dtype=torch.float32)
return {
"coordinates": coordinates,
"features": features,
"labels": torch.tensor(labels, dtype=torch.int64)
}
# Creating a batch for training
def create_input_batch(batch, device="cpu", quantization_size=0.05):
batch["coordinates"][:, 1:] = batch["coordinates"][:, 1:] / quantization_size
return ME.TensorField(coordinates=batch["coordinates"], features=batch["features"], device=device)
# Calculating cross entropy loss and label smoothing
def criterion(pred, labels, smoothing=True):
labels = labels.contiguous().view(-1)
if smoothing:
eps = 0.2
n_class = pred.size(1)
one_hot = torch.zeros_like(pred).scatter(1, labels.view(-1, 1), 1)
one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1)
log_prb = F.log_softmax(pred, dim=1)
loss = -(one_hot * log_prb).sum(dim=1).mean()
else:
loss = F.cross_entropy(pred, labels, reduction="mean")
return loss