17 #include "caffe2/core/blob.h" 18 #include "caffe2/core/init.h" 19 #include "caffe2/core/tensor.h" 20 #include "caffe2/core/logging.h" 26 int main(
int argc,
char** argv) {
28 caffe2::ShowLogInfoToStderr();
31 "This script corresponds to the Blob part of the Caffe2 C++ " 34 LOG(INFO) <<
"Let's create a blob myblob.";
38 LOG(INFO) <<
"Let's set it to int and set the value to 10.";
44 <<
"Is the blob type int? " 48 <<
"Is the blob type float? " 51 const int& myint_const = myblob.
Get<
int>();
53 <<
"The value of the int number stored in the blob is: " 57 <<
"Let's try to get a float pointer. This will trigger an exception.";
60 const float& myfloat = myblob.
Get<
float>();
61 LOG(FATAL) <<
"This line should never happen.";
62 }
catch (std::exception& e) {
64 <<
"As expected, we got an exception. Its content says: " 69 "However, we can change the content type (and destroy the old " 70 "content) by calling GetMutable. Let's change it to double.";
72 double* mydouble = myblob.
GetMutable<
double>();
75 LOG(INFO) <<
"The new content is: " << myblob.
Get<
double>();
78 "If we have a pre-created object, we can use Reset() to transfer the " 81 std::string* pvec =
new std::string();
84 LOG(INFO) <<
"Is the blob now of type string? " 85 << myblob.
IsType<std::string>();
87 LOG(INFO) <<
"This concludes the blob tutorial.";
Blob is a general container that hosts a typed pointer.
bool IsType() const noexcept
Checks if the content stored in the blob is of type T.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
T * GetMutable()
Gets a mutable pointer to the stored object.
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.
const T & Get() const
Gets the const reference of the stored object.