How can I add new images to a trained deep learning network to classify new images ?

2 views (last 30 days)
Hi,
I used the following code to train a network for image classification. I want to know how can I keep training the new network with the already added images ?
imds = imageDatastore('Images','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
net = resnet18;
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(net);
newFCLayer = fullyConnectedLayer(numClasses,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'fc1000',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',newClassLayer);
inputSize = net.Layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',8, ...
'InitialLearnRate',0.0001, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',8, ...
'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(augimdsTrain,lgraph,options);
YPred = classify(trainedNet,augimdsValidation);
accuracy = mean(YPred == imdsValidation.Labels)

Answers (1)

Chetan Gupta
Chetan Gupta on 13 Jul 2021
Hi Thushyanthan,
I understand that you intend to train the neural network with imdsTrain for a larger number of iterations. You can do that by increasing the ‘MaxEpochs’ value in trainingOptions to some value larger than 8.
You can refer to Options for training deep learning neural network - MATLAB trainingOptions (mathworks.com) for more information about Epochs and other training options.
  1 Comment
Thushyanthan KANESALINGAM
Thushyanthan KANESALINGAM on 13 Jul 2021
I think I didn't express correctly what I meant.
I already ran this code and I have the output trainedNet with an accuracy of 86%. Now I want to train trainedNet with new images without using the previous ones. Can I use the following code ?
imds1 = imageDatastore('newImages','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain1,imdsValidation1] = splitEachLabel(imds1,0.7,'randomized');
load('tNet.mat','trainedNet', 'options','inputSize')%tNet.mat is a .mat file with all the outputs from the code below
augimdsTrain1 = augmentedImageDatastore(inputSize(1:2),imdsTrain1);
augimdsValidation1 = augmentedImageDatastore(inputSize(1:2),imdsValidation1);
net2= trainNetwork(augimdsTrain1,layerGraph(trainedNet),options);

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!