dnn(test): replace file content reading
authorAlexander Alekhin <alexander.alekhin@intel.com>
Wed, 29 May 2019 12:29:31 +0000 (15:29 +0300)
committerAlexander Alekhin <alexander.alekhin@intel.com>
Wed, 29 May 2019 20:08:33 +0000 (23:08 +0300)
modules/dnn/test/test_caffe_importer.cpp
modules/dnn/test/test_common.hpp
modules/dnn/test/test_common.impl.hpp
modules/dnn/test/test_darknet_importer.cpp
modules/dnn/test/test_tf_importer.cpp

index 43236ff..f9f17b6 100644 (file)
@@ -83,17 +83,17 @@ TEST(Test_Caffe, memory_read)
     const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);
     const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
 
-    string dataProto;
-    ASSERT_TRUE(readFileInMemory(proto, dataProto));
-    string dataModel;
-    ASSERT_TRUE(readFileInMemory(model, dataModel));
+    std::vector<char> dataProto;
+    readFileContent(proto, dataProto);
+    std::vector<char> dataModel;
+    readFileContent(model, dataModel);
 
-    Net net = readNetFromCaffe(dataProto.c_str(), dataProto.size());
+    Net net = readNetFromCaffe(dataProto.data(), dataProto.size());
     net.setPreferableBackend(DNN_BACKEND_OPENCV);
     ASSERT_FALSE(net.empty());
 
-    Net net2 = readNetFromCaffe(dataProto.c_str(), dataProto.size(),
-                                dataModel.c_str(), dataModel.size());
+    Net net2 = readNetFromCaffe(dataProto.data(), dataProto.size(),
+                                dataModel.data(), dataModel.size());
     ASSERT_FALSE(net2.empty());
 }
 
@@ -124,13 +124,13 @@ TEST_P(Reproducibility_AlexNet, Accuracy)
         const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
         if (readFromMemory)
         {
-            string dataProto;
-            ASSERT_TRUE(readFileInMemory(proto, dataProto));
-            string dataModel;
-            ASSERT_TRUE(readFileInMemory(model, dataModel));
+            std::vector<char> dataProto;
+            readFileContent(proto, dataProto);
+            std::vector<char> dataModel;
+            readFileContent(model, dataModel);
 
-            net = readNetFromCaffe(dataProto.c_str(), dataProto.size(),
-                                   dataModel.c_str(), dataModel.size());
+            net = readNetFromCaffe(dataProto.data(), dataProto.size(),
+                                   dataModel.data(), dataModel.size());
         }
         else
             net = readNetFromCaffe(proto, model);
index 0904a4f..debde65 100644 (file)
@@ -59,7 +59,7 @@ void normAssertDetections(
         double confThreshold = 0.0, double scores_diff = 1e-5,
         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
 bool validateVPUType();
index 51c1c5e..60dedbf 100644 (file)
@@ -158,23 +158,21 @@ void normAssertDetections(
                          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);
-    if (!ifs.is_open())
-        return false;
+    ASSERT_TRUE(ifs.is_open());
 
     content.clear();
 
     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);
 
-    content.assign((std::istreambuf_iterator<char>(ifs)),
-                   std::istreambuf_iterator<char>());
-
-    return true;
+    ifs.read((char*)content.data(), sz);
+    ASSERT_FALSE(ifs.fail());
 }
 
 
index ffc71b6..683c22e 100644 (file)
@@ -93,11 +93,11 @@ TEST(Test_Darknet, read_yolo_voc_stream)
     }
     // Import from bytes array.
     {
-        std::string cfg, weights;
-        readFileInMemory(cfgFile, cfg);
-        readFileInMemory(weightsFile, weights);
+        std::vector<char> cfg, weights;
+        readFileContent(cfgFile, cfg);
+        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.setPreferableBackend(DNN_BACKEND_OPENCV);
         Mat out = net.forward();
index afe9287..abbcf9f 100644 (file)
@@ -96,17 +96,17 @@ public:
         if (memoryLoad)
         {
             // Load files into a memory buffers
-            string dataModel;
-            ASSERT_TRUE(readFileInMemory(netPath, dataModel));
+            std::vector<char> dataModel;
+            readFileContent(netPath, dataModel);
 
-            string dataConfig;
+            std::vector<char> dataConfig;
             if (hasText)
             {
-                ASSERT_TRUE(readFileInMemory(netConfig, dataConfig));
+                readFileContent(netConfig, dataConfig);
             }
 
-            net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(),
-                                        dataConfig.c_str(), dataConfig.size());
+            net = readNetFromTensorflow(dataModel.data(), dataModel.size(),
+                                        dataConfig.data(), dataConfig.size());
         }
         else
             net = readNetFromTensorflow(netPath, netConfig);