0

I want to resize all images in folder [224 224 3].

aug_imds = augmentedImageDatastore([224 224 3], imdsTrain,'ColorPreprocessing', 'rgb2gray');

layers = [
    imageInputLayer([224 224 3])
    convolution2dLayer(3, 8, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(2, 'Stride', 2)
    convolution2dLayer(3, 16, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(2, 'Stride', 2)
    convolution2dLayer(3, 32, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    fullyConnectedLayer(8)
    softmaxLayer
    classificationLayer];

After augmentedImageDataStore, resizing image its returning new file [224 224 3] but after rg2gray image size changed to '224 224 1'

Error:
Error using trainNetwork
The training images are of size 224×224×1 but the input layer expects images of size 224×224×3.

Error in training (line 49)
net = trainNetwork(aug_imds, layers, options);

Tried multiple ways still dimension changed to 1. I want to resize, grayscale image and size should be [224 224 3]

Thanks in advance!

Kavya Shree
  • 1,014
  • 2
  • 17
  • 52
  • The option ColorPreprocessing="rgb2gray" calls the MATLAB function rgb2gray, which converts a 3-channel RGB image to a 1-channel grayscale image. It's unclear what kind of data you are starting with (e.g. 3 channel RGB, 1 channel grayscale, a combination, something else). If what you want is to take single channel grayscale images and make them 3-channel gray looking images, you can use the ColorPreprocessing="gray2rgb" option which just replicates the channel dimension to create a 3 channel gray image. – Alex Taylor Apr 19 '23 at 19:12
  • Thanks for the response. I have multiple rgb images in different dimensions and 3 channel. So by using augumenteddatastore resizing all the images to 224 224 and need to change all images in grayscale , by using rgb2gray my dimension changing to 1, I want to grayscale the image and keep the channel as 3 – Kavya Shree Apr 19 '23 at 20:13
  • If I use gray2rgb my images will be in color, I want to train and test with grayscale images – Kavya Shree Apr 19 '23 at 20:15

1 Answers1

0

augmentedImageDatastore is designed for more simple augmentations. For more flexibility, you can always use transform/combine to define your augmentation. I think what is below is close to what you're looking for.

imds = imageDatastore(___);
imdsTransformed = transform(imds,@augmentData);

function out = augmentData(x)
    out = imresize(x,[224 224]);
    out = rgb2gray(out); % Move to [224,224,1]
    out = repmat(out,[1 1 3]); % Move to [224,224,3] grayscale
end

I would note that it seems like an easier + more efficient strategy would be to just grayscale convert your images to [224,224,1] and define your input layer to accept [224,224,1] data.

aug_imds = augmentedImageDatastore([224 224 3], imdsTrain,'ColorPreprocessing', 'rgb2gray');

layers = [
    imageInputLayer([224 224 1])
    convolution2dLayer(3, 8, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(2, 'Stride', 2)
    convolution2dLayer(3, 16, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    maxPooling2dLayer(2, 'Stride', 2)
    convolution2dLayer(3, 32, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    fullyConnectedLayer(8)
    softmaxLayer
    classificationLayer];
Alex Taylor
  • 1,402
  • 1
  • 9
  • 15