test_tf_importer.cpp 1.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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
*/

#include "test_precomp.hpp"
13
#include "npy_blob.hpp"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

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;
    {
Maksim Shabunin's avatar
Maksim Shabunin committed
31 32
        const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
        Ptr<Importer> importer = createTensorflowImporter(model);
33 34 35 36
        ASSERT_TRUE(importer != NULL);
        importer->populateNet(net);
    }

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

43
    Mat inputBlob = blobFromImage(input);
44

45 46
    net.setInput(inputBlob, "input");
    Mat out = net.forward("softmax2");
47

48
    std::cout << out.dims << std::endl;
49 50
}

51 52 53 54
TEST(Test_TensorFlow, inception_accuracy)
{
    Net net;
    {
Maksim Shabunin's avatar
Maksim Shabunin committed
55 56
        const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
        Ptr<Importer> importer = createTensorflowImporter(model);
57 58 59 60 61 62 63
        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));
64
    Mat inputBlob = blobFromImage(sample);
65

66 67
    net.setInput(inputBlob, "input");
    Mat out = net.forward("softmax2");
68

69
    Mat ref = blobFromNPY(_tf("tf_inception_prob.npy"));
70 71 72 73

    normAssert(ref, out);
}

74
}