Hi,
I understand that you want to implement SMOTE for image classification. You can refer to the following File Exchange submission to download the file containing the implementation of the 'smote' function:
Since SMOTE typically operates on numerical feature vectors, the first step involves converting images into a suitable format. After generating synthetic samples, you'll need to convert these back into images and handle them appropriately for your classification task. Please refer to the following code snippet which implements SMOTE for the Digits dataset using the code provided in the File Exchange submission.
dataFolder = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset');
imds = imageDatastore(dataFolder, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
allFeatures = [];
allLabels = [];
while hasdata(imds)
[img, info] = read(imds);
label = info.Label;
featureVector = double(reshape(img, [], 1));
allFeatures = [allFeatures; featureVector'];
allLabels = [allLabels; label];
end
% Convert labels to numeric if they're categorical
if iscell(allLabels)
[~, ~, allLabels] = unique(allLabels);
end
% Use SMOTE to oversample all classes 100% (using default 5 k-nearest neighbors)
[smoteFeatures, smoteLabels] = smote(allFeatures, 0.5, 'Class', allLabels);
%%
imageSize = [28, 28];
numImages = size(smoteFeatures, 1);
syntheticImages = reshape(smoteFeatures', [imageSize, 1, numImages]);
outputFolder = '<path to syntheticImages>'; % Define where to save images
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
numImages = size(syntheticImages, 4);
for i = 1:numImages
img = syntheticImages(:, :, :, i); % Extract the i-th image
label = smoteLabels(i);
filename = sprintf('%s_%d.png', label, i);
fullPath = fullfile(outputFolder, filename);
imwrite(img, fullPath);
end
syntheticImds = imageDatastore(outputFolder, 'Labels', smoteLabels);
Hope this helps!