I have a dataset of around 370 images of people, and I want to classify their expressions. Should I build my CNN from scratch? How many hidden layers should I aim for?
Prashant Kumar answered .
2025-11-20
% first, the hyperparameters, you will need to play with these
train_percent = 0.90; % amount from each label to use in training, if you want to do cross validation let me know
% test perc = 1 - train
mini_batch = 256; % more is faster, limited by gpu memory if you have a gpu
max_epochs = 30;
initial_learn_rate = 0.001;
learn_rate_drop_period = 30; % reduce this if you decide you want to drop the learn rate
learn_rate_drop_factor = 0.1;
momentum = 0.9;
l2reg = 0.00001;
validation_freq = 256; % in iterations
validation_patience = 3; % early stopping
verbose = false;
augmentedResolution = [128 128]; % or whatever image resolution you want to use
inputResolution = augmentedResolution;
inputResolution(3) = 3; % color dimension, set to 1 for black/white images
layers = [imageInputLayer(inputResolution);
convolution2dLayer(3,32,'Stride',1,'Padding',1); % first number is color dimension, second number is number of neurons/ filters to use (this can be set to whatever positive integer you want, more requiring more computations and memory and parameters - so more means greater chance of overfitting), stride and padding set to 1 ensure that the output resolution is the same as the input resolution
batchNormalizationLayer(); % if r2017b supports it
reluLayer();
maxPooling2dLayer(2,'Stride',2);
dropoutLayer(0.5); % reduces overfitting
fullyConnectedLayer(2); % set "2" to number of classes
softmaxLayer();
classificationLayer()];
augmenter = imageDataAugmenter('RandRotation', [-10 10]); % optional, used to augment data, see documentation for full options
% now getting everything in place and ready to run
datastore = imageDatastore(fullfile('.'), 'IncludeSubFolders', true, 'LabelSource', 'foldernames'); % you need to run Matlab from where you folders are located for this to work
[trainStore, validStore] = splitEachLabel(datastore, train_percent);
trainStoreAug = augmentedImageDatastore(augmentedResolution, trainStore, 'DataAugmentation', augmenter);
options = trainingOptions('sgdm', 'MiniBatchSize', mini_batch_size, ...
'LearnRateSchedule', 'piecewise', 'MaxEpochs', max_epochs, 'InitialLearnRate', initial_learn_rate, ...
'LearnRateDropPeriod', learn_rate_drop_period, ...
'LearnRateDropFactor', learn_rate_drop_factor, ...
'L2Regularization', l2reg, 'Momentum', momentum, ...
'Verbose', verbose, 'VerboseFrequency', validation_freq, ...
'ValidationFrequency', validation_freq, 'ValidationData', validStore, ...
'ValidationPatience', validation_patience, 'Plots', 'training-progess');
convnet = trainNetwork(trainStoreAug, layers, options);