Remove parameter from ONNXParameters
object
params = removeParameter(
removes the parameter specified by params
,name
)name
from the ONNXParameters
object params
.
Import a network saved in the ONNX format as a function and modify the network parameters.
Create an ONNX model from the pretrained alexnet
network. Then import alexnet.onnx
as a function. Import the pretrained ONNX network using importONNXFunction
, which returns an ONNXParamaters
object that contains the network parameters. The function also creates a new model function in the current folder that contains the network architecture. Specify the name of the model function as alexnetFcn
.
net = alexnet; exportONNXNetwork(net,'alexnet.onnx'); params = importONNXFunction('alexnet.onnx','alexnetFcn');
A function containing the imported ONNX network has been saved to the file alexnetFcn.m. To learn how to use this function, type: help alexnetFcn.
Display the parameters that are updated during training (params.Learnables
) and the parameters that remain unchanged during training (params.Nonlearnables
).
params.Learnables
ans = struct with fields:
data_Mean: [227×227×3 dlarray]
conv1_W: [11×11×3×96 dlarray]
conv1_B: [96×1 dlarray]
conv2_W: [5×5×48×256 dlarray]
conv2_B: [256×1 dlarray]
conv3_W: [3×3×256×384 dlarray]
conv3_B: [384×1 dlarray]
conv4_W: [3×3×192×384 dlarray]
conv4_B: [384×1 dlarray]
conv5_W: [3×3×192×256 dlarray]
conv5_B: [256×1 dlarray]
fc6_W: [6×6×256×4096 dlarray]
fc6_B: [4096×1 dlarray]
fc7_W: [1×1×4096×4096 dlarray]
fc7_B: [4096×1 dlarray]
fc8_W: [1×1×4096×1000 dlarray]
fc8_B: [1000×1 dlarray]
params.Nonlearnables
ans = struct with fields:
conv1_Stride: [1×2 dlarray]
conv1_DilationFactor: [1×2 dlarray]
conv1_Padding: [1×1 dlarray]
pool1_PoolSize: [1×2 dlarray]
pool1_Stride: [1×2 dlarray]
pool1_Padding: [1×1 dlarray]
conv2_Stride: [1×2 dlarray]
conv2_DilationFactor: [1×2 dlarray]
conv2_Padding: [2×2 dlarray]
pool2_PoolSize: [1×2 dlarray]
pool2_Stride: [1×2 dlarray]
pool2_Padding: [1×1 dlarray]
conv3_Stride: [1×2 dlarray]
conv3_DilationFactor: [1×2 dlarray]
conv3_Padding: [2×2 dlarray]
conv4_Stride: [1×2 dlarray]
conv4_DilationFactor: [1×2 dlarray]
conv4_Padding: [2×2 dlarray]
conv5_Stride: [1×2 dlarray]
conv5_DilationFactor: [1×2 dlarray]
conv5_Padding: [2×2 dlarray]
pool5_PoolSize: [1×2 dlarray]
pool5_Stride: [1×2 dlarray]
pool5_Padding: [1×1 dlarray]
fc6_Stride: [1×2 dlarray]
fc6_DilationFactor: [1×2 dlarray]
fc6_Padding: [1×1 dlarray]
fc7_Stride: [1×2 dlarray]
fc7_DilationFactor: [1×2 dlarray]
fc7_Padding: [1×1 dlarray]
fc8_Stride: [1×2 dlarray]
fc8_DilationFactor: [1×2 dlarray]
fc8_Padding: [1×1 dlarray]
The network has parameters that represent three fully connected layers. You can remove the parameters of the fully connected layer fc7
to reduce computational complexity. Check the output dimensions of the previous layer and the input dimensions of the subsequent layer before removing a middle layer from params
.
Remove the parameters of layer fc7
by using removeParameter
.
params = removeParameter(params,'fc7_B'); params = removeParameter(params,'fc7_W'); params = removeParameter(params,'fc7_Stride'); params = removeParameter(params,'fc7_DilationFactor'); params = removeParameter(params,'fc7_Padding');
Display the updated learnable and nonlearnable parameters.
params.Learnables
ans = struct with fields:
data_Mean: [227×227×3 dlarray]
conv1_W: [11×11×3×96 dlarray]
conv1_B: [96×1 dlarray]
conv2_W: [5×5×48×256 dlarray]
conv2_B: [256×1 dlarray]
conv3_W: [3×3×256×384 dlarray]
conv3_B: [384×1 dlarray]
conv4_W: [3×3×192×384 dlarray]
conv4_B: [384×1 dlarray]
conv5_W: [3×3×192×256 dlarray]
conv5_B: [256×1 dlarray]
fc6_W: [6×6×256×4096 dlarray]
fc6_B: [4096×1 dlarray]
fc8_W: [1×1×4096×1000 dlarray]
fc8_B: [1000×1 dlarray]
params.Nonlearnables
ans = struct with fields:
conv1_Stride: [1×2 dlarray]
conv1_DilationFactor: [1×2 dlarray]
conv1_Padding: [1×1 dlarray]
pool1_PoolSize: [1×2 dlarray]
pool1_Stride: [1×2 dlarray]
pool1_Padding: [1×1 dlarray]
conv2_Stride: [1×2 dlarray]
conv2_DilationFactor: [1×2 dlarray]
conv2_Padding: [2×2 dlarray]
pool2_PoolSize: [1×2 dlarray]
pool2_Stride: [1×2 dlarray]
pool2_Padding: [1×1 dlarray]
conv3_Stride: [1×2 dlarray]
conv3_DilationFactor: [1×2 dlarray]
conv3_Padding: [2×2 dlarray]
conv4_Stride: [1×2 dlarray]
conv4_DilationFactor: [1×2 dlarray]
conv4_Padding: [2×2 dlarray]
conv5_Stride: [1×2 dlarray]
conv5_DilationFactor: [1×2 dlarray]
conv5_Padding: [2×2 dlarray]
pool5_PoolSize: [1×2 dlarray]
pool5_Stride: [1×2 dlarray]
pool5_Padding: [1×1 dlarray]
fc6_Stride: [1×2 dlarray]
fc6_DilationFactor: [1×2 dlarray]
fc6_Padding: [1×1 dlarray]
fc8_Stride: [1×2 dlarray]
fc8_DilationFactor: [1×2 dlarray]
fc8_Padding: [1×1 dlarray]
Modify the architecture of the model function to reflect the changes in params
so you can use the network for prediction with the new parameters or retrain the network. Open the model function by using open alexnetFcn
and remove the fully connected layer fc7
.
params
— Network parametersONNXParameters
objectNetwork parameters, specified as an ONNXParameters
object. params
contains the network
parameters of the imported ONNX™ model.
name
— Name of parameterName of the parameter, specified as a character vector or string scalar.
Example: 'conv2_W'
Example: 'conv2_Padding'
params
— Network parametersONNXParameters
objectNetwork parameters, returned as an ONNXParameters
object. params
contains the network parameters updated by removeParameter
.
You have a modified version of this example. Do you want to open this example with your edits?