A blob is the fundamental data representation in Mocha. It is used for both data (e.g. mini-batch of data samples) and parameters (e.g. filters of a convolution layer). Conceptually, a blob is an N-dimensional tensor.
For example, in vision, a data blob is usually a 4D-tensor. Following the vision (and Caffe) convention, the four dimensions are called width, height, channels and num. The fastest changing dimension is width and slowest changing dimension is num.
The memory layout of a blob in Mocha is compatible with Caffe’s blob. So a blob (e.g. layer parameters) in Mocha can be saved to HDF5 and loaded from Caffe without doing any dimension permutation, and vise versa. However, since Julia uses the column-major convention for tensor and matrix data, and Caffe uses the row-major convention, in Mocha API, the order of the four dimensions is width, height, channels, and num, while in Caffe API, it is num, channels, height, width.
Each backend has its own blob implementation, as a subtype of
example, a blob in the CPU backend is a shallow wrapper of a Julia
object, while a blob in the GPU backend references to a piece of GPU memory.
Constructors and Destructors¶
A backend-dependent blob can be created with the following function:
make_blob(backend, data_type, dims)¶
NTuple, specifying the dimensions of the blob to be created. Currently
data_typeshould be either
Several helper functions are also provided:
make_blob(backend, data_type, dims...)
Spell out the dimensions explicitly.
arrayis a Julia
AbstractArray. This creates a blob with the same data type and shape as
arrayand initializes the blob contents with
make_zero_blob(backend, data_type, dims)¶
Create a blob and initialize it with zeros.
reshape_blob(backend, blob, new_dims)¶
Create a reference to an existing blob with a possiblely different shape. The behavior is the same as Julia’s
reshapefunction on an array: the new blob shares data with the existing one.
Release the resources of a blob.
The resources need to be released explicitly. A Julia blob object being GC-ed does not release the underlying resource automatically.
Accessing Properties of a Blob¶
The blob implements a simple API similar to a Julia array:
Get the element type of the blob.
Get the tensor dimension of the blob. The same as
Get the shape of the blob. The return value is an
Get the size along a particular dimension.
dimcan be negative. For example,
size(blob, -1)is the same as
size(blob)[end]. For convenience, if
ndims(blob), the function returns
1instead of raising an error.
Get the total number of elements in a blob.
The same as
The same as
The same as
The the feature size in a blob, which is the same as
get_chann was removed when Mocha upgraded from
4D-tensors to general ND-tensors, because the channel dimension is usually
ambiguous for general ND-tensors.
Accessing Data of a Blob¶
Because accessing GPU memory is costly, a blob does not have an interface to do element-wise accessing. The data can be either manipulated in a backend-dependent manner, relying on the underlying implementation details, or in a backend-independent way by copying the contents from and to a Julia array.
Copy the contents of
dstcan be either a blob or a Julia array.
The following utilities can be used to initialize the contents of a blob
Fill every element of
blobwith zeros. Depending on the implementation,
erase!(blob)might be more efficient than