필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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)

답변 (1개)

Chetan Gupta
Chetan Gupta 2021년 7월 13일
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
Thushyanthan KANESALINGAM
Thushyanthan KANESALINGAM 2021년 7월 13일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by