Error using trainNetwork (line 183) Invalid network. Caused by: Layer 'fc7': Input size mismatch. Size of input to this layer is different from the expected input size

1 次查看(过去 30 天)
clc
clear all; close all;
outputFolder=fullfile('recycle101');
rootFolder=fullfile(outputFolder,'recycle');
categories={'Aluminium Can','PET Bottles','Drink Carton Box'};
imds=imageDatastore(fullfile(rootFolder,categories),'LabelSource','foldernames');
tbl=countEachLabel(imds)
minSetCount=min(tbl{:,2});
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
countEachLabel(imds);
%randomly choose file for aluminium can, PET bottles, and drink carton box
AluminiumCan=find(imds.Labels=='Aluminium Can',1);
PETBottles=find(imds.Labels=='PET Bottles',1);
DrinkCartonBox=find(imds.Labels=='Drink Carton Box',1);
%plot image that was pick randomly
figure
subplot(2,2,1);
imshow(readimage(imds,AluminiumCan));
subplot(2,2,2);
imshow(readimage(imds,PETBottles));
subplot(2,2,3);
imshow(readimage(imds,DrinkCartonBox));
%Load pre-trained network
net = alexnet;
%analyzeNetwork(net)
%net =alexnet('Weights','none');
%lys = net.Layers;
%lys(end-3:end)
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(net.Layers);
%Replace the classification layers for new task
newFCLayer = fullyConnectedLayer(3,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'fc6',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'output',newClassLayer);
%Resize the image
imageSize=net.Layers(1).InputSize;
augmentedTrainingSet=augmentedImageDatastore(imageSize,...
imdsTrain,'ColorPreprocessing','gray2rgb');
augmentedTestSet=augmentedImageDatastore(imageSize,...
imdsValidation,'ColorPreprocessing','gray2rgb');
options = trainingOptions('sgdm', ...
'MiniBatchSize',4, ...
'MaxEpochs',8, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augmentedTestSet, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'ExecutionEnvironment','cpu', ...
'Plots','training-progress');
trainedNet = trainNetwork(augmentedTrainingSet,lgraph,options);

采纳的回答

yanqi liu
yanqi liu 2021-12-23
modify
lgraph = replaceLayer(lgraph,'fc6',newFCLayer);
to
lgraph = replaceLayer(lgraph,'fc8',newFCLayer);
net = alexnet;
net.Layers
ans =
25×1 Layer array with layers: 1 'data' Image Input 227×227×3 images with 'zerocenter' normalization 2 'conv1' Convolution 96 11×11×3 convolutions with stride [4 4] and padding [0 0 0 0] 3 'relu1' ReLU ReLU 4 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element 5 'pool1' Max Pooling 3×3 max pooling with stride [2 2] and padding [0 0 0 0] 6 'conv2' Grouped Convolution 2 groups of 128 5×5×48 convolutions with stride [1 1] and padding [2 2 2 2] 7 'relu2' ReLU ReLU 8 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element 9 'pool2' Max Pooling 3×3 max pooling with stride [2 2] and padding [0 0 0 0] 10 'conv3' Convolution 384 3×3×256 convolutions with stride [1 1] and padding [1 1 1 1] 11 'relu3' ReLU ReLU 12 'conv4' Grouped Convolution 2 groups of 192 3×3×192 convolutions with stride [1 1] and padding [1 1 1 1] 13 'relu4' ReLU ReLU 14 'conv5' Grouped Convolution 2 groups of 128 3×3×192 convolutions with stride [1 1] and padding [1 1 1 1] 15 'relu5' ReLU ReLU 16 'pool5' Max Pooling 3×3 max pooling with stride [2 2] and padding [0 0 0 0] 17 'fc6' Fully Connected 4096 fully connected layer 18 'relu6' ReLU ReLU 19 'drop6' Dropout 50% dropout 20 'fc7' Fully Connected 4096 fully connected layer 21 'relu7' ReLU ReLU 22 'drop7' Dropout 50% dropout 23 'fc8' Fully Connected 1000 fully connected layer 24 'prob' Softmax softmax 25 'output' Classification Output crossentropyex with 'tench' and 999 other classes

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by