AlexNetを用いた転移学習を実装したい

options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
エラーメッセージ
nnet.cnn.TrainingOptionsSGDM
'ValidationData'の値は無効です。三番目の次元で入力イメージサイズが異なっているため、augmentedImageDatastoreはデータのMiniBatchesを形成できません。'ColorPreprocessing'オプションを使用してすべての拡張イメージに同数のチャネルを確保することを検討してください。
エラー:trainingOptions(340)opts=nnet.cnn.TrainingOptionsSGDM(varargin{:});
サンプルコードをコピペし、自前のイメージフォルダを使っています。

 Réponse acceptée

Atsushi Ueno
Atsushi Ueno le 25 Oct 2022
Modifié(e) : Atsushi Ueno le 26 Oct 2022

1 vote

%> (Alexnetの)最初のイメージ入力層にはサイズが 227 x 227 x 3 の入力イメージが必要です。ここで 3 はカラーチャネルの数です。
「三番目の次元で入力イメージサイズが異なっている」は即ち「カラーイメージが入力されていない」という事です。自前のイメージフォルダの中にカラーではないイメージファイルが存在するはずです。(そのファイルの変更は不要です)
イメージ データストアにグレースケール イメージと RGB イメージが混在していても、augmentedImageDatastore関数にそれらを統一する機能「ColorPreprocessing — 色の前処理演算」オプションがあります。
「'ColorPreprocessing'オプションを使用してすべての拡張イメージに同数のチャネルを確保することを検討してください」は即ち「'ColorPreprocessing'オプションを使用して入力をカラーイメージに統一してください」という事です。
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter, ... % ←他のデータ拡張を実行
'ColorPreprocessing','gray2rgb'); % ←ここを追加
(2022/10/26追記)
>他のデータ拡張を実行せずに検証イメージのサイズを自動的に変更するには、追加の前処理演算を指定せずに拡張イメージ データストアを使用します。(つまり検証イメージの方は2行目を追加しないという事です)
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation, ...
'ColorPreprocessing','gray2rgb'); % ←ここを追加

8 commentaires

陸
le 26 Oct 2022
ご回答ありがとうございます!
お送りしていただいたコードを入力いたしましたが、同様のエラーが発生しました。
下記は実際のコードです。
ご回答お待ちしております。
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter, ...
'ColorPreprocessing','gray2rgb');
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
Atsushi Ueno
Atsushi Ueno le 26 Oct 2022
引っ掛かってたのは検証用のイメージデータストアですね。
回答に検証イメージの方も追加しました。やっている事は先の回答と同じです。
陸
le 26 Oct 2022
ご回答ありがとうございます!
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation...
'ColorPreprocessing','gray2rgb');
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
を入力したところ、
式が無効です。関数の呼び出しまたは変数のインデックス付けにはかっこを使用してください。そうでない場合、区切り記号の不一致をチェックしてください。
となりました。(inputSize~...'gray2rgb');の()に赤い波線が出てきたので、[inputSize~'gray2rgb'];にしても治りません。
簡単な質問で申し訳ありません、よろしくお願いいたします。
Atsushi Ueno
Atsushi Ueno le 26 Oct 2022
ごめんなさい。コンマを一つ付け忘れていました
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation, ... % ←コンマ付け忘れ
'ColorPreprocessing','gray2rgb'); % ←ここを追加
陸
le 26 Oct 2022
ありがとうございました!!!
実行してみたところ、下記のようなエラーが出てきました。
netTransfer = trainNetwork(augimdsTrain,layers,options);
関数または変数 'augimdsTrain' が認識されません。
エラー: test (52)
netTransfer = trainNetwork(augimdsTrain,layers,options);
以下はコード全体になります。
unzip('Negi.zip');
imds = imageDatastore('Negi', ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
numTrainImages = numel(imdsTrain.Labels);
idx = randperm(numTrainImages,16);
figure
for i = 1:16
subplot(4,4,i)
I = readimage(imdsTrain,idx(i));
imshow(I)
end
net = alexnet;
analyzeNetwork(net)
inputSize = net.Layers(1).InputSize;
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels));
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation, ... % ←コンマ付け忘れ
'ColorPreprocessing','gray2rgb'); % ←ここを追加
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,layers,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);
Atsushi Ueno
Atsushi Ueno le 26 Oct 2022
学習用イメージデータストアの方もAlexnetの入力層に入力可能な画像サイズに合わせる必要があります。
こっちはAlexnetの入力層(227*227*3)ですね。
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
-------------------- % ←ここから
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter, ...
'ColorPreprocessing','gray2rgb');
-------------------- % ←ここまでを追加
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation, ... % ←コンマ付け忘れ
'ColorPreprocessing','gray2rgb'); % ←ここを追加
陸
le 26 Oct 2022
最後まで実行されました!
今回重要なことは画像の色調が統一されておらず、'ColorPreprocessing'オプションを使用して入力をカラーイメージに統一することができる事で間違いないでしょうか?
精度は93%程度でした!本当にありがとうございました。
Atsushi Ueno
Atsushi Ueno le 27 Oct 2022
今回重要なことは画像の色調が統一されておらず、'ColorPreprocessing'オプションを使用して入力をカラーイメージに統一することができる事で間違いないでしょうか?
⇒間違いありません。エラーメッセージの言う通りです。
何度も回答を間違えてしまい申し訳ありません。出来るだけこちらでもコードを動かして検証してから投稿するように致します。

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!