Project Material

Project Proposal

CS766-Project Proposal.pdf

Project Mid-term report

766 Midterm Report.pdf

Project Code

load('object-detection-crowdai_training_label.mat');
training_index = 1:4000;
%training_index = 1:20;
crowdai_training_label.imageFilename = replace(crowdai_training_label.imageFilename,'\','/');
training_data = crowdai_training_label(training_index,:);
inputLayer = imageInputLayer([1200 1920 3]);
middleLayers = [
    convolution2dLayer([11 11], 96,'stride',4, 'Padding', 0, 'NumChannels',3 )
    reluLayer()
    crossChannelNormalizationLayer(5)
    maxPooling2dLayer(3,'stride',2, 'Padding',0)
    convolution2dLayer([3 3], 384,'stride',1, 'Padding', 1,'NumChannels',96)
    reluLayer()
    convolution2dLayer([3 3], 256,'stride',1, 'Padding', 1,'NumChannels',384)
    reluLayer()
    maxPooling2dLayer(3,'stride',2, 'Padding',0)
    ];
finalLayers = [

    fullyConnectedLayer(500)
    reluLayer()
    dropoutLayer(0.5)
    fullyConnectedLayer(100)
    reluLayer()
    dropoutLayer(0.5)
    fullyConnectedLayer(width(training_data))
      % Add the softmax loss layer and classification layer. 
      softmaxLayer()
      classificationLayer()
  ];
layers = [
    inputLayer
    middleLayers
    finalLayers
    ]
optionsStage1 = trainingOptions('sgdm', ...
    'MiniBatchSize', 50, ...
    'MaxEpochs', 5, ...
    'InitialLearnRate', 1e-3, ...
    'CheckpointPath', tempdir);
% Options for step 2
optionsStage2 = trainingOptions('sgdm', ...
    'MiniBatchSize', 50, ...
    'MaxEpochs', 5, ...
    'InitialLearnRate', 1e-3, ...
    'CheckpointPath', tempdir);
% Options for step 3.
optionsStage3 = trainingOptions('sgdm', ...
    'MiniBatchSize', 50, ...
    'MaxEpochs', 5, ...
    'InitialLearnRate', 1e-3, ...
    'CheckpointPath', tempdir);
% Options for step 4.
optionsStage4 = trainingOptions('sgdm', ...
    'MiniBatchSize', 50, ...
    'MaxEpochs', 5, ...
    'InitialLearnRate', 1e-3, ...
    'CheckpointPath', tempdir);
options = [
    optionsStage1
    optionsStage2
    optionsStage3
    optionsStage4
    ];
%%
tic
% A trained network is loaded from disk to save time when running the
% example. Set this flag to true to train the network. 

% Set random seed to ensure example training reproducibility.
rng(0);
  % Train Faster R-CNN detector. Select a BoxPyramidScale of 1.2 to allow
  % for finer resolution for multiscale object detection.
detector = trainFasterRCNNObjectDetector(training_data, layers, options, ...
  'NegativeOverlapRange', [0 0.3], ...
  'PositiveOverlapRange', [0.7 1], ...
  'SmallestImageDimension', 1920, ...
  'BoxPyramidScale', 1.2);

toc

save('crowdai_fasterRCNN_detector.mat','detector');
%%
testData = crowdai_training_label;
%% Read a test image.
I = imread(testData.imageFilename{3756});
% figure(1);
% imshow(I);
% Run the detector.
[bboxes,scores] = detect(detector,I);

%% Annotate detections in the image.
I = insertObjectAnnotation(I,'rectangle',bboxes,scores);
savefig(I);

%%
test_index = 5000:9000;
test_data = crowdai_training_label(test_index,:);

%%
disp('Processing...detector')
tic
% Run detector on each image in the test set and collect results.
resultsStruct = struct([]);
for i = 1:length(test_index)
    % Read the image.
    I = imread(test_data.imageFilename{i});

    % Run the detector.
    [bboxes, scores, labels] = detect(detector, I);

    % Collect the results.
    resultsStruct(i).Boxes = bboxes;
    resultsStruct(i).Scores = scores;
    resultsStruct(i).Labels = labels;
end

% Convert the results into a table.
results = struct2table(resultsStruct);

% Extract expected bounding box locations from test data.
expectedResults = test_data(:, 2:end);

% Evaluate the object detector using Average Precision metric.
[ap, recall, precision] = evaluateDetectionPrecision(results, expectedResults);
toc
%
% Plot precision/recall curve
h = figure;
plot(recall,precision)
xlabel('Recall')
ylabel('Precision')
grid on
title(sprintf('Average Precision = %.2f', ap))
save(h,'PR curve','jpg');
disp('end');