library='MATLAB';
global laps
laps = 0;
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
'nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
X = (1:imds.numpartitions)';
y = imds.Labels;
Nanasi=@(xtrain,ytrain,xtest)myCNNPredict(xtrain,ytrain,xtest,imds,library,laps);
mcr = crossval('mcr',X,y,'Predfun',Nanasi)
function ypred = myCNNPredict(xtrain,ytrain,xtest,imds,library,laps)
switch library
case 'MATLAB'
rng('default');
imdsTrain = imageDatastore(imds.Files(xtrain));
imdsTrain.Labels = ytrain;
imdsValidation = imageDatastore(imds.Files(xtest));
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'Verbose',false);
net = trainNetwork(imdsTrain,layers,options);
ypred = classify(net,imdsValidation);
case 'HALCON'
[RateTable,trainedNet,options]=HalconTestC(laps);
ypred=categorical(RateTable.Var3);
laps=laps+1;
end
end