Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Types | Public Member Functions | Protected Member Functions
caffe2::Workspace Class Reference

Workspace is a class that holds all the related objects created during runtime: (1) all blobs, and (2) all instantiated networks. More...

#include <workspace.h>

Public Types

typedef std::function< bool(int)> ShouldContinue
 
typedef CaffeMap< string, unique_ptr< Blob > > BlobMap
 
typedef CaffeMap< string, unique_ptr< NetBase > > NetMap
 

Public Member Functions

 Workspace ()
 Initializes an empty workspace.
 
 Workspace (const string &root_folder)
 Initializes an empty workspace with the given root folder. More...
 
 Workspace (Workspace *const shared)
 Initializes a workspace with a shared workspace. More...
 
 Workspace (const string &root_folder, Workspace *shared)
 Initializes a workspace with a root folder and a shared workspace.
 
void SetParentWorkspace (Workspace *shared)
 Allows to add a parent workspace post factum after the object was already constructed.
 
vector< string > LocalBlobs () const
 Return list of blobs owned by this Workspace, not including blobs shared from parent workspace.
 
vector< string > Blobs () const
 Return a list of blob names. More...
 
const string & RootFolder ()
 Return the root folder of the workspace.
 
bool HasBlob (const string &name) const
 Checks if a blob with the given name is present in the current workspace.
 
void PrintBlobSizes ()
 
BlobCreateBlob (const string &name)
 Creates a blob of the given name. More...
 
bool RemoveBlob (const string &name)
 Remove the blob of the given name. More...
 
const BlobGetBlob (const string &name) const
 Gets the blob with the given name as a const pointer. More...
 
BlobGetBlob (const string &name)
 Gets the blob with the given name as a mutable pointer. More...
 
NetBaseCreateNet (const NetDef &net_def, bool overwrite=false)
 Creates a network with the given NetDef, and returns the pointer to the network. More...
 
NetBaseGetNet (const string &net_name)
 Gets the pointer to a created net. More...
 
void DeleteNet (const string &net_name)
 Deletes the instantiated network with the given name.
 
bool RunNet (const string &net_name)
 Finds and runs the instantiated network with the given name. More...
 
vector< string > Nets () const
 Returns a list of names of the currently instantiated networks.
 
bool RunPlan (const PlanDef &plan_def, ShouldContinue should_continue=StopOnSignal{})
 Runs a plan that has multiple nets and execution steps.
 
bool RunOperatorOnce (const OperatorDef &op_def)
 
bool RunNetOnce (const NetDef &net_def)
 

Protected Member Functions

bool ExecuteStepRecursive (CompiledExecutionStep &execution)
 

Detailed Description

Workspace is a class that holds all the related objects created during runtime: (1) all blobs, and (2) all instantiated networks.

It is the owner of all these objects and deals with the scaffolding logistics.

Definition at line 53 of file workspace.h.

Constructor & Destructor Documentation

◆ Workspace() [1/2]

caffe2::Workspace::Workspace ( const string &  root_folder)
inlineexplicit

Initializes an empty workspace with the given root folder.

For any operators that are going to interface with the file system, such as load operators, they will write things under this root folder given by the workspace.

Definition at line 70 of file workspace.h.

◆ Workspace() [2/2]

caffe2::Workspace::Workspace ( Workspace *const  shared)
inlineexplicit

Initializes a workspace with a shared workspace.

When we access a Blob, we will first try to access the blob that exists in the local workspace, and if not, access the blob that exists in the shared workspace. The caller keeps the ownership of the shared workspace and is responsible for making sure that its lifetime is longer than the created workspace.

Definition at line 81 of file workspace.h.

Member Function Documentation

◆ Blobs()

vector< string > caffe2::Workspace::Blobs ( ) const

Return a list of blob names.

This may be a bit slow since it will involve creation of multiple temp variables. For best performance, simply use HasBlob() and GetBlob().

Definition at line 203 of file workspace.cc.

◆ CreateBlob()

Blob * caffe2::Workspace::CreateBlob ( const string &  name)

Creates a blob of the given name.

The pointer to the blob is returned, but the workspace keeps ownership of the pointer. If a blob of the given name already exists, the creation is skipped and the existing blob is returned.

Definition at line 215 of file workspace.cc.

◆ CreateNet()

NetBase * caffe2::Workspace::CreateNet ( const NetDef &  net_def,
bool  overwrite = false 
)

Creates a network with the given NetDef, and returns the pointer to the network.

If there is anything wrong during the creation of the network, a nullptr is returned. The Workspace keeps ownership of the pointer.

If there is already a net created in the workspace with the given name, CreateNet will overwrite it if overwrite=true is specified. Otherwise, an exception is thrown.

Definition at line 259 of file workspace.cc.

◆ GetBlob() [1/2]

const Blob * caffe2::Workspace::GetBlob ( const string &  name) const

Gets the blob with the given name as a const pointer.

If the blob does not exist, a nullptr is returned.

Definition at line 238 of file workspace.cc.

◆ GetBlob() [2/2]

Blob * caffe2::Workspace::GetBlob ( const string &  name)

Gets the blob with the given name as a mutable pointer.

If the blob does not exist, a nullptr is returned.

Definition at line 254 of file workspace.cc.

◆ GetNet()

NetBase * caffe2::Workspace::GetNet ( const string &  net_name)

Gets the pointer to a created net.

The workspace keeps ownership of the network.

Definition at line 288 of file workspace.cc.

◆ RemoveBlob()

bool caffe2::Workspace::RemoveBlob ( const string &  name)

Remove the blob of the given name.

Return true if removed and false if not exist. Will NOT remove from the shared workspace.

Definition at line 225 of file workspace.cc.

◆ RunNet()

bool caffe2::Workspace::RunNet ( const string &  net_name)

Finds and runs the instantiated network with the given name.

If the network does not exist or there are errors running the network, the function returns false.

Definition at line 302 of file workspace.cc.


The documentation for this class was generated from the following files: