Generate MATLAB Code from Deep Network Designer
The Deep Network Designer app enables you to generate MATLAB® code that recreates the building, editing, and training of a network in the app.
In the Designer tab, you can generate a live script to:
Recreate the layers in your network, including any initial parameters. Select Export > Generate Network Code With Parameters.
Recreate just the layers in your network. Select Export > Generate Network Code Without Parameters.
In the Training tab, you can generate a live script to:
Recreate the building and training of a network you construct in Deep Network Designer. Select Export > Generate Code for Training.
Generate MATLAB Code to Recreate Network Layers
Generate MATLAB code for recreating the network constructed in Deep Network Designer. In the Designer tab, choose one of these options:
To recreate the layers in your network, including any initial parameters, select Export > Generate Network Code With Parameters. The app creates a live script and a MAT-file containing the initial parameters (weights and biases) from your network. Run the script to recreate the network layers, including the learnable parameters from the MAT-file. Use this option to preserve the weights if you want to perform transfer learning.
To recreate just the layers in your network, select Export > Generate Network Code Without Parameters. This network does not contain initial parameters, such as pretrained weights.
Running the generated script returns the network architecture as a variable in the
workspace. Depending on the network architecture, the variable is a layer graph named
lgraph
or a layer array named layers
. For an example
of training a network exported from Deep Network Designer, see Create Simple Sequence Classification Network Using Deep Network Designer.
Generate MATLAB Code to Train Network
To recreate the construction and training of a network in Deep Network Designer, generate MATLAB code after training. For an example of using Deep Network Designer to train an image classification network, see Transfer Learning with Deep Network Designer.
Once training is complete, on the Training tab, select Export > Generate Code for Training. The app creates a live script and a MAT-file containing the initial parameters (weights and biases) from your network. If you import data from the workspace into Deep Network Designer then this is also contained in the generated MAT-file.
This image shows an example of a live script generated using Deep Network Designer.
Running the generated script builds the network (including the learnable parameters from the MAT-file), imports the data, sets the training options, and trains the network. Examine the generated script to learn how to construct and train a network at the command line.
Note
If you change the network, training and validation data, or training options, click Train before generating the live script.
Use Network for Prediction
Suppose that the trained network is contained in the variable net
.
To use the trained network for prediction, use the predict
function.
For example, suppose you have a trained image classification network. Use the exported
network to predict the class of peppers.png
.
img = imread("peppers.png");
img = imresize(img, net.Layers(1).InputSize(1:2));
label = predict(net, img);
imshow(img);
title(label);
References
[1] Kudo, Mineichi, Jun Toyama, and Masaru Shimbo. “Multidimensional Curve Classification Using Passing-through Regions.” Pattern Recognition Letters 20, no. 11–13 (November 1999): 1103–11. https://doi.org/10.1016/S0167-8655(99)00077-X.
[2] Kudo, Mineichi, Jun Toyama, and Masaru Shimbo. Japanese Vowels Data Set. Distributed by UCI Machine Learning Repository. https://archive.ics.uci.edu/ml/datasets/Japanese+Vowels.
See Also
Deep Network Designer | trainingOptions
| trainNetwork