MATLAB Answers

How to resize test images for neural network deep learning?

94 ビュー (過去 30 日間)
Joon Yong Chu
Joon Yong Chu 2019 年 9 月 5 日
コメント済み: niaz ahmad 2020 年 2 月 18 日
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), ...
%% 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]);
%% 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), ...
%% Testing
labels = classify(net,imds_test);
ii = randi(3);
im = imread(imds_test.Files{ii});
if labels(ii) == imds_test.Labels(ii)
colorText = 'g';
colorText = 'r';

  0 件のコメント


回答 (2 件)

Neuropragmatist 2019 年 9 月 6 日
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?

  1 件のコメント

Joon Yong Chu
Joon Yong Chu 2019 年 9 月 7 日
Yes i substitued it with my own images and i need it to be 32x32. Hence I would like to know how i can trasform any input image of any size to a 32x32x1 image for the program.


Jefferson Pinzon Morantes
Jefferson Pinzon Morantes 2019 年 11 月 22 日
I also have the same error, could you help me with that?

  1 件のコメント

niaz ahmad
niaz ahmad 2020 年 2 月 18 日
i have same error


Community Treasure Hunt

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

Start Hunting!

Translated by