Generate MATLAB Code from Deep Network Designer

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 trainNetwork function.

  • 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.

Generate MATLAB Code and Recreate Network Layers

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.

Train Network

If the layers require training, supply the layer graph or layer array to the trainNetwork function.

net = trainNetwork(data,lgraph,options);
Before training, you must define the data and training options. This example defines data and options suitable for training a GoogLeNet network prepared for transfer learning, as shown in Transfer Learning with Deep Network Designer.

  1. Define the data. For this example, use an image datastore with 5 classes split into training and validation sets.

    imds = imageDatastore('MerchData', ...
        'IncludeSubfolders',true, ...
    [imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize');
    You usually need to resize the images to match the input size of the network. Resize at training time to 224-by-224 to match the pretrained network GoogLeNet.

    augimdsTrain = augmentedImageDatastore([224 224],imdsTrain);
    augimdsValidation = augmentedImageDatastore([224 224],imdsValidation);
  2. 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 transferred layers.

    options = trainingOptions('sgdm', ...
      'MiniBatchSize',10, ...
      'MaxEpochs',6, ...
      'InitialLearnRate',1e-4, ...
      'Shuffle','every-epoch', ...
      'ValidationData',augimdsValidation, ...
      'ValidationFrequency',10, ...
      'Verbose',false, ...

  3. To recreate the network layers, run the generated script.

  4. To train the network, supply the layer graph or layer array to the trainNetwork function, using the specified data and training options.

    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.

Use Network for Prediction

To use the trained network for prediction, use the predict function. For example, use the network to predict the class of peppers.png.

img = imread("peppers.png");
img = imresize(img, [128, 128]);
label = predict(net, img);

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.

See Also

| |

Related Topics