Resize and Convert Image to grayscale with Augmented Image Datastore using Matlab returning error

429 views Asked by At

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!

1

There are 1 answers

0
Alex Taylor On

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];