Suparna_kumar asked . 2021-07-06

What is the best CNN for a small dataset?

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?
 

cnn , image processing , machine learning , facial expressions

Expert Answer

Prashant Kumar answered . 2024-11-11 13:27:26

Building a cnn from scratch isn't too hard. How many expressions are you trying to classify? With only 370 images, even with image augmentation, you won't be able to build a very big dataset, and it will be difficult to classify many different expression types. You will need a small CNN to start with, with only a couple of layers, otherwise you will get overfitting like there is no tomorrow (that is, your network will memorize all the images you gave it, rather than learning to tell the difference).
 
Here is how I would go about doing this. Let's say you want to tell the difference between happy and sad expressions. Organize your photos so they are in two folders, one for each type of expression. You will need to put them there manually. As there are only 370 images this won't be too hard (for my project I've been having to classify over 14,000 images and I've got a lot more to do still, automating this process on more images you might come across is a topic for another time). Now, here is some basic code which will help you. Note that you will need the Neural Network Toolbox and preferably the Parallel Processing Toolbox (optional, but strongly recommended).
 
You will either need to resize all of your images to the same resolution or use the augmentedImageDatastore to do it for you, but note that the augmentedImageDatastore is a bit slower because it has to resize images on the fly, rather than just reading them from the disk. The below example will assume you want to use the augmentedImageDatastore, but it is easy to take out if you want to.
 
I forgot what doesn't work on R2017b vs R2018a so let me know if the below code doesn't work.
 
    % 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);


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.