Import convolutional neural network layers from Caffe
This function requires Deep Learning Toolbox™ Importer for Caffe Models support package. If this support package is not installed, then the function provides a download link.
You can download pretrained networks from Caffe Model Zoo .
Download Deep Learning Toolbox Importer for Caffe Models Support Package
Download and install Deep Learning Toolbox Importer for Caffe Models support package.
Download the required support package by typing
the command line.
If Deep Learning Toolbox Importer for Caffe Models support package is not installed, then the function provides a link to the required support package in the Add-On Explorer. To install the support package, click the link, and then click Install.
Import Layers from Caffe Network
Specify the example file
'digitsnet.prototxt' to import.
protofile = 'digitsnet.prototxt';
Import the network layers.
layers = importCaffeLayers(protofile)
layers = 1x7 Layer array with layers: 1 'testdata' Image Input 28x28x1 images 2 'conv1' Convolution 20 5x5x1 convolutions with stride [1 1] and padding [0 0] 3 'relu1' ReLU ReLU 4 'pool1' Max Pooling 2x2 max pooling with stride [2 2] and padding [0 0] 5 'ip1' Fully Connected 10 fully connected layer 6 'loss' Softmax softmax 7 'output' Classification Output crossentropyex with 'class1', 'class2', and 8 other classes
protofile — File name
character vector | string scalar
File name of the
.prototxt file containing the network
architecture, specified as a character vector or a string scalar.
protofile must be in the current folder, in a folder on the
MATLAB® path, or you must include a full or relative path to the file. If the
.prototxt file does not specify the size of the input data, you
must specify the size using the
sz input argument.
sz — Size of input data
Size of input data, specified as a row vector. Specify a vector
of two or three integer values
to the height, width, and the number of channels of the input data.
[28 28 1]
layers — Network architecture
Layer array |
Network architecture, returned as a
Layer array or a
LayerGraph object. Caffe networks that take color images as input
expect the images to be in BGR format. During import,
modifies the network so that the imported MATLAB network takes RGB images as input.
Generate Code for Imported Network Architecture
You can use MATLAB Coder™ or GPU Coder™ together with Deep Learning Toolbox to generate MEX, standalone CPU, CUDA® MEX, or standalone CUDA code for an imported network. For more information, see Deep Learning Code Generation.
Use MATLAB Coder with Deep Learning Toolbox to generate MEX or standalone CPU code that runs on desktop or embedded targets. You can deploy generated standalone code that uses the Intel® MKL-DNN library or the ARM® Compute library. Alternatively, you can generate generic C or C++ code that does not call third-party library functions. For more information, see Deep Learning with MATLAB Coder (MATLAB Coder).
Use GPU Coder with Deep Learning Toolbox to generate CUDA MEX or standalone CUDA code that runs on desktop or embedded targets. You can deploy generated standalone CUDA code that uses the CUDA deep neural network library (cuDNN), the TensorRT™ high performance inference library, or the ARM Compute library for Mali GPU. For more information, see Deep Learning with GPU Coder (GPU Coder).
importCaffeLayers returns the network architecture
layers as a
object. For code generation, you must first convert the imported
LayerGraph object to a network. Convert a
LayerGraph object to a
SeriesNetwork object by using
assembleNetwork. Convert a
LayerGraph object to a
dlnetwork object by using
dlnetwork. For more information on MATLAB
Coder and GPU Coder support for Deep Learning Toolbox objects, see Supported Classes (MATLAB Coder) and Supported Classes (GPU Coder), respectively.
You can generate code for any imported network whose layers support code generation. For lists
of the layers that support code generation with MATLAB
Coder and GPU Coder, see Supported Layers (MATLAB Coder) and Supported Layers (GPU Coder), respectively. For
more information on the code generation capabilities and limitations of each built-in
MATLAB layer, see the Extended Capabilities section of the layer. For example, see
Code Generation and GPU Code Generation of
Use Imported Network Layers on GPU
importCaffeLayers does not execute on a GPU. However,
importCaffeLayers imports the layers of a pretrained neural network for
deep learning as a
Layer array or
LayerGraph object, which
you can use on a GPU.
Convert the imported layers to a
DAGNetworkobject by using
assembleNetwork. On the
DAGNetworkobject, you can then predict class labels on either a CPU or GPU by using
classify. Specify the hardware requirements using the name-value argument
ExecutionEnvironment. For networks with multiple outputs, use the
predictfunction and specify the name-value argument
Convert the imported layers to a
dlnetworkobject by using
dlnetwork. On the
dlnetworkobject, you can then predict class labels on either a CPU or GPU by using
predict. The function
predictexecutes on the GPU if either the input data or network parameters are stored on the GPU.
You can train the imported layers on either a CPU or GPU by using
trainNetwork. To specify training options, including options for the execution environment, use the
trainingOptionsfunction. Specify the hardware requirements using the name-value argument
ExecutionEnvironment. For more information on how to accelerate training, see Scale Up Deep Learning in Parallel, on GPUs, and in the Cloud.
Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, see GPU Support by Release (Parallel Computing Toolbox).
importCaffeLayerscan import networks with the following Caffe layer types, with some limitations:
Caffe Layer Deep Learning Toolbox Layer
LRNLayer(Local Response Normalization)
If the network contains any other type of layer, then the software returns an error.
The function imports only the layers that
protofilespecifies with the include-phase TEST. The function ignores any layers that
protofilespecifies with the include-phase TRAIN.
MATLAB uses one-based indexing, whereas Python® uses zero-based indexing. In other words, the first element in an array has an index of 1 and 0 in MATLAB and Python, respectively. For more information on MATLAB indexing, see Array Indexing. In MATLAB, to use an array of indices (
ind) created in Python, convert the array to
 Caffe. https://caffe.berkeleyvision.org/.
 Caffe Model Zoo. https://caffe.berkeleyvision.org/model_zoo.html.