How can to divide my entire dataset of images in subset of train, test and validation? After i made this division , i should apply a speckle noise for each subset

3 views (last 30 days)
clear all
clc
close all
% outputFolder = pwd;
% dataURL = ['https://ssd.mathworks.com/supportfiles/radar/data/' ...
% 'MSTAR_TargetData.tar.gz'];
% helperDownloadMSTARTargetData(outputFolder,dataURL);
sarDatasetPath = fullfile(pwd,'Data');
imds = imageDatastore(sarDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
rng('default')
figure
% Shuffle the datastore.
imds = shuffle(imds);
imgSize = [128,128];
%
%imds_re = augmentedImageDatastore(imgSize, imds);
%
tic
for i=1:16950
bb(:,:,i)= imresize(imread(imds.Files{i}),imgSize);
labels(i)=imds.Labels(i);
end
toc
images=bb;
file='/home/contest/Desktop/ATR_classification/data_mat/';
save(fullfile(file,'images.mat'),'images')
save(fullfile(file,'labels.mat'),'labels')
load('/home/contest/Desktop/ATR_classification/data_mat/images.mat');
load('/home/contest/Desktop/ATR_classification/data_mat/labels.mat');
L = 4;
s = [128,128];
sigma = 1/sqrt(2*L);
for i = 1:size(images,3)
f = raylrnd(sigma,s(1),s(2));
img_m(:,:,i) = double(images(:,:,i)).*f;
end
for i = 1:20
subplot(4,5,i)
img = read(imds);
imshow(img)
title(imds.Labels(i))
sgtitle('Sample training images')
end
trainingPct = 0.8;
validationPct = 0.1;
[imdsTrain,imdsValidation,imdsTest] = splitEachLabel(imds,...
trainingPct,validationPct,'randomize');
imgSize = [128,128];
auimdsTrain = augmentedImageDatastore(imgSize, imdsTrain);
auimdsValidation = augmentedImageDatastore(imgSize, imdsValidation);
auimdsTest = augmentedImageDatastore(imgSize, imdsTest);
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.001, ...
'MaxEpochs',6, ...
'Shuffle','every-epoch', ...
'MiniBatchSize',48,...
'ValidationData',auimdsValidation, ...
'ValidationFrequency',15, ...
'Verbose',false, ...
'CheckpointPath',tempdir,...
'Plots','training-progress');
layers = createNetwork(imgSize);
net = trainNetwork(auimdsTrain,layers,options);
YPred = classify(net,auimdsTest);
YTest = imdsTest.Labels;
accuracy = sum(YPred == YTest)/numel(YTest)
figure
cm = confusionchart(YPred, YTest);
cm.RowSummary = 'row-normalized';
cm.Title = 'SAR Target Classification Confusion Matrix';

Answers (1)

Aastha
Aastha on 24 Mar 2025
I understand that you want to divide the input dataset into training, testing, and validation sets and apply speckle noise to each split.
You can use the “imnoise” function in MATLAB to add speckle noise to each split. To do this, define the variance of the speckle noise. You may refer the MATLAB code below:
var_speckle = 1;
Add the speckle noise with the specified variance to the image “I” as illustrated below:
noisy_I = imnoise(I, 'speckle', var_speckle);
You may refer to the MathWorks documentation of the “imnoise” function for more information : https://www.mathworks.com/help/releases/R2024b/images/ref/imnoise.html
I hope this is helpful!

Community Treasure Hunt

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

Start Hunting!