How to modify my MATLAB code to train a neural network with images from different directions?
I am working on a project to estimate the weight of pears using their RGB and Depth images in MATLAB. Initially, I used only front-facing images for training, but now I want to improve the accuracy by including images taken from approximately 90 degrees to the left or right of the pear, in addition to the Depth images.
I tried modifying the code to accommodate these new images, but I’m facing some issues.
I would like to change the following points.
・The pear image uses the front, an image moved approximately 90 degrees to the left or right from the front, and a Depth image.
・I want to put all my data in a datastore.
Currently, the code listed is the code that I have modified, but in this state an error occurs and it is impossible to predict.Please lend me your strength.
cd ‘RGB front file path’
folder_name = pwd;
XImageTrainFront = imageDatastore(folder_name);
cd ‘RGB side file path’
folder_name = pwd;
XImageTrainSide = imageDatastore(folder_name);
cd ‘Depth file path’
folder_name = pwd;
XDispTrain = imageDatastore(folder_name);
YTrain = [ data ]; % weight labels
YTrain = arrayDatastore(YTrain);
% Combine datasets
ds = combine(XImageTrainFront, XImageTrainSide, XDispTrain, YTrain);
dsrand = shuffle(ds);
dsVali = partition(dsrand, 10, 1);
dsTest = partition(dsrand, 10, 2);
ds1=partition(dsrand,10,3);
ds2=partition(dsrand,10,4);
ds3=partition(dsrand,10,5);
ds4=partition(dsrand,10,6);
ds5=partition(dsrand,10,7);
ds6=partition(dsrand,10,8);
ds7=partition(dsrand,10,9);
ds8=partition(dsrand,10,10);
dsTrain=combine(ds1,ds2,ds3,ds4,ds5,ds6,ds7,ds8,ReadOrder="sequential");
YTest = readall(dsTest);
YTest = YTest(:,4);
YTest = cell2mat(YTest);
cd ‘path to googlenet300400_multiple.mat’
load googlenet300400_multiple.mat
miniBatchSize = 16;
options = trainingOptions(‘sgdm’, …
‘MiniBatchSize’, miniBatchSize, …
‘MaxEpochs’, 300, …
‘InitialLearnRate’, 1e-7, …
‘LearnRateSchedule’, ‘piecewise’, …
‘LearnRateDropFactor’, 0.1, …
‘LearnRateDropPeriod’, 30, …
‘Shuffle’, ‘every-epoch’, …
‘ValidationData’, dsVali, …
‘ValidationFrequency’, 50, …
‘Plots’, ‘training-progress’, …
‘Verbose’, true);
net = trainNetwork(dsTrain, lgraph_2, options);
YPredicted = predict(net, dsTest);
predictionError = YTest – YPredicted;
squares = predictionError.^2;
rmse = sqrt(mean(squares));
figure
scatter(YPredicted, YTest, ‘+’);
xlabel("Predicted Value");
ylabel("True Value");
hold on;
plot([100 550], [100 550], ‘r–‘);
Error message that occurred during modification.
Please respond to the following error. Error using: trainNetwork (line 191)
Invalid training data. The output size of the last layer ([1 1 1]) is the response size ([1 1 360000])
does not match.
Error: newcord2 (line 93)
net = trainNetwork(dsTrain, lgraph_2, options);
Error using: nnet.cnn.LayerGraph/replaceLayer (line 300)
Layer ‘finalLayerName’ does not exist.
Error: newcord2 (line 85)
lgraph_2 = replaceLayer(lgraph_2, ‘finalLayerName’, finalLayer);I am working on a project to estimate the weight of pears using their RGB and Depth images in MATLAB. Initially, I used only front-facing images for training, but now I want to improve the accuracy by including images taken from approximately 90 degrees to the left or right of the pear, in addition to the Depth images.
I tried modifying the code to accommodate these new images, but I’m facing some issues.
I would like to change the following points.
・The pear image uses the front, an image moved approximately 90 degrees to the left or right from the front, and a Depth image.
・I want to put all my data in a datastore.
Currently, the code listed is the code that I have modified, but in this state an error occurs and it is impossible to predict.Please lend me your strength.
cd ‘RGB front file path’
folder_name = pwd;
XImageTrainFront = imageDatastore(folder_name);
cd ‘RGB side file path’
folder_name = pwd;
XImageTrainSide = imageDatastore(folder_name);
cd ‘Depth file path’
folder_name = pwd;
XDispTrain = imageDatastore(folder_name);
YTrain = [ data ]; % weight labels
YTrain = arrayDatastore(YTrain);
% Combine datasets
ds = combine(XImageTrainFront, XImageTrainSide, XDispTrain, YTrain);
dsrand = shuffle(ds);
dsVali = partition(dsrand, 10, 1);
dsTest = partition(dsrand, 10, 2);
ds1=partition(dsrand,10,3);
ds2=partition(dsrand,10,4);
ds3=partition(dsrand,10,5);
ds4=partition(dsrand,10,6);
ds5=partition(dsrand,10,7);
ds6=partition(dsrand,10,8);
ds7=partition(dsrand,10,9);
ds8=partition(dsrand,10,10);
dsTrain=combine(ds1,ds2,ds3,ds4,ds5,ds6,ds7,ds8,ReadOrder="sequential");
YTest = readall(dsTest);
YTest = YTest(:,4);
YTest = cell2mat(YTest);
cd ‘path to googlenet300400_multiple.mat’
load googlenet300400_multiple.mat
miniBatchSize = 16;
options = trainingOptions(‘sgdm’, …
‘MiniBatchSize’, miniBatchSize, …
‘MaxEpochs’, 300, …
‘InitialLearnRate’, 1e-7, …
‘LearnRateSchedule’, ‘piecewise’, …
‘LearnRateDropFactor’, 0.1, …
‘LearnRateDropPeriod’, 30, …
‘Shuffle’, ‘every-epoch’, …
‘ValidationData’, dsVali, …
‘ValidationFrequency’, 50, …
‘Plots’, ‘training-progress’, …
‘Verbose’, true);
net = trainNetwork(dsTrain, lgraph_2, options);
YPredicted = predict(net, dsTest);
predictionError = YTest – YPredicted;
squares = predictionError.^2;
rmse = sqrt(mean(squares));
figure
scatter(YPredicted, YTest, ‘+’);
xlabel("Predicted Value");
ylabel("True Value");
hold on;
plot([100 550], [100 550], ‘r–‘);
Error message that occurred during modification.
Please respond to the following error. Error using: trainNetwork (line 191)
Invalid training data. The output size of the last layer ([1 1 1]) is the response size ([1 1 360000])
does not match.
Error: newcord2 (line 93)
net = trainNetwork(dsTrain, lgraph_2, options);
Error using: nnet.cnn.LayerGraph/replaceLayer (line 300)
Layer ‘finalLayerName’ does not exist.
Error: newcord2 (line 85)
lgraph_2 = replaceLayer(lgraph_2, ‘finalLayerName’, finalLayer); I am working on a project to estimate the weight of pears using their RGB and Depth images in MATLAB. Initially, I used only front-facing images for training, but now I want to improve the accuracy by including images taken from approximately 90 degrees to the left or right of the pear, in addition to the Depth images.
I tried modifying the code to accommodate these new images, but I’m facing some issues.
I would like to change the following points.
・The pear image uses the front, an image moved approximately 90 degrees to the left or right from the front, and a Depth image.
・I want to put all my data in a datastore.
Currently, the code listed is the code that I have modified, but in this state an error occurs and it is impossible to predict.Please lend me your strength.
cd ‘RGB front file path’
folder_name = pwd;
XImageTrainFront = imageDatastore(folder_name);
cd ‘RGB side file path’
folder_name = pwd;
XImageTrainSide = imageDatastore(folder_name);
cd ‘Depth file path’
folder_name = pwd;
XDispTrain = imageDatastore(folder_name);
YTrain = [ data ]; % weight labels
YTrain = arrayDatastore(YTrain);
% Combine datasets
ds = combine(XImageTrainFront, XImageTrainSide, XDispTrain, YTrain);
dsrand = shuffle(ds);
dsVali = partition(dsrand, 10, 1);
dsTest = partition(dsrand, 10, 2);
ds1=partition(dsrand,10,3);
ds2=partition(dsrand,10,4);
ds3=partition(dsrand,10,5);
ds4=partition(dsrand,10,6);
ds5=partition(dsrand,10,7);
ds6=partition(dsrand,10,8);
ds7=partition(dsrand,10,9);
ds8=partition(dsrand,10,10);
dsTrain=combine(ds1,ds2,ds3,ds4,ds5,ds6,ds7,ds8,ReadOrder="sequential");
YTest = readall(dsTest);
YTest = YTest(:,4);
YTest = cell2mat(YTest);
cd ‘path to googlenet300400_multiple.mat’
load googlenet300400_multiple.mat
miniBatchSize = 16;
options = trainingOptions(‘sgdm’, …
‘MiniBatchSize’, miniBatchSize, …
‘MaxEpochs’, 300, …
‘InitialLearnRate’, 1e-7, …
‘LearnRateSchedule’, ‘piecewise’, …
‘LearnRateDropFactor’, 0.1, …
‘LearnRateDropPeriod’, 30, …
‘Shuffle’, ‘every-epoch’, …
‘ValidationData’, dsVali, …
‘ValidationFrequency’, 50, …
‘Plots’, ‘training-progress’, …
‘Verbose’, true);
net = trainNetwork(dsTrain, lgraph_2, options);
YPredicted = predict(net, dsTest);
predictionError = YTest – YPredicted;
squares = predictionError.^2;
rmse = sqrt(mean(squares));
figure
scatter(YPredicted, YTest, ‘+’);
xlabel("Predicted Value");
ylabel("True Value");
hold on;
plot([100 550], [100 550], ‘r–‘);
Error message that occurred during modification.
Please respond to the following error. Error using: trainNetwork (line 191)
Invalid training data. The output size of the last layer ([1 1 1]) is the response size ([1 1 360000])
does not match.
Error: newcord2 (line 93)
net = trainNetwork(dsTrain, lgraph_2, options);
Error using: nnet.cnn.LayerGraph/replaceLayer (line 300)
Layer ‘finalLayerName’ does not exist.
Error: newcord2 (line 85)
lgraph_2 = replaceLayer(lgraph_2, ‘finalLayerName’, finalLayer); prediction, multi-input network, 予測, 多入力ネットワーク MATLAB Answers — New Questions