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

18 ビュー (過去 30 日間)
Phil Elamparo
Phil Elamparo 2022 年 9 月 14 日
回答済み: Cris LaPierre 2025 年 5 月 31 日
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.

回答 (2 件)

Ganesh Gudipati
Ganesh Gudipati 2022 年 9 月 19 日
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.

Cris LaPierre
Cris LaPierre 2025 年 5 月 31 日
If I understand the issue correctly, I believe you have a few options.
1. Modify the size of the imageInputLayer in your code to match the size of your images. Not sure how this will impace downstream layers, though. If you images vary in size, this won't work.
imageInputLayer([3024 4032 3], 'Name', 'input')
2. Create an augmentedImageDatastore to resize your images to match imageInputLayer. This ensures all inputs to the network are of the expected size.
outputSize = [224 224 3]
auimds = augmentedImageDatastore(outputSize,X);
3. Define an imagedatastore with imresize incorporated into the ReadFcn. This also ensures all inputs to the network are of the expected size.
imds = imageDatastore(location, 'ReadFcn', @(x) imresize(imread(x),[224,224]);
4. Some mix of the two approaches. Resizing a [3024 4032 3] image to [224 224 3] will certainly loose some information, but maybe the network gets really slow for large images (I don't know). In that case, use a value between the two and modify the inputsize of the network and resize the images to that size.
5. Another technique for larger images to to crop them to just the area you are interetsed in.
You may find this answer helpful as well: [YOLOv2] image size and Input size of the network

カテゴリ

Help Center および File ExchangeExternal Language Interfaces についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by