交差検証を用いた誤分類率の検証について
Show older comments
交差検証を行おうとした結果、以下のようなエラーが出ました。
関数 '@(xtrain,ytrain,xtest)myCNNPredict(xtrain,ytrain,xtest,imds)' により次のエラーが発生しました:
'WindowChannelSize' の値は無効です。 入力 は以下のいずれかのタイプが必要です:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
エラー: crossval>getLossVal (line 525)
funResult = evalFun(funorStr,arg(1:end-1));
エラー: crossval (line 424)
[funResult,outarg] = getLossVal(i, nData, cvp, data, predfun);
エラー: crosval2 (line 20)
mcr = crossval('mcr',X,y,'Predfun',@(xtrain,ytrain,xtest)myCNNPredict(xtrain,ytrain,xtest,imds),'partition',cp)
以下のコードを実行したのですが、エラーが、どういう意味なのか教えて頂けますでしょうか。
また、解決方法があれば教えて頂けますでしょうか。
よろしくお願いいたします。
imds = imageDatastore('houdenmatlab1', ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
figure;
perm = randperm(200,20);
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
labelCount = countEachLabel(imds)
img = readimage(imds,1);
size(img)
numTrainFiles = 80;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
%% ダミーのトレーニングインデックスを生成
X = (1:imds.numpartitions)';
y = imds.Labels;
%% 交差検定にCNNの予測ラベル関数のポインタを渡す
cp = cvpartition(y,'k',3); % Stratified cross-validation
mcr = crossval('mcr',X,y,'Predfun',@(xtrain,ytrain,xtest)myCNNPredict(xtrain,ytrain,xtest,imds),'partition',cp)
%% CNNを学習し、予測ラベルを出力する関数
function ypred = myCNNPredict(xtrain,ytrain,xtest,imds)
% 結果が一意になるように乱数シードをデフォルト値に設定
rng('default');
% ダミーの変数ベクトルを受けてimageDatastoreを学習用とテスト用に分割
imdsTrain = imageDatastore(imds.Files(xtrain));
imdsTrain.Labels = ytrain;
imdsValidation = imageDatastore(imds.Files(xtest));
% レイヤーの設定
layers = [
imageInputLayer([150 200 3],'Name','input')
convolution2dLayer(3,8,'Padding','same','Name','conv1')
batchNormalizationLayer('Name','BN1')
reluLayer('Name','relu1')
maxPooling2dLayer(2,'Stride',2,'Name','pool1')
convolution2dLayer(3,16,'Padding','same','Name','conv2')
batchNormalizationLayer('Name','BN2')
reluLayer('Name','relu2')
maxPooling2dLayer(2,'Stride',2,'Name','pool2')
convolution2dLayer(3,32,'Padding','same','Name','conv3')
batchNormalizationLayer('Name','BN3')
reluLayer('Name','relu3')
fullyConnectedLayer(2,'Name','fc')
softmaxLayer('Name','softmax')
classificationLayer('Name','classoutput')];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',100, ...
'Shuffle','every-epoch', ...
'Verbose',false);
net3 = trainNetwork(imdsTrain,layers,options);
ypred = classify(net3,imdsValidation);
save net3
end
1 Comment
Kenta
on 8 May 2020
私の回答に補足ですが、交差検証をしている例はここにありました。参考になれば幸いです。
Accepted Answer
More Answers (1)
Hiro Yoshino
on 31 Jan 2020
0 votes
Breakpointを使って、どこでエラーが発生しているか調べられますか?
3 Comments
Kaneko
on 31 Jan 2020
Kenta
on 31 Jan 2020
例えば、下のようにすれば、layersに関するエラーは回避できます。
crossChannelNormalizationLayerはチャンネルサイズをはじめに定義しないといけないですが、
それが抜けているのでエラーを返しています。
dropout層も少し間違っていたので訂正しています。
layers = [
imageInputLayer([150 200 3],'Name','input')
convolution2dLayer(3,8,'Padding','same','Name','conv1')
batchNormalizationLayer('Name','BN1')
reluLayer('Name','relu1')
crossChannelNormalizationLayer(5)
maxPooling2dLayer(2,'Stride',2,'Name','pool1')
convolution2dLayer(3,16,'Padding','same','Name','conv2')
batchNormalizationLayer('Name','BN2')
reluLayer('Name','relu2')
crossChannelNormalizationLayer(5)
maxPooling2dLayer(2,'Stride',2,'Name','pool2')
convolution2dLayer(3,32,'Padding','same','Name','conv3')
batchNormalizationLayer('Name','BN3')
reluLayer('Name','relu3')
dropoutLayer('probability',0.5,'Name','drop6')
fullyConnectedLayer(2,'Name','fc')
softmaxLayer('Name','softmax')
classificationLayer('Name','classoutput')];
Kaneko
on 31 Jan 2020
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!