Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 . Load the image dataset: load the Digits dataset from a predefined path in the MATLAB toolbox. The imageDatastore function handles the loading of
Load the image dataset: load the Digits dataset from a predefined path in the MATLAB toolbox. The imageDatastore function handles the loading of image data. All subfolders are included and folder names are used as labels. The images are resized to x pixels for compatibility with the LeNet architecture. The data is then split into training and validation sets, with used for training and for validation.
Load the Digits dataset
digitDatasetPath fullfiletoolboxdirnnet 'nndemos',
'nndatasets', 'DigitDataset';
imds imageDatastoredigitDatasetPath
'IncludeSubfolders', true, 'LabelSource', 'foldernames';
imds.ReadFcn @locimresizeimreadloc;
Split the data into training and validation datasets
imdsTrain imdsValidation splitEachLabelimds 'randomized';
Define the architecture of the CNN using the layers function: It begins with an input layer expecting images of size x with one channel greyscale Following this are alternating convolutional, ReLU activation, and max pooling layers. Then, there are several fully connected layers with ReLU activation, ending with a softmax layer and a classification output layer. Refer to the MATLAB documentation to understand how to define each of these layers. In Matlab, the loss function is implicitly defined through the classificationLayer. Refer to the MATLAB documentation to understand how to define each of these layers.
Define the LeNet architecture
layers
imageInputLayer'Name','input'
convolutiondLayer'Padding','same','Name','conv
averagePoolingdLayer'Stride','Name','avgpool
convolutiondLayer'Padding','same','Name','conv
averagePoolingdLayer'Stride','Name','avgpool
fullyConnectedLayer'Name',fc
fullyConnectedLayer'Name',fc
fullyConnectedLayer'Name',fc
softmaxLayerName'softmax'
classificationLayerName'output';
Configure training options using the trainingOptions function. Set parameters like the optimisation algorithm, learning rate, batch size, and number of epochs.
In this block, you specify the training options. You use stochastic gradient descent with momentum sgdm as your optimization algorithm, set an initial learning rate of and limit the training to a maximum of epochs. The training data will be shuffled at the start of each epoch. The progress of training will be displayed as a plot.
Specify the training options
options trainingOptionssgdm
'InitialLearnRate',
'MaxEpochs',
'Shuffle','everyepoch',
'ValidationData',imdsValidation,
'ValidationFrequency',
'Verbose',false,
'Plots','trainingprogress';
Train the CNN using the trainNetwork function, passing in the image data, the layer definitions, and the training options. Please keep in mind that it may require some time to train your model.
Train the network
net trainNetworkimdsTrainlayers,options;
CNN Performance Evaluation
After the network is trained, it's used to classify the images in the validation dataset. The classify function is used to perform this classification. The accuracy of the network on the validation images is then calculated and printed. This is the proportion of images that were correctly classified by the network. It's calculated as the number of correctly classified images divided by the total number of images in the validation set.
Classify validation images and compute accuracy
YPred classifynetimdsValidation;
YValidation imdsValidation.Labels;
accuracy sumYPred YValidationnumelYValidation;
fprintfAccuracy of the network on the validation images: f
accuracy;
VI Task : Improving the Performance of the CNN Algorithm
Guidance on several methods to improve the image classification accuracy:
Regularisation Techniques: In your quest to enhance the CNN accuracy, a key strategy involves regularisation methods such as L and L norm regularisation. For understanding of L and L norm regularisation, please refer to Tewari and Bilogour et al from the Kaggle tutorial. The L and L norm regularisation methods are instrumental in overcoming overfitting. You can implement L and L regularisation in MATLAB by adjusting the WeightLFactor and WeightLFactor properties within the fullyConnectedLayer:
fullyConnectedLayer'Name',fc 'WeightLFactor', 'WeightLFactor',
Additionally, dropout is another valuable regularisation method for reducing overfitting in neural networks. It can be integrated using the dropoutLayer function, further helping in the model's robustness. For your report present results with two regularisation methods, eg with L or L norm regularisation or dropout or combinations of them.
Activation Functions: Another way to improve the CNN accuracy and efficiency is with different activation functions such as ReLU, Sigmoid, or Tanh. These functions play a pivotal role in enabling neural networks to capture complex data patterns and nonlinear relationships. This can be achieved by integrating functions such as reluLayer for ReLU activation. For your report present results with two activation functions.
CNN Architecture Exploration: Delving into various CNN architectures is a critical step towards optimising your networks performance. You can explore modifications such as altering the number of convolutional or fully connected layers, or experimenting with different types of pooling operations, such as max pooling and average pooling. For a more advanced exploration, you might consider studying complex architectures such as AlexNet Krizhevsky et al Please keep in mind that increasing the models complexity may reduce computational efficiency.
The function dropoutLayer defines a dropout layer to your model.
Hyperparameter Tuning: The finetuning of hyperparameters, including the learning rate and the number of epochs, is an essential aspect of enhancing CNN performance. Adjusting these parameters can lead to significant improvements in training efficacy and model accuracy. However, it is crucial to be aware of the potential for overfitting, particularly when increasing the number of epochs. To counteract this effect, you might need to incorporate regularisation methods, which can help in maintaining a model that generalises well to new data. The process of hyperparameter tuning is iterative and requires careful observation and analysis to identify the optimal configuration for your specific model and dataset. For your report present results with two sets of hyperparameters those that give the best results and another set that provides less accuracy and efficiency.
The methods outlined above are recommended and could also be combined for potentially enhanced outcomes. Furthermore, you are encouraged to experiment with other innovative approaches to refine your CNNs accuracy. It is essential to conduct a thorough analysis of your results, delving into the implications and effectiveness of each method employed, to gain deeper insights into your model's performance.
Provide your results of the accuracy and the analysis in your report.
Calculate the Precision, Recall, and the F score of your classification results.
Can you improve the results? Please explain how you improve the accuracy and analyse the results in detail.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started