Unreezeparameters
Convert nonlearnable network parameters inONNXParameters
至learnable
Description
params = utreezeparameters(
unfreezes the network parameters specified byparams
,,,,names
)names
in theONNXParameters
目的params
。该函数将指定参数从参数
in the input argumentparams
至params.Learnables
在输出参数中params
。
Examples
使用自定义培训循环导入的火车在ONNX功能
Import thesqueezenet
convolution neural network as a function and fine-tune the pretrained network with transfer learning to perform classification on a new collection of images.
此示例使用多个辅助功能。要查看这些功能的代码,请参见Helper Functions。
Unzip and load the new images as an image datastore.imageDatastore
automatically labels the images based on folder names and stores the data as an成像
目的。图像数据存储使您能够存储大型图像数据,包括不适合内存的数据,并在卷积神经网络训练期间有效地读取图像的批次。指定迷你批量大小。
解压缩('MerchData.zip');miniBatchSize = 8; imds = imageDatastore('merchdata',,,,...'IncludeSubfolders',真的,...'LabelSource',,,,“折叠式”,,,,...“读取尺寸”,,,,miniBatchSize);
该数据集很小,包含75张培训图像。显示一些示例图像。
numImages = numel(imds.Labels); idx = randperm(numImages,16); figure为了i = 1:16子图(4,4,i)i = readImage(imds,idx(i));imshow(i)结尾
Extract the training set and one-hot encode the categorical classification labels.
XTrain = readall(imds); XTrain = single(cat(4,XTrain{:})); YTrain_categ = categorical(imds.Labels); YTrain = onehotencode(YTrain_categ,2)';
确定数据中的类数。
class =类别(ytrain_categ);numClasses = numel(class)
数字= 5
squeezenet
是一个卷积神经网络,对来自Imagenet数据库的一百万张图像进行了培训。结果,该网络已经为广泛的图像学习了丰富的功能表示。该网络可以将图像分类为1000个对象类别,例如键盘,鼠标,铅笔和许多动物。
Import the pretrainedsqueezenet
网络作为函数。
squeezenetonnx()params = experconnxFunction('squeezenet.onnx',,,,'squeezenetFcn')
A function containing the imported ONNX network has been saved to the file squeezenetFcn.m. To learn how to use this function, type: help squeezenetFcn.
params = ONNXParameters with properties: Learnables: [1×1 struct] Nonlearnables: [1×1 struct] State: [1×1 struct] NumDimensions: [1×1 struct] NetworkFunctionName: 'squeezenetFcn'
params
is anONNXParameters
目的that contains the network parameters.squeezenetFcn
是包含网络体系结构的模型函数。experconnxfunction
保存squeezenetFcn
在当前文件夹中。
计算新培训集中预审计网络的分类精度。
accuracyBeforeTraining = getNetworkAccuracy(XTrain,YTrain,params); fprintf('%.2f accuracy before transfer learning\n',,,,accuracyBeforeTraining);
0.01 accuracy before transfer learning
准确性非常低。
Display the learnable parameters of the network by typingparams.Learnables
。这se parameters, such as the weights (w
)和偏见(b
)of convolution and fully connected layers, are updated by the network during training. Nonlearnable parameters remain constant during training.
这last two learnable parameters of the pretrained network are configured for 1000 classes.
conv10_W: [1×1×512×1000 dlarray]
conv10_b:[1000×1 dlarray]
参数conv10_W
andconv10_B
must be fine-tuned for the new classification problem. Transfer the parameters to classify five classes by initializing the parameters.
params.Learnables.conv10_W = rand(1,1,512,5); params.Learnables.conv10_B = rand(5,1);
冻结网络的所有参数,以将其转换为不可验证的参数。因为您不需要计算冷冻层的梯度,所以冻结许多初始层的重量可以显着加快网络训练。
params = freezeParameters(params,'all');
解冻the last two parameters of the network to convert them to learnable parameters.
params = utreezeparameters(params,'conv10_w');params = utreezeparameters(params,'conv10_b');
Now the network is ready for training. Initialize the training progress plot.
情节=“训练过程”;if情节==“训练过程”figure lineLossTrain = animatedline; xlabel(“迭代”)ylabel(“失利”)结尾
指定培训选项。
速度= [];numepochs = 5;minibatchsize = 16;numObservations = size(ytrain,2);NumiterationsPerePoch = floor(NumObservations./minibatchsize);初始leartlearnrate = 0.01;动量= 0.9;衰减= 0.01;
训练网络。
iteration = 0; start = tic; executionEnvironment =“中央处理器”;% Change to "gpu" to train on a GPU.%循环时期。为了epoch = 1:numepochs% Shuffle data.idx = randperm(numObservations);Xtrain = Xtrain(:,:,:,:,IDX);ytrain = ytrain(:,idx);% Loop over mini-batches.为了i = 1:numIterationsPerEpoch iteration = iteration + 1;%读取迷你数据的数据。idx = (i-1)*miniBatchSize+1:i*miniBatchSize; X = XTrain(:,:,:,idx); Y = YTrain(:,idx);% If training on a GPU, then convert data to gpuArray.if(executionEnvironment ==“汽车”&& canusegpu)||executionEnvironment ==“ GPU”X = gpuArray(X);结尾%使用DLFEVAL评估模型梯度和损失% modelGradients function.[渐变,损失,状态] = dlfeval(@modelGradients,x,y,params);params.state = state;%确定基于时间的衰减学习率计划的学习率。learnRate = initialLearnRate/(1 + decay*iteration);%更新网络参数s using the SGDM optimizer.[params.Learnables,velocity] = sgdmupdate(params.Learnables,gradients,velocity);%显示培训进度。if情节==“训练过程”D = duration(0,0,toc(start),'格式',,,,'HH:MM:SS');addpoints(lineLossTrain,iteration,double(gather(extractdata(loss)))) title(“时代:”+ epoch +“,经过:”+ string(D)) drawnow结尾结尾结尾
微调后计算网络的分类精度。
cecurefacyFtraining = getNetworkAccuracy(Xtrain,Ytrain,params);fprintf('%.2F转移学习后的精度\ n',准确训练);
1.00 accuracy after transfer learning
Helper Functions
本节提供了本示例中使用的助手功能的代码。
这getNetworkCcuracy
function evaluates the network performance by calculating the classification accuracy.
functionaccuracy = getNetworkAccuracy(X,Y,onnxParams) N = size(X,4); Ypred = squeezenetFcn(X,onnxParams,'训练',错误的);[〜,yidx] = max(y,[],1);[〜,ypredidx] = max(ypred,[],1);numIncorrect = sum(abs(yidx-ypredidx)> 0);精度= 1 -numIncorct/n;结尾
这modelGradients
function calculates the loss and gradients.
function[grad,损失,状态] = modelgradients(x,y,onnxparams)[y,state] = squeezenetfcn(x,onnxparams,'训练',,,,true); loss = crossentropy(y,Y,'DataFormat',,,,'CB');grad = dlgradient(loss,onnxParams.Learnables);结尾
这Squeezenetonnx
function generates an ONNX model of thesqueezenet
network.
functionSqueezenetonnx() exportONNXNetwork(squeezenet,'squeezenet.onnx');结尾
Input Arguments
params
-网络参数
ONNXParameters
目的
网络参数,指定为ONNXParameters
目的。params
包含导入ONNX™模型的网络参数。
names
-Names of parameters to unfreeze
'all'
|细绳array
要解冻的参数的名称,指定为'all'
或字符串数组。通过设置解开所有不可检测的参数names
至'all'
。解冻k
nonlearnable parameters by defining the parameter names in the 1-by-k
细绳arraynames
。
Example:[“ GPU_0_SL_PRED_B_0”,“ GPU_0_SL_PRED_W_0”]
Data Types:char
|细绳
输出参数
params
-网络参数
ONNXParameters
目的
网络参数,返回ONNXParameters
目的。params
包含由Unreezeparameters
。
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
您单击了与此MATLAB命令相对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
选择一个网站以获取可用的翻译内容,并查看本地事件和优惠。根据您的位置,我们建议您选择:。
You can also select a web site from the following list:
如何获得最佳网站性能
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。
Americas
- América Latina(Español)
- Canada(English)
- United States(English)