trainNetwo​rkを使用し「入力イ​メージは4チャネル未​満でなければなりませ​ん。」と出た時の対処​法

7 views (last 30 days)
塁 小熊
塁 小熊 on 1 Jul 2021
Commented: Kenta on 20 Jul 2021
imds=imageDatastore('Oguma_TestImages_Copy','IncludeSubfolder',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation]=splitEachLabel(imds,0.7);
numTrainImages = numel(imdsTrain.Labels);
net=squeezenet;
inputSize = net.Layers(1).InputSize
lgraph = layerGraph(net);
[learnableLayer,classLayer] = findLayersToReplace(lgraph);
[learnableLayer,classLayer]
numClasses = numel(categories(imdsTrain.Labels))
newConvLayer = convolution2dLayer([1, 1],numClasses,'WeightLearnRateFactor',10,'BiasLearnRateFactor',10,"Name",'new_conv');
lgraph = replaceLayer(lgraph,'conv10',newConvLayer);
newClassificatonLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',newClassificatonLayer);
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore([227 227 3],imdsTrain,'ColorPreprocessing','gray2rgb', ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore([227 227 3],imdsValidation,'ColorPreprocessing','gray2rgb');
options = trainingOptions('sgdm', ...
'MiniBatchSize',11, ...
'MaxEpochs',7, ...
'InitialLearnRate',2e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
[YPred,scores] = classify(netTransfer,augimdsValidation);
idx = randperm(numel(imdsValidation.Files),4);
figure
for i = 1:4
subplot(2,2,i)
I = readimage(imdsValidation,idx(i));
imshow(I)
label = YPred(idx(i));
title(string(label));
end
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
SqueezNetによる画像の転移学習をしようと思ったのですが、
エラー: trainNetwork (line 170)
入力イメージは数値で、4 チャネル未満でなければなりません。
エラー: Oguma_SqueezeNet_Code_03 (line 38)
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
原因:
エラー: imageDataAugmenter/augment (line 596)
入力イメージは数値で、4 チャネル未満でなければなりません。
と出て、うまくいきません。
24、26行目でイメージデータストアは[227 227 3]として3チャネルになるように指定しているにも関わらず、なぜtrainNetwork内では4チャネルと認識されているのでしょうか?
対処法をご存知の方がいれば教えていただきたいです。
製品は MATLAB R2020a です。
よろしくお願いいたします。
  17 Comments
塁 小熊
塁 小熊 on 19 Jul 2021
Kentaさん こんにちは。
そのコードを試そうと思い、エラー出た際に使ったファイルを改めて見返しました。
前回指摘されたのもサイズでしたので、その時はサイズやピクセルばかり見ていたのですが、
なんとなくファイルの表示形式を変えて改めて目を通したらエラーの原因は一目瞭然でした。
この醜態を晒さずにそっと心の片隅にしまう事も一瞬考えたのですが、後学のために、そして戒めとして、この事実を共有しようと思います。
1 枚 だ け G I F で し た。
これを見つけた時、思わず笑ってしまいました。
Kusanoさんにかけたご迷惑と奪ってしまった時間は全て、この一枚のGIFと自分の間抜けさが原因だったと思うと、なんというかもう笑うしかありません。
改めてKusanoさん、ありがとうございました。と同時にほんっとにすいませんでした!
Kenta
Kenta on 20 Jul 2021
なるほど!小熊さんありがとうございます。データストアにGIFも格納されてしまうのですね、、、私もGIFはよく使うので参考になります。原因がわかったようでよかったです。
「タグ」のところにimageDataAugmenterなど、今回のご質問で関連していそうな単語をいれておくと、今後似た問題が検索されたときに引っかかる確率があがりそうです。

Sign in to comment.

Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!