-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatlab2.m
222 lines (218 loc) · 6.38 KB
/
matlab2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
doTrainingAndEval = true;
load('myResizedData.mat')
trainingData = myResizedData.TrainData;
testData = myResizedData.TestData;
trainingData = trainingData(1:200,:);
testData = testData(20:50,:);
%
%
%
% % Load a pretrained ResNet-50.
% net = resnet50;
% lgraph = layerGraph(net);
%
% % Remove the the last 3 layers.
% layersToRemove = {
% 'fc1000'
% 'fc1000_softmax'
% 'ClassificationLayer_fc1000'
% };
% lgraph = removeLayers(lgraph, layersToRemove);
%
% % Specify the number of classes the network should classify.
% numClasses = 1;
% numClassesPlusBackground = numClasses + 1;
%
% % Define new classification layers.
% newLayers = [
% fullyConnectedLayer(numClassesPlusBackground, 'Name', 'rcnnFC')
% softmaxLayer('Name', 'rcnnSoftmax')
% classificationLayer('Name', 'rcnnClassification')
% ];
%
% % Add new object classification layers.
% lgraph = addLayers(lgraph, newLayers);
%
% % Connect the new layers to the network.
% lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnFC');
%
% % Define the number of outputs of the fully connected layer.
% numOutputs = 4 * numClasses;
%
% % Create the box regression layers.
% boxRegressionLayers = [
% fullyConnectedLayer(numOutputs,'Name','rcnnBoxFC')
% rcnnBoxRegressionLayer('Name','rcnnBoxDeltas')
% ];
%
% % Add the layers to the network.
% lgraph = addLayers(lgraph, boxRegressionLayers);
%
% % Connect the regression layers to the layer named 'avg_pool'.
% lgraph = connectLayers(lgraph,'avg_pool','rcnnBoxFC');
%
% % Select a feature extraction layer.
% featureExtractionLayer = 'activation_40_relu';
%
% % Disconnect the layers attached to the selected feature extraction layer.
% lgraph = disconnectLayers(lgraph, featureExtractionLayer,'res5a_branch2a');
% lgraph = disconnectLayers(lgraph, featureExtractionLayer,'res5a_branch1');
%
% % Add ROI max pooling layer.
% outputSize = [14 14];
% roiPool = roiMaxPooling2dLayer(outputSize,'Name','roiPool');
% lgraph = addLayers(lgraph, roiPool);
%
% % Connect feature extraction layer to ROI max pooling layer.
% lgraph = connectLayers(lgraph, featureExtractionLayer,'roiPool/in');
%
% % Connect the output of ROI max pool to the disconnected layers from above.
% lgraph = connectLayers(lgraph, 'roiPool','res5a_branch2a');
% lgraph = connectLayers(lgraph, 'roiPool','res5a_branch1');
%
% % Define anchor boxes.
% anchorBoxes = [
% 16 16
% 32 16
% ];
%
% % Create the region proposal layer.
% proposalLayer = regionProposalLayer(anchorBoxes,'Name','regionProposal');
%
% lgraph = addLayers(lgraph, proposalLayer);
%
% % Number of anchor boxes.
% numAnchors = size(anchorBoxes,1);
%
% % Number of feature maps in coming out of the feature extraction layer.
% numFilters = 1024;
%
% rpnLayers = [
% convolution2dLayer(3, numFilters,'padding',[1 1],'Name','rpnConv3x3')
% reluLayer('Name','rpnRelu')
% ];
%
% lgraph = addLayers(lgraph, rpnLayers);
%
% % Connect to RPN to feature extraction layer.
% lgraph = connectLayers(lgraph, featureExtractionLayer, 'rpnConv3x3');
%
% % Add RPN classification layers.
% rpnClsLayers = [
% convolution2dLayer(1, numAnchors*2,'Name', 'rpnConv1x1ClsScores')
% rpnSoftmaxLayer('Name', 'rpnSoftmax')
% rpnClassificationLayer('Name','rpnClassification')
% ];
% lgraph = addLayers(lgraph, rpnClsLayers);
%
% % Connect the classification layers to the RPN network.
% lgraph = connectLayers(lgraph, 'rpnRelu', 'rpnConv1x1ClsScores');
%
% % Add RPN regression layers.
% rpnRegLayers = [
% convolution2dLayer(1, numAnchors*4, 'Name', 'rpnConv1x1BoxDeltas')
% rcnnBoxRegressionLayer('Name', 'rpnBoxDeltas');
% ];
%
% lgraph = addLayers(lgraph, rpnRegLayers);
%
% % Connect the regression layers to the RPN network.
% lgraph = connectLayers(lgraph, 'rpnRelu', 'rpnConv1x1BoxDeltas');
%
% % Connect region proposal network.
% lgraph = connectLayers(lgraph, 'rpnConv1x1ClsScores', 'regionProposal/scores');
% lgraph = connectLayers(lgraph, 'rpnConv1x1BoxDeltas', 'regionProposal/boxDeltas');
%
% % Connect region proposal layer to roi pooling.
% lgraph = connectLayers(lgraph, 'regionProposal', 'roiPool/roi');
%
% % Show the network after adding the RPN layers.
% figure
% plot(lgraph)
% ylim([30 42])
%
%
%
% optionsStage1 = trainingOptions('sgdm', ...
% 'MaxEpochs', 10, ...
% 'MiniBatchSize', 1, ...
% 'InitialLearnRate', 1e-3);
%
% optionsStage2 = trainingOptions('sgdm', ...
% 'MaxEpochs', 10, ...
% 'MiniBatchSize', 1, ...
% 'InitialLearnRate', 1e-3);
%
% optionsStage3 = trainingOptions('sgdm', ...
% 'MaxEpochs', 10, ...
% 'MiniBatchSize', 1, ...
% 'InitialLearnRate', 1e-3);
%
% optionsStage4 = trainingOptions('sgdm', ...
% 'MaxEpochs', 10, ...
% 'MiniBatchSize', 1, ...
% 'InitialLearnRate', 1e-3);
%
% options = [
% optionsStage1
% optionsStage2
% optionsStage3
% optionsStage4
% ];
%
% if doTrainingAndEval
% rng(0);
% detector = trainFasterRCNNObjectDetector(trainingData, lgraph, options, ...
% 'NegativeOverlapRange', [0 0.3], ...
% 'PositiveOverlapRange', [0.6 1], ...
% 'NumRegionsToSample', [256 128 256 128]);
% else
% % detector = data.detector;
% end
%
% save detector
% Test on one image
I = imread(testData.imageFilename{1});
% I = imread('IraniData/X.jpg');
% I = imresize(I,[300, NaN]);
[bboxes, scores] = detect(detector, I);
if bboxes
I = insertObjectAnnotation(I, 'rectangle', bboxes, scores);
figure
imshow(I)
end
%
% % Evaluate Detector Using Test Set
% if doTrainingAndEval
% resultsStruct = struct([]);
% for i = 1:height(testData)
% I = imread(testData.imageFilename{i});
% [bboxes, scores, labels] = detect(detector, I);
% resultsStruct(i).Boxes = bboxes;
% resultsStruct(i).Scores = scores;
% resultsStruct(i).Labels = labels;
%
% % disp (i)
% % disp(bboxes)
% % if bboxes
% % disp (i)
% % disp(bboxes)
% % I = insertObjectAnnotation(I, 'rectangle', bboxes, scores);
% % figure
% % imshow(I)
% % end
% end
% results = struct2table(resultsStruct);
% else
% results = data.results;
% end
%
% expectedResults = testData(:, 2:end);
% [ap, recall, precision] = evaluateDetectionPrecision(results, expectedResults);
%
% figure
% plot(recall, precision)
% xlabel('Recall')
% ylabel('Precision')
% grid on
% title(sprintf('Average Precision = %.2f', ap))