clc
clearvars
clear
close all
Newlayers = [
imageInputLayer([32 32 3],"Name","imageinput")
convolution2dLayer([5 5],32,"Name","conv","BiasLearnRateFactor",2,"Padding",[2 2 2 2],"WeightsInitializer","narrow-normal")
maxPooling2dLayer([3 3],"Name","maxpool","Stride",[2 2])
reluLayer("Name","relu")
convolution2dLayer([5 5],32,"Name","conv_1","BiasLearnRateFactor",2,"Padding",[2 2 2 2],"WeightsInitializer","narrow-normal")
reluLayer("Name","relu_1")
averagePooling2dLayer([3 3],"Name","avgpool","Stride",[2 2])
convolution2dLayer([5 5],64,"Name","conv_2","BiasLearnRateFactor",2,"Padding",[2 2 2 2],"WeightsInitializer","narrow-normal")
reluLayer("Name","relu_2")
averagePooling2dLayer([3 3],"Name","avgpool_1","Stride",[2 2])
fullyConnectedLayer(64,"Name","fc","BiasLearnRateFactor",2,"WeightsInitializer","narrow-normal")
reluLayer("Name","relu_3")
fullyConnectedLayer(2,"Name","fc_rcnn","BiasL2Factor",1,"BiasLearnRateFactor",5,"WeightLearnRateFactor",8,"WeightsInitializer","narrow-normal")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")]
load('Wgtruth.mat');
save gun.mat Wgtruth Newlayers
load('gun.mat', 'Wgtruth', 'Newlayers')
imDir = fullfile(matlabroot, 'Wgtruth')
addpath(imDir)
options = trainingOptions('sgdm','MiniBatchSize', 10,'InitialLearnRate', 1e-4,'MaxEpochs', 67)
rcnn = trainRCNNObjectDetector(Wgtruth, Newlayers, options, 'NegativeOverlapRange', [0 0.3])
img = imread('14.jpg');
[bbox, score, label] = detect(rcnn, img, 'MiniBatchSize', 10)
[score, idx] = max(score)
bbox = bbox(idx, :)
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);
detectedImg = insertObjectAnnotation(img, 'rectangle', bbox, annotation);
figure
imshow(detectedImg);