Clear Filters
Clear Filters

Training regression network problem

2 views (last 30 days)
Goran
Goran on 22 Apr 2024
Answered: Paras Gupta on 2 May 2024
Hello guys. I want to train regression network to predict certain values from images. I have images sorted in 6 different folders (folder name is the number which the images belong to). Now I want to train my GoogLeNet network, that I modifided to work for regression and saved it to workspace as lgraph_1. Here is my code:
pathToImages = "iskre_Reg";
% Load images and labels
iskre_slike = imageDatastore(pathToImages, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% Convert folder names (labels) to numeric values if they are not already
labels = string(iskre_slike.Labels); % Convert labels to string if they are categorical
numeric_labels = str2double(labels); % Convert to double
% Check for NaNs and handle them if any
if any(isnan(numeric_labels))
error('Labels contain NaNs. Please ensure all folder names are valid numeric values.');
end
% Update Labels in imageDatastore
iskre_slike.Labels = numeric_labels;
% Splitting the datastore
[trainImgs,testImgs] = splitEachLabel(iskre_slike,0.75);
% Resizing images
resizeTrainImgs = augmentedImageDatastore([224 224], trainImgs, 'OutputSizeMode', 'resize');
resizeTestImgs = augmentedImageDatastore([224 224], testImgs, 'OutputSizeMode', 'resize');
disp(class(trainImgs.Labels)); % Should display 'double' or 'single'
disp(size(trainImgs.Labels)); % Should display [n 1] where n is number of training images
% Define network and options
neural_net = lgraph_1;
opts = trainingOptions("sgdm", ...
'InitialLearnRate', 0.001, ...
'MaxEpochs', 20, ...
'VerboseFrequency', 2, ...
'Shuffle', 'every-epoch', ...
'MiniBatchSize', 32, ...
'Plots', 'training-progress');
% Train the network
[iskrenet,info] = trainNetwork(resizeTrainImgs,neural_net,opts);
iskre_preds = predict(iskrenet,resizeTestImgs);
iskre_actual = testImgs.Labels;
error = rmse(iskre_preds,iskre_actual)
After running the code I get the following error, and I do not know what to do:
Error using trainNetwork
Incorrectly defined MiniBatchable Datastore. Error in partitionByIndex method of
E:\Program
Files\MATLAB\R2023b\toolbox\nnet\cnn\+nnet\+internal\+cnn\+datastore\ImageDatastoreMiniBatchDatastore.m
at line 68: Undefined function 'categories' for input arguments of type 'double'.
Error in program_regresija (line 39)
[iskrenet,info] = trainNetwork(resizeTrainImgs,neural_net,opts);

Answers (1)

Paras Gupta
Paras Gupta on 2 May 2024
Hi Goran,
The error in the question occurs when the 'augmentedImageDatastore' function uses an underlying 'imageDatastore' with numeric labels. It is to be noted that the documentation of 'augmentedImageDatastore' shows the use of the below syntax containing 'imageDatastore' only for classification problems:
auimds = augmentedImageDatastore(outputSize,imds)
To use 'augmentedImageDatastore' for regression problems, you can use a different syntax that supports regression. For example, the below syntax uses an input table 'tbl':
auimds = augmentedImageDatastore(outputSize,tbl)
The table 'tbl' contains predictors and responses such that the first column of the table is paths to images, and the second column is scalar regression targets.
Please refer to the following documentation on 'augmentedImageDatastore' for more information on the above syntax:
Hope this helps you with your work.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!