Error using reshape To RESHAPE the number of elements must not change in Matlab2016b

hi I wrote these codes for classify Drone Images into 2 Objects:
imds=imageDatastore('C:\users\hossein\my documents\matlab\droneimagesfinal','fileextensions','.jpg');
target=[2;2;1;1;2;2;2;2;1;1;2;2;2;2;1;1;1;2;2;2;2;1;1;1;1;1;1;2;2;2;1;1;1;1;1;1;1;1;1;1;1;1;2;2;2;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1;1;1;1;1;2;1;1;1;1;1;1;
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;2;1;1;1;1;1;1;1;1;2;2;2;1;1;1;2;2;1;1;2;2;2;1;2;2;2;1;1;1;1;1;1;1;1;1;2;2;2;2;2;2;2;2;1;1;1;1;1;1;1;
1;1;1;1;1;1;1;1;2;2;2;2;2;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;2;2;1;1;1;1;1;1;1;1;1;1;1;
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;2;2];
imds.Labels=target;
[testset,trainingset]= splitEachLabel(imds,0.3,'randomize');
tr_data=zeros(227,227,3,size(trainingset.Labels,1));
te_data=zeros(227,227,3,size(testset.Labels,1));
net=alexnet;
layer='fc7';
Te_F=[];
for i=1:size(testset.Labels,1)
img= readimage(testset,i);
I=zeros(227,227,3);
img=imresize(img,[227 227]);
I(:,:,1:3)=cat(3,img,img,img);
F=activations(net,I,layer);
F=reshape(F(1,1,:),1,4096);
Te_F=[Te_F,F];
end
Tr_F=[];
for i=1:size(trainingset.Labels,1)
img= readimage(trainingset,i);
I=zeros(227,227,3);
img=imresize(img,[227 227]);
I(:,:,1:3)=cat(3,img,img,img);
F=activations(net,I,layer);
F=reshape(F(1,1,:),1,4096);
Tr_F=[Tr_F,F];
end
but after run codes Matlab2016b says this Error: " Error using reshape
To RESHAPE the number of elements must not change."
Error in dronetest1 (line 24)
F=reshape(F(1,:,[]),1,4096);
please help me to fix this error.

4 Comments

hi thanks a lot that error fixed but My codes were incomplete , I add these code lines:
trainingLabels= trainingset.Labels;
testLabels= testset.Labels;
classifier = fitcecoc(Tr_F,trainingLabels);
predictedLabels= predict(classifier,Te_F);
accuracy= Sum(predictedLabels==testLabels)/numel(predictedLabels);
above codes are for calculate of Model Accuracy and Matlab Shows Following Errors:
Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 201)
X and Y do not have the same number of observations.
Error in classreg.learning.classif.FullClassificationModel.prepareData (line 487)
classreg.learning.FullClassificationRegressionModel.prepareDataCR(...
Error in classreg.learning.FitTemplate/fit (line 213)
this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in ClassificationECOC.fit (line 116)
this = fit(temp,X,Y);
Error in fitcecoc (line 383)
obj = ClassificationECOC.fit(X,Y,ecocArgs{:});
Error in dronetest1 (line 41)
classifier = fitcecoc(Tr_F,trainingLabels);
Thanks Please help me Again
Also In WorkSpace Section Of Matlab, Output Variables calculated incorrectly
I Attach WorkSpace Section Image for My codes in this Comment.
Please See WorkSpace Section Image Dronetest2.jpg
for example In WorkSpace Section in Above Image Variable F must be Vector 1*4096 single
that in Above Image F calculated incorrectly equal to -0.4052
also Tr_F variable Must be 191*4096 single (Training data are 191 Number)
Te_F Variable Must be 82*4096 single (Test data are 82 Number)
I guess All of My codes Must modify
and Please See Errors For Last Lines Of codes for Accuracy Of model.

Answers (0)

This question is closed.

Products

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!