How to change size of input layer from 224x224x3 to 224x224x1 in Pretrained GoogLeNet convolutional neural network?
6 views (last 30 days)
Show older comments
Hi the amazing community. Neural Network Toolbox can be used to classify images using googlenet and alexnet. The model here takes input size in 3d but I have to classify 2d images. How to change input layer size from 224x224x3 to 224x224x1? and what else would be needed to change in the layers for the change in input size?
2 Comments
Answers (1)
Prasanna
on 10 Dec 2024
Hi Jahandad,
To adapt a pre-trained model like GoogleNet or AlexNet for 2D (224*224*1) images instead of 3D images (224*224*3), you need to modify the network’s input layer and adjust the other layers accordingly. To do the above, refer the following:
- Load the pre-trained network
- Obtain the layers and change the input layer to accept single channel images
- If necessary, modify the first convolutional layer to accept single channel inputs. You can also manually adjust the weights if needed.
- Ensure the weights of the first convolutional layer accommodate the single channel. This might involve averaging the weights across the three channels or retraining the network.
- Fine-tune the network using the grayscale dataset. This step is crucial because the pre-trained weights were learned from color images.
A sample MATLAB code for the same is given below assuming the network as a pre trained GoogleNet:
net = googlenet; % or alexnet
lgraph = layerGraph(net);
inputLayer = imageInputLayer([224 224 1], 'Name', 'input', 'Normalization', 'zerocenter');
lgraph = replaceLayer(lgraph, 'data', inputLayer);
firstConvLayer = lgraph.Layers(2); % Assuming the first conv layer is the second layer
newConvLayer = convolution2dLayer(firstConvLayer.FilterSize, firstConvLayer.NumFilters, ...
'Stride', firstConvLayer.Stride, ...
'Padding', firstConvLayer.PaddingSize, ...
'Name', firstConvLayer.Name);
lgraph = replaceLayer(lgraph, firstConvLayer.Name, newConvLayer);
analyzeNetwork(lgraph);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1820496/image.png)
Ensure that the normalization of the input layer matches the expected input range of the network. Also given that the network was initially trained on color images, fine-tuning with a grayscale dataset is important to adapt the network to the new input format. For more information regarding the above, refer to the following documentations:
- Imageinputlayer: https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.imageinputlayer.html
- Convolution2dLayer: https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.convolution2dlayer.html
- Change input size of pre-trained network: https://www.mathworks.com/matlabcentral/answers/458610-change-input-size-of-a-pre-trained-network
Hope this helps!
0 Comments
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!