Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-3. How to solve this?

1 回表示 (過去 30 日間)
clc;
clear all;
outputFolder=fullfile('recycle101');
rootFolder=fullfile(outputFolder,'recycle');
categories={'can','plastic','drinkcartonbox'};
imds=imageDatastore(fullfile(rootFolder,categories),'LabelSource','foldernames');
tbl=countEachLabel(imds)
minSetCount=min(tbl{:,2});
imds=splitEachLabel(imds,minSetCount,'randomize');
countEachLabel(imds);
can=find(imds.Labels=='can',1); %randomly choose file for can
plastic=find(imds.Labels=='plastic',1);
drinkcartonbox=find(imds.Labels=='drinkcartonbox',1);
figure %plot iamge that was pick randomly
subplot(2,2,1);
imshow(readimage(imds,can));
subplot(2,2,2);
imshow(readimage(imds,plastic));
subplot(2,2,3);
imshow(readimage(imds,drinkcartonbox));
net=resnet50();
figure
plot(net)
title('Architecture of ResNet-50')
set(gca,'YLim',[150 170]);
net.Layers(1);
net.Layers(end)
numel(net.Layers(end).ClassNames)
[trainingSet,testSet]=splitEachLabel(imds,0.3,'randomize');
imageSize=net.Layers(1).InputSize;
augmentedTrainingSet=augmentedImageDatastore(imageSize,...
trainingSet,'ColorPreprocessing','gray2rgb');
augmentedTestSet=augmentedImageDatastore(imageSize,...
testSet,'ColorPreprocessing','gray2rgb');
w1=net.Layers(2).Weights;
w1=mat2gray(w1);
figure
montage(w1)
title('First Concolutional Layer Weight')
featureLayer='fc1000';
trainingFeatures=activations(net,...
augmentedTrainingSet,featureLayer,'MiniBatchSize',32,'OutputAs','columns');
trainingLabels=trainingSet.Labels;
classifier=fitcecoc(trainingFeatures,trainingLabels,...
'Learner','Linear','Coding','onevsall','ObservationsIn','columns');
testFeatures=activations(net,...
augmentedTestSet,featureLayer,'MiniBatchSize',32,'OutputAs','columns');
predictLabels=predict(classifier,testFeatures,'ObservationsIn','columns');
testLabels=testSet.Labels;
confMat=confusionmat(testLabels,predictLabels);
confMat=bsxfun(@rdivide,confMat,sum(confMat,2));
z=mean(diag(confMat));
Accuracy=sum( diag (confMat)) / sum (confMat(:)) *100
[XTrain,YTrain] = digitTrain4DArrayData;
idx = randperm(size(XTrain,4),403);
XValidation = XTrain(:,:,:,idx);
XTrain(:,:,:,idx) = [];
YValidation = YTrain(idx);
YTrain(idx) = [];
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',8, ...
'ValidationData',{XValidation,YValidation}, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
NET = trainNetwork(XTrain,YTrain,layers,options);
load fisheriris
species = categorical(species);
G = categories(species) % Class names
numClasses = size(G,1);
H = countcats(species) % Number of observations in each class
rng('default') % For reproducibility
cv = cvpartition(species,'KFold',5,'Stratify',false)
numFolds = cv.NumTestSets;
nTestData = zeros(numFolds,numClasses);
for i = 1:numFolds
testClasses = species(cv.test(i));
nCounts = countcats(testClasses); % Number of test set observations in each class
nTestData(i,:) = nCounts';
end
bar(nTestData)
xlabel('Test Set (Fold)')
ylabel('Number of Observations')
title('Nonstratified Partition')
legend(g)
  6 件のコメント
Image Analyst
Image Analyst 2021 年 10 月 15 日
That's not all the red text. I know because the red text would give a line number and the actual line of code. Again, read the link I gave you and then paste ALL THE RED TEXT back here.
The code you gave two comments ago runs fine up until the
legend(g);
line because you didn't define g. There is no error about trying to put a 3 element vector into a single element.
Dave B
Dave B 2021 年 10 月 15 日
@TAN HOR YAN - are you sure you got the relevant code snippet? This bit runs okay (I updated the g to G because, as noted, there's no g defined).
load fisheriris
species = categorical(species);
G = categories(species) % Class names
G = 3×1 cell array
{'setosa' } {'versicolor'} {'virginica' }
numClasses = size(G,1);
H = countcats(species) % Number of observations in each class
H = 3×1
50 50 50
rng('default') % For reproducibility
cv = cvpartition(species,'KFold',5,'Stratify',false)
cv =
K-fold cross validation partition NumObservations: 150 NumTestSets: 5 TrainSize: 120 120 120 120 120 TestSize: 30 30 30 30 30
numFolds = cv.NumTestSets;
nTestData = zeros(numFolds,numClasses);
for i = 1:numFolds
testClasses = species(cv.test(i));
nCounts = countcats(testClasses); % Number of test set observations in each class
nTestData(i,:) = nCounts';
end
bar(nTestData)
xlabel('Test Set (Fold)')
ylabel('Number of Observations')
title('Nonstratified Partition')
legend(G)

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSequence and Numeric Feature Data Workflows についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by