Commit 8ea2f59d authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #14671 from alalek:dnn_test_fast_read

parents d6d45716 52548bde
...@@ -83,17 +83,17 @@ TEST(Test_Caffe, memory_read) ...@@ -83,17 +83,17 @@ TEST(Test_Caffe, memory_read)
const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false); const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);
const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false); const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
string dataProto; std::vector<char> dataProto;
ASSERT_TRUE(readFileInMemory(proto, dataProto)); readFileContent(proto, dataProto);
string dataModel; std::vector<char> dataModel;
ASSERT_TRUE(readFileInMemory(model, dataModel)); readFileContent(model, dataModel);
Net net = readNetFromCaffe(dataProto.c_str(), dataProto.size()); Net net = readNetFromCaffe(dataProto.data(), dataProto.size());
net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableBackend(DNN_BACKEND_OPENCV);
ASSERT_FALSE(net.empty()); ASSERT_FALSE(net.empty());
Net net2 = readNetFromCaffe(dataProto.c_str(), dataProto.size(), Net net2 = readNetFromCaffe(dataProto.data(), dataProto.size(),
dataModel.c_str(), dataModel.size()); dataModel.data(), dataModel.size());
ASSERT_FALSE(net2.empty()); ASSERT_FALSE(net2.empty());
} }
...@@ -124,13 +124,13 @@ TEST_P(Reproducibility_AlexNet, Accuracy) ...@@ -124,13 +124,13 @@ TEST_P(Reproducibility_AlexNet, Accuracy)
const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false); const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
if (readFromMemory) if (readFromMemory)
{ {
string dataProto; std::vector<char> dataProto;
ASSERT_TRUE(readFileInMemory(proto, dataProto)); readFileContent(proto, dataProto);
string dataModel; std::vector<char> dataModel;
ASSERT_TRUE(readFileInMemory(model, dataModel)); readFileContent(model, dataModel);
net = readNetFromCaffe(dataProto.c_str(), dataProto.size(), net = readNetFromCaffe(dataProto.data(), dataProto.size(),
dataModel.c_str(), dataModel.size()); dataModel.data(), dataModel.size());
} }
else else
net = readNetFromCaffe(proto, model); net = readNetFromCaffe(proto, model);
......
...@@ -59,7 +59,7 @@ void normAssertDetections( ...@@ -59,7 +59,7 @@ void normAssertDetections(
double confThreshold = 0.0, double scores_diff = 1e-5, double confThreshold = 0.0, double scores_diff = 1e-5,
double boxes_iou_diff = 1e-4); double boxes_iou_diff = 1e-4);
bool readFileInMemory(const std::string& filename, std::string& content); void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content);
#ifdef HAVE_INF_ENGINE #ifdef HAVE_INF_ENGINE
bool validateVPUType(); bool validateVPUType();
......
...@@ -158,23 +158,21 @@ void normAssertDetections( ...@@ -158,23 +158,21 @@ void normAssertDetections(
testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff); testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff);
} }
bool readFileInMemory(const std::string& filename, std::string& content) void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content)
{ {
std::ios::openmode mode = std::ios::in | std::ios::binary; const std::ios::openmode mode = std::ios::in | std::ios::binary;
std::ifstream ifs(filename.c_str(), mode); std::ifstream ifs(filename.c_str(), mode);
if (!ifs.is_open()) ASSERT_TRUE(ifs.is_open());
return false;
content.clear(); content.clear();
ifs.seekg(0, std::ios::end); ifs.seekg(0, std::ios::end);
content.reserve(ifs.tellg()); const size_t sz = ifs.tellg();
content.resize(sz);
ifs.seekg(0, std::ios::beg); ifs.seekg(0, std::ios::beg);
content.assign((std::istreambuf_iterator<char>(ifs)), ifs.read((char*)content.data(), sz);
std::istreambuf_iterator<char>()); ASSERT_FALSE(ifs.fail());
return true;
} }
......
...@@ -93,11 +93,11 @@ TEST(Test_Darknet, read_yolo_voc_stream) ...@@ -93,11 +93,11 @@ TEST(Test_Darknet, read_yolo_voc_stream)
} }
// Import from bytes array. // Import from bytes array.
{ {
std::string cfg, weights; std::vector<char> cfg, weights;
readFileInMemory(cfgFile, cfg); readFileContent(cfgFile, cfg);
readFileInMemory(weightsFile, weights); readFileContent(weightsFile, weights);
Net net = readNetFromDarknet(&cfg[0], cfg.size(), &weights[0], weights.size()); Net net = readNetFromDarknet(cfg.data(), cfg.size(), weights.data(), weights.size());
net.setInput(inp); net.setInput(inp);
net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setPreferableBackend(DNN_BACKEND_OPENCV);
Mat out = net.forward(); Mat out = net.forward();
......
...@@ -96,17 +96,17 @@ public: ...@@ -96,17 +96,17 @@ public:
if (memoryLoad) if (memoryLoad)
{ {
// Load files into a memory buffers // Load files into a memory buffers
string dataModel; std::vector<char> dataModel;
ASSERT_TRUE(readFileInMemory(netPath, dataModel)); readFileContent(netPath, dataModel);
string dataConfig; std::vector<char> dataConfig;
if (hasText) if (hasText)
{ {
ASSERT_TRUE(readFileInMemory(netConfig, dataConfig)); readFileContent(netConfig, dataConfig);
} }
net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(), net = readNetFromTensorflow(dataModel.data(), dataModel.size(),
dataConfig.c_str(), dataConfig.size()); dataConfig.data(), dataConfig.size());
} }
else else
net = readNetFromTensorflow(netPath, netConfig); net = readNetFromTensorflow(netPath, netConfig);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment