Main Content

blockedNetwork

Create network with repeating block structure

Description

example

net = blockedNetwork(fun,numBlocks) creates an uninitialized network, net, that consists of numBlocks blocks of layers connected sequentially. The function fun creates each block of layers.

This function requires Deep Learning Toolbox™.

net = blockedNetwork(fun,numBlocks,'NamePrefix',namePrefix) adds the prefix namePrefix to all layer names in the network.

Examples

collapse all

Define a function that creates an array of layers. The first block has 32 filters in the convolution layers. The number of filters doubles in each successive block.

unetBlock = @(block) [
    convolution2dLayer(3,2^(5+block))
    reluLayer
    convolution2dLayer(3,2^(5+block))
    reluLayer
    maxPooling2dLayer(2,"Stride",2)];

Create a network that consists of four repeating blocks of layers. Add the prefix "encoder_" to all layer names in the network.

net = blockedNetwork(unetBlock,4,"NamePrefix","encoder_")
net = 
  dlnetwork with properties:

         Layers: [20x1 nnet.cnn.layer.Layer]
    Connections: [19x2 table]
     Learnables: [16x3 table]
          State: [0x3 table]
     InputNames: {'encoder_Block1Layer1'}
    OutputNames: {'encoder_Block4Layer5'}
    Initialized: 0

Initialize network weights for input of size [224 224 3].

net = initialize(net,dlarray(zeros(224,224,3),"SSC"));

Display the network.

analyzeNetwork(net)

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

Display the network.

analyzeNetwork(net)

Input Arguments

collapse all

Function that creates blocks of layers, specified as a function with this signature:

block = fun(blockIndex)

  • The input to fun, blockIndex, is an integer in the range [1, numBlocks].

  • The output from fun, block, is a layer or layer array.

Number of blocks in the network, specified as a positive integer.

Prefix to all layer names in the network, specified as a string or character vector.

Data Types: char | string

Output Arguments

collapse all

Network with a repeating block structure, returned as a dlnetwork (Deep Learning Toolbox) object.

Tips

  • The dlnetwork (Deep Learning Toolbox) returned by blockedNetwork is uninitialized and not ready for use with training or inference. To initialize the network, use the initialize (Deep Learning Toolbox) function.

  • Connect an encoder network to a decoder network using the encoderDecoderNetwork function.

Introduced in R2021a