MATLAB: I’m trying to apply the Alexnet transfer learning example but it’s not working with the dataset

alexnetcnndeep learningDeep Learning Toolboxgooglenet

I'm trying to follow the steps in this example Transfer learning using AlexNet using my set of images but it's giving me this error
Error using trainNetwork (line 154)
augmentedImageSource cannot form MiniBatches of data because input image sizes differ in 3rd dimension. Consider
using 'ColorPreprocessing' option to ensure all augmented images have same number of channels.
Error in TrainAlexnet (line 42)
netTransfer = trainNetwork(augimdsTrain,layers,options);
Caused by:
Error using augmentedImageDatastore/applyAugmentationPipeline (line 335)
augmentedImageSource cannot form MiniBatches of data because input image sizes differ in 3rd dimension.
Consider using 'ColorPreprocessing' option to ensure all augmented images have same number of channels.
I even resized all images to [227 227 3], same error. I also had the same problem with GoogLeNet

Best Answer

imresize does not accept [227 227 3] as a parameter. You would have used imresize with [227 227] as a parameter. However, imresize does not change the number of color channels.
In short: some of your files are not RGB images. They are one of:
  • binary images (PNG) (TIFF)
  • grayscale images (GIF) (PNG) (TIFF) (JPG but quite rare)
  • pseudocolor images (GIF) (PNG) (TIFF)
  • CMYK images (TIFF)
  • RGBA images (TIFF) (possible with PNG even if not officially supported; PNG prefers RGB+A)
JPEG 2000 should also be in that table somewhere, but I do not remember exactly what it supports.
Possibly you have some JPEG that you are thinking are grayscale. Grayscale JPEG are very very uncommon: I have only encountered one in about 20 years that was not a test image to prove that it could be done. Nearly all JPEG images that appear to be grayscale are instead RGB images.
Related Question