Main Content

Import Custom Layer into Deep Network Designer

This example shows how to import a custom weighted classification layer and add it to a pretrained network in Deep Network Designer. Use a weighted classification layer for classification problems with an imbalanced distribution of classes.

Define a custom weighted classification layer. To create this layer, save the file weightedClassificationLayer.m in the current folder. For more information on constructing this layer, see Define Custom Weighted Classification Layer.

Suppose you have a data set with two classes. Specify the class weights as a vector with two elements corresponding to two classes.

classWeights = [0.2 0.8];

Create an instance of the layer.

 weightedOutputLayer = weightedClassificationLayer(classWeights);

Open Deep Network Designer with a pretrained GoogLeNet network.

deepNetworkDesigner(googlenet);

To adapt a pretrained network, replace the last learnable layer and the final classification layer with new layers adapted to the new data set. In GoogLeNet, these layers have the names 'loss3-classifier' and 'output', respectively.

In the Designer pane, drag a new fullyConnectedLayer from the Layer Library onto the canvas. Set OutputSize to the new number of classes, in this example, 2.

Edit learning rates to learn faster in the new layers than in the transferred layers. Set WeightLearnRateFactor and BiasLearnRateFactor to 10. Delete the last fully connected layer and connect your new layer instead.

Next, replace the output layer with your custom weighted output layer. Click New in the Designer pane. Pause on From Workspace and click Import. To import the custom weighted classification layer, select weightedOutputLayer and click OK.

Add the layer to the current GoogLeNet pretrained network by clicking Add. The app adds the custom layer to the top of the Designer pane. To see the new layer, zoom-in using a mouse or click Zoom in.

Drag the custom layer to the bottom of the Designer pane. Replace the final classification output layer with the new weighted output layer and connect the new layer.

Check your network by clicking Analyze. The network is ready for training if Deep Learning Network Analyzer reports zero errors.

After you construct your network, you are ready to import data and train. For more information on importing data and training in Deep Network Designer, see Transfer Learning with Deep Network Designer.

See Also

Related Topics