Skip to content

Commit

Permalink
Minor test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Lyudvichenko committed Aug 12, 2015
1 parent b3dcc39 commit 7d2e745
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 57 deletions.
9 changes: 3 additions & 6 deletions modules/dnn/test/cnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ cnpy::NpyArray cnpy::npz_load(std::string fname, std::string varname) {
FILE* fp = fopen(fname.c_str(),"rb");

if(!fp) {
printf("npz_load: Error! Unable to open file %s!\n",fname.c_str());
abort();
throw std::runtime_error("npz_load: Error! Unable to open file " + fname + "!\n");
}

while(1) {
Expand Down Expand Up @@ -227,17 +226,15 @@ cnpy::NpyArray cnpy::npz_load(std::string fname, std::string varname) {
}

fclose(fp);
printf("npz_load: Error! Variable name %s not found in %s!\n",varname.c_str(),fname.c_str());
abort();
throw std::runtime_error("npz_load: Error! Variable name " + varname + " not found in " + fname + "!\n");
}

cnpy::NpyArray cnpy::npy_load(std::string fname) {

FILE* fp = fopen(fname.c_str(), "rb");

if(!fp) {
printf("npy_load: Error! Unable to open file %s!\n",fname.c_str());
abort();
throw std::runtime_error("npy_load: Error! Unable to open file " + fname + "!\n");
}

NpyArray arr = load_the_npy_file(fp);
Expand Down
2 changes: 1 addition & 1 deletion modules/dnn/test/npy_blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inline cv::dnn::Blob blobFromNPY(const cv::String &path)
inline void saveBlobToNPY(cv::dnn::Blob &blob, const cv::String &path)
{
cv::dnn::BlobShape shape = blob.shape();
cnpy::npy_save(path.c_str(), blob.ptr<float>(), (unsigned*)&shape[0], shape.dims());
cnpy::npy_save(path.c_str(), blob.ptrf(), (unsigned*)&shape[0], shape.dims());
}

#endif
12 changes: 5 additions & 7 deletions modules/dnn/test/test_alexnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
namespace cvtest
{

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

template<typename TString>
static std::string getTestFile(TString filename)
static std::string _tf(TString filename)
{
return (getOpenCVExtraDir() + "/dnn/") + filename;
}
Expand All @@ -19,21 +17,21 @@ TEST(Reproducibility_AlexNet, Accuracy)
{
Net net;
{
Ptr<Importer> importer = createCaffeImporter(getTestFile("bvlc_alexnet.prototxt"), getTestFile("bvlc_alexnet.caffemodel"));
Ptr<Importer> importer = createCaffeImporter(_tf("bvlc_alexnet.prototxt"), _tf("bvlc_alexnet.caffemodel"));
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}

std::vector<Mat> inpMats;
inpMats.push_back( imread(getTestFile("alexnet_0.png")) );
inpMats.push_back( imread(getTestFile("alexnet_1.png")) );
inpMats.push_back( imread(_tf("alexnet_0.png")) );
inpMats.push_back( imread(_tf("alexnet_1.png")) );
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());

net.setBlob(".data", Blob(inpMats));
net.forward();

Blob out = net.getBlob("prob");
Blob ref = blobFromNPY(getTestFile("alexnet.npy"));
Blob ref = blobFromNPY(_tf("alexnet.npy"));
normAssert(ref, out, "prob");
}

Expand Down
15 changes: 4 additions & 11 deletions modules/dnn/test/test_caffe_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
namespace cvtest
{

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

static std::string getOpenCVExtraDir()
{
return cvtest::TS::ptr()->get_data_path();
}

template<typename TStr>
static std::string getTestFile(TStr filename)
template<typename TString>
static std::string _tf(TString filename)
{
return (getOpenCVExtraDir() + "/dnn/") + filename;
}
Expand All @@ -23,7 +16,7 @@ TEST(ReadCaffe_GTSRB, Accuracy)
{
Net net;
{
Ptr<Importer> importer = createCaffeImporter(getTestFile("gtsrb.prototxt"), "");
Ptr<Importer> importer = createCaffeImporter(_tf("gtsrb.prototxt"), "");
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
Expand All @@ -33,7 +26,7 @@ TEST(ReadCaffe_GoogLeNet, Accuracy)
{
Net net;
{
Ptr<Importer> importer = createCaffeImporter(getTestFile("bvlc_googlenet.prototxt"), "");
Ptr<Importer> importer = createCaffeImporter(_tf("bvlc_googlenet.prototxt"), "");
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
Expand Down
12 changes: 6 additions & 6 deletions modules/dnn/test/test_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ inline const std::string &getOpenCVExtraDir()
return cvtest::TS::ptr()->get_data_path();
}

inline void normAssert(cv::InputArray ref, cv::InputArray get, const char *comment = "")
inline void normAssert(cv::InputArray ref, cv::InputArray test, const char *comment = "")
{
double normL1 = cvtest::norm(ref, get, cv::NORM_L1)/ ref.getMat().total();
EXPECT_NEAR(normL1, 0, 0.0001) << comment;
double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
EXPECT_LE(normL1, 0.0001) << comment;

double normInf = cvtest::norm(ref, get, cv::NORM_INF);
EXPECT_NEAR(normInf, 0, 0.001) << comment;
double normInf = cvtest::norm(ref, test, cv::NORM_INF);
EXPECT_LE(normInf, 0.001) << comment;
}

inline void normAssert(cv::dnn::Blob &ref, cv::dnn::Blob &test, const char *comment = "")
{
ASSERT_EQ(ref.shape(), test.shape());
ASSERT_EQ(ref.shape(), test.shape()) << comment;
normAssert(ref.getMatRef(), test.getMatRef(), comment);
}

Expand Down
12 changes: 5 additions & 7 deletions modules/dnn/test/test_googlenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
namespace cvtest
{

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

template<typename TString>
static std::string getTestFile(TString filename)
static std::string _tf(TString filename)
{
return (getOpenCVExtraDir() + "/dnn/") + filename;
}
Expand All @@ -19,22 +17,22 @@ TEST(Reproducibility_GoogLeNet, Accuracy)
{
Net net;
{
Ptr<Importer> importer = createCaffeImporter(getTestFile("bvlc_googlenet.prototxt"), getTestFile("bvlc_googlenet.caffemodel"));
Ptr<Importer> importer = createCaffeImporter(_tf("bvlc_googlenet.prototxt"), _tf("bvlc_googlenet.caffemodel"));
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}

std::vector<Mat> inpMats;
inpMats.push_back( imread(getTestFile("googlenet_0.jpg")) );
inpMats.push_back( imread(getTestFile("googlenet_1.jpg")) );
inpMats.push_back( imread(_tf("googlenet_0.jpg")) );
inpMats.push_back( imread(_tf("googlenet_1.jpg")) );
ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());

Blob inp(inpMats);
net.setBlob(".data", inp);
net.forward();

Blob out = net.getBlob("prob");
Blob ref = blobFromNPY(getTestFile("googlenet_prob.npy"));
Blob ref = blobFromNPY(_tf("googlenet_prob.npy"));
normAssert(out, ref);
}

Expand Down
22 changes: 3 additions & 19 deletions modules/dnn/test/test_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@
namespace cvtest
{

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

static std::string getOpenCVExtraDir()
{
return cvtest::TS::ptr()->get_data_path();
}

template<typename TStr>
static String _tf(TStr filename)
template<typename TString>
static String _tf(TString filename)
{
return (getOpenCVExtraDir() + "/dnn/layers/") + filename;
}
Expand All @@ -40,16 +33,7 @@ static void testLayer(String basename, bool useCaffeModel = false)
net.forward();
Blob out = net.getBlob("output");

EXPECT_EQ(ref.shape(), out.shape());

Mat &mRef = ref.getMatRef();
Mat &mOut = out.getMatRef();

double normL1 = cvtest::norm(mRef, mOut, NORM_L1) / ref.total();
EXPECT_LE(normL1, 0.0001);

double normInf = cvtest::norm(mRef, mOut, NORM_INF);
EXPECT_LE(normInf, 0.0001);
normAssert(ref, out);
}

TEST(Layer_Test_Softmax, Accuracy)
Expand Down

0 comments on commit 7d2e745

Please sign in to comment.