Open3D (C++ API)
TensorList.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <cstddef>
30 #include <memory>
31 #include <string>
32 
33 #include "Open3D/Core/Blob.h"
34 #include "Open3D/Core/Device.h"
35 #include "Open3D/Core/Dtype.h"
36 #include "Open3D/Core/ShapeUtil.h"
37 #include "Open3D/Core/SizeVector.h"
38 #include "Open3D/Core/Tensor.h"
39 #include "Open3D/Core/TensorKey.h"
40 namespace open3d {
41 
49 class TensorList {
50 public:
59  TensorList(const SizeVector& shape,
60  Dtype dtype,
61  const Device& device = Device("CPU:0"),
62  const int64_t& size = 0);
63 
70  TensorList(const std::vector<Tensor>& tensors,
71  const Device& device = Device("CPU:0"));
72 
78  TensorList(const std::initializer_list<Tensor>& tensors,
79  const Device& device = Device("CPU:0"));
80 
83  template <class InputIterator>
84  TensorList(InputIterator first,
85  InputIterator last,
86  const Device& device = Device("CPU:0"))
87  : device_(device) {
88  ConstructFromIterators(first, last);
89  }
90 
100  TensorList(const Tensor& internal_tensor, bool inplace = true);
101 
103  static TensorList FromTensor(const Tensor& tensor, bool inplace = false);
104 
107  TensorList(const TensorList& other);
108 
110  void CopyFrom(const TensorList& other);
111 
117  TensorList& operator=(const TensorList& other) &;
118 
120  void ShallowCopyFrom(const TensorList& other);
121 
123  Tensor AsTensor() const;
124 
128  void Resize(int64_t n);
129 
133  void PushBack(const Tensor& tensor);
134 
138  static TensorList Concatenate(const TensorList& a, const TensorList& b);
139 
141  TensorList operator+(const TensorList& other) const {
142  return Concatenate(*this, other);
143  }
144 
148  void Extend(const TensorList& other);
149 
151  Extend(other);
152  return *this;
153  }
154 
157  Tensor operator[](int64_t index) const;
158 
160  void Clear();
161 
162  std::string ToString() const;
163 
164  SizeVector GetShape() const { return shape_; }
165 
166  Device GetDevice() const { return device_; }
167 
168  Dtype GetDtype() const { return dtype_; }
169 
170  int64_t GetSize() const { return size_; }
171 
172  int64_t GetReservedSize() const { return reserved_size_; }
173 
174  const Tensor& GetInternalTensor() const { return internal_tensor_; }
175 
176 protected:
177  // The shared internal constructor for iterators.
178  template <class InputIterator>
179  void ConstructFromIterators(InputIterator first, InputIterator last) {
180  int64_t size = std::distance(first, last);
181  if (size == 0) {
183  "Empty input tensors cannot initialize a TensorList.");
184  }
185 
186  // Infer size and reserved_size
187  size_ = size;
189 
190  // Infer shape
191  shape_ = std::accumulate(
192  std::next(first), last, first->GetShape(),
193  [](const SizeVector shape, const Tensor& tensor) {
194  return shape_util::BroadcastedShape(std::move(shape),
195  tensor.GetShape());
196  });
197 
198  // Infer dtype
199  dtype_ = first->GetDtype();
200  bool dtype_consistent = std::accumulate(
201  std::next(first), last, true,
202  [&](bool same_type, const Tensor& tensor) {
203  return same_type && (dtype_ == tensor.GetDtype());
204  });
205  if (!dtype_consistent) {
207  "Inconsistent tensor dtypes in tensors are not supported "
208  "in TensorList.");
209  }
210 
211  // Construct internal tensor
212  SizeVector expanded_shape = ExpandFrontDim(shape_, reserved_size_);
213  internal_tensor_ = Tensor(expanded_shape, dtype_, device_);
214 
215  // Assign tensors
216  size_t i = 0;
217  for (auto iter = first; iter != last; ++iter, ++i) {
218  internal_tensor_[i] = *iter;
219  }
220  }
221 
223  void ExpandTensor(int64_t new_reserved_size);
224 
227  static SizeVector ExpandFrontDim(const SizeVector& shape,
228  int64_t new_dim_size = 1);
229 
232  int64_t ReserveSize(int64_t n);
233 
234 protected:
238 
241 
247  int64_t reserved_size_ = 0;
248 
252  int64_t size_ = 0;
253 
256 };
257 } // namespace open3d
Dtype GetDtype() const
Definition: Tensor.h:742
int64_t GetReservedSize() const
Definition: TensorList.h:172
Device GetDevice() const
Definition: TensorList.h:166
TensorList & operator+=(const TensorList &other)
Definition: TensorList.h:150
Dtype dtype_
Definition: TensorList.h:239
SizeVector GetShape() const
Definition: TensorList.h:164
static TensorList Concatenate(const TensorList &a, const TensorList &b)
Definition: TensorList.cpp:144
int64_t GetSize() const
Definition: TensorList.h:170
Device device_
Definition: TensorList.h:240
void Extend(const TensorList &other)
Definition: TensorList.cpp:150
SizeVector shape_
Definition: TensorList.h:237
Definition: TensorList.h:49
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
std::string ToString() const
Definition: TensorList.cpp:249
Tensor operator[](int64_t index) const
Definition: TensorList.cpp:190
int64_t size_
Definition: TensorList.h:252
void ConstructFromIterators(InputIterator first, InputIterator last)
Definition: TensorList.h:179
void ShallowCopyFrom(const TensorList &other)
Shallow copy.
Definition: TensorList.cpp:101
void PushBack(const Tensor &tensor)
Definition: TensorList.cpp:128
int size
Definition: FilePCD.cpp:57
Tensor internal_tensor_
The internal tensor for data storage.
Definition: TensorList.h:255
TensorList & operator=(const TensorList &other) &
Definition: TensorList.cpp:96
void Resize(int64_t n)
Definition: TensorList.cpp:114
Definition: SizeVector.h:40
void ExpandTensor(int64_t new_reserved_size)
Expand the size of the internal tensor.
Definition: TensorList.cpp:198
TensorList(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"), const int64_t &size=0)
Definition: TensorList.cpp:32
static SizeVector ExpandFrontDim(const SizeVector &shape, int64_t new_dim_size=1)
Definition: TensorList.cpp:213
void Clear()
Clear the tensor list by discarding all data and creating a empty one.
Definition: TensorList.cpp:195
int64_t reserved_size_
Definition: TensorList.h:247
void CopyFrom(const TensorList &other)
Deep copy.
Definition: TensorList.cpp:87
Definition: Open3DViewer.h:29
TensorList operator+(const TensorList &other) const
Concatenate two TensorLists.
Definition: TensorList.h:141
static TensorList FromTensor(const Tensor &tensor, bool inplace=false)
Factory constructor from a raw tensor.
Definition: TensorList.cpp:81
Tensor AsTensor() const
Return the reference of the contained valid tensors with shared memory.
Definition: TensorList.cpp:110
int64_t ReserveSize(int64_t n)
Definition: TensorList.cpp:220
Definition: Tensor.h:46
SizeVector BroadcastedShape(const SizeVector &l_shape, const SizeVector &r_shape)
Returns the broadcasted shape of two shapes.
Definition: ShapeUtil.cpp:72
const Tensor & GetInternalTensor() const
Definition: TensorList.h:174
Definition: Device.h:38
TensorList(InputIterator first, InputIterator last, const Device &device=Device("CPU:0"))
Definition: TensorList.h:84
Dtype GetDtype() const
Definition: TensorList.h:168
Dtype
Definition: Dtype.h:49
SizeVector GetShape() const
Definition: Tensor.h:722