how to make the same image size ?

2 ビュー (過去 30 日間)
Mohamed Nasr
Mohamed Nasr 2020 年 4 月 28 日
回答済み: Sanyam 2022 年 7 月 13 日
clear all;
clc;
close all;
imds = imageDatastore('D:\matlab aml\dataset1','FileExtensions',{'.jpg'},'IncludeSubfolders',true,'LabelSource','foldernames');
imgs = readall(imds);
figure;
perm = randperm(200,20);%Display some of the images in the datastore.
for i = 1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
img = readimage(imds,1); %Check the size of the first image in digitData. Each image is 201-by-173-by-3 pixels.
size(img)
%Specify Training and Validation Sets
labelCount = countEachLabel(imds) %Calculate the number of images in each category
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7);%Divide the data into training and validation data sets. Use 70% of the images for training and 30% for validation.
%Define the convolutional neural network architecture.
%%%%%%%code for resizing
inputSize=[227 227 1];
imdsTrain=augmentedImageDatastore(inputSize, imdsTrain,'ColorPreprocessing','RGB');
imdsValidation=augmentedImageDatastore(inputSize, imdsValidation,'ColorPreprocessing','RGB');
layers = [
imageInputLayer([227 227 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
%Specify Training Options
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
%Train Network Using Training Data
net = trainNetwork(imdsTrain,layers,options);
%Classify Validation Images and Compute Accuracy
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)
this is my code and error is
ans =
201 173 3
labelCount =
2×2 table
Label Count
_____ _____
no 91
yes 154
Unrecognized method, property, or field 'Labels' for class 'augmentedImageDatastore'.
Error in ccn (line 59)
YValidation = imdsValidation.Labels;

回答 (1 件)

Sanyam
Sanyam 2022 年 7 月 13 日
The imdsValidation in your code is an augmentedImageDatastore, it is used to apply transformations to imageDatastore and does not have a 'labels' field.
Instead, you can proceed in this manner.
//refer to code
Here we have seperated the imageDatastore and augmentedImageDatastore and we can access the labels by
YValidation = imdsValidation.Labels; i.e referring the labels field of imageDataStore.
Also, when training the model and making predictions, please use the newly created augmentedImageDatastores (augdsTrain, augdsValidation).
Hope that helps! Thanks!!
[imdsTrain imdsValidation] = splitEachLabel(imds,0.7);
augdsTrain=augmentedImageDatastore(inputSize, imdsTrain,'ColorPreprocessing','RGB');
augdsValidation=augmentedImageDatastore(inputSize, imdsValidation,'ColorPreprocessing','RGB');

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by