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{:});
サンプルコードをコピペし、自前のイメージフォルダを使っています。
采纳的回答
Atsushi Ueno
2022-10-25
编辑:Atsushi Ueno
2022-10-26
%> (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 个评论
ご回答ありがとうございます!
お送りしていただいたコードを入力いたしましたが、同様のエラーが発生しました。
下記は実際のコードです。
ご回答お待ちしております。
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');
引っ掛かってたのは検証用のイメージデータストアですね。
回答に検証イメージの方も追加しました。やっている事は先の回答と同じです。
ご回答ありがとうございます!
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'];にしても治りません。
簡単な質問で申し訳ありません、よろしくお願いいたします。
ごめんなさい。コンマを一つ付け忘れていました
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation, ... % ←コンマ付け忘れ
'ColorPreprocessing','gray2rgb'); % ←ここを追加
ありがとうございました!!!
実行してみたところ、下記のようなエラーが出てきました。
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);
学習用イメージデータストアの方も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'); % ←ここを追加
陸
2022-10-26
最後まで実行されました!
今回重要なことは画像の色調が統一されておらず、'ColorPreprocessing'オプションを使用して入力をカラーイメージに統一することができる事で間違いないでしょうか?
精度は93%程度でした!本当にありがとうございました。
今回重要なことは画像の色調が統一されておらず、'ColorPreprocessing'オプションを使用して入力をカラーイメージに統一することができる事で間違いないでしょうか?
⇒間違いありません。エラーメッセージの言う通りです。
何度も回答を間違えてしまい申し訳ありません。出来るだけこちらでもコードを動かして検証してから投稿するように致します。
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Deep Learning Toolbox 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)