Skip to content

Commit 929d39f

Browse files
committed
Merge pull request opencv#11728 from dkurt:dnn_update_tf_ssd
2 parents f72633b + 40765c5 commit 929d39f

File tree

4 files changed

+76
-45
lines changed

4 files changed

+76
-45
lines changed

Diff for: modules/dnn/perf/perf_net.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,19 @@ PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_Caffe)
158158
Mat(cv::Size(300, 300), CV_32FC3));
159159
}
160160

161-
// TODO: update MobileNet model.
162-
PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_TensorFlow)
161+
PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)
163162
{
164-
if (backend == DNN_BACKEND_HALIDE ||
165-
backend == DNN_BACKEND_INFERENCE_ENGINE)
163+
if (backend == DNN_BACKEND_HALIDE)
164+
throw SkipTestException("");
165+
processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "ssd_mobilenet_v1_coco_2017_11_17.pbtxt", "",
166+
Mat(cv::Size(300, 300), CV_32FC3));
167+
}
168+
169+
PERF_TEST_P_(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)
170+
{
171+
if (backend == DNN_BACKEND_HALIDE)
166172
throw SkipTestException("");
167-
processNet("dnn/ssd_mobilenet_v1_coco.pb", "ssd_mobilenet_v1_coco.pbtxt", "",
173+
processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "ssd_mobilenet_v2_coco_2018_03_29.pbtxt", "",
168174
Mat(cv::Size(300, 300), CV_32FC3));
169175
}
170176

@@ -217,9 +223,7 @@ PERF_TEST_P_(DNNTestNetwork, opencv_face_detector)
217223

218224
PERF_TEST_P_(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
219225
{
220-
if (backend == DNN_BACKEND_HALIDE ||
221-
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL) ||
222-
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16))
226+
if (backend == DNN_BACKEND_HALIDE)
223227
throw SkipTestException("");
224228
processNet("dnn/ssd_inception_v2_coco_2017_11_17.pb", "ssd_inception_v2_coco_2017_11_17.pbtxt", "",
225229
Mat(cv::Size(300, 300), CV_32FC3));

Diff for: modules/dnn/test/test_backends.cpp

+24-15
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DNNTestNetwork : public TestWithParam <tuple<DNNBackend, DNNTarget> >
3838
void processNet(std::string weights, std::string proto,
3939
Mat inp, const std::string& outputLayer = "",
4040
std::string halideScheduler = "",
41-
double l1 = 0.0, double lInf = 0.0)
41+
double l1 = 0.0, double lInf = 0.0, double detectionConfThresh = 0.2)
4242
{
4343
if (backend == DNN_BACKEND_OPENCV && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16))
4444
{
@@ -87,7 +87,7 @@ class DNNTestNetwork : public TestWithParam <tuple<DNNBackend, DNNTarget> >
8787
}
8888
Mat out = net.forward(outputLayer).clone();
8989

90-
check(outDefault, out, outputLayer, l1, lInf, "First run");
90+
check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "First run");
9191

9292
// Test 2: change input.
9393
float* inpData = (float*)inp.data;
@@ -101,10 +101,11 @@ class DNNTestNetwork : public TestWithParam <tuple<DNNBackend, DNNTarget> >
101101
net.setInput(inp);
102102
outDefault = netDefault.forward(outputLayer).clone();
103103
out = net.forward(outputLayer).clone();
104-
check(outDefault, out, outputLayer, l1, lInf, "Second run");
104+
check(outDefault, out, outputLayer, l1, lInf, detectionConfThresh, "Second run");
105105
}
106106

107-
void check(Mat& ref, Mat& out, const std::string& outputLayer, double l1, double lInf, const char* msg)
107+
void check(Mat& ref, Mat& out, const std::string& outputLayer, double l1, double lInf,
108+
double detectionConfThresh, const char* msg)
108109
{
109110
if (outputLayer == "detection_out")
110111
{
@@ -119,7 +120,7 @@ class DNNTestNetwork : public TestWithParam <tuple<DNNBackend, DNNTarget> >
119120
}
120121
out = out.rowRange(0, numDetections);
121122
}
122-
normAssertDetections(ref, out, msg, 0.2, l1, lInf);
123+
normAssertDetections(ref, out, msg, detectionConfThresh, l1, lInf);
123124
}
124125
else
125126
normAssert(ref, out, msg, l1, lInf);
@@ -188,20 +189,30 @@ TEST_P(DNNTestNetwork, MobileNet_SSD_Caffe)
188189
inp, "detection_out", "", l1, lInf);
189190
}
190191

191-
// TODO: update MobileNet model.
192-
TEST_P(DNNTestNetwork, MobileNet_SSD_TensorFlow)
192+
TEST_P(DNNTestNetwork, MobileNet_SSD_v1_TensorFlow)
193193
{
194-
if (backend == DNN_BACKEND_HALIDE ||
195-
backend == DNN_BACKEND_INFERENCE_ENGINE)
194+
if (backend == DNN_BACKEND_HALIDE)
196195
throw SkipTestException("");
197196
Mat sample = imread(findDataFile("dnn/street.png", false));
198197
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
199-
float l1 = (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ? 0.008 : 0.0;
200-
float lInf = (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) ? 0.06 : 0.0;
201-
processNet("dnn/ssd_mobilenet_v1_coco.pb", "dnn/ssd_mobilenet_v1_coco.pbtxt",
198+
float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : 0.0;
199+
float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.06 : 0.0;
200+
processNet("dnn/ssd_mobilenet_v1_coco_2017_11_17.pb", "dnn/ssd_mobilenet_v1_coco_2017_11_17.pbtxt",
202201
inp, "detection_out", "", l1, lInf);
203202
}
204203

204+
TEST_P(DNNTestNetwork, MobileNet_SSD_v2_TensorFlow)
205+
{
206+
if (backend == DNN_BACKEND_HALIDE)
207+
throw SkipTestException("");
208+
Mat sample = imread(findDataFile("dnn/street.png", false));
209+
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);
210+
float l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.011 : 0.0;
211+
float lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.06 : 0.0;
212+
processNet("dnn/ssd_mobilenet_v2_coco_2018_03_29.pb", "dnn/ssd_mobilenet_v2_coco_2018_03_29.pbtxt",
213+
inp, "detection_out", "", l1, lInf, 0.25);
214+
}
215+
205216
TEST_P(DNNTestNetwork, SSD_VGG16)
206217
{
207218
if (backend == DNN_BACKEND_HALIDE && target == DNN_TARGET_CPU)
@@ -265,9 +276,7 @@ TEST_P(DNNTestNetwork, opencv_face_detector)
265276

266277
TEST_P(DNNTestNetwork, Inception_v2_SSD_TensorFlow)
267278
{
268-
if (backend == DNN_BACKEND_HALIDE ||
269-
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL) ||
270-
(backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL_FP16))
279+
if (backend == DNN_BACKEND_HALIDE)
271280
throw SkipTestException("");
272281
Mat sample = imread(findDataFile("dnn/street.png", false));
273282
Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false);

Diff for: modules/dnn/test/test_layers.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ TEST_P(Layer_Test_DWconv_Prelu, Accuracy)
877877
int shape[] = {1, num_input, 16, 16};
878878
Mat in_blob(4, &shape[0], CV_32FC1, Scalar(1));
879879

880+
net.setPreferableBackend(DNN_BACKEND_OPENCV);
880881
net.setInput(in_blob);
881882
Mat out = net.forward();
882883

Diff for: samples/dnn/tf_text_graph_ssd.py

+39-22
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,40 @@ def removeIdentity():
160160
# Create SSD postprocessing head ###############################################
161161

162162
# Concatenate predictions of classes, predictions of bounding boxes and proposals.
163+
def tensorMsg(values):
164+
if all([isinstance(v, float) for v in values]):
165+
dtype = 'DT_FLOAT'
166+
field = 'float_val'
167+
elif all([isinstance(v, int) for v in values]):
168+
dtype = 'DT_INT32'
169+
field = 'int_val'
170+
else:
171+
raise Exception('Wrong values types')
163172

164-
concatAxis = NodeDef()
165-
concatAxis.name = 'concat/axis_flatten'
166-
concatAxis.op = 'Const'
167-
text_format.Merge(
168-
'tensor {'
169-
' dtype: DT_INT32'
170-
' tensor_shape { }'
171-
' int_val: -1'
172-
'}', concatAxis.attr["value"])
173-
graph_def.node.extend([concatAxis])
174-
175-
def addConcatNode(name, inputs):
173+
msg = 'tensor { dtype: ' + dtype + ' tensor_shape { dim { size: %d } }' % len(values)
174+
for value in values:
175+
msg += '%s: %s ' % (field, str(value))
176+
return msg + '}'
177+
178+
def addConstNode(name, values):
179+
node = NodeDef()
180+
node.name = name
181+
node.op = 'Const'
182+
text_format.Merge(tensorMsg(values), node.attr["value"])
183+
graph_def.node.extend([node])
184+
185+
def addConcatNode(name, inputs, axisNodeName):
176186
concat = NodeDef()
177187
concat.name = name
178188
concat.op = 'ConcatV2'
179189
for inp in inputs:
180190
concat.input.append(inp)
181-
concat.input.append(concatAxis.name)
191+
concat.input.append(axisNodeName)
182192
graph_def.node.extend([concat])
183193

194+
addConstNode('concat/axis_flatten', [-1])
195+
addConstNode('PriorBox/concat/axis', [-2])
196+
184197
for label in ['ClassPredictor', 'BoxEncodingPredictor']:
185198
concatInputs = []
186199
for i in range(args.num_layers):
@@ -193,19 +206,14 @@ def addConcatNode(name, inputs):
193206

194207
concatInputs.append(flatten.name)
195208
graph_def.node.extend([flatten])
196-
addConcatNode('%s/concat' % label, concatInputs)
209+
addConcatNode('%s/concat' % label, concatInputs, 'concat/axis_flatten')
197210

198211
# Add layers that generate anchors (bounding boxes proposals).
199212
scales = [args.min_scale + (args.max_scale - args.min_scale) * i / (args.num_layers - 1)
200213
for i in range(args.num_layers)] + [1.0]
201214

202-
def tensorMsg(values):
203-
msg = 'tensor { dtype: DT_FLOAT tensor_shape { dim { size: %d } }' % len(values)
204-
for value in values:
205-
msg += 'float_val: %f ' % value
206-
return msg + '}'
207-
208215
priorBoxes = []
216+
addConstNode('reshape_prior_boxes_to_4d', [1, 2, -1, 1])
209217
for i in range(args.num_layers):
210218
priorBox = NodeDef()
211219
priorBox.name = 'PriorBox_%d' % i
@@ -232,9 +240,18 @@ def tensorMsg(values):
232240
text_format.Merge(tensorMsg([0.1, 0.1, 0.2, 0.2]), priorBox.attr["variance"])
233241

234242
graph_def.node.extend([priorBox])
235-
priorBoxes.append(priorBox.name)
236243

237-
addConcatNode('PriorBox/concat', priorBoxes)
244+
# Reshape from 1x2xN to 1x2xNx1
245+
reshape = NodeDef()
246+
reshape.name = priorBox.name + '/4d'
247+
reshape.op = 'Reshape'
248+
reshape.input.append(priorBox.name)
249+
reshape.input.append('reshape_prior_boxes_to_4d')
250+
graph_def.node.extend([reshape])
251+
252+
priorBoxes.append(reshape.name)
253+
254+
addConcatNode('PriorBox/concat', priorBoxes, 'PriorBox/concat/axis')
238255

239256
# Sigmoid for classes predictions and DetectionOutput layer
240257
sigmoid = NodeDef()

0 commit comments

Comments
 (0)