How can I modify the first layer of a pre-trained DNN

6 ビュー (過去 30 日間)
Wilmer Ariza
Wilmer Ariza 2020 年 8 月 6 日
回答済み: Wilmer Ariza 2020 年 8 月 7 日
I am using a DNN(resnet-50) for feature extraction. However, the visual features of a category are very small and the resizing of the features delete the data. CNN and pool layer do not get affected by dimmension size change.
When i try to change the input size i get the error:
net.Layers(1).InputSize=[280,280,3]
You cannot set the read-only property 'InputSize' of ImageInputLayer.
How can i modify this layer?

回答 (1 件)

Wilmer Ariza
Wilmer Ariza 2020 年 8 月 7 日
I found the solution,
The network has to be move to layer graph, replace layer then can be applied.
I am attaching the code for feature extraction example from amtlab with modified input size
unzip('MerchData.zip');
imds = imageDatastore('MerchData','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsTest] = 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 = resnet18;
analyzeNetwork(net)
inputSize = net.Layers(1).InputSize;
% create layer that is going to replace input layer
% the size of your image must be bigger or equal to the input size.
layer=imageInputLayer([28 28 3],'Name','input','Normalization','zscore');
% move data for normalization and mean
layer.Mean=lgraph.Layers(1,1).Mean;
layer.StandardDeviation=lgraph.Layers(1, 1).StandardDeviation;
%% Move network to layer graph
lgraph = layerGraph(net);
newlgraph = replaceLayer(lgraph,'data',layer);
% Assemble DNN to work again with it
net = assembleNetwork(newlgraph);
analyzeNetwork(net)
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest);
layer = 'pool5';
featuresTrain = activations(net,augimdsTrain,layer,'OutputAs','channels');
featuresTrain = squeeze(featuresTrain);
featuresTrain=featuresTrain';
featuresTest = activations(net,augimdsTest,layer,'OutputAs','channels');
featuresTest = squeeze(featuresTest);
featuresTest=featuresTest';
whos featuresTrain
analyzeNetwork(net)
YTrain = imdsTrain.Labels;
YTest = imdsTest.Labels;
classifier = fitcecoc(featuresTrain,YTrain);
YPred = predict(classifier,featuresTest);
idx = [1 5 10 15];
figure
for i = 1:numel(idx)
subplot(2,2,i)
I = readimage(imdsTest,idx(i));
label = YPred(idx(i));
imshow(I)
title(char(label))
end
accuracy = mean(YPred == YTest)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by