-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathdata.py
161 lines (147 loc) · 7.41 KB
/
data.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
153
154
155
156
157
158
159
160
161
import numpy as np
import h5py
import os
import os.path as osp
import shutil
from glob import glob
import torch
from torch_geometric.data import InMemoryDataset, Data, extract_zip
from tqdm import tqdm
def scale_translate_pointcloud(pointcloud, shift=[-0.2, 0.2], scale=[2. / 3., 3. /2.]):
"""
for scaling and shifting the point cloud
:param pointcloud:
:return:
"""
B, C, N = pointcloud.shape[0:3]
scale = scale[0] + torch.rand([B, C, 1, 1])*(scale[1]-scale[0])
shift = shift[0] + torch.rand([B, C, 1, 1]) * (shift[1]-shift[0])
translated_pointcloud = torch.mul(pointcloud, scale) + shift
return translated_pointcloud
class PartNet(InMemoryDataset):
r"""The PartNet dataset from
the `"PartNet: A Large-scale Benchmark for Fine-grained and Hierarchical Part-level 3D Object Understanding"
<https://arxiv.org/abs/1812.02713>`_
paper, containing 3D objects annotated with fine-grained, instance-level, and hierarchical 3D part information.
Args:
root (string): Root directory where the dataset should be saved.
dataset (str, optional): Which dataset to use (ins_seg_h5, or sem_seg_h5).
(default: :obj:`sem_seg_h5`)
obj_category (str, optional): which category to load.
(default: :obj:`Bed`)
level (str, optional): Which level of part semantic segmentation to use.
(default: :obj:`3`)
phase (str, optional): If :obj:`test`, loads the testing dataset,
If :obj:`val`, loads the validation dataset,
otherwise the training dataset. (default: :obj:`train`)
transform (callable, optional): A function/transform that takes in an
:obj:`torch_geometric.data.Data` object and returns a transformed
version. The data object will be transformed before every access.
(default: :obj:`None`)
pre_transform (callable, optional): A function/transform that takes in
an :obj:`torch_geometric.data.Data` object and returns a
transformed version. The data object will be transformed before
being saved to disk. (default: :obj:`None`)
pre_filter (callable, optional): A function that takes in an
:obj:`torch_geometric.data.Data` object and returns a boolean
value, indicating whether the data object should be included in the
final dataset. (default: :obj:`None`)
"""
# the dataset we use for our paper is pre-released version
def __init__(self,
root,
dataset='sem_seg_h5',
obj_category='Bed',
level=3,
phase='train',
transform=None,
pre_transform=None,
pre_filter=None):
self.dataset = dataset
self.level = level
self.obj_category = obj_category
self.object = '-'.join([self.obj_category, str(self.level)])
self.level_folder = 'level_'+str(self.level)
self.processed_file_folder = osp.join(self.dataset, self.level_folder, self.object)
super(PartNet, self).__init__(root, transform, pre_transform, pre_filter)
if phase == 'test':
path = self.processed_paths[1]
elif phase == 'val':
path = self.processed_paths[2]
else:
path = self.processed_paths[0]
self.data, self.slices = torch.load(path)
@property
def raw_file_names(self):
return [self.dataset]
@property
def processed_file_names(self):
return osp.join(self.processed_file_folder, 'train.pt'), osp.join(self.processed_file_folder, 'test.pt'), \
osp.join(self.processed_file_folder, 'val.pt')
def download(self):
path = osp.join(self.raw_dir, self.dataset)
if not osp.exists(path):
raise FileExistsError('PartNet can only downloaded via application. '
'See details in https://cs.stanford.edu/~kaichun/partnet/')
# path = download_url(self.url, self.root)
extract_zip(path, self.root)
os.unlink(path)
shutil.rmtree(self.raw_dir)
name = self.url.split(os.sep)[-1].split('.')[0]
os.rename(osp.join(self.root, name), self.raw_dir)
def process(self):
# save to processed_paths
processed_path = osp.join(self.processed_dir, self.processed_file_folder)
if not osp.exists(processed_path):
os.makedirs(osp.join(processed_path))
torch.save(self.process_set('train'), self.processed_paths[0])
torch.save(self.process_set('test'), self.processed_paths[1])
torch.save(self.process_set('val'), self.processed_paths[2])
def process_set(self, dataset):
if self.dataset == 'ins_seg_h5':
raw_path = osp.join(self.raw_dir, 'ins_seg_h5_for_sgpn', self.dataset)
categories = glob(osp.join(raw_path, '*'))
categories = sorted([x.split(os.sep)[-1] for x in categories])
data_list = []
for target, category in enumerate(tqdm(categories)):
folder = osp.join(raw_path, category)
paths = glob('{}/{}-*.h5'.format(folder, dataset))
labels, nors, opacitys, pts, rgbs = [], [], [], [], []
for path in paths:
f = h5py.File(path)
pts += torch.from_numpy(f['pts'][:]).unbind(0)
labels += torch.from_numpy(f['label'][:]).to(torch.long).unbind(0)
nors += torch.from_numpy(f['nor'][:]).unbind(0)
opacitys += torch.from_numpy(f['opacity'][:]).unbind(0)
rgbs += torch.from_numpy(f['rgb'][:]).to(torch.float32).unbind(0)
for i, (pt, label, nor, opacity, rgb) in enumerate(zip(pts, labels, nors, opacitys, rgbs)):
data = Data(pos=pt[:, :3], y=label, norm=nor[:, :3], x=torch.cat((opacity.unsqueeze(-1), rgb/255.), 1))
if self.pre_filter is not None and not self.pre_filter(data):
continue
if self.pre_transform is not None:
data = self.pre_transform(data)
data_list.append(data)
else:
raw_path = osp.join(self.raw_dir, self.dataset)
categories = glob(osp.join(raw_path, self.object))
categories = sorted([x.split(os.sep)[-1] for x in categories])
data_list = []
# class_name = []
for target, category in enumerate(tqdm(categories)):
folder = osp.join(raw_path, category)
paths = glob('{}/{}-*.h5'.format(folder, dataset))
labels, pts = [], []
# clss = category.split('-')[0]
for path in paths:
f = h5py.File(path)
pts += torch.from_numpy(f['data'][:].astype(np.float32)).unbind(0)
labels += torch.from_numpy(f['label_seg'][:].astype(np.float32)).to(torch.long).unbind(0)
for i, (pt, label) in enumerate(zip(pts, labels)):
data = Data(pos=pt[:, :3], y=label)
# data = PartData(pos=pt[:, :3], y=label, clss=clss)
if self.pre_filter is not None and not self.pre_filter(data):
continue
if self.pre_transform is not None:
data = self.pre_transform(data)
data_list.append(data)
return self.collate(data_list)