Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80998b3

Browse files
added opencl fp16 target for int8 tests
1 parent4298e27 commit80998b3

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

‎modules/dnn/src/dnn.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ struct Net::Impl : public detail::NetImplBase
25442544

25452545
CV_Assert(layerShapesIt != layersShapes.end());
25462546

2547-
if (preferableBackend == DNN_BACKEND_OPENCV && preferableTarget == DNN_TARGET_OPENCL_FP16)
2547+
if (preferableBackend == DNN_BACKEND_OPENCV && preferableTarget == DNN_TARGET_OPENCL_FP16 && ld.dtype == CV_32F)
25482548
ld.dtype = CV_16S;
25492549

25502550
std::vector<LayerPin> pinsForInternalBlobs;
@@ -3152,7 +3152,8 @@ struct Net::Impl : public detail::NetImplBase
31523152
Mat& inp = layers[0].outputBlobs[i];
31533153
CV_Assert(inp.total());
31543154
if (preferableBackend == DNN_BACKEND_OPENCV &&
3155-
preferableTarget == DNN_TARGET_OPENCL_FP16)
3155+
preferableTarget == DNN_TARGET_OPENCL_FP16 &&
3156+
layers[0].dtype == CV_32F)
31563157
{
31573158
layers[0].outputBlobs[i].create(inp.dims, inp.size, CV_16S);
31583159
}
@@ -3611,7 +3612,8 @@ struct Net::Impl : public detail::NetImplBase
36113612
Mat& inp = layers[0].outputBlobs[i];
36123613
CV_Assert(inp.total());
36133614
if (preferableBackend == DNN_BACKEND_OPENCV &&
3614-
preferableTarget == DNN_TARGET_OPENCL_FP16)
3615+
preferableTarget == DNN_TARGET_OPENCL_FP16 &&
3616+
layers[0].dtype == CV_32F)
36153617
{
36163618
layers[0].outputBlobs[i].create(inp.dims, inp.size, CV_16S);
36173619
}

‎modules/dnn/src/int8layers/quantize_dequantize_layer.cpp‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,36 @@ class QuantizeLayerImpl CV_FINAL : public QuantizeLayer
4242
outputs_arr.getMatVector(outputs);
4343
}
4444

45+
#ifdef HAVE_OPENCL
46+
boolforward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_)
47+
{
48+
std::vector<UMat> inputs, outputs;
49+
inputs_.getUMatVector(inputs);
50+
outputs_.getUMatVector(outputs);
51+
52+
if (inputs_.depth() == CV_16S)
53+
{
54+
UMatinputFp32(shape(inputs[0]), CV_32F);
55+
convertFp16(inputs[0], inputFp32);
56+
inputFp32.copyTo(inputs[0]);
57+
}
58+
59+
inputs[0].convertTo(outputs[0], CV_8S,1.f/scale, zeropoint);
60+
returntrue;
61+
}
62+
#endif
63+
4564
voidforward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
4665
{
4766
CV_TRACE_FUNCTION();
4867
CV_TRACE_ARG_VALUE(name,"name", name.c_str());
4968

50-
std::vector<Mat> inputs, outputs, internals;
69+
CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
70+
forward_ocl(inputs_arr, outputs_arr, internals_arr))
71+
72+
std::vector<Mat> inputs, outputs;
5173
inputs_arr.getMatVector(inputs);
5274
outputs_arr.getMatVector(outputs);
53-
internals_arr.getMatVector(internals);
5475

5576
inputs[0].convertTo(outputs[0], CV_8S,1.f/scale, zeropoint);
5677
}
@@ -88,15 +109,35 @@ class DequantizeLayerImpl CV_FINAL : public DequantizeLayer
88109
outputs_arr.getMatVector(outputs);
89110
}
90111

112+
#ifdef HAVE_OPENCL
113+
boolforward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_)
114+
{
115+
std::vector<UMat> inputs, outputs;
116+
inputs_.getUMatVector(inputs);
117+
outputs_.getUMatVector(outputs);
118+
119+
UMatoutputFp32(shape(outputs[0]), CV_32F);
120+
inputs[0].convertTo(outputFp32, CV_32F, scale, -(scale*zeropoint));
121+
122+
if (outputs_.depth() == CV_16S)
123+
convertFp16(outputFp32, outputs[0]);
124+
else
125+
outputFp32.copyTo(outputs[0]);
126+
returntrue;
127+
}
128+
#endif
129+
91130
voidforward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
92131
{
93132
CV_TRACE_FUNCTION();
94133
CV_TRACE_ARG_VALUE(name,"name", name.c_str());
95134

96-
std::vector<Mat> inputs, outputs, internals;
135+
CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget),
136+
forward_ocl(inputs_arr, outputs_arr, internals_arr))
137+
138+
std::vector<Mat> inputs, outputs;
97139
inputs_arr.getMatVector(inputs);
98140
outputs_arr.getMatVector(outputs);
99-
internals_arr.getMatVector(internals);
100141

101142
inputs[0].convertTo(outputs[0], CV_32F, scale, -(scale*zeropoint));
102143
}

‎modules/dnn/src/int8layers/softmax_layer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SoftMaxLayerInt8Impl CV_FINAL : public SoftmaxLayerInt8
4747
virtualbooltryFuse(Ptr<Layer>& top) CV_OVERRIDE
4848
{
4949
Ptr<DequantizeLayer> dequantize_layer = top.dynamicCast<DequantizeLayer>();
50-
return !dequantize_layer.empty();
50+
return !dequantize_layer.empty() && preferableTarget != DNN_TARGET_OPENCL_FP16;
5151
}
5252

5353
voidforward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE

‎modules/dnn/test/test_int8_layers.cpp‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,8 @@ TEST_P(Test_Int8_layers, Eltwise)
341341
testLayer("split_max","ONNX",0.004,0.012);
342342
}
343343

344-
// TODO : Only CPU backend has int8 implementation, so testing is done on OCV/CPU and OCV/OCL backend.
345-
// Need to test the behaviour of int8 layers on other backends.
346-
INSTANTIATE_TEST_CASE_P(/**/, Test_Int8_layers, Combine(Values(DNN_BACKEND_OPENCV), Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL)));
347-
344+
// TODO : Only CPU backend has int8 implementation. Need to test the behaviour of int8 layers on other backends.
345+
INSTANTIATE_TEST_CASE_P(/**/, Test_Int8_layers, Combine(Values(DNN_BACKEND_OPENCV), ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV))));
348346

349347
classTest_Int8_nets :publicDNNTestLayer
350348
{
@@ -399,18 +397,24 @@ class Test_Int8_nets : public DNNTestLayer
399397
baseNet.setPreferableBackend(backend);
400398
baseNet.setPreferableTarget(target);
401399

400+
Net qnet = baseNet.quantize(blob, CV_32F, CV_32F);
401+
qnet.setInput(blob);
402+
Mat out = qnet.forward();
403+
402404
if (useSoftmax)
403405
{
404406
LayerParams lp;
405-
baseNet.addLayerToPrev("softmaxLayer","Softmax", lp);
407+
Net netSoftmax;
408+
netSoftmax.addLayerToPrev("softmaxLayer","Softmax", lp);
409+
netSoftmax.setPreferableBackend(DNN_BACKEND_OPENCV);
406410

407-
baseNet.setInput(blob);
408-
ref = baseNet.forward();
411+
netSoftmax.setInput(out);
412+
out = netSoftmax.forward();
413+
414+
netSoftmax.setInput(ref);
415+
ref = netSoftmax.forward();
409416
}
410417

411-
Net qnet = baseNet.quantize(blob, CV_32F, CV_32F);
412-
qnet.setInput(blob);
413-
Mat out = qnet.forward();
414418
normAssert(ref, out,"", l1, lInf);
415419
}
416420

@@ -547,7 +551,7 @@ TEST_P(Test_Int8_nets, AlexNet)
547551
Mat blob =blobFromImage(inp,1.0,Size(227,227),Scalar(),false);
548552
Mat ref =blobFromNPY(_tf("caffe_alexnet_prob.npy"));
549553

550-
float l1 =1e-5, lInf =0.0013;
554+
float l1 =1e-4, lInf =0.003;
551555
testClassificationNet(net, blob, ref, l1, lInf);
552556
}
553557

@@ -578,7 +582,7 @@ TEST_P(Test_Int8_nets, ResNet50)
578582
Mat blob =blobFromImage(inp,1.0,Size(224,224),Scalar(),false);
579583
Mat ref =blobFromNPY(_tf("resnet50_prob.npy"));
580584

581-
float l1 =2e-4, lInf =0.035;
585+
float l1 =3e-4, lInf =0.035;
582586
testClassificationNet(net, blob, ref, l1, lInf);
583587
}
584588

@@ -626,7 +630,7 @@ TEST_P(Test_Int8_nets, CaffeNet)
626630
&&getInferenceEngineVPUType() == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
627631
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X, CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
628632
#endif
629-
float l1 =1e-5, lInf =0.0022;
633+
float l1 =4e-5, lInf =0.0025;
630634
testONNXNet("caffenet", l1, lInf);
631635
}
632636

@@ -751,7 +755,7 @@ TEST_P(Test_Int8_nets, opencv_face_detector)
751755
0,1,0.97203469,0.67965847,0.06876482,0.73999709,0.1513494,
752756
0,1,0.95097077,0.51901293,0.45863652,0.5777427,0.5347801);
753757

754-
float confThreshold =0.5, scoreDiff =0.002, iouDiff =0.2;
758+
float confThreshold =0.5, scoreDiff =0.002, iouDiff =0.21;
755759
testDetectionNet(net, blob, ref, confThreshold, scoreDiff, iouDiff);
756760
}
757761

@@ -1122,7 +1126,7 @@ TEST_P(Test_Int8_nets, YOLOv4)
11221126

11231127
std::string config_file ="yolov4.cfg";
11241128
std::string weights_file ="yolov4.weights";
1125-
double scoreDiff =0.1, iouDiff =0.15;
1129+
double scoreDiff =0.1, iouDiff =0.17;
11261130
{
11271131
SCOPED_TRACE("batch size 1");
11281132
testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff);
@@ -1175,7 +1179,8 @@ TEST_P(Test_Int8_nets, YOLOv4_tiny)
11751179

11761180
std::string config_file ="yolov4-tiny.cfg";
11771181
std::string weights_file ="yolov4-tiny.weights";
1178-
double scoreDiff =0.11, iouDiff =0.082;
1182+
double scoreDiff =0.12;
1183+
double iouDiff = target == DNN_TARGET_OPENCL_FP16 ?0.2 :0.082;
11791184

11801185
#if defined(INF_ENGINE_RELEASE)
11811186
if (target == DNN_TARGET_MYRIAD)// bad accuracy
@@ -1210,7 +1215,6 @@ TEST_P(Test_Int8_nets, YOLOv4_tiny)
12101215
#endif
12111216
}
12121217

1213-
// TODO : Only CPU backend has int8 implementation, so testing is done on OCV/CPU and OCV/OCL backend.
1214-
// Need to test the behaviour of int8 layers on other backends.
1215-
INSTANTIATE_TEST_CASE_P(/**/, Test_Int8_nets, Combine(Values(DNN_BACKEND_OPENCV), Values(DNN_TARGET_CPU, DNN_TARGET_OPENCL)));
1218+
// TODO : Only CPU backend has int8 implementation. Need to test the behaviour of int8 layers on other backends.
1219+
INSTANTIATE_TEST_CASE_P(/**/, Test_Int8_nets, Combine(Values(DNN_BACKEND_OPENCV), ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV))));
12161220
}}// namespace

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp