findPlaceholderLayers
Find placeholder layers in network architecture imported from Keras orONNX
Syntax
Description
returns all placeholder layers that exist in the network architectureplaceholderLayers
= findPlaceholderLayers(importedLayers
)importedLayers
imported by theimportKerasLayers
orimportONNXLayers
functions, or created by thefunctionToLayerGraph
function. Placeholder layers are the layers that these functions insert in place of layers that are not supported by Deep Learning Toolbox™.
To use with an imported network, this function requires either theDeep Learning Toolbox Converter for TensorFlow™ Modelssupport package or theDeep Learning Toolbox Converter for ONNX™ Model Formatsupport package.
[
also returns the indices of the placeholder layers.placeholderLayers
,indices
] = findPlaceholderLayers(importedLayers
)
Examples
Find and Explore Placeholder Layers
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. TheimportKerasLayers
function replaces each unsupported layer with a placeholder layer and returns a warning message.
lgraph = importKerasLayers(modelfile)
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: Layers: [15x1 nnet.cnn.layer.Layer] Connections: [15x2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_activation_1'}
Display the imported layers of the network. Two placeholder layers replace the Gaussian noise layers in the Keras network.
lgraph.Layers
ans x1 = 15层与层:数组1 input_1 Image Input 28x28x1 images 2 'conv2d_1' Convolution 20 7x7 convolutions with stride [1 1] and padding 'same' 3 'conv2d_1_relu' ReLU ReLU 4 'conv2d_2' Convolution 20 3x3 convolutions with stride [1 1] and padding 'same' 5 'conv2d_2_relu' ReLU ReLU 6 'gaussian_noise_1' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer 7 'gaussian_noise_2' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer 8 'max_pooling2d_1' Max Pooling 2x2 max pooling with stride [2 2] and padding 'same' 9 'max_pooling2d_2' Max Pooling 2x2 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 usingfindPlaceholderLayers
. The output argument contains the two placeholder layers thatimportKerasLayers
inserted in place of the Gaussian noise layers of the Keras network.
placeholders = findPlaceholderLayers(lgraph)
placeholders = 2x1 PlaceholderLayer array with layers: 1 'gaussian_noise_1' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer 2 'gaussian_noise_2' PLACEHOLDER LAYER 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
gaussian2.KerasConfiguration
ans =struct with fields:trainable: 1 name: 'gaussian_noise_2' stddev: 0.7000
Assemble Network from Pretrained Keras Layers
This example shows how to import the layers from a pretrained Keras network, replace the unsupported layers with custom layers, and assemble the layers into a network ready for prediction.
Import Keras Network
Import the layers from a Keras network model. The network in'digitsDAGnetwithnoise.h5'
classifies images of digits.
filename ='digitsDAGnetwithnoise.h5'; lgraph = importKerasLayers(filename,'ImportWeights',true);
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.
The Keras network contains some layers that are not supported by Deep Learning Toolbox. TheimportKerasLayers
function displays a warning and replaces the unsupported layers with placeholder layers.
Plot the layer graph usingplot
.
figure plot(lgraph) title("Imported Network")
替换占位符Laye之类rs
To replace the placeholder layers, first identify the names of the layers to replace. Find the placeholder layers usingfindPlaceholderLayers
.
placeholderLayers = findPlaceholderLayers(lgraph)
placeholderLayers = 2x1 PlaceholderLayer array with layers: 1 'gaussian_noise_1' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer 2 'gaussian_noise_2' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer
Display the Keras configurations of these layers.
placeholderLayers.KerasConfiguration
ans =struct with fields:trainable: 1 name: 'gaussian_noise_1' stddev: 1.5000
ans =struct with fields:trainable: 1 name: 'gaussian_noise_2' stddev: 0.7000
Define a custom Gaussian noise layer. To create this layer, save the filegaussianNoiseLayer.m
in the current folder. Then, create two Gaussian noise layers with the same configurations as the imported Keras layers.
gnLayer1 = gaussianNoiseLayer(1.5,'new_gaussian_noise_1'); gnLayer2 = gaussianNoiseLayer(0.7,'new_gaussian_noise_2');
Replace the placeholder layers with the custom layers usingreplaceLayer
.
lgraph = replaceLayer(lgraph,'gaussian_noise_1',gnLayer1); lgraph = replaceLayer(lgraph,'gaussian_noise_2',gnLayer2);
Plot the updated layer graph usingplot
.
figure plot(lgraph) title("Network with Replaced Layers")
Specify Class Names
If the imported classification layer does not contain the classes, then you must specify these before prediction. If you do not specify the classes, then the software automatically sets the classes to1
,2
, ...,N
, whereN
is the number of classes.
Find the index of the classification layer by viewing theLayers
property of the layer graph.
lgraph.Layers
ans x1 = 15层与层:数组1 input_1 Image Input 28x28x1 images 2 'conv2d_1' Convolution 20 7x7x1 convolutions with stride [1 1] and padding 'same' 3 'conv2d_1_relu' ReLU ReLU 4 'conv2d_2' Convolution 20 3x3x1 convolutions with stride [1 1] and padding 'same' 5 'conv2d_2_relu' ReLU ReLU 6 'new_gaussian_noise_1' Gaussian Noise Gaussian noise with standard deviation 1.5 7 'new_gaussian_noise_2' Gaussian Noise Gaussian noise with standard deviation 0.7 8 'max_pooling2d_1' Max Pooling 2x2 max pooling with stride [2 2] and padding 'same' 9 'max_pooling2d_2' Max Pooling 2x2 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
The classification layer has the name'ClassificationLayer_activation_1'
. View the classification layer and check theClasses
property.
cLayer = lgraph.Layers(end)
cLayer = ClassificationOutputLayer with properties: Name: 'ClassificationLayer_activation_1' Classes: 'auto' ClassWeights: 'none' OutputSize: 'auto' Hyperparameters LossFunction: 'crossentropyex'
Because theClasses
property of the layer is'auto'
, you must specify the classes manually. Set the classes to0
,1
, ...,9
, and then replace the imported classification layer with the new one.
cLayer.Classes = string(0:9)
cLayer = ClassificationOutputLayer with properties: Name: 'ClassificationLayer_activation_1' Classes: [0 1 2 3 4 5 6 7 8 9] ClassWeights: 'none' OutputSize: 10 Hyperparameters LossFunction: 'crossentropyex'
lgraph = replaceLayer(lgraph,'ClassificationLayer_activation_1',cLayer);
Assemble Network
Assemble the layer graph usingassembleNetwork
. The function returns aDAGNetwork
object that is ready to use for prediction.
net = assembleNetwork(lgraph)
net = DAGNetwork with properties: Layers: [15x1 nnet.cnn.layer.Layer] Connections: [15x2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_activation_1'}
Input Arguments
importedLayers
—Network architecture imported from Keras or ONNX or created byfunctionToLayerGraph
Layer
array|LayerGraph
object
Network architecture imported from Keras or ONNX or created byfunctionToLayerGraph
, specified as aLayer
array orLayerGraph
object.
Output Arguments
placeholderLayers
— All placeholder layers in network architecture
array ofPlaceholderLayer
objects
All placeholder layers in the network architecture, returned as an array ofPlaceholderLayer
objects.
indices
— Indices of placeholder layers
vector
Indices of placeholder layers, returned as a vector.
If
importedLayers
is a layer array, thenindices
are the indices of the placeholder layers inimportedLayers
.If
importedLayers
is aLayerGraph
object, thenindices
are the indices of the placeholder layers inimportedLayers.Layers
.
If you remove a layer from or add a layer to aLayer
array orLayerGraph
object, then the indices of the other layers in the object can change. You must usefindPlaceholderLayers
again to find the updated indices of the rest of the placeholder layers.
Tips
If you have installedDeep Learning Toolbox Converter for TensorFlow Modelsand
findPlaceholderLayers
is unable to find placeholder layers created when the ONNX network is imported, then try updating theDeep Learning Toolbox Converter for TensorFlow Modelssupport package in the Add-On Explorer.
See Also
importKerasLayers
|PlaceholderLayer
|replaceLayer
|assembleNetwork
|importONNXLayers
|functionToLayerGraph
|functionLayer
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)