How to use Alexnet to train images & calculate its accuracy.

How to use Alexnet to train images & calculate its accuracy.

Spread the love
imds = imageDatastore('test', ...
    'IncludeSubfolders',true, ...
    'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
net = alexnet;
inputSize = net.Layers(1).InputSize
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
    layersTransfer
    fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
    softmaxLayer
    classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
    'RandXReflection',true, ...
    'RandXTranslation',pixelRange, ...
    'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
    'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
    'MiniBatchSize',10, ...
    'MaxEpochs',6, ...
    'InitialLearnRate',1e-4, ...
    'Shuffle','every-epoch', ...
    'ValidationData',augimdsValidation, ...
    'ValidationFrequency',3, ...
    'Verbose',false, ...
    'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,layers,options);
[YPred,scores] = classify(netTransfer,augimdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)


The Alexnet code explanation
 The code starts by creating an imageDatastore object named imds. This object represents a collection of images in the 'test' folder and its subfolders. The images are labeled based on their folder names.

The imds object is then split into training and validation sets using the splitEachLabel function. 70% of the images are randomly assigned to the training set (imdsTrain), and the remaining 30% are assigned to the validation set (imdsValidation).

The next line creates an AlexNet neural network model named net. AlexNet is a deep convolutional neural network commonly used for image classification tasks.

The input size of the first layer in the net model is obtained using net.Layers(1).InputSize. This will be used later to define the input size for the augmentedImageDatastore.

The layersTransfer variable is created by extracting all layers of the net except the last three layers. These layers will be used as the base layers for transfer learning.

The number of classes in the training set is determined using numel(categories(imdsTrain.Labels)). This will be used to define the number of neurons in the fully connected layer that will be added to the network for classification.

The layers variable is created to define the complete network architecture. It consists of the layersTransfer, a fully connected layer with numClasses neurons, a softmax layer for probability estimation, and a classification layer to assign labels to the input images.

The pixelRange variable defines the range of translation for data augmentation. This is used to randomly shift the images during training.

The imageAugmenter object is created to perform data augmentation on the training images. It includes random horizontal reflection and random translation within the specified pixel range.

The augmentedImageDatastore objects augimdsTrain and augimdsValidation are created. They resize and apply the data augmentation defined by imageAugmenter to the imdsTrain and imdsValidation datastores, respectively.

The options variable is set to define the training options for the network. These options include using stochastic gradient descent with momentum ('sgdm'), setting the mini-batch size to 10, the maximum number of epochs to 6, the initial learning rate to 1e-4, shuffling the data every epoch, validating the network using augimdsValidation every 3 batches, and displaying the training progress without verbosity.

The trainNetwork function is called to train the network (netTransfer) using the augmented training data (augimdsTrain), the defined layers (layers), and the specified options (options).

After training, the classify function is used to classify the augmented validation data (augimdsValidation) using the trained network (netTransfer). This returns predicted labels (YPred) and corresponding classification scores (scores).

The true labels for the validation set (YValidation) are obtained from imdsValidation.Labels.

Finally, the accuracy of the classification is calculated by comparing the predicted labels (YPred) with the true labels (YValidation) and taking the mean.

This code demonstrates the process of fine-tuning the pre-trained AlexNet model for a specific image classification task using transfer learning. The network is trained on the augmented training data, and its performance is evaluated on the augmented validation data.
More: Top 10 digital skills that are in High Demand
Falana William is a passionate writer who has a keen interest in various topics. With expertise as a certified Google digital marketing expert, Falana William possesses the skills and knowledge to navigate the ever-evolving digital landscape. Combining a love for writing with proficiency in digital marketing, Falana William is equipped to create engaging and effective content that resonates with target audiences.
Back To Top