Main Content

pretrainedEncoderNetwork

Create encoder network from pretrained network

Since R2021a

Description

net = pretrainedEncoderNetwork(networkName,depth) creates an encoder network, net, from a pretrained network, networkName. The encoder network performs depth downsampling operations.

This function requires Deep Learning Toolbox™.

example

[net,outputNames] = pretrainedEncoderNetwork(networkName,depth) also returns the names, outputNames, of activation layers that occur directly before downsampling operations. These activations correspond to features of interest at particular spatial resolutions or scales.

example

Examples

collapse all

Create an encoder with three downsampling operations based on the SqueezeNet pretrained network.

encoderNet = pretrainedEncoderNetwork('squeezenet',3)
encoderNet = 
  dlnetwork with properties:

         Layers: [33x1 nnet.cnn.layer.Layer]
    Connections: [36x2 table]
     Learnables: [26x3 table]
          State: [0x3 table]
     InputNames: {'data'}
    OutputNames: {'fire5-concat'}
    Initialized: 1

  View summary with summary.

Display the encoder network.

analyzeNetwork(encoderNet)

Create a GAN encoder network with four downsampling operations from a pretrained GoogLeNet network.

depth = 4;
[encoder,outputNames] = pretrainedEncoderNetwork('googlenet',depth);

Determine the input size of the encoder network.

inputSize = encoder.Layers(1).InputSize;

Determine the output size of the activation layers in the encoder network by creating a sample data input and then calling forward, which returns the activations.

exampleInput = dlarray(zeros(inputSize),'SSC');
exampleOutput = cell(1,length(outputNames));
[exampleOutput{:}] = forward(encoder,exampleInput,'Outputs',outputNames);

Determine the number of channels in the decoder blocks as the length of the third channel in each activation.

numChannels = cellfun(@(x) size(extractdata(x),3),exampleOutput);
numChannels = fliplr(numChannels(1:end-1));

Define a function that creates an array of layers for one decoder block.

decoderBlock = @(block) [
    transposedConv2dLayer(2,numChannels(block),'Stride',2)
    convolution2dLayer(3,numChannels(block),'Padding','same')
    reluLayer
    convolution2dLayer(3,numChannels(block),'Padding','same')
    reluLayer];

Create the decoder module with the same number of upsampling blocks as there are downsampling blocks in the encoder module.

decoder = blockedNetwork(decoderBlock,depth);

Create the U-Net network by connecting the encoder module and decoder module and adding skip connections.

net = encoderDecoderNetwork([224 224 3],encoder,decoder, ...
   'OutputChannels',3,'SkipConnections','concatenate')
net = 
  dlnetwork with properties:

         Layers: [139x1 nnet.cnn.layer.Layer]
    Connections: [167x2 table]
     Learnables: [116x3 table]
          State: [0x3 table]
     InputNames: {'data'}
    OutputNames: {'encoderDecoderFinalConvLayer'}
    Initialized: 1

  View summary with summary.

Display the network.

analyzeNetwork(net)

Input Arguments

collapse all

Pretrained network name, specified as one of these string values. You must install the associated Add-On for the selected pretrained network.

  • "alexnet" — See alexnet (Deep Learning Toolbox) for more information.

  • "googlenet" — See googlenet (Deep Learning Toolbox) for more information.

  • "inceptionresnetv2" — See inceptionresnetv2 (Deep Learning Toolbox) for more information.

  • "inceptionv3" — See inceptionv3 (Deep Learning Toolbox) for more information.

  • "mobilenetv2" — See mobilenetv2 (Deep Learning Toolbox) for more information.

  • "resnet18" — See resnet18 (Deep Learning Toolbox) for more information.

  • "resnet50" — See resnet50 (Deep Learning Toolbox) for more information.

  • "resnet101" — See resnet101 (Deep Learning Toolbox) for more information.

  • "squeezenet" — See squeezenet (Deep Learning Toolbox) for more information.

  • "vgg16" — See vgg16 (Deep Learning Toolbox) for more information.

  • "vgg19" — See vgg19 (Deep Learning Toolbox) for more information.

Data Types: char | string

Number of downsampling operations in the encoder, specified as a positive integer. The encoder downsamples the input by a factor of 2^depth. You cannot specify a depth greater than the depth of the pretrained network.

Output Arguments

collapse all

Encoder network, returned as a dlnetwork (Deep Learning Toolbox) object. The network has depth distinct spatial resolutions. The final layer of the encoder network is the layer that comes directly before the next downsampling operation of the pretrained network.

Layer names in the network net that come directly before downsampling operations, returned as a string vector.

Version History

Introduced in R2021a