Regarding using trainnet, testnet in binary image classification(size difference between network output and test data output)
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, every MATLAB users,
I'm trying to make simple binary classification network
model is designed to check whether the image has certain object or not
input datastore is combined of image datastore and label datastore as shown:
imdsTrain = imageDatastore(trainingDataTbl.imageFileName);
imdsTrain.Labels = trainingDataTbl.existence;
imdsTrain.Labels = categorical(imdsTrain.Labels)
labelsTrain = categorical(trainingDataTbl.existence);
ldsTrain = arrayDatastore(labelsTrain);
cdsTrain = combine(imdsTrain, ldsTrain);
(i know already imdsTrain has label data but i modified to debug the error even it doesn't matter)
Each labels is one of 2 categories : True, False
Designed network structure is as follows:
fcn = @(X) X(:,:,1);
bClayers = [
imageInputLayer([800 800 3],"Name","imageinput")
functionLayer(fcn, "Name","gray")
convolution2dLayer([5 5],8,"Name","conv","Padding","same")
reluLayer("Name","relu")
maxPooling2dLayer([8 8],"Name","maxpool","Padding","same","Stride",[8 8])
convolution2dLayer([3 3],16,"Name","conv_1","Padding","same")
reluLayer("Name","relu_1")
maxPooling2dLayer([5 5],"Name","maxpool_1","Padding","same","Stride",[5 5])
fullyConnectedLayer(2,"Name","fc")
softmaxLayer("Name","softmax")];
It's simple CNN structure
I set the options as below:
options = trainingOptions("adam", ...
GradientDecayFactor=0.9, ...
SquaredGradientDecayFactor=0.999, ...
InitialLearnRate=0.001, ...
LearnRateSchedule="none", ...
MiniBatchSize=1, ...
L2Regularization=0.0005, ...
MaxEpochs=4, ...
BatchNormalizationStatistics="moving", ...
DispatchInBackground=false, ...
ResetInputNormalization=false, ...
Shuffle="every-epoch", ...
VerboseFrequency=20, ...
CheckpointPath=tempdir);
I set the MiniBatchSize with 1.
Because, I don't know why but some reason error came up when i execute the trainnet function.
net = trainnet(cdsTrain, bClayers, "crossentropy", options);
the error message is that size of prediction(maybe output of the network) is not same with size of desired value(maybe ground truth label data).
and the desired value size is affected by MiniBatchSize.
I have no idea why this error is occuring.
How Can I Adjust MiniBatchSize or Modify the Code to Run Succesfully??
다음 사용 중 오류가 발생함: validateTrueValues (54번 라인) 예측값 및 목표값의 크기가 일치해야 합니다. 예측값의 크기: 2(C) × 1(B) 목표값의 크기: 2(C) × 16(B)
(This is the Korean error message)
I trained with minibatch size of 1 Anyway.
Another problem happens.
metricValues = testnet(net, cdsTest, "accuracy");
While test the network, Even I make Traindata and Testdata with Same size and same formality,
Code couldn't run with error message the size between network output and desired value(maybe Test data) should same.
This is korean error message for anyone who can understand:
다음 사용 중 오류가 발생함: testnet (40번 라인)
메트릭 "Accuracy"의 경우 네트워크 출력값과 목표값의 크기가 동일해야 합니다.
How can I fix this code??
I hope anyone could respond my question.
Thank you for reading.
답변 (1개)
Nithin
2025년 6월 13일
The primary issue originated from how the network handles batch processing and the setup of the training data. The use of "functionLayer(fcn, "Name", "gray")" led to batch size mismatch because custom function layers typically don’t handle batches correctly. Moreover, pairing an image datastore with a separate label datastore caused inconsistencies that didn’t align with the requirements of "trainnet".
Refer to the following steps to resolve the issue:
- Replace the custom grayscale conversion with a convolution layer that replicates grayscale behavior using fixed weights which ensures proper batch handling:
convolution2dLayer([1 1], 1, "Name", "rgb2gray", "Padding", "same", ...
"Weights", reshape([0.299, 0.587, 0.114], [1 1 3 1]), "Bias", 0)
- Simplify the data setup by using just the image datastore "imdsTrain" directly, rather than combining it with a separate label datastore and use "trainnet" with the simplified setup.
net = trainnet(imdsTrain, bClayers, "crossentropy", options);
- Add batchNormalizationLayer and dropoutLayer to improve training stability and reduce overfitting and ensure consistent data formatting between training and testing datasets.
Refer to the complete implementation in the attached code file, which also includes dummy image generation for testing.
Kindly refer to the following MATLAB documentation for more information:
- trainnet: https://www.mathworks.com/help/deeplearning/ref/trainnet.html
- convolution2dLayer: https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.convolution2dlayer.html
- Example for Simple Image Classification Network: https://www.mathworks.com/help/deeplearning/gs/create-simple-deep-learning-classification-network.html
- Example for Image Category Classification using Deep Learning: https://www.mathworks.com/help/vision/ug/image-category-classification-using-deep-learning.html
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!