The Deep Network Designer app enables you to generate MATLAB® code for a network that you create or edit in the app. After generating a script, you can:
Run the script to recreate the network layers created in the app.
To train the network, run the script and then supply the layers to the
Examine the code to learn how to create and connect layers programmatically.
To modify the layers, edit the code, or run the script and import the network back into the app for editing.
To generate MATLAB code in Deep Network Designer, choose one of these options:
To generate a script to recreate the layers in your network, select Export > Generate Code.
To generate a script to recreate your network including any learnable parameters, select Export > Generate Code with Pretrained Parameters. The app creates a script and a MAT-file containing the learnable 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.
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.
If the layers require training, supply the layer graph or layer array to the
net = trainNetwork(data,lgraph,options);
Define the data. For this example, use an image datastore with 5 classes split into training and validation sets.
unzip('MerchData.zip'); imds = imageDatastore('MerchData', ... 'IncludeSubfolders',true, ... 'LabelSource','foldernames'); [imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize');
augimdsTrain = augmentedImageDatastore([224 224],imdsTrain); augimdsValidation = augmentedImageDatastore([224 224],imdsValidation);
Define the training options. For example, turn on the progress plot, specify the
validation data, specify the number of images to use in each iteration
MiniBatchSize) and the number of training cycles to perform on
the entire data set (
MaxEpochs). For transfer learning, set
InitialLearnRate to a small value to slow down learning in the
options = trainingOptions('sgdm', ... 'MiniBatchSize',10, ... 'MaxEpochs',6, ... 'InitialLearnRate',1e-4, ... 'Shuffle','every-epoch', ... 'ValidationData',augimdsValidation, ... 'ValidationFrequency',10, ... 'Verbose',false, ... 'Plots','training-progress');
To recreate the network layers, run the generated script.
To train the network, supply the layer graph or layer array to the
trainNetwork function, using the specified data and training
net = trainNetwork(augimdsTrain,lgraph,options);
For an example script that sets training options for transfer learning on a network prepared in Deep Network Designer, see Train Network Exported from Deep Network Designer.
To use the trained network for prediction, use the
function. For example, use the network to predict the class of
img = imread("peppers.png"); img = imresize(img, [128, 128]); label = predict(net, img); imshow(img); title(label);
For command-line examples showing how to set training options and assess trained network accuracy, see Create Simple Deep Learning Network for Classification and Train Residual Network for Image Classification.