test_tf_importer.cpp 1.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

// Copyright (C) 2016, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.

/*
Test for Tensorflow models loading
*/

12 13
#if defined(ENABLE_TF_INCEPTION_TESTS)

14
#include "test_precomp.hpp"
15
#include "npy_blob.hpp"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

namespace cvtest
{

using namespace cv;
using namespace cv::dnn;

template<typename TString>
static std::string _tf(TString filename)
{
    return (getOpenCVExtraDir() + "/dnn/") + filename;
}

TEST(Test_TensorFlow, read_inception)
{
    Net net;
    {
        Ptr<Importer> importer = createTensorflowImporter(_tf("tensorflow_inception_graph.pb"));
        ASSERT_TRUE(importer != NULL);
        importer->populateNet(net);
    }

38
    Mat sample = imread(_tf("grace_hopper_227.png"));
39 40 41 42 43
    ASSERT_TRUE(!sample.empty());
    Mat input;
    resize(sample, input, Size(224, 224));
    input -= 128; // mean sub

arrybn's avatar
arrybn committed
44
    dnn::Blob inputBlob = dnn::Blob::fromImages(input);
45

arrybn's avatar
arrybn committed
46
    net.setBlob("_input.input", inputBlob);
47 48
    net.forward();

arrybn's avatar
arrybn committed
49
    Blob out = net.getBlob("softmax2");
50 51 52
    std::cout << out.dims() << std::endl;
}

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
TEST(Test_TensorFlow, inception_accuracy)
{
    Net net;
    {
        Ptr<Importer> importer = createTensorflowImporter(_tf("tensorflow_inception_graph.pb"));
        ASSERT_TRUE(importer != NULL);
        importer->populateNet(net);
    }

    Mat sample = imread(_tf("grace_hopper_227.png"));
    ASSERT_TRUE(!sample.empty());
    resize(sample, sample, Size(224, 224));
    cv::cvtColor(sample, sample, cv::COLOR_BGR2RGB);
    dnn::Blob inputBlob = dnn::Blob::fromImages(sample);

    net.setBlob(".input", inputBlob);
    net.forward();

    Blob out = net.getBlob("softmax2");

    Blob ref = blobFromNPY(_tf("tf_inception_prob.npy"));

    normAssert(ref, out);
}

78
}
79 80

#endif