test_torch_importer.cpp 5.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

Maksim Shabunin's avatar
Maksim Shabunin committed
42 43
#ifdef ENABLE_TORCH_IMPORTER

44
#include "test_precomp.hpp"
45
#include "npy_blob.hpp"
46
#include <opencv2/dnn/shape_utils.hpp>
47 48 49 50 51 52 53 54 55 56

namespace cvtest
{

using namespace std;
using namespace testing;
using namespace cv;
using namespace cv::dnn;

template<typename TStr>
57
static std::string _tf(TStr filename, bool inTorchDir = true)
58
{
59 60 61 62 63
    String path = getOpenCVExtraDir() + "/dnn/";
    if (inTorchDir)
        path += "torch/";
    path += filename;
    return path;
64 65 66 67 68 69 70 71 72 73 74 75
}

TEST(Torch_Importer, simple_read)
{
    Net net;
    Ptr<Importer> importer;

    ASSERT_NO_THROW( importer = createTorchImporter(_tf("net_simple_net.txt"), false) );
    ASSERT_TRUE( importer != NULL );
    importer->populateNet(net);
}

arrybn's avatar
arrybn committed
76 77
static void runTorchNet(String prefix, String outLayerName = "",
                        bool check2ndBlob = false, bool isBinary = false)
78 79 80 81 82 83 84 85
{
    String suffix = (isBinary) ? ".dat" : ".txt";

    Net net;
    Ptr<Importer> importer = createTorchImporter(_tf(prefix + "_net" + suffix), isBinary);
    ASSERT_TRUE(importer != NULL);
    importer->populateNet(net);

86
    Mat inp, outRef;
87 88 89 90 91
    ASSERT_NO_THROW( inp = readTorchBlob(_tf(prefix + "_input" + suffix), isBinary) );
    ASSERT_NO_THROW( outRef = readTorchBlob(_tf(prefix + "_output" + suffix), isBinary) );

    net.setBlob(".0", inp);
    net.forward();
92 93
    if (outLayerName.empty())
        outLayerName = net.getLayerNames().back();
94
    Mat out = net.getBlob(outLayerName);
95 96

    normAssert(outRef, out);
arrybn's avatar
arrybn committed
97 98 99

    if (check2ndBlob)
    {
100 101
        Mat out2 = net.getBlob(outLayerName + ".1");
        Mat ref2 = readTorchBlob(_tf(prefix + "_output_2" + suffix), isBinary);
arrybn's avatar
arrybn committed
102 103
        normAssert(out2, ref2);
    }
104 105 106 107
}

TEST(Torch_Importer, run_convolution)
{
arrybn's avatar
arrybn committed
108
    runTorchNet("net_conv");
109 110 111 112
}

TEST(Torch_Importer, run_pool_max)
{
arrybn's avatar
arrybn committed
113
    runTorchNet("net_pool_max", "", true);
114 115 116 117
}

TEST(Torch_Importer, run_pool_ave)
{
arrybn's avatar
arrybn committed
118
    runTorchNet("net_pool_ave");
119 120 121 122
}

TEST(Torch_Importer, run_reshape)
{
arrybn's avatar
arrybn committed
123 124
    runTorchNet("net_reshape");
    runTorchNet("net_reshape_batch");
125 126 127 128
}

TEST(Torch_Importer, run_linear)
{
arrybn's avatar
arrybn committed
129
    runTorchNet("net_linear_2d");
130 131 132 133
}

TEST(Torch_Importer, run_paralel)
{
arrybn's avatar
arrybn committed
134
    runTorchNet("net_parallel", "l2_torchMerge");
135 136 137 138
}

TEST(Torch_Importer, run_concat)
{
arrybn's avatar
arrybn committed
139
    runTorchNet("net_concat", "l2_torchMerge");
140 141
}

142 143
TEST(Torch_Importer, run_deconv)
{
arrybn's avatar
arrybn committed
144
    runTorchNet("net_deconv");
145 146
}

147 148
TEST(Torch_Importer, run_batch_norm)
{
arrybn's avatar
arrybn committed
149 150 151 152 153 154 155 156 157 158 159
    runTorchNet("net_batch_norm");
}

TEST(Torch_Importer, net_prelu)
{
    runTorchNet("net_prelu");
}

TEST(Torch_Importer, net_cadd_table)
{
    runTorchNet("net_cadd_table");
160 161
}

162 163 164 165
TEST(Torch_Importer, ENet_accuracy)
{
    Net net;
    {
Maksim Shabunin's avatar
Maksim Shabunin committed
166 167
        const string model = findDataFile("dnn/Enet-model-best.net", false);
        Ptr<Importer> importer = createTorchImporter(model, true);
168 169 170 171 172
        ASSERT_TRUE(importer != NULL);
        importer->populateNet(net);
    }

    Mat sample = imread(_tf("street.png", false));
173
    Mat inputBlob = blobFromImage(sample, 1./255);
174 175 176

    net.setBlob("", inputBlob);
    net.forward();
177 178
    Mat out = net.getBlob(net.getLayerNames().back());
    Mat ref = blobFromNPY(_tf("torch_enet_prob.npy", false));
179 180 181 182
    // Due to numerical instability in Pooling-Unpooling layers (indexes jittering)
    // thresholds for ENet must be changed. Accuracy of resuults was checked on
    // Cityscapes dataset and difference in mIOU with Torch is 10E-4%
    normAssert(ref, out, "", 0.00044, 0.44);
183 184
}

185
}
Maksim Shabunin's avatar
Maksim Shabunin committed
186

187
#endif