How to Resize images stored in matlab.io.​datastore.​ImageDatas​tore to [224 224 3]

5 views (last 30 days)
Hello I'm new in using pretrained networks in MATLAB for Object Detection and I was following a guide in Youtube which uses YOLO for detection. In this guide, it also used the resnet50 pretrained network which has an input layer that expects images of size (224x224x3).
Below is the code:
trainData1 = Data;
%%% Create resnet50 pretrained network
netWidth = 16;
layers = [
imageInputLayer([224 224 3], 'Name', 'input')
convolution2dLayer(3, netWidth, 'Padding','same', 'Name', 'convInp')
batchNormalizationLayer('Name', 'BNInp')
reluLayer('Name', 'reluInp')
convolutionalUnit(netWidth, 1, 'S1U1')
additionLayer(2, 'Name', 'add11')
reluLayer('Name', 'relu11')
convolutionalUnit(netWidth, 1, 'S1U2')
additionLayer(2, 'Name', 'add12')
reluLayer('Name', 'relu12')
convolutionalUnit(2*netWidth, 2, 'S2U1')
additionLayer(2, 'Name', 'add21')
reluLayer('Name', 'relu21')
convolutionalUnit(2*netWidth , 1, 'S2U2')
additionLayer(2, 'Name', 'add22')
reluLayer('Name', 'relu22')
convolutionalUnit(4*netWidth, 2, 'S3U1')
additionLayer(2, 'Name', 'add31')
reluLayer('Name', 'relu31')
convolutionalUnit(4*netWidth, 1, 'S3U2')
additionLayer(2, 'Name', 'add32')
reluLayer('Name', 'relu32')
averagePooling2dLayer(8, 'Name', 'globalPool')
fullyConnectedLayer(4, 'Name', 'fcFinal')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'classoutput')
];
lgraph = layerGraph(layers);
lgraph = connectLayers(lgraph, 'reluInp', 'add11/in2');
lgraph = connectLayers(lgraph, 'relu11', 'add12/in2');
skip1 = [
convolution2dLayer(1,2*netWidth, 'Stride', 2, 'Name', 'skipConv1')
batchNormalizationLayer('Name', 'skipBN1')];
lgraph = addLayers(lgraph, skip1);
lgraph = connectLayers(lgraph, 'relu12', 'skipConv1');
lgraph = connectLayers(lgraph, 'skipBN1', 'add21/in2');
lgraph = connectLayers(lgraph, 'relu21', 'add22/in2');
skip2 = [
convolution2dLayer(1, 4*netWidth, 'Stride',2, 'Name', 'skipConv2')
batchNormalizationLayer('Name','skipBN2')];
lgraph = addLayers(lgraph, skip2);
lgraph = connectLayers(lgraph, 'relu22', 'skipConv2');
lgraph = connectLayers(lgraph, 'skipBN2', 'add31/in2');
%add last identity connection and plot the final layer graph
lgraph = connectLayers(lgraph, 'relu31', 'add32/in2');
%training options
options = trainingOptions("sgdm", ...
'MiniBatchSize', 128, ...
'MaxEpochs', 1, ...
'InitialLearnRate',1e-4);
% network training
[trainedNet1, traininfo] = trainNetwork(trainData1, lgraph, options);
And the output error is this:
% network training
[trainedNet1, traininfo] = trainNetwork(trainData1, lgraph, options);
Error using trainNetwork
The training images are of size 3024×4032×3 but the input layer expects images of size 224×224×3.
My workspace is this:
Thank You. Hope You Can Help.

Answers (1)

Ganesh Gudipati
Ganesh Gudipati on 19 Sep 2022
Hi,
The resolution of input image is different from the expected resolution.
Resizing the images will resolve your issue. Please refer to Resize image documentation.
I hope this resolves your issue.

Community Treasure Hunt

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

Start Hunting!