-
Notifications
You must be signed in to change notification settings - Fork 14
/
curvature_encoder.py
194 lines (154 loc) · 5.53 KB
/
curvature_encoder.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright 2025 Thousand Brains Project
# Copyright 2023-2024 Numenta Inc.
#
# Copyright may exist in Contributors' modifications
# and/or contributions to the work.
#
# Use of this source code is governed by the MIT
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
import argparse
import hashlib
import os
import pickle
import time
import numpy as np
import ray
import torch
def hash_curvature_for_order(curvature):
return int(int(hashlib.md5(curvature.tobytes()).hexdigest(), 16)) / (2**128)
@ray.remote
def hash_slice_for_order(s):
return np.array(list(map(hash_curvature_for_order, s)))
def hash_curvature_for_sdr(curvature):
rng = np.random.default_rng(
int(int(hashlib.md5(curvature.tobytes()).hexdigest(), 16))
)
return rng.integers(0, N, size=1)
@ray.remote
def hash_slice_for_sdr(s):
return np.array(list(map(hash_curvature_for_sdr, s)))
if __name__ == "__main__":
ray.init()
# args parser
parser = argparse.ArgumentParser(description="Create SDRS for curvatures.")
parser.add_argument(
"-sdr_p",
type=str,
default="~/tbp/tbp.monty/projects/temporal_memory/tm_dataset",
help="Enter SDR_YCBMeshDataset relative path in the form of: ~/path/of/dataset",
)
parser.add_argument(
"-r", type=int, nargs="?", default=5,
help="Hash radius for Gaussian curvature *amount*."
)
parser.add_argument(
"-d1", type=int, nargs="?", default=0,
help="Point cloud start index (inclusive) to save from the YCB dataset."
)
parser.add_argument(
"-d2", type=int, nargs="?", default=20,
help="Point cloud end index (exclusive) to save from the YCB dataset."
)
parser.add_argument(
"-n", type=int, nargs="?", default=1024,
help="Size of SDR."
)
parser.add_argument(
"-w", type=int, nargs="?", default=11,
help="Number of 'on' bits in the SDR."
)
args = parser.parse_args()
sdr_dataset_path = os.path.expanduser(args.sdr_p)
hash_radius = args.r
data_range = (args.d1, args.d2)
N = args.n
W = args.w
assert (N > (11 * W))
# load dataset
coordinate_dir = os.path.join(sdr_dataset_path, "coordinate_data")
curvature_dir = os.path.join(sdr_dataset_path, "curvature_data")
# get processed coordinates and curvatures
processed_coord_file = os.path.join(coordinate_dir, "processed_coordinate_data.pkl")
processed_curve_file = os.path.join(curvature_dir, "processed_curvature_data.pkl")
if not os.path.exists(
processed_coord_file
) or not os.path.exists(
processed_curve_file
):
raise Exception(
"Missing files. Please run `python process_data.py -sdr_p "
"{0} -ycb_p <YCB objects relative path>`".format(args.sdr_p)
)
with open(processed_coord_file, "rb") as f:
coordinates = pickle.load(f)
with open(processed_curve_file, "rb") as f:
curvatures = pickle.load(f)
# create curvature dataset directory
os.makedirs(curvature_dir, exist_ok=True)
# 1D "cube" of neighborhood points
add = torch.arange(-hash_radius, hash_radius + 1)
assert len(add) >= W
# for every specified object, convert its curvature data into an SDR
for d in np.arange(*data_range):
start_time = time.time()
int_cloud = coordinates[d]
int_curvatures = curvatures[d]
# find "neighborhood" of curvatures
int_curvatures = (torch.from_numpy(int_curvatures).unsqueeze(1) + add).numpy()
if not os.path.exists(
os.path.join(
curvature_dir,
"hash_radius={0}/hash_orders/orders{1}.npy".format(hash_radius, d)
)
):
hash_ordering = ray.get([
hash_slice_for_order.remote(s) for s in int_curvatures
])
hash_ordering = np.stack(hash_ordering)
os.makedirs(
os.path.join(
curvature_dir,
"hash_radius={0}/hash_orders".format(hash_radius)
),
exist_ok=True
)
np.save(
os.path.join(
curvature_dir,
"hash_radius={0}/hash_orders/orders{1}.npy".format(hash_radius, d)
),
hash_ordering
)
print("Saved hash ordering for object {0}".format(d))
else:
hash_ordering = np.load(
os.path.join(
curvature_dir,
"hash_radius={0}/hash_orders/orders{1}.npy".format(hash_radius, d)
)
)
print("Loaded hash ordering for object {0}".format(d))
x_inds = np.arange(int_curvatures.shape[0]).repeat(W).reshape(
int_curvatures.shape[0], W
)
selected_curvatures = int_curvatures[
x_inds, hash_ordering.argsort(axis=1)[:, -W:]
]
sdr_slots = ray.get([
hash_slice_for_sdr.remote(s) for s in selected_curvatures
])
sdr_slots = np.stack(sdr_slots).squeeze()
sdr = np.zeros((int_curvatures.shape[0], N), dtype=np.uint8)
sdr[x_inds, sdr_slots] = 1
np.save(
os.path.join(
curvature_dir,
"hash_radius={0}/sdr{1}.npy".format(hash_radius, d)
),
sdr
)
print("Saved SDR for object {0}".format(d))
print("Time taken: {:.3f}".format(time.time() - start_time))
print()
ray.shutdown()