-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
152 lines (122 loc) · 5.8 KB
/
dataloader.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import numpy as np
import math
import random
import os
import torch
from path import Path
path = Path("ModelNet10-ttv") #folder where dataset is located
folders = [dir for dir in sorted(os.listdir(path)) if os.path.isdir(path/dir)]
classes = {folder: i for i, folder in enumerate(folders)};
def read_off(file): #to read pointclouds
if 'OFF' != file.readline().strip():
raise('Not a valid OFF header')
n_verts, n_faces, __ = tuple([int(s) for s in file.readline().strip().split(' ')])
verts = [[float(s) for s in file.readline().strip().split(' ')] for i_vert in range(n_verts)]
faces = [[int(s) for s in file.readline().strip().split(' ')][1:] for i_face in range(n_faces)]
return verts, faces
class PointSampler(object):
def __init__(self, output_size):
assert isinstance(output_size, int)
self.output_size = output_size
def triangle_area(self, pt1, pt2, pt3):
side_a = np.linalg.norm(pt1 - pt2)
side_b = np.linalg.norm(pt2 - pt3)
side_c = np.linalg.norm(pt3 - pt1)
s = 0.5 * ( side_a + side_b + side_c)
return max(s * (s - side_a) * (s - side_b) * (s - side_c), 0)**0.5
def sample_point(self, pt1, pt2, pt3):
# barycentric coordinates on a triangle
# https://mathworld.wolfram.com/BarycentricCoordinates.html
s, t = sorted([random.random(), random.random()])
f = lambda i: s * pt1[i] + (t-s)*pt2[i] + (1-t)*pt3[i]
return (f(0), f(1), f(2))
def __call__(self, mesh):
verts, faces = mesh
verts = np.array(verts)
areas = np.zeros((len(faces)))
for i in range(len(areas)):
areas[i] = (self.triangle_area(verts[faces[i][0]],
verts[faces[i][1]],
verts[faces[i][2]]))
sampled_faces = (random.choices(faces,
weights=areas,
cum_weights=None,
k=self.output_size))
sampled_points = np.zeros((self.output_size, 3))
for i in range(len(sampled_faces)):
sampled_points[i] = (self.sample_point(verts[sampled_faces[i][0]],
verts[sampled_faces[i][1]],
verts[sampled_faces[i][2]]))
return sampled_points
class Normalize(object): #To normalize
def __call__(self, pointcloud):
assert len(pointcloud.shape)==2
norm_pointcloud = pointcloud - np.mean(pointcloud, axis=0)
norm_pointcloud /= np.max(np.linalg.norm(norm_pointcloud, axis=1))
return norm_pointcloud
class RandRotation_z(object): #adding random rotations
def __call__(self, pointcloud):
assert len(pointcloud.shape)==2
theta = random.random() * 2. * math.pi
rot_matrix = np.array([[ math.cos(theta), -math.sin(theta), 0],
[ math.sin(theta), math.cos(theta), 0],
[0, 0, 1]])
rot_pointcloud = rot_matrix.dot(pointcloud.T).T
return rot_pointcloud
class RandomNoise(object): #adding random noise
def __call__(self, pointcloud):
assert len(pointcloud.shape)==2
noise = np.random.normal(0, 0.02, (pointcloud.shape))
noisy_pointcloud = pointcloud + noise
return noisy_pointcloud
class ToTensor(object): #converting to tensor
def __call__(self, pointcloud):
assert len(pointcloud.shape)==2
return torch.from_numpy(pointcloud)
def default_transforms():#this is for test and validation data
return transforms.Compose([
PointSampler(1024),
Normalize(),
ToTensor()
])
train_transforms = transforms.Compose([ #this is for train data
PointSampler(1024),
Normalize(),
RandRotation_z(),
# RandomNoise(),
ToTensor()
])
class PointCloudData(Dataset): #Dataset class
def __init__(self, root_dir, valid=False, test=False, folder="train", transform=default_transforms()):
self.root_dir = root_dir
folders = [dir for dir in sorted(os.listdir(root_dir)) if os.path.isdir(root_dir/dir)]
self.classes = {folder: i for i, folder in enumerate(folders)}
self.transforms = transform if not valid else default_transforms()
self.valid = valid
self.test = test
self.files = []
for category in self.classes.keys():
new_dir = root_dir/Path(category)/folder
for file in os.listdir(new_dir):
if file.endswith('.off'):
sample = {}
sample['pcd_path'] = new_dir/file
sample['category'] = category
self.files.append(sample)
def __len__(self):
return len(self.files)
def __preproc__(self, file):
verts, faces = read_off(file)
if self.transforms:
pointcloud = self.transforms((verts, faces))
return pointcloud
def __getitem__(self, idx):
pcd_path = self.files[idx]['pcd_path']
category = self.files[idx]['category']
with open(pcd_path, 'r') as f:
pointcloud = self.__preproc__(f)
return {'pointcloud': pointcloud,
'category': self.classes[category]}