TEST(Test_Darknet, read_yolo_voc_stream) { Mat ref; Mat sample = imread(_tf("dog416.png")); Mat inp = blobFromImage(sample, 1.0/255, Size(416, 416), Scalar(), true, false); const std::string cfgFile = findDataFile("dnn/yolo-voc.cfg", false); const std::string weightsFile = findDataFile("dnn/yolo-voc.weights", false); // Import by paths. { Net net = readNetFromDarknet(cfgFile, weightsFile); net.setInput(inp); net.setPreferableBackend(DNN_BACKEND_OPENCV); ref = net.forward(); } // Import from bytes array. { std::string cfg, weights; readFileInMemory(cfgFile, cfg); readFileInMemory(weightsFile, weights); Net net = readNetFromDarknet(&cfg[0], cfg.size(), &weights[0], weights.size()); net.setInput(inp); net.setPreferableBackend(DNN_BACKEND_OPENCV); Mat out = net.forward(); normAssert(ref, out); } }
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)); Net net = readNetFromCaffe(dataProto.c_str(), dataProto.size()); ASSERT_FALSE(net.empty()); Net net2 = readNetFromCaffe(dataProto.c_str(), dataProto.size(), dataModel.c_str(), dataModel.size()); ASSERT_FALSE(net2.empty()); }
void runTensorFlowNet(const std::string& prefix, bool hasText = false, double l1 = 0.0, double lInf = 0.0, bool memoryLoad = false) { std::string netPath = path(prefix + "_net.pb"); std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : ""); std::string inpPath = path(prefix + "_in.npy"); std::string outPath = path(prefix + "_out.npy"); cv::Mat input = blobFromNPY(inpPath); cv::Mat ref = blobFromNPY(outPath); checkBackend(&input, &ref); Net net; if (memoryLoad) { // Load files into a memory buffers string dataModel; ASSERT_TRUE(readFileInMemory(netPath, dataModel)); string dataConfig; if (hasText) { ASSERT_TRUE(readFileInMemory(netConfig, dataConfig)); } net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(), dataConfig.c_str(), dataConfig.size()); } else net = readNetFromTensorflow(netPath, netConfig); ASSERT_FALSE(net.empty()); net.setPreferableBackend(backend); net.setPreferableTarget(target); net.setInput(input); cv::Mat output = net.forward(); normAssert(ref, output, "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf); }
static void runTensorFlowNet(const std::string& prefix, int targetId = DNN_TARGET_CPU, bool hasText = false, double l1 = 1e-5, double lInf = 1e-4, bool memoryLoad = false) { std::string netPath = path(prefix + "_net.pb"); std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : ""); std::string inpPath = path(prefix + "_in.npy"); std::string outPath = path(prefix + "_out.npy"); Net net; if (memoryLoad) { // Load files into a memory buffers string dataModel; ASSERT_TRUE(readFileInMemory(netPath, dataModel)); string dataConfig; if (hasText) ASSERT_TRUE(readFileInMemory(netConfig, dataConfig)); net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(), dataConfig.c_str(), dataConfig.size()); } else net = readNetFromTensorflow(netPath, netConfig); ASSERT_FALSE(net.empty()); net.setPreferableBackend(DNN_BACKEND_DEFAULT); net.setPreferableTarget(targetId); cv::Mat input = blobFromNPY(inpPath); cv::Mat target = blobFromNPY(outPath); net.setInput(input); cv::Mat output = net.forward(); normAssert(target, output, "", l1, lInf); }
TEST_P(Reproducibility_AlexNet, Accuracy) { bool readFromMemory = get<0>(GetParam()); Net net; { const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false); 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)); net = readNetFromCaffe(dataProto.c_str(), dataProto.size(), dataModel.c_str(), dataModel.size()); } else net = readNetFromCaffe(proto, model); ASSERT_FALSE(net.empty()); } int targetId = get<1>(GetParam()); const float l1 = 1e-5; const float lInf = (targetId == DNN_TARGET_OPENCL_FP16) ? 3e-3 : 1e-4; net.setPreferableTarget(targetId); Mat sample = imread(_tf("grace_hopper_227.png")); ASSERT_TRUE(!sample.empty()); net.setInput(blobFromImage(sample, 1.0f, Size(227, 227), Scalar(), false), "data"); Mat out = net.forward("prob"); Mat ref = blobFromNPY(_tf("caffe_alexnet_prob.npy")); normAssert(ref, out, "", l1, lInf); }