resize images for cnn

조회 수: 18 (최근 30일)
Hamid Ebrahimi
Hamid Ebrahimi 2019년 12월 26일
댓글: Hamid Ebrahimi 2019년 12월 26일
Hi, I use below code to run cnn for images but my size of images are 875*656*3 and I want to imput images for cnn in size 64*64*1 how can resize images ?
clear all
clc
% digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
% 'nndatasets','DigitDataset');
subject2path='D:\data\';
imds = imageDatastore(subject2path, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
figure;
perm = randperm(45,20);
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
labelCount1 = countEachLabel(imds)
img = readimage(imds,1);
size(img)
numTrainFiles1 = 25;
[imdsTrain1,imdsValidation] = splitEachLabel(imds,numTrainFiles1,'randomize');
labelCount2 = countEachLabel(imdsTrain1)
numTrainFiles2= 25;
[imdsTrain2,imdsTest] = splitEachLabel(imdsTrain1,numTrainFiles2,'randomize');
labelCount3 = countEachLabel(imdsTrain2)
layers = [
imageInputLayer([64 64 1])
convolution2dLayer([64, 2],6) % 1, 328, 6 % spatial conv
batchNormalizationLayer
reluLayer
maxPooling2dLayer([1, 2],'Stride',[2, 1]) % 1, 164, 6
convolution2dLayer([1, 11], 12) % 1, 154, 12 % temporal conv
batchNormalizationLayer
reluLayer
maxPooling2dLayer([1,2],'Stride',[2,1]) % 1, 77, 12
%
convolution2dLayer([1, 10], 24) % 1, 68, 24
batchNormalizationLayer
reluLayer
maxPooling2dLayer([1,2],'Stride',[2,1]) % 1, 34, 24
dropoutLayer
fullyConnectedLayer(15)
reluLayer
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'Momentum',0.9,...
'InitialLearnRate', 0.001, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 10, ...
'MiniBatchSize', 100, ...
'MaxEpochs', 20 , ...
'Verbose', false, ...
'VerboseFrequency', 10 , ...
'ValidationData', imdsValidation, ...
'ValidationFrequency',10, ...
'ValidationPatience', 15, ...
'Shuffle', 'once', ...
'ExecutionEnvironment', 'auto', ...
'Plots','training-progress', ...
'SequenceLength', 'longest');
%
% 'Epsilon', 1.0000e-08, ...,
% 'SquaredGradientDecayFactor',0.99, ...
net = trainNetwork(imdsTrain2,layers,options);
YPred_val = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy_Val = sum(YPred_val == YValidation)/numel(YValidation)
YPred_test = classify(net,imdsTest);
YTest = imdsTest.Labels;
accuracy_Test = sum(YPred_test == YTest)/numel(YTest)
CMstoc=crosstab(YPred_test, YTest), save CMstoc CMstoc

답변 (1개)

Image Analyst
Image Analyst 2019년 12월 26일
Use imresize():
img = imresize(img, [64, 64]);
  댓글 수: 1
Hamid Ebrahimi
Hamid Ebrahimi 2019년 12월 26일
Thank you for answered , where should I use this code ,I used it after img = readimage(imds,1); but gave below error:
Error using trainNetwork (line 165)
The training images are of size 656x875x3 but the input layer expects images of size 64x64x1.
Error in test1 (line 67)
net = trainNetwork(imdsTrain2,layers,options);

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

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by