detect() using trained yolov3detector gives error on transformed test datastore

3 次查看(过去 30 天)
Hello!
I trained a YOLO v3 network on MATLAB 2022a according to instructions (https://www.mathworks.com/help/vision/ug/object-detection-using-yolo-v3-deep-learning.html), with the network input size [384 384 3]. My data (training, test) are all single color in uint8, of size [384 384 1], and I used preprocessData() (also on the instructions page) to transform the sizes of the combined datastores (of imds and blds) into [384 384 3].
The training went well. But when I tried to use detect() on the transformed test datastore testData:
results = detect(yolov3Detector, testData,'MiniBatchSize',32);
[ap, recall, precision] = ...
evaluateDetectionPrecision(results, testData);
I got the following error:
Error using vision.internal.cnn.validation.checkDetectionInputImage
Invalid input image channel size: 1.
The input image channel size (1) must be the same as the network's input channel
size (3).
Error in yolov3ObjectDetector/parseDetectInputs (line 667)
[sz,params.DetectionInputWasBatchOfImages] = vision.internal.cnn.validation.checkDetectionInputImage(...
Error in yolov3ObjectDetector/detect (line 622)
params = parseDetectInputs(detector,I,varargin{:});
testData is as follows: the transformation appears to be in place. detect() should be able to work with transformed datastores, according to Help. However, it seems like checkDetectionInputImage isn't picking up the single-channel to three-channel transformation?
testData =
TransformedDatastore with properties:
UnderlyingDatastores: {matlab.io.datastore.CombinedDatastore}
SupportedOutputFormats: ["txt" "csv" "xlsx" "xls" ]
Transforms: {@(data)preprocessData(data,networkInputSize)}
IncludeInfo: 0
May I know how to solve this problem? Also, is there a way to train a single-channel yolov3 detector based on the instructions? That is really my preference. Any help is very much appreciated!
  1 个评论
Vivek Akkala
Vivek Akkala 2022-10-7
yolov3ObjectDetector should train any input dimension by preserving the channel size. Can you share the "InputSize" property of trained detector?

请先登录,再进行评论。

回答(1 个)

prabhat kumar sharma
Hi JW,
It looks like the error you're encountering indicates that the detect function is not recognizing the transformed images as having three channels. This is likely due to how the transformation is applied within the TransformedDatastore.
you can ensure that the preprocessData function correctly converts single-channel images to three-channel images. Here’s a typical implementation for the preprocessData function.
function data = preprocessData(data, inputSize)
data{1} = imresize(data{1}, inputSize(1:2));
% Convert grayscale images to RGB by replicating the single channel
if size(data{1}, 3) == 1
data{1} = cat(3, data{1}, data{1}, data{1});
end
% Resize the bounding boxes
scale = inputSize(1:2) ./ size(data{1}, 1:2);
data{2} = bboxresize(data{2}, scale);
end
Additonally you can change the inputSize in the object of detector only.
You can follow this link to update the input layer size:
You can see the below psuedocode for reference.
net = yolov3ObjectDetector('tiny-yolov3-coco');
lgraph = layerGraph(net.Layers);
inputLayer = imageInputLayer([384 384 1], 'Name', 'input', 'Normalization', 'none');
lgraph = replaceLayer(lgraph, 'input', inputLayer);
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.001, ...
'MaxEpochs', 30, ...
'MiniBatchSize', 16, ...
'Shuffle', 'every-epoch', ...
'Verbose', true, ...
'Plots', 'training-progress');
% Assuming trainingData is your combined datastore
yolov3Detector = trainYOLOv3ObjectDetector(trainingData, lgraph, options);
I hope it helps!

标签

Community Treasure Hunt

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

Start Hunting!

Translated by