Main Content

pretrainedEncoderNetwork

Create encoder network from pretrained network

Description

example

net= pretrainedEncoderNetwork(networkName,depth)creates an encoder network,net, from a pretrained network,networkName. The encoder network performsdepthdownsampling 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.

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

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向前, 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

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"— Seealexnet(Deep Learning Toolbox)for more information.

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

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

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

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

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

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

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

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

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

  • "vgg19"— Seevgg19(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 adlnetwork(Deep Learning Toolbox)object. The network hasdepth不同的空间分辨率。金宝搏官方网站最后拉yer of the encoder network is the layer that comes directly before the next downsampling operation of the pretrained network.

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

Introduced in R2021a