Apollo 10.0
自动驾驶开放平台
apollo::perception::base::Blob< Dtype > 模板类 参考

A wrapper around SyncedMemory holders serving as the basic computational unit for images, feature maps, etc. 更多...

#include <blob.h>

apollo::perception::base::Blob< Dtype > 的协作图:

Public 成员函数

 Blob ()
 
 Blob (bool use_cuda_host_malloc)
 
 Blob (const int num, const int channels, const int height, const int width, const bool use_cuda_host_malloc=false)
 Deprecated; use Blob(const std::vector<int>& shape).
 
 Blob (const std::vector< int > &shape, const bool use_cuda_host_malloc=false)
 
 Blob (const Blob &)=delete
 
void operator= (const Blob &)=delete
 
void Reshape (const int num, const int channels, const int height, const int width)
 Deprecated; use Reshape(const std::vector<int>& shape).
 
void Reshape (const std::vector< int > &shape)
 Change the dimensions of the blob, allocating new memory if necessary.
 
void ReshapeLike (const Blob &other)
 
std::string shape_string () const
 
const std::vector< int > & shape () const
 
int shape (int index) const
 Returns the dimension of the index-th axis (or the negative index-th axis from the end, if index is negative).
 
int num_axes () const
 
int count () const
 
int count (int start_axis, int end_axis) const
 Compute the volume of a slice; i.e., the product of dimensions among a range of axes.
 
int count (int start_axis) const
 Compute the volume of a slice spanning from a particular first axis to the final axis.
 
int CanonicalAxisIndex (int axis_index) const
 create RoI Blob.
 
int num () const
 Deprecated legacy shape accessor num: use shape(0) instead.
 
int channels () const
 Deprecated legacy shape accessor channels: use shape(1) instead.
 
int height () const
 Deprecated legacy shape accessor height: use shape(2) instead.
 
int width () const
 Deprecated legacy shape accessor width: use shape(3) instead.
 
int LegacyShape (int index) const
 
int offset (const int n, const int c=0, const int h=0, const int w=0) const
 
int offset (const std::vector< int > &indices) const
 
void CopyFrom (const Blob< Dtype > &source, bool reshape=false)
 Copy from a source Blob.
 
Dtype data_at (const int n, const int c, const int h, const int w) const
 
Dtype data_at (const std::vector< int > &index) const
 
const std::shared_ptr< SyncedMemory > & data () const
 
const Dtype * cpu_data () const
 
void set_cpu_data (Dtype *data)
 
const int * gpu_shape () const
 
const Dtype * gpu_data () const
 
void set_gpu_data (Dtype *data)
 
Dtype * mutable_cpu_data ()
 
Dtype * mutable_gpu_data ()
 
void set_head_gpu ()
 
void set_head_cpu ()
 
SyncedMemory::SyncedHead head () const
 
void ShareData (const Blob &other)
 Set the data_ std::shared_ptr to point to the SyncedMemory holding the data_ of Blob other – useful in Layers which simply perform a copy in their Forward pass.
 

Protected 属性

std::shared_ptr< SyncedMemorydata_
 
std::shared_ptr< SyncedMemoryshape_data_
 
std::vector< int > shape_
 
int count_
 
int capacity_
 
bool use_cuda_host_malloc_
 

详细描述

template<typename Dtype>
class apollo::perception::base::Blob< Dtype >

A wrapper around SyncedMemory holders serving as the basic computational unit for images, feature maps, etc.

TODO(dox): more thorough description.

在文件 blob.h88 行定义.

构造及析构函数说明

◆ Blob() [1/5]

template<typename Dtype >
apollo::perception::base::Blob< Dtype >::Blob ( )
inline

在文件 blob.h90 行定义.

◆ Blob() [2/5]

template<typename Dtype >
apollo::perception::base::Blob< Dtype >::Blob ( bool  use_cuda_host_malloc)
inlineexplicit

在文件 blob.h91 行定义.

92 : data_(),
93 count_(0),
94 capacity_(0),
95 use_cuda_host_malloc_(use_cuda_host_malloc) {}

◆ Blob() [3/5]

template<typename Dtype >
apollo::perception::base::Blob< Dtype >::Blob ( const int  num,
const int  channels,
const int  height,
const int  width,
const bool  use_cuda_host_malloc = false 
)

Deprecated; use Blob(const std::vector<int>& shape).

在文件 blob.cc115 行定义.

118 : capacity_(0), use_cuda_host_malloc_(use_cuda_host_malloc) {
120}
void Reshape(const int num, const int channels, const int height, const int width)
Deprecated; use Reshape(const std::vector<int>& shape).
Definition blob.cc:72
int height() const
Deprecated legacy shape accessor height: use shape(2) instead.
Definition blob.h:214
int channels() const
Deprecated legacy shape accessor channels: use shape(1) instead.
Definition blob.h:212
int num() const
Deprecated legacy shape accessor num: use shape(0) instead.
Definition blob.h:210
int width() const
Deprecated legacy shape accessor width: use shape(3) instead.
Definition blob.h:216

◆ Blob() [4/5]

template<typename Dtype >
apollo::perception::base::Blob< Dtype >::Blob ( const std::vector< int > &  shape,
const bool  use_cuda_host_malloc = false 
)
explicit

在文件 blob.cc123 行定义.

126 : capacity_(0), use_cuda_host_malloc_(use_cuda_host_malloc) {
127 Reshape(shape);
128}
const std::vector< int > & shape() const
Definition blob.h:130

◆ Blob() [5/5]

template<typename Dtype >
apollo::perception::base::Blob< Dtype >::Blob ( const Blob< Dtype > &  )
delete

成员函数说明

◆ CanonicalAxisIndex()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::CanonicalAxisIndex ( int  axis_index) const
inline

create RoI Blob.

参数
roi_beginbegin of roi
roi_endend of roi

Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e.g., -1 for the last axis).

参数
axis_indexthe axis index. If 0 <= index < num_axes(), return index. If -num_axes <= index <= -1, return (num_axes() - (-index)), e.g., the last axis index (num_axes() - 1) if index == -1, the second to last if index == -2, etc. Dies on out of range index.

在文件 blob.h196 行定义.

196 {
197 CHECK_GE(axis_index, -num_axes())
198 << "axis " << axis_index << " out of range for " << num_axes()
199 << "-D Blob with shape " << shape_string();
200 CHECK_LT(axis_index, num_axes())
201 << "axis " << axis_index << " out of range for " << num_axes()
202 << "-D Blob with shape " << shape_string();
203 if (axis_index < 0) {
204 return axis_index + num_axes();
205 }
206 return axis_index;
207 }
std::string shape_string() const
Definition blob.h:125

◆ channels()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::channels ( ) const
inline

Deprecated legacy shape accessor channels: use shape(1) instead.

在文件 blob.h212 行定义.

212{ return LegacyShape(1); }
int LegacyShape(int index) const
Definition blob.h:217

◆ CopyFrom()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::CopyFrom ( const Blob< Dtype > &  source,
bool  reshape = false 
)

Copy from a source Blob.

参数
sourcethe Blob to copy from
reshapeif false, require this Blob to be pre-shaped to the shape of other (and die otherwise); if true, Reshape this Blob to other's shape if necessary

◆ count() [1/3]

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::count ( ) const
inline

在文件 blob.h143 行定义.

143{ return count_; }

◆ count() [2/3]

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::count ( int  start_axis) const
inline

Compute the volume of a slice spanning from a particular first axis to the final axis.

参数
start_axisThe first axis to include in the slice.

在文件 blob.h171 行定义.

171 {
172 return count(start_axis, num_axes());
173 }

◆ count() [3/3]

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::count ( int  start_axis,
int  end_axis 
) const
inline

Compute the volume of a slice; i.e., the product of dimensions among a range of axes.

参数
start_axisThe first axis to include in the slice.
end_axisThe first axis to exclude from the slice.

在文件 blob.h153 行定义.

153 {
154 CHECK_LE(start_axis, end_axis);
155 CHECK_GE(start_axis, 0);
156 CHECK_GE(end_axis, 0);
157 CHECK_LE(start_axis, num_axes());
158 CHECK_LE(end_axis, num_axes());
159 int count = 1;
160 for (int i = start_axis; i < end_axis; ++i) {
161 count *= shape(i);
162 }
163 return count;
164 }

◆ cpu_data()

template<typename Dtype >
const Dtype * apollo::perception::base::Blob< Dtype >::cpu_data ( ) const

在文件 blob.cc137 行定义.

137 {
138 ACHECK(data_);
139 return (const Dtype*)data_->cpu_data();
140}
#define ACHECK(cond)
Definition log.h:80

◆ data()

template<typename Dtype >
const std::shared_ptr< SyncedMemory > & apollo::perception::base::Blob< Dtype >::data ( ) const
inline

在文件 blob.h276 行定义.

276 {
277 ACHECK(data_);
278 return data_;
279 }

◆ data_at() [1/2]

template<typename Dtype >
Dtype apollo::perception::base::Blob< Dtype >::data_at ( const int  n,
const int  c,
const int  h,
const int  w 
) const
inline

在文件 blob.h267 行定义.

268 {
269 return cpu_data()[offset(n, c, h, w)];
270 }
const Dtype * cpu_data() const
Definition blob.cc:137
int offset(const int n, const int c=0, const int h=0, const int w=0) const
Definition blob.h:231

◆ data_at() [2/2]

template<typename Dtype >
Dtype apollo::perception::base::Blob< Dtype >::data_at ( const std::vector< int > &  index) const
inline

在文件 blob.h272 行定义.

272 {
273 return cpu_data()[offset(index)];
274 }

◆ gpu_data()

template<typename Dtype >
const Dtype * apollo::perception::base::Blob< Dtype >::gpu_data ( ) const

在文件 blob.cc154 行定义.

154 {
155 ACHECK(data_);
156 return (const Dtype*)data_->gpu_data();
157}

◆ gpu_shape()

template<typename Dtype >
const int * apollo::perception::base::Blob< Dtype >::gpu_shape ( ) const

在文件 blob.cc131 行定义.

131 {
133 return (const int*)shape_data_->gpu_data();
134}
std::shared_ptr< SyncedMemory > shape_data_
Definition blob.h:305

◆ head()

template<typename Dtype >
SyncedMemory::SyncedHead apollo::perception::base::Blob< Dtype >::head ( ) const
inline

在文件 blob.h290 行定义.

290{ return data_->head(); }

◆ height()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::height ( ) const
inline

Deprecated legacy shape accessor height: use shape(2) instead.

在文件 blob.h214 行定义.

214{ return LegacyShape(2); }

◆ LegacyShape()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::LegacyShape ( int  index) const
inline

在文件 blob.h217 行定义.

217 {
218 CHECK_LE(num_axes(), 4)
219 << "Cannot use legacy accessors on Blobs with > 4 axes.";
220 CHECK_LT(index, 4);
221 CHECK_GE(index, -4);
222 if (index >= num_axes() || index < -num_axes()) {
223 // Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
224 // indexing) -- this special case simulates the one-padding used to fill
225 // extraneous axes of legacy blobs.
226 return 1;
227 }
228 return shape(index);
229 }

◆ mutable_cpu_data()

template<typename Dtype >
Dtype * apollo::perception::base::Blob< Dtype >::mutable_cpu_data ( )

在文件 blob.cc171 行定义.

171 {
172 ACHECK(data_);
173 return static_cast<Dtype*>(data_->mutable_cpu_data());
174}

◆ mutable_gpu_data()

template<typename Dtype >
Dtype * apollo::perception::base::Blob< Dtype >::mutable_gpu_data ( )

在文件 blob.cc177 行定义.

177 {
178 ACHECK(data_);
179 return static_cast<Dtype*>(data_->mutable_gpu_data());
180}

◆ num()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::num ( ) const
inline

Deprecated legacy shape accessor num: use shape(0) instead.

在文件 blob.h210 行定义.

210{ return LegacyShape(0); }

◆ num_axes()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::num_axes ( ) const
inline

在文件 blob.h142 行定义.

142{ return static_cast<int>(shape_.size()); }
std::vector< int > shape_
Definition blob.h:306

◆ offset() [1/2]

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::offset ( const int  n,
const int  c = 0,
const int  h = 0,
const int  w = 0 
) const
inline

在文件 blob.h231 行定义.

232 {
233 CHECK_GE(n, 0);
234 CHECK_LE(n, num());
235 CHECK_GE(channels(), 0);
236 CHECK_LE(c, channels());
237 CHECK_GE(height(), 0);
238 CHECK_LE(h, height());
239 CHECK_GE(width(), 0);
240 CHECK_LE(w, width());
241 return ((n * channels() + c) * height() + h) * width() + w;
242 }

◆ offset() [2/2]

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::offset ( const std::vector< int > &  indices) const
inline

在文件 blob.h244 行定义.

244 {
245 CHECK_LE(indices.size(), static_cast<size_t>(num_axes()));
246 int offset = 0;
247 for (int i = 0; i < num_axes(); ++i) {
248 offset *= shape(i);
249 if (static_cast<int>(indices.size()) > i) {
250 CHECK_GE(indices[i], 0);
251 CHECK_LT(indices[i], shape(i));
252 offset += indices[i];
253 }
254 }
255 return offset;
256 }

◆ operator=()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::operator= ( const Blob< Dtype > &  )
delete

◆ Reshape() [1/2]

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::Reshape ( const int  num,
const int  channels,
const int  height,
const int  width 
)

Deprecated; use Reshape(const std::vector<int>& shape).

在文件 blob.cc72 行定义.

73 {
74 std::vector<int> shape(4);
75 shape[0] = num;
76 shape[1] = channels;
77 shape[2] = height;
78 shape[3] = width;
80}

◆ Reshape() [2/2]

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::Reshape ( const std::vector< int > &  shape)

Change the dimensions of the blob, allocating new memory if necessary.

This function can be called both to create an initial allocation of memory, and to adjust the dimensions of a top blob during Layer::Reshape or Layer::Forward. When changing the size of blob, memory will only be reallocated if sufficient memory does not already exist, and excess memory will never be freed.

Note that reshaping an input blob and immediately calling Net::Backward is an error; either Net::Forward or Net::Reshape need to be called to propagate the new input shape to higher layers.

在文件 blob.cc83 行定义.

83 {
84 CHECK_LE(shape.size(), kMaxBlobAxes);
85 count_ = 1;
86 shape_.resize(shape.size());
87 if (!shape_data_ || shape_data_->size() < shape.size() * sizeof(int)) {
88 shape_data_.reset(
89 new SyncedMemory(shape.size() * sizeof(int), use_cuda_host_malloc_));
90 }
91 int* shape_data = static_cast<int*>(shape_data_->mutable_cpu_data());
92 for (size_t i = 0; i < shape.size(); ++i) {
93 CHECK_GE(shape[i], 0);
94 if (count_ != 0) {
95 CHECK_LE(shape[i], std::numeric_limits<int>::max() / count_)
96 << "blob size exceeds std::numeric_limits<int>::max()";
97 }
98 count_ *= shape[i];
99 shape_[i] = shape[i];
100 shape_data[i] = shape[i];
101 }
102 if (count_ > capacity_) {
104 data_.reset(
105 new SyncedMemory(capacity_ * sizeof(Dtype), use_cuda_host_malloc_));
106 }
107}
constexpr size_t kMaxBlobAxes
Definition blob.h:79

◆ ReshapeLike()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::ReshapeLike ( const Blob< Dtype > &  other)

在文件 blob.cc110 行定义.

110 {
111 Reshape(other.shape());
112}

◆ set_cpu_data()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::set_cpu_data ( Dtype *  data)

在文件 blob.cc143 行定义.

143 {
144 ACHECK(data);
145 // Make sure CPU and GPU sizes remain equal
146 size_t size = count_ * sizeof(Dtype);
147 if (data_->size() != size) {
148 data_.reset(new SyncedMemory(size, use_cuda_host_malloc_));
149 }
150 data_->set_cpu_data(data);
151}
const std::shared_ptr< SyncedMemory > & data() const
Definition blob.h:276

◆ set_gpu_data()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::set_gpu_data ( Dtype *  data)

在文件 blob.cc160 行定义.

160 {
161 ACHECK(data);
162 // Make sure CPU and GPU sizes remain equal
163 size_t size = count_ * sizeof(Dtype);
164 if (data_->size() != size) {
165 data_.reset(new SyncedMemory(size, use_cuda_host_malloc_));
166 }
167 data_->set_gpu_data(data);
168}

◆ set_head_cpu()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::set_head_cpu ( )
inline

在文件 blob.h289 行定义.

289{ data_->set_head_cpu(); }

◆ set_head_gpu()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::set_head_gpu ( )
inline

在文件 blob.h288 行定义.

288{ data_->set_head_gpu(); }

◆ shape() [1/2]

template<typename Dtype >
const std::vector< int > & apollo::perception::base::Blob< Dtype >::shape ( ) const
inline

在文件 blob.h130 行定义.

130{ return shape_; }

◆ shape() [2/2]

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::shape ( int  index) const
inline

Returns the dimension of the index-th axis (or the negative index-th axis from the end, if index is negative).

参数
indexthe axis index, which may be negative as it will be "canonicalized" using CanonicalAxisIndex. Dies on out of range index.

在文件 blob.h139 行定义.

139 {
140 return shape_[CanonicalAxisIndex(index)];
141 }
int CanonicalAxisIndex(int axis_index) const
create RoI Blob.
Definition blob.h:196

◆ shape_string()

template<typename Dtype >
std::string apollo::perception::base::Blob< Dtype >::shape_string ( ) const
inline

在文件 blob.h125 行定义.

125 {
126 return shape_.empty()
127 ? absl::StrCat("(", count_, ")")
128 : absl::StrCat(absl::StrJoin(shape_, " "), " (", count_, ")");
129 }

◆ ShareData()

template<typename Dtype >
void apollo::perception::base::Blob< Dtype >::ShareData ( const Blob< Dtype > &  other)

Set the data_ std::shared_ptr to point to the SyncedMemory holding the data_ of Blob other – useful in Layers which simply perform a copy in their Forward pass.

This deallocates the SyncedMemory holding this Blob's data_, as std::shared_ptr calls its destructor when reset with the "=" operator.

在文件 blob.cc183 行定义.

183 {
184 CHECK_EQ(count_, other.count());
185 data_ = other.data();
186}

◆ width()

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::width ( ) const
inline

Deprecated legacy shape accessor width: use shape(3) instead.

在文件 blob.h216 行定义.

216{ return LegacyShape(3); }

类成员变量说明

◆ capacity_

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::capacity_
protected

在文件 blob.h308 行定义.

◆ count_

template<typename Dtype >
int apollo::perception::base::Blob< Dtype >::count_
protected

在文件 blob.h307 行定义.

◆ data_

template<typename Dtype >
std::shared_ptr<SyncedMemory> apollo::perception::base::Blob< Dtype >::data_
protected

在文件 blob.h304 行定义.

◆ shape_

template<typename Dtype >
std::vector<int> apollo::perception::base::Blob< Dtype >::shape_
protected

在文件 blob.h306 行定义.

◆ shape_data_

template<typename Dtype >
std::shared_ptr<SyncedMemory> apollo::perception::base::Blob< Dtype >::shape_data_
protected

在文件 blob.h305 行定义.

◆ use_cuda_host_malloc_

template<typename Dtype >
bool apollo::perception::base::Blob< Dtype >::use_cuda_host_malloc_
protected

在文件 blob.h309 行定义.


该类的文档由以下文件生成: