From: Li Peng Date: Fri, 24 Nov 2017 10:22:59 +0000 (+0800) Subject: Add ocl accuracy test for a few dnn nets X-Git-Tag: accepted/tizen/6.0/unified/20201030.111113~352^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a47fbd261030eab003540a737e318e80640fc14a;p=platform%2Fupstream%2Fopencv.git Add ocl accuracy test for a few dnn nets They are alexnet, mobilenet-ssd, resnet50, squeezeNet_v1_1, yolo and fast_neural_style. Signed-off-by: Li Peng --- diff --git a/modules/dnn/test/test_caffe_importer.cpp b/modules/dnn/test/test_caffe_importer.cpp index d6f57a3..09c33c6 100644 --- a/modules/dnn/test/test_caffe_importer.cpp +++ b/modules/dnn/test/test_caffe_importer.cpp @@ -42,6 +42,8 @@ #include "test_precomp.hpp" #include "npy_blob.hpp" #include +#include +#include namespace cvtest { @@ -119,6 +121,43 @@ TEST_P(Reproducibility_AlexNet, Accuracy) INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_AlexNet, testing::Values(true, false)); +typedef testing::TestWithParam > Reproducibility_OCL_AlexNet; +OCL_TEST_P(Reproducibility_OCL_AlexNet, Accuracy) +{ + bool readFromMemory = get<0>(GetParam()); + Net net; + { + const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false); + const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false); + if (readFromMemory) + { + string dataProto; + ASSERT_TRUE(readFileInMemory(proto, dataProto)); + string dataModel; + ASSERT_TRUE(readFileInMemory(model, dataModel)); + + net = readNetFromCaffe(dataProto.c_str(), dataProto.size(), + dataModel.c_str(), dataModel.size()); + } + else + net = readNetFromCaffe(proto, model); + ASSERT_FALSE(net.empty()); + } + + net.setPreferableBackend(DNN_BACKEND_DEFAULT); + net.setPreferableTarget(DNN_TARGET_OPENCL); + + Mat sample = imread(_tf("grace_hopper_227.png")); + ASSERT_TRUE(!sample.empty()); + + net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data"); + Mat out = net.forward("prob"); + Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy")); + normAssert(ref, out); +} + +OCL_INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_OCL_AlexNet, testing::Values(true, false)); + #if !defined(_WIN32) || defined(_WIN64) TEST(Reproducibility_FCN, Accuracy) { @@ -201,6 +240,38 @@ TEST(Reproducibility_MobileNet_SSD, Accuracy) } } +OCL_TEST(Reproducibility_MobileNet_SSD, Accuracy) +{ + const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false); + const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false); + Net net = readNetFromCaffe(proto, model); + + net.setPreferableBackend(DNN_BACKEND_DEFAULT); + net.setPreferableTarget(DNN_TARGET_OPENCL); + + Mat sample = imread(_tf("street.png")); + + Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false); + net.setInput(inp); + Mat out = net.forward(); + + Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy")); + normAssert(ref, out); + + // Check that detections aren't preserved. + inp.setTo(0.0f); + net.setInput(inp); + out = net.forward(); + + const int numDetections = out.size[2]; + ASSERT_NE(numDetections, 0); + for (int i = 0; i < numDetections; ++i) + { + float confidence = out.ptr(0, 0, i)[2]; + ASSERT_EQ(confidence, 0); + } +} + TEST(Reproducibility_ResNet50, Accuracy) { Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false), @@ -216,6 +287,24 @@ TEST(Reproducibility_ResNet50, Accuracy) normAssert(ref, out); } +OCL_TEST(Reproducibility_ResNet50, Accuracy) +{ + Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false), + findDataFile("dnn/ResNet-50-model.caffemodel", false)); + + net.setPreferableBackend(DNN_BACKEND_DEFAULT); + net.setPreferableTarget(DNN_TARGET_OPENCL); + + Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(224,224), Scalar(), false); + ASSERT_TRUE(!input.empty()); + + net.setInput(input); + Mat out = net.forward(); + + Mat ref = blobFromNPY(_tf("resnet50_prob.npy")); + normAssert(ref, out); +} + TEST(Reproducibility_SqueezeNet_v1_1, Accuracy) { Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false), @@ -231,6 +320,24 @@ TEST(Reproducibility_SqueezeNet_v1_1, Accuracy) normAssert(ref, out); } +OCL_TEST(Reproducibility_SqueezeNet_v1_1, Accuracy) +{ + Net net = readNetFromCaffe(findDataFile("dnn/squeezenet_v1.1.prototxt", false), + findDataFile("dnn/squeezenet_v1.1.caffemodel", false)); + + net.setPreferableBackend(DNN_BACKEND_DEFAULT); + net.setPreferableTarget(DNN_TARGET_OPENCL); + + Mat input = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(227,227), Scalar(), false); + ASSERT_TRUE(!input.empty()); + + net.setInput(input); + Mat out = net.forward(); + + Mat ref = blobFromNPY(_tf("squeezenet_v1.1_prob.npy")); + normAssert(ref, out); +} + TEST(Reproducibility_AlexNet_fp16, Accuracy) { const float l1 = 1e-5; diff --git a/modules/dnn/test/test_darknet_importer.cpp b/modules/dnn/test/test_darknet_importer.cpp index d3d3acc..17b4722 100644 --- a/modules/dnn/test/test_darknet_importer.cpp +++ b/modules/dnn/test/test_darknet_importer.cpp @@ -184,6 +184,68 @@ TEST(Reproducibility_TinyYoloVoc, Accuracy) normAssert(ref, detection); } +OCL_TEST(Reproducibility_YoloVoc, Accuracy) +{ + Net net; + { + const string cfg = findDataFile("dnn/yolo-voc.cfg", false); + const string model = findDataFile("dnn/yolo-voc.weights", false); + net = readNetFromDarknet(cfg, model); + ASSERT_FALSE(net.empty()); + } + + net.setPreferableBackend(DNN_BACKEND_DEFAULT); + net.setPreferableTarget(DNN_TARGET_OPENCL); + + // dog416.png is dog.jpg that resized to 416x416 in the lossless PNG format + Mat sample = imread(_tf("dog416.png")); + ASSERT_TRUE(!sample.empty()); + + Size inputSize(416, 416); + + if (sample.size() != inputSize) + resize(sample, sample, inputSize); + + net.setInput(blobFromImage(sample, 1 / 255.F), "data"); + Mat out = net.forward("detection_out"); + + Mat detection; + const float confidenceThreshold = 0.24; + + for (int i = 0; i < out.rows; i++) { + const int probability_index = 5; + const int probability_size = out.cols - probability_index; + float *prob_array_ptr = &out.at(i, probability_index); + size_t objectClass = std::max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr; + float confidence = out.at(i, (int)objectClass + probability_index); + + if (confidence > confidenceThreshold) + detection.push_back(out.row(i)); + } + + // obtained by: ./darknet detector test ./cfg/voc.data ./cfg/yolo-voc.cfg ./yolo-voc.weights -thresh 0.24 ./dog416.png + // There are 3 objects (6-car, 1-bicycle, 11-dog) with 25 values for each: + // { relative_center_x, relative_center_y, relative_width, relative_height, unused_t0, probability_for_each_class[20] } + float ref_array[] = { + 0.740161F, 0.214100F, 0.325575F, 0.173418F, 0.750769F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, + 0.000000F, 0.000000F, 0.750469F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, + 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, + + 0.501618F, 0.504757F, 0.461713F, 0.481310F, 0.783550F, 0.000000F, 0.780879F, 0.000000F, 0.000000F, + 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, + 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, + + 0.279968F, 0.638651F, 0.282737F, 0.600284F, 0.901864F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, + 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.901615F, + 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F, 0.000000F + }; + + const int number_of_objects = 3; + Mat ref(number_of_objects, sizeof(ref_array) / (number_of_objects * sizeof(float)), CV_32FC1, &ref_array); + + normAssert(ref, detection); +} + TEST(Reproducibility_YoloVoc, Accuracy) { Net net; diff --git a/modules/dnn/test/test_torch_importer.cpp b/modules/dnn/test/test_torch_importer.cpp index 5015d5d..691a028 100644 --- a/modules/dnn/test/test_torch_importer.cpp +++ b/modules/dnn/test/test_torch_importer.cpp @@ -382,6 +382,39 @@ TEST(Torch_Importer, FastNeuralStyle_accuracy) } } +OCL_TEST(Torch_Importer, FastNeuralStyle_accuracy) +{ + std::string models[] = {"dnn/fast_neural_style_eccv16_starry_night.t7", + "dnn/fast_neural_style_instance_norm_feathers.t7"}; + std::string targets[] = {"dnn/lena_starry_night.png", "dnn/lena_feathers.png"}; + + for (int i = 0; i < 2; ++i) + { + const string model = findDataFile(models[i], false); + Net net = readNetFromTorch(model); + + net.setPreferableBackend(DNN_BACKEND_DEFAULT); + net.setPreferableTarget(DNN_TARGET_OPENCL); + + Mat img = imread(findDataFile("dnn/googlenet_1.png", false)); + Mat inputBlob = blobFromImage(img, 1.0, Size(), Scalar(103.939, 116.779, 123.68), false); + + net.setInput(inputBlob); + Mat out = net.forward(); + + // Deprocessing. + getPlane(out, 0, 0) += 103.939; + getPlane(out, 0, 1) += 116.779; + getPlane(out, 0, 2) += 123.68; + out = cv::min(cv::max(0, out), 255); + + Mat ref = imread(findDataFile(targets[i])); + Mat refBlob = blobFromImage(ref, 1.0, Size(), Scalar(), false); + + normAssert(out, refBlob, "", 0.5, 1.1); + } +} + } #endif