How to apply attention mechanism to object detection in RGB images
3 views (last 30 days)
Show older comments
Tian,HCong
on 26 May 2024
Commented: Image Analyst
on 26 May 2024
Hello, as this is my first time using MATLAB for research in deep learning, I am not very proficient yet. Can someone give me an example of how to apply attention mechanisms such as attention layers and self-attention layers to object detection in RGB images? Thank you very much.
0 Comments
Accepted Answer
Image Analyst
on 26 May 2024
I don't know what an attention mechanism is.
Try these links on abandoned object detection:
Or maybe you're looking for "saliency":
2 Comments
Image Analyst
on 26 May 2024
Looks like you'll need to replace your images with gray scale images. You can either use rgb2gray or you can simply take one of the color channels, whichever one has the best contrast and least noise. Save them into a different folder. Here is a little script to convert all your images to 28x28 grayscale and save them to a different folder:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
% Specify the folder where the files live. CHANGE THESE LINES!
inputRGBFolder = 'C:\Users\yourUserName\Documents\My Pictures';
outputGrayscaleFolder = 'C:\Users\yourUserName\Documents\My Pictures\GrayScale';
% Check to make sure that input folder actually exists. Warn user if it doesn't.
if ~isfolder(inputRGBFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', inputRGBFolder);
uiwait(warndlg(errorMessage));
inputRGBFolder = uigetdir(); % Ask for a new one.
if inputRGBFolder == 0
% User clicked Cancel
return;
end
else
% Check to make sure that output folder actually exists.
% Create it if it doesn't as a subfolder of the input folder.
if ~isfolder(outputGrayscaleFolder)
mkdir(outputGrayscaleFolder);
end
end
% Get a list of all files in the input folder with the desired file name pattern.
filePattern = fullfile(inputRGBFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
% Get this filename.
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Read it in as an image array with imread();
imageArray = imread(fullFileName);
% Display it.
subplot(2, 1, 1);
imshow(imageArray); % Display image.
axis('on', 'image')
caption = sprintf('Original Image : %s', baseFileName);
title(caption);
% See if it's RGB.
[rows, columns, numberOfColorChannels] = size(imageArray);
if numberOfColorChannels >= 3
% Convert to grayscale
imageArray = rgb2gray(imageArray);
% Or alternatively (a little faster):
%imageArray = imageArray(:, :, 2); % Take green channel.
end
% Now resize it to 28 x 28 (it will be almost unrecognizable since that is so very tiny).
imageArray = imresize(imageArray, [28, 28]);
subplot(2, 1, 2);
imshow(imageArray); % Display image.
title('Grayscale Image')
axis('on', 'image')
drawnow; % Force display to update immediately.
% Save grayscale version to output folder
outputFullFileName = fullfile(outputGrayscaleFolder, baseFileName);
imwrite(imageArray, outputFullFileName);
end
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!