【发布时间】:2021-11-02 16:21:11
【问题描述】:
我正在尝试使用 SSD Resnet50 对分辨率为 640x360 的数据集(图像)进行迁移学习,其中一个类作为输出。我按照 Matlab 的例子进行车辆检测。 https://www.mathworks.com/help/vision/ug/object-detection-using-single-shot-detector.html 我将网络输入大小设置为 [300 300] 并保留相同的训练选项。 然而,当训练开始时,在第一次迭代中,小批量损失和验证损失都属于 NAN。 根据这个论坛上的建议和答案,我首先降低学习率,并测试了几个值 1e-1、1e-3、1e-5、1e-15,我还将 VerboseFrequency 更改为 50、10 和 1,但我得到了相同的错误(小批量损失和验证损失归 NAN)。 我还尝试用较低的值初始化第一个 conv 层的权重和偏差,但是,我得到了同样的错误。
conv01 = convolution2dLayer([7,7],64,'Stride',2,'Padding',[3,3,3,3],'BiasLearnRateFactor',1,'name','conv1');
conv01.Weights = gpuArray(single(randn([7 7 3 64])*1e-15));
conv01.Bias = gpuArray(single(randn([1 1 64])*0.00001+1));
我尝试运行车辆检测示例,它运行良好,所以我仔细检查了我的数据,我的数据集中的图像是 8 位的 jpg 格式,与车辆数据集中的一样。 我想我在这里遗漏了一些东西。我附上了脚本以及显示下面 Nan 的输出屏幕截图。 非常感谢任何帮助。
addpath('C:\dataset');
%%
%Load the pedestrian ground truth data.
data = load('labelling640360.mat');
gTruth = data.gTruth;
pedestriandataset=[gTruth.DataSource.Source data.gTruth.LabelData];
pedestriandataset.Properties.VariableNames([1])={'imageFilename'};
pedestriandataset(1:4,:)
summary(pedestriandataset)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Split the data
%Use 60% of the data for training set and the rest for the test set.
rng(0);
shuffledIndices = randperm(height(pedestriandataset));
idx = floor(0.6 * length(shuffledIndices));
trainingDataTbl = pedestriandataset(shuffledIndices(1:idx), :);
testDataTbl = pedestriandataset(shuffledIndices(idx+1:end), :);
%Create an image datastore for loading the images.
imdsTrain = imageDatastore(trainingDataTbl.imageFilename);
imdsTest = imageDatastore(testDataTbl.imageFilename);
% Create a datastore for the ground truth bounding boxes.
bldsTrain = boxLabelDatastore(trainingDataTbl(:, 2:end));
bldsTest = boxLabelDatastore(testDataTbl(:, 2:end));
% Combine the image and box label datastores.
trainingData = combine(imdsTrain, bldsTrain);
testData = combine(imdsTest, bldsTest);
%%
%%%%%%%%%%%%%%%%%%%%%% SSD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
inputSize = [300 300 3];
%Define number of object classes to detect.
numClasses = width(pedestriandataset)-1;
%Create the SSD object detection network.
lgraph = ssdLayers(inputSize, numClasses, 'resnet50'); %'vgg16'
analyzeNetwork(lgraph);
% plot(lgraph)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
augmentedTrainingData = transform(trainingData,@augmentData);
augmentedData = cell(4,1);
for k = 1:4
data = read(augmentedTrainingData);
augmentedData{k} = insertShape(data{1},'Rectangle',data{2});
reset(augmentedTrainingData);
end
figure
montage(augmentedData,'BorderSize',10)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preprocess the augmented training data to prepare for training
preprocessedTrainingData = transform(augmentedTrainingData,@(data)preprocessData(data,inputSize));
% Read the preprocessed training data.
data = read(preprocessedTrainingData);
%Display the image and bounding boxes.
I = data{1};
bbox = data{2};
annotatedImage = insertShape(I,'Rectangle',bbox);
annotatedImage = imresize(annotatedImage,2);
figure
imshow(annotatedImage)
%%
%%%%%%%%%%%%%%%%%%%%%% Train SSD Object Detector %%%%%%%%%%%%%%%%%%%
options = trainingOptions('sgdm', 'MiniBatchSize', 16, ....
'InitialLearnRate',1e-1, 'LearnRateSchedule', 'piecewise', ...
'LearnRateDropPeriod', 30, 'LearnRateDropFactor', 0.8, ...
'MaxEpochs', 300, 'VerboseFrequency', 50, ...
'CheckpointPath', tempdir, 'Shuffle','every-epoch'); %'ExecutionEnvironment','cpu'
[detector, info] = trainSSDObjectDetector(preprocessedTrainingData,lgraph,options);
【问题讨论】:
标签: matlab deep-learning object-detection nan transfer-learning