Miltary Target Classification in MSTAR Dataset using MATLAB

Version 1.0.0 (8,66 MB) von Fazal
Image Processing
6 Downloads
Aktualisiert 22. Sep 2024

Lizenz anzeigen

% Step 1: Load and Preprocess the Dataset
trainFolder = 'E:\Project\3 Vehicles\TRAIN DATA'; % Update with the path to the "train" folder
testFolder = 'E:\Project\3 Vehicles\TEST DATA'; % Update with the path to the "test" folder
% Load the training dataset and label based on folder names (train folder)
imdsTrainVal = imageDatastore(trainFolder, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% Convert the images to grayscale (if not already grayscale) and resize to 128x128
imdsTrainVal.ReadFcn = @(x) imresize(imread(x), [128 128]);
% Split the dataset into 80% training and 20% validation
[imdsTrain, imdsValidation] = splitEachLabel(imdsTrainVal, 0.8, 'randomized');
% Step 2: Define the CNN Architecture
layers = [
imageInputLayer([128 128 1]) % Grayscale images of size 128x128 with 1 channel
% First convolutional layer and ReLU activation
convolution2dLayer(3, 16, 'Padding', 'same')
batchNormalizationLayer
reluLayer
% First max pooling layer
maxPooling2dLayer(2, 'Stride', 2)
% Second convolutional layer and ReLU activation
convolution2dLayer(3, 32, 'Padding', 'same')
batchNormalizationLayer
reluLayer
% Second max pooling layer
maxPooling2dLayer(2, 'Stride', 2)
% Fully connected layer
fullyConnectedLayer(64)
reluLayer
% Output layer (3 classes: BMP2, BTR70, T72)
fullyConnectedLayer(3)
softmaxLayer
classificationLayer
];
% Step 3: Specify Training Options
options = trainingOptions('sgdm', ...
'MaxEpochs', 15, ... % Number of epochs
'MiniBatchSize', 32, ... % Mini-batch size
'InitialLearnRate', 0.001, ... % Learning rate
'ValidationData', imdsValidation, ...
'ValidationFrequency', 10, ... % Validate every 10 iterations
'Verbose', false, ...
'Plots', 'training-progress'); % Display training progress
% Step 4: Train the CNN
net = trainNetwork(imdsTrain, layers, options);
% Step 5: Evaluate the Model
YPred = classify(net, imdsValidation); % Predict the validation set
YValidation = imdsValidation.Labels; % True labels
% Calculate accuracy
accuracy = mean(YPred == YValidation);
disp(['Validation accuracy: ', num2str(accuracy * 100), '%']);
% Confusion matrix for performance evaluation
confusionchart(YValidation, YPred);
% Step 6: Test the Model (using the test folder)
% Load test images from the "test" folder
imdsTest = imageDatastore(testFolder, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
imdsTest.ReadFcn = @(x) imresize(imread(x), [128 128]);
% Classify the test set images
YPredTest = classify(net, imdsTest);
YTest = imdsTest.Labels; % True labels for test images
% Calculate test accuracy
testAccuracy = mean(YPredTest == YTest);
disp(['Test accuracy: ', num2str(testAccuracy * 100), '%']);
% Confusion matrix for test set performance evaluation
confusionchart(YTest, YPredTest);
% Step 7: Visualize Some Test Results
% Randomly select 9 images from the test set to visualize
numImagesToShow = 20; % Number of images to display
idx = randperm(length(imdsTest.Files), numImagesToShow); % Randomly select images
figure;
for i = 1:numImagesToShow
subplot(5, 4, i);
% Load and preprocess the image
img = readimage(imdsTest, idx(i));
imgResized = imresize(img, [128 128]); % Ensure resizing
% Classify the image
[label, score] = classify(net, imgResized);
% Display the image and the predicted label
imshow(img);
title(['Predicted: ', char(label), ' (', num2str(max(score) * 100, '%.2f'), '%)']);
end
% Step 8: Save the Trained Model
save('E:\Project\3 Vehicles\Custom CNN method\TrainedModel.mat', 'net'); % Save the trained model

Zitieren als

Fazal (2024). Miltary Target Classification in MSTAR Dataset using MATLAB (https://www.mathworks.com/matlabcentral/fileexchange/172985-miltary-target-classification-in-mstar-dataset-using-matlab), MATLAB Central File Exchange. Abgerufen.

Kompatibilität der MATLAB-Version
Erstellt mit R2019a
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Tags Tags hinzufügen
Quellenangaben

Inspiriert von: ChatGPT Generated MATLAB program

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Veröffentlicht Versionshinweise
1.0.0