MATLAB Answers

0

How do I use a trained network to make new predictions?

Asked by Kanushka Gajjar on 10 Jul 2019
Latest activity Answered by Divya Gaddipati on 18 Jul 2019
Hi,
I am relatively new to MATLAB and desperately need some assistance!
I have trained and saved the following model:
%Load training data
dataSetDir = fullfile(toolboxdir('vision'),'visiondata','GravelRoad');
imageDir = fullfile(dataSetDir,'trainingImages');
labelDir = fullfile(dataSetDir,'trainingLabels');
%Creating an image datastore
imds = imageDatastore(imageDir);
%Creating a pixelLabelDatastore for the ground truth pixel labels
classNames = ["Pothole","Background"];
labelIDs = [255 0];
pxds = pixelLabelDatastore(labelDir,classNames,labelIDs);
%Visualizing training images and ground truth pixel labels.
I = read(imds);
C = read(pxds);
I = imresize(I,5);
L = imresize(uint8(C),5);
imshowpair(I,L,'montage');
%Creating semantic segmentation network
numFilters = 64;
filterSize = 3;
numClasses = 2;
layers = [
imageInputLayer([400 640 3])
convolution2dLayer(filterSize,numFilters,'Padding',1)
reluLayer()
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(filterSize,numFilters,'Padding',1)
reluLayer()
transposedConv2dLayer(4,numFilters,'Stride',2,'Cropping',1);
convolution2dLayer(1,numClasses);
softmaxLayer()
pixelClassificationLayer()
];
%Setting up training options
opts = trainingOptions('sgdm', ...
'CheckpointPath','D:\CNNcheckPoint',...
'InitialLearnRate',1e-3, ...
'MaxEpochs',100, ...
'MiniBatchSize',2,...
'Plots','training-progress'); %decrease to 5 , crop image and not resize, use approx 100 images, augment data
%Create a pixel label image datastore that contains training data.
trainingData = pixelLabelImageDatastore(imds,pxds);
%Training the network
KanushkaNet = net;
net = trainNetwork(trainingData,layers,opts);
%Reading and displaying test image
testImage = imread('D:\toolbox\vision\visiondata\testingGravel.jpg');
imshow(testImage)
%Segmenting test image and displaying results
C = semanticseg(testImage,net);
B = labeloverlay(testImage,C);
imshow(B)
%Improving the results
tbl = countEachLabel(trainingData);
totalNumberOfPixels = sum(tbl.PixelCount);
frequency = tbl.PixelCount / totalNumberOfPixels;
classWeights = 1./frequency;
layers(end) = pixelClassificationLayer('Classes',tbl.Name,'ClassWeights',classWeights);
net = trainNetwork(trainingData,layers,opts);
C = semanticseg(testImage,net);
B = labeloverlay(testImage,C);
imshow(B)
save KanushkaNet;
I would now like to use the saved model to load the weights and perform a prediction.
(i.e. remove the training part of the code and replace with some sort of prediction)
Any assistance would be greatly appreciated!!
Thanking you in advance!
Kanushka

  0 Comments

Sign in to comment.

1 Answer

Answer by Divya Gaddipati on 18 Jul 2019

save KanushkaNet;
The above command which you used will save all your current variables from the workspace. If you want to save only your trained network (the variable “net” in your case), you can use:
save file_name net;
In this case, the network will be saved under the specified filename (file_name).
The next time you want to use the saved pre-trained network, you can just call the load function as:
load file_name;
To test the network on any new data, you can use the predict parameter as:
predictions = net.predict(test_data);
You can refer to the following links on how to train and predict models:

  0 Comments

Sign in to comment.