How to resize test images for neural network deep learning?
6 views (last 30 days)
Show older comments
Hi I am currently experiencing a problem with reading picture recognition with deep learning.
I am consistently getting the following error despite trying to resize the image with a few attempts.
>> labels = classify(net,imds_test);
Error using DAGNetwork/predict>predictBatch (line 232)
Incorrect input size. The input images must have a size of [32 32 1].
Error in DAGNetwork/predict (line 118)
Y = predictBatch( ...
Error in DAGNetwork/classify (line 115)
scores = this.predict( X, varargin{:} );
Error in SeriesNetwork/classify (line 458)
[labels, scores] = this.UnderlyingDAGNetwork.classify(X, varargin{:});
By following the tutorial closely from matlab, I have been able to get the neural network up and training with my own pictures of line (which i generated from a series of matrix manipulation from matlab) in the form of [32 32 1] however those are just training data.
How can I convert or make the images that are not from the training data set (say I downloaded a line picture off google) for the testing phase be of the same variable at [32 32 1].
I tried the repmat function which caused the image to be enlarged to 1024x1024, thereafter trying the imresize, my line is totally gone.
the pictures i am training looks like this : and its in [32 32 1] format
and those picture downloaded off google will be larger in pixels and I cant resize them using the imresize function because it doesnt shift the properties to a [32 32 1] array. I have included the code for the neural net below too which, is mostly the same as per the matlab tutorial.
Please help, thanks in advance.
categories = {'Horizontal','Slanted','Vertical'};
rootFolder = 'linetrain';
imds = imageDatastore(fullfile(rootFolder, categories), ...
'LabelSource','foldernames');
%% Layer Definitions
varSize = 32;
conv1 = convolution2dLayer(5,varSize,'Padding',2,'BiasLearnRateFactor',2);
conv1.Weights = gpuArray(single(randn([5 5 1 varSize])*0.0001));
fc1 = fullyConnectedLayer(64,'BiasLearnRateFactor',2);
fc1.Weights = gpuArray(single(randn([64 576])*0.1));
fc2 = fullyConnectedLayer(3,'BiasLearnRateFactor',2);
fc2.Weights = gpuArray(single(randn([3 64])*0.1));
layers = [
imageInputLayer([varSize varSize 1]);
conv1;
maxPooling2dLayer(3,'Stride',2);
reluLayer();
convolution2dLayer(5,32,'Padding',2,'BiasLearnRateFactor',2);
reluLayer();
averagePooling2dLayer(3,'Stride',2);
convolution2dLayer(5,64,'Padding',2,'BiasLearnRateFactor',2);
reluLayer();
averagePooling2dLayer(3,'Stride',2);
fc1;
reluLayer();
fc2;
softmaxLayer()
classificationLayer()];
%% Training parameters
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.001, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 8, ...
'L2Regularization', 0.004, ...
'MaxEpochs', 10, ...
'MiniBatchSize', 100, ...
'Verbose', true);
%% Training
[net, info] = trainNetwork(imds, layers, options);
rootFolder = 'linetest';
imds_test = imageDatastore(fullfile(rootFolder,categories), ...
'LabelSource','foldernames');
%% Testing
labels = classify(net,imds_test);
ii = randi(3);
im = imread(imds_test.Files{ii});
imshow(im);
if labels(ii) == imds_test.Labels(ii)
colorText = 'g';
else
colorText = 'r';
end
title(char(labels(ii)),'Color',colorText)
0 Comments
Answers (3)
Neuropragmatist
on 6 Sep 2019
Did you just substitute your own input image for the Matlab tutorial one? Because it looks like the whole code is built around 32x32 images - i.e. in the 'layer definitions' section.
If you change these to match the size of your real images does it still not work?
M.
Jefferson Pinzon Morantes
on 22 Nov 2019
I also have the same error, could you help me with that?
2 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!