-
Notifications
You must be signed in to change notification settings - Fork 5
/
dataset.py
112 lines (94 loc) · 4.37 KB
/
dataset.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
import config
import sys
sys.path.insert(0, config.mxnet_path)
import mxnet as mx
import mxnet.ndarray as nd
import numpy as np
import provider
import os
class DummyIter(mx.io.DataIter):
def __init__(self, batch_size, num_point, files, is_training=True):
super(DummyIter, self).__init__(batch_size)
# self.batch = mx.io.DataBatch(data=[mx.nd.zeros(self.data_shape)],
# label=[mx.nd.zeros(self.label_shape)])
self.batch_size = batch_size
self.num_point = num_point
self.num_batches = 0
self.files = files
self.is_train = is_training
self.data_shape = (batch_size, num_point, 3)
self.label_shape = (batch_size,)
self.provide_data = [('data', self.data_shape)]
self.provide_label = [('softmax_label', self.label_shape)]
self.train_file_idxs = np.arange(0, len(files))
np.random.shuffle(self.train_file_idxs)
self.cur_file_idx = 0
self.cur_batch = 0
self.current_data = []
self.current_label = []
self.load_datafile(self.cur_file_idx)
def next(self):
if self.cur_batch < self.num_batches:
start_idx = self.cur_batch * self.batch_size
end_idx = (self.cur_batch + 1) * self.batch_size
self.cur_batch += 1
return self.get_batch(start_idx, end_idx)
else:
if self.cur_file_idx == len(self.train_file_idxs)-1:
self.cur_batch = 0
raise StopIteration
else:
self.cur_file_idx += 1
self.cur_batch = 0
self.load_datafile(self.cur_file_idx)
start_idx = self.cur_batch * self.batch_size
end_idx = (self.cur_batch + 1) * self.batch_size
self.cur_batch += 1
return self.get_batch(start_idx, end_idx)
def get_batch(self, start_idx, end_idx):
if self.is_train:
# Augment batched point clouds by rotation and jittering
rotated_data = provider.rotate_point_cloud(self.current_data[start_idx:end_idx, :, :])
jittered_data = provider.jitter_point_cloud(rotated_data)
label = self.current_label[start_idx:end_idx]
return mx.io.DataBatch(data=[nd.array(jittered_data)], label=[nd.array(label)],
provide_data=self.provide_data, provide_label=self.provide_label)
else:
data = self.current_data[start_idx:end_idx, :, :]
label = self.current_label[start_idx:end_idx]
return mx.io.DataBatch(data=[nd.array(data)], label=[nd.array(label)],
provide_data=self.provide_data, provide_label=self.provide_label)
def load_datafile(self, file_idx):
self.current_data, self.current_label = provider.loadDataFile(self.files[self.train_file_idxs[file_idx]])
self.current_data = self.current_data[:, 0:self.num_point, :]
self.current_data, self.current_label, _ = provider.shuffle_data(self.current_data, np.squeeze(self.current_label))
self.current_label = np.squeeze(self.current_label)
file_size = self.current_data.shape[0]
self.num_batches = file_size // self.batch_size
def reset(self):
np.random.shuffle(self.train_file_idxs)
self.cur_file_idx = 0
self.cur_batch = 0
self.current_data = []
self.current_label = []
self.load_datafile(self.cur_file_idx)
def dummy_iterator(batch_size, num_point, train_files, val_files):
train_iter = DummyIter(batch_size, num_point, train_files)
val_iter = DummyIter(batch_size, num_point, val_files, is_training=False)
num_examples = 9840
return train_iter, val_iter, num_examples
if __name__ == '__main__':
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# ModelNet40 official train/test split
TRAIN_FILES = provider.getDataFiles( \
os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/train_files.txt'))
TEST_FILES = provider.getDataFiles( \
os.path.join(BASE_DIR, 'data/modelnet40_ply_hdf5_2048/test_files.txt'))
NUM_POINT = 1024
train_iter, val_iter, _ = dummy_iterator(1, NUM_POINT, train_files=TRAIN_FILES, val_files=TEST_FILES)
for iter in [train_iter, val_iter]:
num_batch = 0
for i in iter:
# print(i)
num_batch+=1
print(num_batch)