YOLO object detector error
6 次查看(过去 30 天)
显示 更早的评论
i have 1388 images with the size 646x618x3. i read that i have to resize them to 512x512x3 to rain a YOLO object detector. after i resized them i labeled the ROI rectangles with imageLabeler and i exported them as the table gTruth. the table contained all images so i createdthe variable labeledData = gTruth(1:217, :) to only have all images with their corresponding bounding boxes.
here is a hyperlink to the Database . the files are too large to insert any, Sorry for the inconvenience.
i attach ere the code
labeledData = gTruth(1:217, :);
% Split the data into training and validation sets
rng(0); % For reproducibility
shuffledIndices = randperm(height(labeledData));
numTrain = floor(0.8 * height(labeledData));
trainingData = labeledData(shuffledIndices(1:numTrain), :);
validationData = labeledData(shuffledIndices(numTrain+1:end), :);
% Training datastores
imdsTrain = imageDatastore(trainingData{:, 1});
bldsTrain = boxLabelDatastore(trainingData(:, 2));
trainingDatastore = combine(imdsTrain, bldsTrain);
% Validation datastores
imdsVal = imageDatastore(validationData{:, 1});
bldsVal = boxLabelDatastore(validationData(:, 2));
validationDatastore = combine(imdsVal, bldsVal);
% Load a pretrained feature extractor.
featureExtractionNetwork = resnet50;
featureLayer = 'activation_40_relu'; % Feature layer from ResNet-50
% Define the number of object classes to detect.
numClasses = 1; % Update this if you have more than one object class
% Define the image input size for the network. This should match your dataset.
inputSize = [512 512 3]; % You may need to adjust this based on your dataset
% Create an anchor box configuration. This requires some experimentation and
% might depend on the common sizes of the objects in your dataset.
anchorBoxes = [165 379;229 381; 142 292; 189 383; 143 379]; % Example sizes, adjust based on your dataset
% Create the YOLOv2 object detection network.
lgraph = yolov2Layers(inputSize, numClasses, anchorBoxes, featureExtractionNetwork, featureLayer);
% Assuming 'vas' as the class for all bounding boxes
trainingDataTbl = table(imdsTrain.Files, bldsTrain.LabelData);
trainingDataTbl.Properties
% Convert the bounding boxes to the required format [x, y, width, height, classLabel]
% Here, we assume all objects belong to a single class named 'vas'.
% Adjust this code if your class labels are stored differently.
% numTrainingSamples = size(trainingDataTbl, 1);
% for i = 1:numTrainingSamples
% bboxData = trainingDataTbl.Var2{i}; % Original bounding boxes
% classLabels = repmat("vas", size(bboxData, 1), 1); % Creating class labels
% trainingDataTbl.Var2{i} = [bboxData, classLabels]; % Appending class labels to bounding boxes
% end
% Assuming 'vas' as the class for all bounding boxes, and it's the single class
numTrainingSamples = size(trainingDataTbl, 1);
for i = 1:numTrainingSamples
bboxData = trainingDataTbl.Var2{i}; % Original bounding boxes
% Ensure class labels are strings
classLabels = repmat("vas", size(bboxData, 1), 1); % Creating class labels as strings
% Combine bounding boxes and class labels
combinedData = [bboxData, classLabels];
% Convert combined data to a cell array if it's not already
if ~iscell(combinedData)
combinedData = num2cell(combinedData, 2);
% For each bounding box, ensure the class label is a string (not categorical)
for j = 1:size(combinedData, 1)
combinedData{j, end} = string(combinedData{j, end});
end
end
trainingDataTbl.Var2{i} = combinedData; % Update the table with corrected data
end
% Define training options without ValidationData
options = trainingOptions('sgdm', ...
'MiniBatchSize', 4, ...
'InitialLearnRate', 1e-3, ...
'MaxEpochs', 30, ...
'Shuffle', 'every-epoch', ...
'Verbose', true, ...
'VerboseFrequency', 1, ...
'Plots', 'training-progress');
% Train the YOLOv2 object detector without direct validation data
[detector, info] = trainYOLOv2ObjectDetector(trainingDataTbl, lgraph, options);
the resizing code is here
% Define the source and target directories
sourceDir = 'D:\baza de date\IMAGES';
resizedDir = 'D:\baza de date\RESIZED';
% Create the target directory if it doesn't exist
if ~exist(resizedDir, 'dir')
mkdir(resizedDir);
end
% Get a list of all files in the source directory
imageFiles = dir(fullfile(sourceDir, '*.tiff')); % Adjust the extension if necessary
% Loop over each file in the directory
for i = 1:length(imageFiles)
% Full path to the current image
currentImagePath = fullfile(imageFiles(i).folder, imageFiles(i).name);
% Read the current image
img = imread(currentImagePath);
% Resize the image to 512x512
resizedImg = imresize(img, [512 512]);
% Define the path for the resized image
resizedImagePath = fullfile(resizedDir, imageFiles(i).name);
% Save the resized image
imwrite(resizedImg, resizedImagePath);
end
disp('Resizing completed.');
here are some bounding boxes
'D:\baza de date\RESIZED\clin_0001_L.tiff' [61,127,385,191]
'D:\baza de date\RESIZED\clin_0001_R.tiff' [58,121,389,195]
'D:\baza de date\RESIZED\clin_0002_L.tiff' [63,184,380,138]
'D:\baza de date\RESIZED\clin_0002_R.tiff' [64,186,378,131]
'D:\baza de date\RESIZED\clin_0003_L.tiff' [63,215,382,163]
'D:\baza de date\RESIZED\clin_0003_R.tiff' [102,121,310,163]
'D:\baza de date\RESIZED\clin_0004_L.tiff' [107,166,290,144]
'D:\baza de date\RESIZED\clin_0004_R.tiff' [107,176,290,118]
'D:\baza de date\RESIZED\clin_0005_L.tiff' [109,173,288,148]
'D:\baza de date\RESIZED\clin_0005_R.tiff' [108,143,288,146]
'D:\baza de date\RESIZED\clin_0006_L.tiff' [109,130,289,117]
'D:\baza de date\RESIZED\clin_0006_R.tiff' [63,139,379,145]
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!