How can I apply PCA on thousand of images to reduce the dimension of the data?
1 view (last 30 days)
Show older comments
Hi,
I applied a simple CNN on dataset of 1000 images and the code works fine. However, I want to apply PCA on both training and testing set in order to reduce reduce its dimensionality (features selection) the code is below. Many thanks.
close all
clear all
clc
%% Load image data
imds = imageDatastore('the path of images folder',...
'IncludeSubfolders',true,'LabelSource','foldernames')
tbl = countEachLabel(imds)
%% Divide the data into 70% training data and 30% validation data
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%% Specify training options.
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',3, ...
'Shuffle','every-epoch', ...
'InitialLearnRate',1e-4, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',6, ...
'Verbose',false, ...
'Plots','training-progress', ...
'ExecutionEnvironment','gpu');
%% Layers for simple CNN.
layers_1 = [
imageInputLayer([512 512 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];
%% train the network
netTransfer = trainNetwork(imdsTrain,layers_1,options);
Answers (0)
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!