Is it possible to use a pretrained YOLO DL NW to train new data without changing its layers?

2 views (last 30 days)
Hi all,
I was following the example from https://www.mathworks.com/help/vision/ref/trainyolov4objectdetector.html#mw_c9123667-38e8-4d10-959c-35af840d92d0 that teaches how to use a pretrained YOLOv4 DL NW to train it for detection of new objects.
Following the instructions from MATLAB documentation, I currently have the below code, that basically changes the dataset used to train the pretrained loaded YOLOv4 NW.
My goal is to train a DL network to detect LED matrices automatically. So, what I would like to know is: It is ok to use the pretrained NW to detect new objects from my own dataset ?
If yes, should I change a specific layer or training option?
Regards.
detector = yolov4ObjectDetector("tiny-yolov4-coco");
data = load("LEDMatrixData.mat");
trainingData = data.LEDMatrixData;
imds = imageDatastore('C:\Users\Fortu\OneDrive\Área de Trabalho\OCC\OCC_codes\Movie Frames from AMOSTRAS_4_ARDUINO_P4_CL_BRILHO_0');
blds = boxLabelDatastore(trainingData(:,2:end));
ds = combine(imds,blds);
inputSize = [224 224 3];
trainingDataForEstimation = transform(ds,@(data)preprocessData(data,inputSize));
numAnchors = 4;
[anchors, meanIoU] = estimateAnchorBoxes(trainingDataForEstimation,numAnchors);
area = anchors(:,1).*anchors(:,2);
[~,idx] = sort(area,"descend");
anchors = anchors(idx,:);
anchorBoxes = {anchors(1:3,:);anchors(4:6,:)};
classes = {'LEDMatrix'};
detector = yolov4ObjectDetector("tiny-yolov4-coco",classes,anchorBoxes,InputSize=inputSize);
options = trainingOptions("sgdm", ...
InitialLearnRate=0.001, ...
MiniBatchSize=16,...
MaxEpochs=40, ...
BatchNormalizationStatistics="moving",...
ResetInputNormalization=false,...
VerboseFrequency=30);
trainedDetector = trainYOLOv4ObjectDetector(ds,detector,options)
%-------------------------------------------------------------------------------------------------%
function data = preprocessData(data,targetSize)
for num = 1:size(data,1)
I = data{num,1};
imgSize = size(I);
bboxes = data{num,2};
I = im2single(imresize(I,targetSize(1:2)));
scale = targetSize(1:2)./imgSize(1:2);
bboxes = bboxresize(bboxes,scale);
data(num,1:2) = {I,bboxes};
end
end

Accepted Answer

Varun Sai Alaparthi
Varun Sai Alaparthi on 17 Nov 2022
Hello Matheus,
I understand that you want to know whether it’s possible to use pre-trained ‘YOLOv4’ for new dataset to predict LED matrices.
Yes, it’s okay to train pre trained ‘YOLOv4’ on new dataset to predict new objects. It uses its prior gradients and can help get good accuracy quickly on new datasets.
Changing any layer is not required as the input size and classes options are already given, this would adjust with the layers accordingly and some training options can be fine-tuned if required based on your dataset.
I hope this information helps and please reach out for any further issues.
Sincerely
Varun
  1 Comment
Matheus Fortunato Alves
Matheus Fortunato Alves on 19 Nov 2022
Hi Varun,
It helped a lot, I am still learning to work with Deep Learning and I was not sure if I had to do transfer learning to train a new data set (of course I can, but I tought it was necessary).
Thanks!!
Regards,
Matheus

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!