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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
# ----------------------------------------------------------------------------
# - Open3D: www.open3d.org -
# ----------------------------------------------------------------------------
# The MIT License (MIT)
#
# Copyright (c) 2018-2021 www.open3d.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# ----------------------------------------------------------------------------
import open3d as o3d
import open3d.core as o3c
import numpy as np
import pytest
import pickle
import tempfile
import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../..")
from open3d_test import list_devices
class WrongType():
pass
@pytest.mark.parametrize("device", list_devices())
def test_tensormap(device):
dtype = o3c.float32
# Constructor.
tm = o3d.t.geometry.TensorMap("positions")
# Get primary key().
assert tm.primary_key == "positions"
# Map member access, assignment and "contains" check. This should be the
# preferred way to construct a TensorMap with values in python.
points = o3c.Tensor.ones((0, 3), dtype, device)
colors = o3c.Tensor.ones((0, 3), dtype, device)
tm = o3d.t.geometry.TensorMap("positions")
assert "positions" not in tm
tm.positions = points
assert "positions" in tm
assert "colors" not in tm
tm.colors = colors
assert "colors" in tm
# Constructor with tm values.
tm = o3d.t.geometry.TensorMap("positions", {
"positions": points,
"colors": colors
})
assert "positions" in tm
assert "colors" in tm
# __delitem__ operator.
with pytest.raises(RuntimeError) as excinfo:
del tm.positions
assert 'cannot be deleted' in str(excinfo.value)
# Test setter.
tm = o3d.t.geometry.TensorMap("positions")
# Set attributes.
tm.positions = o3c.Tensor.ones((2, 3), dtype, device)
tm.colors = o3c.Tensor.ones((2, 3), dtype, device)
# Set attributes with numpy array.
tm.positions = np.ones((3, 3), np.float32)
tm.colors = np.ones((3, 3), np.float32)
assert len(tm.positions) == 3
assert len(tm.colors) == 3
# Set existing attributes with wrong type.
with pytest.raises(TypeError) as e:
tm.positions = WrongType()
# Set new attributes with wrong type.
with pytest.raises(TypeError) as e:
tm.normals = WrongType()
# Set primary key.
with pytest.raises(KeyError) as e:
tm.primary_key = o3c.Tensor.ones((2, 3), dtype, device)
# Test getter.
tm = o3d.t.geometry.TensorMap("positions")
assert isinstance(tm, o3d.t.geometry.TensorMap)
# Set attributes.
tm.positions = o3c.Tensor.ones((2, 3), dtype, device)
tm.colors = o3c.Tensor.ones((2, 3), dtype, device)
# Get existing attributes.
colors = tm.colors
assert len(colors) == 2
# Get unknown attributes.
with pytest.raises(KeyError) as e:
normals = tm.normals
# Get primary key.
primary_key = tm.primary_key
assert primary_key == "positions"
@pytest.mark.parametrize("device", list_devices())
def test_tensormap_modify(device):
# Assigning to the *elements* of an alias will change the value in the map.
# This tests that the alias shares the same memory as the tensor in the map.
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
a_alias = tm.a
a_alias[:] = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm.a.cpu().numpy(), [200])
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
a_alias = tm.a
tm.a[:] = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm.a.cpu().numpy(), [200])
# Assigning a new tensor to an alias should not change the tensor in the
# map. The alias name simply points to a new tensor.
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
a_alias = tm.a
a_alias = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm.a.cpu().numpy(), [100])
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
a_alias = tm.a
tm.a = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [100])
np.testing.assert_equal(tm.a.cpu().numpy(), [200])
# Pybind always returns a "shallow copy" of the tensor. This is a copy since
# the new variable points to a different tensor object, and thus the id() is
# different. The copy is shallow because the new tensor shares the same
# memory as the tensor in the map.
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
a_alias = tm.a
assert id(a_alias) != id(tm.a)
# After deleting the key-value from the map, the alias shall still be alive.
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
a_alias = tm.a
assert len(tm) == 1
del tm.a
assert len(tm) == 0
np.testing.assert_equal(a_alias.cpu().numpy(), [100])
a_alias[:] = 200
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
# With this, swapping elements are supported.
tm = o3d.t.geometry.TensorMap("positions")
tm.a = o3c.Tensor([100], device=device)
tm.b = o3c.Tensor([200], device=device)
a_alias = tm.a
b_alias = tm.b
tm.a, tm.b = tm.b, tm.a
np.testing.assert_equal(a_alias.cpu().numpy(), [100])
np.testing.assert_equal(b_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm.a.cpu().numpy(), [200])
np.testing.assert_equal(tm.b.cpu().numpy(), [100])
@pytest.mark.parametrize("device", list_devices())
def test_tensor_dict_modify(device):
"""
Same as test_tensormap_modify(), but we put Tensors in a python dict.
The only difference is that the id of the alias will be the same.
"""
# Assign to elements.
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
a_alias = tm["a"]
a_alias[:] = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm["a"].cpu().numpy(), [200])
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
a_alias = tm["a"]
tm["a"][:] = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm["a"].cpu().numpy(), [200])
# Assign a new tensor.
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
a_alias = tm["a"]
a_alias = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm["a"].cpu().numpy(), [100])
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
a_alias = tm["a"]
tm["a"] = o3c.Tensor([200], device=device)
np.testing.assert_equal(a_alias.cpu().numpy(), [100])
np.testing.assert_equal(tm["a"].cpu().numpy(), [200])
# Object id.
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
a_alias = tm["a"]
assert id(a_alias) == id(tm["a"])
# Liveness of alias.
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
a_alias = tm["a"]
assert len(tm) == 1
del tm["a"]
assert len(tm) == 0
np.testing.assert_equal(a_alias.cpu().numpy(), [100])
a_alias[:] = 200
np.testing.assert_equal(a_alias.cpu().numpy(), [200])
# Swap.
tm = dict()
tm["a"] = o3c.Tensor([100], device=device)
tm["b"] = o3c.Tensor([200], device=device)
a_alias = tm["a"]
b_alias = tm["b"]
tm["a"], tm["b"] = tm["b"], tm["a"]
np.testing.assert_equal(a_alias.cpu().numpy(), [100])
np.testing.assert_equal(b_alias.cpu().numpy(), [200])
np.testing.assert_equal(tm["a"].cpu().numpy(), [200])
np.testing.assert_equal(tm["b"].cpu().numpy(), [100])
def test_numpy_dict_modify():
"""
Same as test_tensor_dict_modify(), but we put numpy arrays in a python dict.
The id of the alias will be the same.
"""
# Assign to elements.
tm = dict()
tm["a"] = np.array([100])
a_alias = tm["a"]
a_alias[:] = np.array([200])
np.testing.assert_equal(a_alias, [200])
np.testing.assert_equal(tm["a"], [200])
tm = dict()
tm["a"] = np.array([100])
a_alias = tm["a"]
tm["a"][:] = np.array([200])
np.testing.assert_equal(a_alias, [200])
np.testing.assert_equal(tm["a"], [200])
# Assign a new tensor.
tm = dict()
tm["a"] = np.array([100])
a_alias = tm["a"]
tm["a"] = np.array([200])
np.testing.assert_equal(a_alias, [100])
np.testing.assert_equal(tm["a"], [200])
tm = dict()
tm["a"] = np.array([100])
a_alias = tm["a"]
a_alias = np.array([200])
np.testing.assert_equal(a_alias, [200])
np.testing.assert_equal(tm["a"], [100])
# Object id.
tm = dict()
tm["a"] = np.array([100])
a_alias = tm["a"]
assert id(a_alias) == id(tm["a"])
# Liveness of alias.
tm = dict()
tm["a"] = np.array([100])
a_alias = tm["a"]
assert len(tm) == 1
del tm["a"]
assert len(tm) == 0
np.testing.assert_equal(a_alias, [100])
a_alias[:] = 200
np.testing.assert_equal(a_alias, [200])
# Swap.
tm = dict()
tm["a"] = np.array([100])
tm["b"] = np.array([200])
a_alias = tm["a"]
b_alias = tm["b"]
tm["a"], tm["b"] = tm["b"], tm["a"]
np.testing.assert_equal(a_alias, [100])
np.testing.assert_equal(b_alias, [200])
np.testing.assert_equal(tm["a"], [200])
np.testing.assert_equal(tm["b"], [100])
@pytest.mark.parametrize("device", list_devices())
def test_pickle(device):
tm = o3d.t.geometry.TensorMap("positions")
with tempfile.TemporaryDirectory() as temp_dir:
file_name = f"{temp_dir}/tm.pkl"
tm.positions = o3c.Tensor.ones((10, 3), o3c.float32, device=device)
pickle.dump(tm, open(file_name, "wb"))
tm_load = pickle.load(open(file_name, "rb"))
assert tm_load.positions.device == device and tm_load.positions.dtype == o3c.float32
np.testing.assert_equal(tm.positions.cpu().numpy(),
tm_load.positions.cpu().numpy())
|