PlaceholderLayer - Layer replacing an unsupported Keras or ONNX layer - MATLAB (original) (raw)

Main Content

Layer replacing an unsupported Keras or ONNX layer

Description

Creation

Importing layers from a Keras or ONNX network that has layers that are not supported by Deep Learning Toolbox™ creates PlaceholderLayer objects.

Properties

expand all

Layer name, specified as a character vector or a string scalar.

Data Types: char | string

Layer description, specified as a character vector or a string scalar.

Data Types: char | string

Layer type, specified as a character vector or a string scalar.

Data Types: char | string

Keras configuration of a layer, specified as a structure. The fields of the structure depend on the layer type.

Note

This property only exists if the layer was created when importing a Keras network.

Data Types: struct

ONNX configuration of a layer, specified as a structure. The fields of the structure depend on the layer type.

Note

This property only exists if the layer was created when importing an ONNX network.

Data Types: struct

Imported weights, specified as a structure.

Data Types: struct

Examples

collapse all

Specify the Keras network file to import layers from.

modelfile = 'digitsDAGnetwithnoise.h5';

Import the network architecture. The network includes some layer types that are not supported by Deep Learning Toolbox. The importKerasLayers function replaces each unsupported layer with a placeholder layer and returns a warning message.

lgraph = importKerasLayers(modelfile)

Warning: "importKerasLayers" is not recommended and will be removed in a future release. To import TensorFlow-Keras models, save using the SavedModel format and use importNetworkFromTensorFlow function.

Warning: Unable to import some Keras layers, because they are not supported by the Deep Learning Toolbox. They have been replaced by placeholder layers. To find these layers, call the function findPlaceholderLayers on the returned object.

lgraph = LayerGraph with properties:

 InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
     Layers: [15×1 nnet.cnn.layer.Layer]
Connections: [15×2 table]

Display the imported layers of the network. Two placeholder layers replace the Gaussian noise layers in the Keras network.

ans = 15×1 Layer array with layers:

 1   'input_1'                            Image Input             28×28×1 images
 2   'conv2d_1'                           2-D Convolution         20 7×7 convolutions with stride [1  1] and padding 'same'
 3   'conv2d_1_relu'                      ReLU                    ReLU
 4   'conv2d_2'                           2-D Convolution         20 3×3 convolutions with stride [1  1] and padding 'same'
 5   'conv2d_2_relu'                      ReLU                    ReLU
 6   'gaussian_noise_1'                   GaussianNoise           Placeholder for "GaussianNoise" Keras layer
 7   'gaussian_noise_2'                   GaussianNoise           Placeholder for "GaussianNoise" Keras layer
 8   'max_pooling2d_1'                    2-D Max Pooling         2×2 max pooling with stride [2  2] and padding 'same'
 9   'max_pooling2d_2'                    2-D Max Pooling         2×2 max pooling with stride [2  2] and padding 'same'
10   'flatten_1'                          Keras Flatten           Flatten activations into 1-D assuming C-style (row-major) order
11   'flatten_2'                          Keras Flatten           Flatten activations into 1-D assuming C-style (row-major) order
12   'concatenate_1'                      Depth concatenation     Depth concatenation of 2 inputs
13   'dense_1'                            Fully Connected         10 fully connected layer
14   'activation_1'                       Softmax                 softmax
15   'ClassificationLayer_activation_1'   Classification Output   crossentropyex

Find the placeholder layers using findPlaceholderLayers. The output argument contains the two placeholder layers that importKerasLayers inserted in place of the Gaussian noise layers of the Keras network.

placeholders = findPlaceholderLayers(lgraph)

placeholders = 2×1 PlaceholderLayer array with layers:

 1   'gaussian_noise_1'   GaussianNoise   Placeholder for "GaussianNoise" Keras layer
 2   'gaussian_noise_2'   GaussianNoise   Placeholder for "GaussianNoise" Keras layer

Specify a name for each placeholder layer.

gaussian1 = placeholders(1); gaussian2 = placeholders(2);

Display the configuration of each placeholder layer.

gaussian1.KerasConfiguration

ans = struct with fields: trainable: 1 name: 'gaussian_noise_1' stddev: 1.5000 inbound_nodes: {{1×1 cell}}

gaussian2.KerasConfiguration

ans = struct with fields: trainable: 1 name: 'gaussian_noise_2' stddev: 0.7000 inbound_nodes: {{1×1 cell}}

Version History

Introduced in R2017b