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

4 visualizzazioni (ultimi 30 giorni)
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?

Risposte (1)

Wilmer Ariza
Wilmer Ariza il 7 Ago 2020
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)

Categorie

Scopri di più su Image Data Workflows in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by