assembleNetwork
Assemble deep learning network from pretrained layers
Description
assembleNetwork
creates deep learning networks from layers
without training.
Use assembleNetwork
for the following tasks:
Convert a layer array or layer graph to a network ready for prediction.
Assemble networks from imported layers.
Modify the weights of a trained network.
To train a network from scratch, use the trainnet
or
trainNetwork
function.
assembles the layer array or layer graph assembledNet
= assembleNetwork(layers
)layers
into a deep learning
network ready to use for prediction.
Examples
Assemble Network from Pretrained Keras Layers
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: '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.
The Keras network contains some layers that are not supported by Deep Learning Toolbox™. The importKerasLayers
function displays a warning and replaces the unsupported layers with placeholder layers.
Replace Placeholder Layers
To replace the placeholder layers, first identify the names of the layers to replace. Find the placeholder layers using findPlaceholderLayers
and display their Keras configurations.
placeholderLayers = findPlaceholderLayers(lgraph); placeholderLayers.KerasConfiguration
ans = struct with fields:
trainable: 1
name: 'gaussian_noise_1'
stddev: 1.5000
inbound_nodes: {{1x1 cell}}
ans = struct with fields:
trainable: 1
name: 'gaussian_noise_2'
stddev: 0.7000
inbound_nodes: {{1x1 cell}}
Create two Gaussian noise layers with the same configurations as the imported Keras layers using the helper gaussianNoiseLayer
function.
gnLayer1 = gaussianNoiseLayer(1.5,'new_gaussian_noise_1'); gnLayer2 = gaussianNoiseLayer(0.7,'new_gaussian_noise_2');
Replace the placeholder layers with the custom layers using replaceLayer
.
lgraph = replaceLayer(lgraph,'gaussian_noise_1',gnLayer1); lgraph = replaceLayer(lgraph,'gaussian_noise_2',gnLayer2);
Specify Class Names
The imported classification layer does not contain the classes, so you must specify these before assembling the network. If you do not specify the classes, then the software automatically sets the classes to 1
, 2
, ..., N
, where N
is the number of classes.
The classification layer has the name 'ClassificationLayer_activation_1'
. Set the classes to 0
, 1
, ..., 9
, and then replace the imported classification layer with the new one.
cLayer = lgraph.Layers(end);
cLayer.Classes = string(0:9);
lgraph = replaceLayer(lgraph,'ClassificationLayer_activation_1',cLayer);
Assemble Network
Assemble the layer graph using assembleNetwork
. The function returns a DAGNetwork
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
layers
— Neural network layers
Layer
array | LayerGraph
object
Neural network layers, specified as a Layer
array
or a LayerGraph
object.
To create a neural network with all layers connected sequentially, you can use a
Layer
array as the input argument. In this case, the returned
neural network is a SeriesNetwork
object.
A directed acyclic graph (DAG) neural network has a complex structure in which layers
can have multiple inputs and outputs. To create a DAG neural network, specify the neural
network architecture as a LayerGraph
object and then use that layer graph as the input argument to
assembleNetwork
.
The assembleNetwork
function supports neural networks with at most one
sequence input layer.
For a list of built-in layers, see List of Deep Learning Layers.
Output Arguments
assembledNet
— Assembled network
SeriesNetwork
object | DAGNetwork
object
Assembled network ready for prediction, returned as a SeriesNetwork
object or a DAGNetwork
object. The returned network depends on the layers
input argument:
If
layers
is aLayer
array, thenassembledNet
is aSeriesNetwork
object.If
layers
is aLayerGraph
object, thenassembledNet
is aDAGNetwork
object.
Version History
Introduced in R2018b
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes 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)