MATLAB Answers

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

8 ビュー (過去 30 日間)
Kanushka Gajjar
Kanushka Gajjar 2019 年 7 月 10 日
回答済み: Divya Gaddipati 2019 年 7 月 18 日
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);
%Creating semantic segmentation network
numFilters = 64;
filterSize = 3;
numClasses = 2;
layers = [
imageInputLayer([400 640 3])
%Setting up training options
opts = trainingOptions('sgdm', ...
'InitialLearnRate',1e-3, ...
'MaxEpochs',100, ...
'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');
%Segmenting test image and displaying results
C = semanticseg(testImage,net);
B = labeloverlay(testImage,C);
%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);
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!

  0 件のコメント


回答 (1 件)

Divya Gaddipati
Divya Gaddipati 2019 年 7 月 18 日
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 件のコメント



Community Treasure Hunt

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

Start Hunting!

Translated by