Help solving error "Undefined function or variable 'objFcn'. with Bayesian Optimization Transfer Learning. How do I fix?

3 ビュー (過去 30 日間)
I am trying to follow the example for Deep Learning Using Bayesian Optimization (https://www.mathworks.com/help/deeplearning/examples/deep-learning-using-bayesian-optimization.html), but with transfer learning instead. I keep running into the error:
"Undefined function or variable 'objFcn'.
Error in test_BayesianOptimization (line 38)
BayesObj = bayesopt(objFcn,optimVars,..."
I am using imageDataStores instead of 4-D uint8 arrays and categorical arrays to store the images and I think this might be part of the problem but I'm not sure how to go about fixing it.
Some of the code I think is relevant to my problem is as follows:
%load data
imds = imageDatastore('D:\Wavelets\Transfer Learning\Images', ...
'IncludeSubfolders', true, ...
'FileExtensions', '.jpg', ...
'LabelSource', 'foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.8,'randomize');
%Create the object function for the Bayesian optimizer.
%defined at bottom of script.
ObjFcn = makeObjFcn(imdsTrain,imdsValidation);
%this is where line 38 is that I keep getting the error
BayesObj = bayesopt(objFcn,optimVars,...
'MaxTime',1.5*60*60,...
'IsObjectDeterministic',false,...
'UseParallel',false);
%-------------------------ObjectiveFunction-------------------
function ObjFcn = makeObjFcn(imdsTrain,imdsValidation)
ObjFcn = @valErrorFun;
function [valError,cons,fileName] = valErrorFun(optVars)
%load the pretrained network
net = alexnet;
%analyzeNetwork(net);
%******************define network architecture********************
inputSize = net.Layers(1).InputSize;
%replace final layers of network for new training classifications
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels));
layers = [
layersTransfer
fullyConnectedLayer(numClasses,...
'WeightLearnRateFactor',10,...
'BiasLearnRateFactor',10)
softmaxLayer
classificationLayer];
%---------------------Resize Images--------------------------
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
%-------------------set up training options------------------
miniBatchSize = optVars.miniBatchSize;
validationFreq = floor(numel(imdsTrain)/miniBatchSize);
options = trainingOptions('sgdm',...
'MiniBatchSize',miniBatchSize,...
'MaxEpochs',optVars.MaxEpochs,...
'Shuffle','every-epoch',...
'ValidationData',augimdsValidation,...
'ValidationFrequency',valFreq,...
'InitialLearnRate',optVars.InitialLearnRate, ...
'L2Regularization',optVars.L2Regularization, ...
'Momentum',optVars.Momentum, ...
'Verbose',false,...
'Plots','training-progress');
%train network
netTransfer = trainNetwork(augimdsTrain,layers,options);
%Evaluate training
YPredict = classify(netTransfer,augimdsValidation);
valError = 1 - mean(YPredict == imdsValidation.Labels);
fileName = num2str(valError) + ".mat";
save(fileName,'netTransfer','valError','options')
cons = [];
end
end

採用された回答

Don Mathis
Don Mathis 2019 年 7 月 3 日
It looks like you named your variable ObjFcn but then passed it as objFcn.
ObjFcn = makeObjFcn(imdsTrain,imdsValidation);
BayesObj = bayesopt(objFcn,optimVars,...

その他の回答 (3 件)

rajnish kumar
rajnish kumar 2019 年 7 月 19 日
Where have you defined the optimVars variable ?

TZU CHIN CHUANG
TZU CHIN CHUANG 2020 年 3 月 19 日
what does it mean that you can answer it in detail?

Arunkumar
Arunkumar 2024 年 3 月 13 日
Unrecognized function or variable 'augimdsValidation'

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by