diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp
index f65f503529..7cc95ca0c4 100644
--- a/modules/dnn/include/opencv2/dnn/dnn.hpp
+++ b/modules/dnn/include/opencv2/dnn/dnn.hpp
@@ -644,6 +644,24 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/
CV_EXPORTS_W Net readNetFromDarknet(const String &cfgFile, const String &darknetModel = String());
+ /** @brief Reads a network model stored in Darknet model files.
+ * @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
+ * @param bufferModel A buffer contains a content of .weights file with learned network.
+ * @returns Net object.
+ */
+ CV_EXPORTS_W Net readNetFromDarknet(const std::vector& bufferCfg,
+ const std::vector& bufferModel = std::vector());
+
+ /** @brief Reads a network model stored in Darknet model files.
+ * @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
+ * @param lenCfg Number of bytes to read from bufferCfg
+ * @param bufferModel A buffer contains a content of .weights file with learned network.
+ * @param lenModel Number of bytes to read from bufferModel
+ * @returns Net object.
+ */
+ CV_EXPORTS Net readNetFromDarknet(const char *bufferCfg, size_t lenCfg,
+ const char *bufferModel = NULL, size_t lenModel = 0);
+
/** @brief Reads a network model stored in Caffe framework's format.
* @param prototxt path to the .prototxt file with text description of the network architecture.
* @param caffeModel path to the .caffemodel file with learned network.
@@ -651,6 +669,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/
CV_EXPORTS_W Net readNetFromCaffe(const String &prototxt, const String &caffeModel = String());
+ /** @brief Reads a network model stored in Caffe model in memory.
+ * @param bufferProto buffer containing the content of the .prototxt file
+ * @param bufferModel buffer containing the content of the .caffemodel file
+ * @returns Net object.
+ */
+ CV_EXPORTS_W Net readNetFromCaffe(const std::vector& bufferProto,
+ const std::vector& bufferModel = std::vector());
+
/** @brief Reads a network model stored in Caffe model in memory.
* @details This is an overloaded member function, provided for convenience.
* It differs from the above function only in what argument(s) it accepts.
@@ -672,6 +698,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/
CV_EXPORTS_W Net readNetFromTensorflow(const String &model, const String &config = String());
+ /** @brief Reads a network model stored in TensorFlow framework's format.
+ * @param bufferModel buffer containing the content of the pb file
+ * @param bufferConfig buffer containing the content of the pbtxt file
+ * @returns Net object.
+ */
+ CV_EXPORTS_W Net readNetFromTensorflow(const std::vector& bufferModel,
+ const std::vector& bufferConfig = std::vector());
+
/** @brief Reads a network model stored in TensorFlow framework's format.
* @details This is an overloaded member function, provided for convenience.
* It differs from the above function only in what argument(s) it accepts.
@@ -735,6 +769,18 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/
CV_EXPORTS_W Net readNet(const String& model, const String& config = "", const String& framework = "");
+ /**
+ * @brief Read deep learning network represented in one of the supported formats.
+ * @details This is an overloaded member function, provided for convenience.
+ * It differs from the above function only in what argument(s) it accepts.
+ * @param[in] framework Name of origin framework.
+ * @param[in] bufferModel A buffer with a content of binary file with weights
+ * @param[in] bufferConfig A buffer with a content of text file contains network configuration.
+ * @returns Net object.
+ */
+ CV_EXPORTS_W Net readNet(const String& framework, const std::vector& bufferModel,
+ const std::vector& bufferConfig = std::vector());
+
/** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework.
* @warning This function has the same limitations as readNetFromTorch().
*/
diff --git a/modules/dnn/misc/java/test/DnnTensorFlowTest.java b/modules/dnn/misc/java/test/DnnTensorFlowTest.java
index 5dd423649e..4e96c73e28 100644
--- a/modules/dnn/misc/java/test/DnnTensorFlowTest.java
+++ b/modules/dnn/misc/java/test/DnnTensorFlowTest.java
@@ -1,10 +1,14 @@
package org.opencv.test.dnn;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
+import org.opencv.core.MatOfFloat;
+import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.DictValue;
@@ -26,6 +30,15 @@ public class DnnTensorFlowTest extends OpenCVTestCase {
Net net;
+ private static void normAssert(Mat ref, Mat test) {
+ final double l1 = 1e-5;
+ final double lInf = 1e-4;
+ double normL1 = Core.norm(ref, test, Core.NORM_L1) / ref.total();
+ double normLInf = Core.norm(ref, test, Core.NORM_INF) / ref.total();
+ assertTrue(normL1 < l1);
+ assertTrue(normLInf < lInf);
+ }
+
@Override
protected void setUp() throws Exception {
super.setUp();
@@ -46,7 +59,7 @@ public class DnnTensorFlowTest extends OpenCVTestCase {
File testDataPath = new File(envTestDataPath);
- File f = new File(testDataPath, "dnn/space_shuttle.jpg");
+ File f = new File(testDataPath, "dnn/grace_hopper_227.png");
sourceImageFile = f.toString();
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
@@ -77,31 +90,55 @@ public class DnnTensorFlowTest extends OpenCVTestCase {
}
- public void testTestNetForward() {
- Mat rawImage = Imgcodecs.imread(sourceImageFile);
+ public void checkInceptionNet(Net net)
+ {
+ Mat image = Imgcodecs.imread(sourceImageFile);
+ assertNotNull("Loading image from file failed!", image);
- assertNotNull("Loading image from file failed!", rawImage);
-
- Mat image = new Mat();
- Imgproc.resize(rawImage, image, new Size(224,224));
-
- Mat inputBlob = Dnn.blobFromImage(image);
+ Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
assertNotNull("Converting image to blob failed!", inputBlob);
- Mat inputBlobP = new Mat();
- Core.subtract(inputBlob, new Scalar(117.0), inputBlobP);
-
- net.setInput(inputBlobP, "input" );
-
- Mat result = net.forward();
+ net.setInput(inputBlob, "input");
+ Mat result = new Mat();
+ try {
+ net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
+ result = net.forward("softmax2");
+ }
+ catch (Exception e) {
+ fail("DNN forward failed: " + e.getMessage());
+ }
assertNotNull("Net returned no result!", result);
- Core.MinMaxLocResult minmax = Core.minMaxLoc(result.reshape(1, 1));
+ result = result.reshape(1, 1);
+ Core.MinMaxLocResult minmax = Core.minMaxLoc(result);
+ assertEquals("Wrong prediction", (int)minmax.maxLoc.x, 866);
- assertTrue("No image recognized!", minmax.maxVal > 0.9);
+ Mat top5RefScores = new MatOfFloat(new float[] {
+ 0.63032645f, 0.2561979f, 0.032181446f, 0.015721032f, 0.014785315f
+ }).reshape(1, 1);
+ Core.sort(result, result, Core.SORT_DESCENDING);
+ normAssert(result.colRange(0, 5), top5RefScores);
}
+ public void testTestNetForward() {
+ checkInceptionNet(net);
+ }
+
+ public void testReadFromBuffer() {
+ File modelFile = new File(modelFileName);
+ byte[] modelBuffer = new byte[ (int)modelFile.length() ];
+
+ try {
+ FileInputStream fis = new FileInputStream(modelFile);
+ fis.read(modelBuffer);
+ fis.close();
+ } catch (IOException e) {
+ fail("Failed to read a model: " + e.getMessage());
+ }
+ net = Dnn.readNetFromTensorflow(new MatOfByte(modelBuffer));
+ checkInceptionNet(net);
+ }
}
diff --git a/modules/dnn/src/caffe/caffe_importer.cpp b/modules/dnn/src/caffe/caffe_importer.cpp
index 37db7f039a..59f47eef1a 100644
--- a/modules/dnn/src/caffe/caffe_importer.cpp
+++ b/modules/dnn/src/caffe/caffe_importer.cpp
@@ -453,6 +453,15 @@ Net readNetFromCaffe(const char *bufferProto, size_t lenProto,
return net;
}
+Net readNetFromCaffe(const std::vector& bufferProto, const std::vector& bufferModel)
+{
+ const char* bufferProtoPtr = reinterpret_cast(&bufferProto[0]);
+ const char* bufferModelPtr = bufferModel.empty() ? NULL :
+ reinterpret_cast(&bufferModel[0]);
+ return readNetFromCaffe(bufferProtoPtr, bufferProto.size(),
+ bufferModelPtr, bufferModel.size());
+}
+
#endif //HAVE_PROTOBUF
CV__DNN_EXPERIMENTAL_NS_END
diff --git a/modules/dnn/src/darknet/darknet_importer.cpp b/modules/dnn/src/darknet/darknet_importer.cpp
index 8bd64d099c..282b37277c 100644
--- a/modules/dnn/src/darknet/darknet_importer.cpp
+++ b/modules/dnn/src/darknet/darknet_importer.cpp
@@ -44,6 +44,7 @@
#include "../precomp.hpp"
#include
+#include
#include
#include
#include