How do I use a dataset of both grayscale and rgb images for training a Neural Network?

22 vues (au cours des 30 derniers jours)
Hey,
as I am currently creating a Neural Network with the Deep Network Designer and have been using an image dataset [7527 images] with 8 classes to train it, I stumbled across the problem that I have both grayscale and rgb images in there.
Converting my ImageDatastore to an augmentedImageDatastore and applying Colorpreprocessing - in a first try 'gray2rgb' and then a second time with 'rgb2gray' - I realized that all of my former grayscale images then were completely blacked out after the preprocessing and I guess not recognizable by my Neural Network, since I receive a very low accuracy in training which apart from my structure and training options could also result from unusable images I guess.
Does anybody have an idea how to solve this problem: only convert the rgb images to grayscale (1 color channel) and keep the gray images just as they are? With my folder and label structure I am questioning how to sort / divide this large dataset to colored and gray images without doing it too complicated.
Thanks in advance!!

Réponses (1)

Lokesh
Lokesh le 28 Sep 2023
Modifié(e) : Lokesh le 28 Sep 2023
Hi Theresa,
I understand from your query that you want to convert RGB images to grayscale and keep the grayscale images just as they are.
To handle a dataset with mixed grayscale and RGB images, we can apply “ColorPreprocessing on the augmentedimagedatastore. When the image datastore contains a mixture of grayscale and RGB images, ColorPreprocessing ensures that all output images have the number of channels required by imageInputLayer. No color preprocessing operation is performed when an input image already has the required number of color channels. For example, if you specify the value rgb2gray and an input image already has one channel, then no color preprocessing occurs.
However, if you want to divide your dataset into separate RGB and grayscale datasets, the following workaround might be helpful:
To classify each image as colored or gray, you can check the number of color channels in each image. Colored images typically have three color channels (red, green, and blue), while gray images have only one channel. You can use the size function to get the dimensions of each image and check the number of color channels.
Here is an example code that identifies whether an image is RGB or grayscale, and creates RGBimageDatastore and GrayscaleimageDatastore:
% Create imageDatastore to read the images from your dataset directory
datasetPath = 'path_to_dataset_directory';
imds = imageDatastore(datasetPath);
isRGB = arrayfun(@(x) size(readimage(imds, x), 3) == 3,1:numel(imds.Files));
isGray = ~isRGB
RGBimageDatastore = subset(imds, isRGB);
GrayscaleimageDatastore = subset(imds, isGray);
You can refer to the below mentioned documentation to know more about the usage of “readimage”, arrayfun” and “subset”:
I hope you find this helpful.
Best Regards,
Lokesh

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by