mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 22:44:02 +08:00
Replace std::vector<char> to std::vector<uchar> for Java bindings of dnn importers
This commit is contained in:
parent
d57e5406f0
commit
8b5f061dae
@ -649,8 +649,8 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
* @param bufferModel A buffer contains a content of .weights file with learned network.
|
||||
* @returns Net object.
|
||||
*/
|
||||
CV_EXPORTS_W Net readNetFromDarknet(const std::vector<char>& bufferCfg,
|
||||
const std::vector<char>& bufferModel = std::vector<char>());
|
||||
CV_EXPORTS_W Net readNetFromDarknet(const std::vector<uchar>& bufferCfg,
|
||||
const std::vector<uchar>& bufferModel = std::vector<uchar>());
|
||||
|
||||
/** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
|
||||
* @param bufferCfg A buffer contains a content of .cfg file with text description of the network architecture.
|
||||
@ -674,8 +674,8 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
* @param bufferModel buffer containing the content of the .caffemodel file
|
||||
* @returns Net object.
|
||||
*/
|
||||
CV_EXPORTS_W Net readNetFromCaffe(const std::vector<char>& bufferProto,
|
||||
const std::vector<char>& bufferModel = std::vector<char>());
|
||||
CV_EXPORTS_W Net readNetFromCaffe(const std::vector<uchar>& bufferProto,
|
||||
const std::vector<uchar>& bufferModel = std::vector<uchar>());
|
||||
|
||||
/** @brief Reads a network model stored in Caffe model in memory.
|
||||
* @details This is an overloaded member function, provided for convenience.
|
||||
@ -703,8 +703,8 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
* @param bufferConfig buffer containing the content of the pbtxt file
|
||||
* @returns Net object.
|
||||
*/
|
||||
CV_EXPORTS_W Net readNetFromTensorflow(const std::vector<char>& bufferModel,
|
||||
const std::vector<char>& bufferConfig = std::vector<char>());
|
||||
CV_EXPORTS_W Net readNetFromTensorflow(const std::vector<uchar>& bufferModel,
|
||||
const std::vector<uchar>& bufferConfig = std::vector<uchar>());
|
||||
|
||||
/** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format.
|
||||
* @details This is an overloaded member function, provided for convenience.
|
||||
@ -778,8 +778,8 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
* @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<char>& bufferModel,
|
||||
const std::vector<char>& bufferConfig = std::vector<char>());
|
||||
CV_EXPORTS_W Net readNet(const String& framework, const std::vector<uchar>& bufferModel,
|
||||
const std::vector<uchar>& bufferConfig = std::vector<uchar>());
|
||||
|
||||
/** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework.
|
||||
* @warning This function has the same limitations as readNetFromTorch().
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -453,10 +453,13 @@ Net readNetFromCaffe(const char *bufferProto, size_t lenProto,
|
||||
return net;
|
||||
}
|
||||
|
||||
Net readNetFromCaffe(const std::vector<char>& bufferProto, const std::vector<char>& bufferModel)
|
||||
Net readNetFromCaffe(const std::vector<uchar>& bufferProto, const std::vector<uchar>& bufferModel)
|
||||
{
|
||||
return readNetFromCaffe(&bufferProto[0], bufferProto.size(),
|
||||
bufferModel.empty() ? NULL : &bufferModel[0], bufferModel.size());
|
||||
const char* bufferProtoPtr = reinterpret_cast<const char*>(&bufferProto[0]);
|
||||
const char* bufferModelPtr = bufferModel.empty() ? NULL :
|
||||
reinterpret_cast<const char*>(&bufferModel[0]);
|
||||
return readNetFromCaffe(bufferProtoPtr, bufferProto.size(),
|
||||
bufferModelPtr, bufferModel.size());
|
||||
}
|
||||
|
||||
#endif //HAVE_PROTOBUF
|
||||
|
@ -242,10 +242,13 @@ Net readNetFromDarknet(const char *bufferCfg, size_t lenCfg, const char *bufferM
|
||||
return readNetFromDarknet(cfgStream);
|
||||
}
|
||||
|
||||
Net readNetFromDarknet(const std::vector<char>& bufferCfg, const std::vector<char>& bufferModel)
|
||||
Net readNetFromDarknet(const std::vector<uchar>& bufferCfg, const std::vector<uchar>& bufferModel)
|
||||
{
|
||||
return readNetFromDarknet(&bufferCfg[0], bufferCfg.size(),
|
||||
bufferModel.empty() ? NULL : &bufferModel[0], bufferModel.size());
|
||||
const char* bufferCfgPtr = reinterpret_cast<const char*>(&bufferCfg[0]);
|
||||
const char* bufferModelPtr = bufferModel.empty() ? NULL :
|
||||
reinterpret_cast<const char*>(&bufferModel[0]);
|
||||
return readNetFromDarknet(bufferCfgPtr, bufferCfg.size(),
|
||||
bufferModelPtr, bufferModel.size());
|
||||
}
|
||||
|
||||
CV__DNN_EXPERIMENTAL_NS_END
|
||||
|
@ -3047,8 +3047,8 @@ Net readNet(const String& _model, const String& _config, const String& _framewor
|
||||
model + (config.empty() ? "" : ", " + config));
|
||||
}
|
||||
|
||||
Net readNet(const String& _framework, const std::vector<char>& bufferModel,
|
||||
const std::vector<char>& bufferConfig)
|
||||
Net readNet(const String& _framework, const std::vector<uchar>& bufferModel,
|
||||
const std::vector<uchar>& bufferConfig)
|
||||
{
|
||||
String framework = _framework.toLowerCase();
|
||||
if (framework == "caffe")
|
||||
|
@ -1856,10 +1856,13 @@ Net readNetFromTensorflow(const char* bufferModel, size_t lenModel,
|
||||
return net;
|
||||
}
|
||||
|
||||
Net readNetFromTensorflow(const std::vector<char>& bufferModel, const std::vector<char>& bufferConfig)
|
||||
Net readNetFromTensorflow(const std::vector<uchar>& bufferModel, const std::vector<uchar>& bufferConfig)
|
||||
{
|
||||
return readNetFromCaffe(&bufferModel[0], bufferModel.size(),
|
||||
bufferConfig.empty() ? NULL : &bufferConfig[0], bufferConfig.size());
|
||||
const char* bufferModelPtr = reinterpret_cast<const char*>(&bufferModel[0]);
|
||||
const char* bufferConfigPtr = bufferConfig.empty() ? NULL :
|
||||
reinterpret_cast<const char*>(&bufferConfig[0]);
|
||||
return readNetFromTensorflow(bufferModelPtr, bufferModel.size(),
|
||||
bufferConfigPtr, bufferConfig.size());
|
||||
}
|
||||
|
||||
CV__DNN_EXPERIMENTAL_NS_END
|
||||
|
Loading…
Reference in New Issue
Block a user