14 #pragma warning(disable : 4996)
27 t_cast = t_cast.
To(dtype.
value());
30 t_cast = t_cast.
To(device.
value());
37 if (!tensor.
IsCPU()) {
39 "Can only convert CPU Tensor to numpy. Copy Tensor to CPU "
40 "before converting to numpy.");
44 py::array::ShapeContainer py_shape(tensor.
GetShape());
47 for (
auto& s : strides) {
48 s *= element_byte_size;
50 py::array::StridesContainer py_strides(strides);
82 auto capsule_destructor = [](PyObject* data) {
84 PyCapsule_GetPointer(data,
"open3d::Tensor"));
92 py::capsule base_tensor_capsule(base_tensor,
"open3d::Tensor",
94 return py::array(py_dtype, py_shape, py_strides, tensor.
GetDataPtr(),
99 py::buffer_info info = array.request();
101 SizeVector shape(info.shape.begin(), info.shape.end());
102 SizeVector strides(info.strides.begin(), info.strides.end());
103 for (
size_t i = 0; i < strides.
size(); ++i) {
104 strides[i] /= info.itemsize;
109 auto shared_array = std::make_shared<py::array>(array);
110 std::function<void(
void*)> deleter = [shared_array](
void*)
mutable ->
void {
111 py::gil_scoped_acquire acquire;
112 shared_array.reset();
114 auto blob = std::make_shared<Blob>(device, info.ptr, deleter);
115 Tensor t_inplace(shape, strides, info.ptr, dtype, blob);
120 return t_inplace.
Clone();
127 py::object numpy = py::module::import(
"numpy");
128 py::array np_array = numpy.attr(
"array")(list);
136 py::object numpy = py::module::import(
"numpy");
137 py::array np_array = numpy.attr(
"array")(tuple);
147 dtype_value = dtype.
value();
149 Device device_value(
"CPU:0");
151 device_value = device.
value();
163 dtype_value = dtype.
value();
165 Device device_value(
"CPU:0");
167 device_value = device.
value();
179 dtype_value = dtype.
value();
181 Device device_value(
"CPU:0");
183 device_value = device.
value();
185 return Tensor(std::vector<bool>{scalar_value}, {},
core::Bool, device_value)
200 if (py::isinstance<py::bool_>(handle)) {
202 static_cast<bool>(py::reinterpret_borrow<py::bool_>(handle)),
204 }
else if (py::isinstance<py::int_>(handle)) {
206 static_cast<int64_t
>(py::reinterpret_borrow<py::int_>(handle)),
208 }
else if (py::isinstance<py::float_>(handle)) {
210 static_cast<double>(py::reinterpret_borrow<py::float_>(handle)),
212 }
else if (py::isinstance<py::list>(handle)) {
213 return PyListToTensor(py::reinterpret_borrow<py::list>(handle), dtype,
215 }
else if (py::isinstance<py::tuple>(handle)) {
216 return PyTupleToTensor(py::reinterpret_borrow<py::tuple>(handle), dtype,
218 }
else if (py::isinstance<py::array>(handle)) {
223 }
else if (py::isinstance<Tensor>(handle)) {
236 "PyHandleToTensor has invalid input type {}.",
237 static_cast<std::string
>(py::str(py::type::of(handle))));
SizeVector GetStrides() const
Tensor Clone() const
Copy Tensor to the same device.
SizeVector GetShape() const
Tensor To(Dtype dtype, bool copy=false) const
constexpr bool has_value() const noexcept
constexpr T const & value() const &
Tensor PyHandleToTensor(const py::handle &handle, utility::optional< Dtype > dtype, utility::optional< Device > device, bool force_copy)
Tensor PyListToTensor(const py::list &list, utility::optional< Dtype > dtype, utility::optional< Device > device)
Tensor BoolToTensor(bool scalar_value, utility::optional< Dtype > dtype, utility::optional< Device > device)
py::array TensorToPyArray(const Tensor &tensor)
Convert Tensor class to py::array (Numpy array).
Tensor DoubleToTensor(double scalar_value, utility::optional< Dtype > dtype, utility::optional< Device > device)
Tensor IntToTensor(int64_t scalar_value, utility::optional< Dtype > dtype, utility::optional< Device > device)
static Tensor CastOptionalDtypeDevice(const Tensor &t, utility::optional< Dtype > dtype, utility::optional< Device > device)
Tensor PyArrayToTensor(py::array array, bool inplace)
Tensor PyTupleToTensor(const py::tuple &tuple, utility::optional< Dtype > dtype, utility::optional< Device > device)
core::Dtype ArrayFormatToDtype(const std::string &format, size_t byte_size)
std::string DtypeToArrayFormat(const core::Dtype &dtype)
void To(const core::Tensor &src, core::Tensor &dst, double scale, double offset)
Generic file read and write utility for python interface.