mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
Merge pull request #10979 from dkurt:unite_dnn_samples
This commit is contained in:
@@ -3159,7 +3159,7 @@ protected:
|
||||
|
||||
struct Param {
|
||||
enum { INT=0, BOOLEAN=1, REAL=2, STRING=3, MAT=4, MAT_VECTOR=5, ALGORITHM=6, FLOAT=7,
|
||||
UNSIGNED_INT=8, UINT64=9, UCHAR=11 };
|
||||
UNSIGNED_INT=8, UINT64=9, UCHAR=11, SCALAR=12 };
|
||||
};
|
||||
|
||||
|
||||
@@ -3252,6 +3252,14 @@ template<> struct ParamType<uchar>
|
||||
enum { type = Param::UCHAR };
|
||||
};
|
||||
|
||||
template<> struct ParamType<Scalar>
|
||||
{
|
||||
typedef const Scalar& const_param_type;
|
||||
typedef Scalar member_type;
|
||||
|
||||
enum { type = Param::SCALAR };
|
||||
};
|
||||
|
||||
//! @} core_basic
|
||||
|
||||
} //namespace cv
|
||||
|
||||
@@ -104,6 +104,12 @@ static void from_str(const String& str, int type, void* dst)
|
||||
ss >> *(double*)dst;
|
||||
else if( type == Param::STRING )
|
||||
*(String*)dst = str;
|
||||
else if( type == Param::SCALAR)
|
||||
{
|
||||
Scalar& scalar = *(Scalar*)dst;
|
||||
for (int i = 0; i < 4 && !ss.eof(); ++i)
|
||||
ss >> scalar[i];
|
||||
}
|
||||
else
|
||||
CV_Error(Error::StsBadArg, "unknown/unsupported parameter type");
|
||||
|
||||
|
||||
@@ -261,4 +261,26 @@ TEST(AutoBuffer, allocate_test)
|
||||
EXPECT_EQ(6u, abuf.size());
|
||||
}
|
||||
|
||||
TEST(CommandLineParser, testScalar)
|
||||
{
|
||||
static const char * const keys3 =
|
||||
"{ s0 | 3 4 5 | default scalar }"
|
||||
"{ s1 | | single value scalar }"
|
||||
"{ s2 | | two values scalar (default with zeros) }"
|
||||
"{ s3 | | three values scalar }"
|
||||
"{ s4 | | four values scalar }"
|
||||
"{ s5 | | five values scalar }";
|
||||
|
||||
const char* argv[] = {"<bin>", "--s1=1.1", "--s3=1.1 2.2 3",
|
||||
"--s4=-4.2 1 0 3", "--s5=5 -4 3 2 1"};
|
||||
const int argc = 5;
|
||||
CommandLineParser parser(argc, argv, keys3);
|
||||
EXPECT_EQ(parser.get<Scalar>("s0"), Scalar(3, 4, 5));
|
||||
EXPECT_EQ(parser.get<Scalar>("s1"), Scalar(1.1));
|
||||
EXPECT_EQ(parser.get<Scalar>("s2"), Scalar(0));
|
||||
EXPECT_EQ(parser.get<Scalar>("s3"), Scalar(1.1, 2.2, 3));
|
||||
EXPECT_EQ(parser.get<Scalar>("s4"), Scalar(-4.2, 1, 0, 3));
|
||||
EXPECT_EQ(parser.get<Scalar>("s5"), Scalar(5, -4, 3, 2));
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
@@ -153,7 +153,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
*/
|
||||
|
||||
int inputNameToIndex(String inputName);
|
||||
int outputNameToIndex(String outputName);
|
||||
int outputNameToIndex(const String& outputName);
|
||||
};
|
||||
|
||||
/** @brief Classical recurrent layer
|
||||
|
||||
@@ -222,7 +222,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
/** @brief Returns index of output blob in output array.
|
||||
* @see inputNameToIndex()
|
||||
*/
|
||||
virtual int outputNameToIndex(String outputName);
|
||||
CV_WRAP virtual int outputNameToIndex(const String& outputName);
|
||||
|
||||
/**
|
||||
* @brief Ask layer if it support specific backend for doing computations.
|
||||
@@ -683,6 +683,29 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
*/
|
||||
CV_EXPORTS_W Net readNetFromTorch(const String &model, bool isBinary = true);
|
||||
|
||||
/**
|
||||
* @brief Read deep learning network represented in one of the supported formats.
|
||||
* @param[in] model Binary file contains trained weights. The following file
|
||||
* extensions are expected for models from different frameworks:
|
||||
* * `*.caffemodel` (Caffe, http://caffe.berkeleyvision.org/)
|
||||
* * `*.pb` (TensorFlow, https://www.tensorflow.org/)
|
||||
* * `*.t7` | `*.net` (Torch, http://torch.ch/)
|
||||
* * `*.weights` (Darknet, https://pjreddie.com/darknet/)
|
||||
* @param[in] config Text file contains network configuration. It could be a
|
||||
* file with the following extensions:
|
||||
* * `*.prototxt` (Caffe, http://caffe.berkeleyvision.org/)
|
||||
* * `*.pbtxt` (TensorFlow, https://www.tensorflow.org/)
|
||||
* * `*.cfg` (Darknet, https://pjreddie.com/darknet/)
|
||||
* @param[in] framework Explicit framework name tag to determine a format.
|
||||
* @returns Net object.
|
||||
*
|
||||
* This function automatically detects an origin framework of trained model
|
||||
* and calls an appropriate function such @ref readNetFromCaffe, @ref readNetFromTensorflow,
|
||||
* @ref readNetFromTorch or @ref readNetFromDarknet. An order of @p model and @p config
|
||||
* arguments does not matter.
|
||||
*/
|
||||
CV_EXPORTS_W Net readNet(const String& model, const String& config = "", const String& framework = "");
|
||||
|
||||
/** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework.
|
||||
* @warning This function has the same limitations as readNetFromTorch().
|
||||
*/
|
||||
|
||||
+40
-2
@@ -399,7 +399,7 @@ struct DataLayer : public Layer
|
||||
void forward(std::vector<Mat*>&, std::vector<Mat>&, std::vector<Mat> &) {}
|
||||
void forward(InputArrayOfArrays inputs, OutputArrayOfArrays outputs, OutputArrayOfArrays internals) {}
|
||||
|
||||
int outputNameToIndex(String tgtName)
|
||||
int outputNameToIndex(const String& tgtName)
|
||||
{
|
||||
int idx = (int)(std::find(outNames.begin(), outNames.end(), tgtName) - outNames.begin());
|
||||
return (idx < (int)outNames.size()) ? idx : -1;
|
||||
@@ -2521,7 +2521,7 @@ int Layer::inputNameToIndex(String)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Layer::outputNameToIndex(String)
|
||||
int Layer::outputNameToIndex(const String&)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@@ -2813,5 +2813,43 @@ BackendWrapper::BackendWrapper(const Ptr<BackendWrapper>& base, const MatShape&
|
||||
|
||||
BackendWrapper::~BackendWrapper() {}
|
||||
|
||||
Net readNet(const String& _model, const String& _config, const String& _framework)
|
||||
{
|
||||
String framework = _framework.toLowerCase();
|
||||
String model = _model;
|
||||
String config = _config;
|
||||
const std::string modelExt = model.substr(model.rfind('.') + 1);
|
||||
const std::string configExt = config.substr(config.rfind('.') + 1);
|
||||
if (framework == "caffe" || modelExt == "caffemodel" || configExt == "caffemodel" ||
|
||||
modelExt == "prototxt" || configExt == "prototxt")
|
||||
{
|
||||
if (modelExt == "prototxt" || configExt == "caffemodel")
|
||||
std::swap(model, config);
|
||||
return readNetFromCaffe(config, model);
|
||||
}
|
||||
if (framework == "tensorflow" || modelExt == "pb" || configExt == "pb" ||
|
||||
modelExt == "pbtxt" || configExt == "pbtxt")
|
||||
{
|
||||
if (modelExt == "pbtxt" || configExt == "pb")
|
||||
std::swap(model, config);
|
||||
return readNetFromTensorflow(model, config);
|
||||
}
|
||||
if (framework == "torch" || modelExt == "t7" || modelExt == "net" ||
|
||||
configExt == "t7" || configExt == "net")
|
||||
{
|
||||
return readNetFromTorch(model.empty() ? config : model);
|
||||
}
|
||||
if (framework == "darknet" || modelExt == "weights" || configExt == "weights" ||
|
||||
modelExt == "cfg" || configExt == "cfg")
|
||||
{
|
||||
if (modelExt == "cfg" || configExt == "weights")
|
||||
std::swap(model, config);
|
||||
return readNetFromDarknet(config, model);
|
||||
}
|
||||
CV_Error(Error::StsError, "Cannot determine an origin framework of files: " +
|
||||
model + (config.empty() ? "" : ", " + config));
|
||||
return Net();
|
||||
}
|
||||
|
||||
CV__DNN_EXPERIMENTAL_NS_END
|
||||
}} // namespace
|
||||
|
||||
@@ -355,7 +355,7 @@ int LSTMLayer::inputNameToIndex(String inputName)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int LSTMLayer::outputNameToIndex(String outputName)
|
||||
int LSTMLayer::outputNameToIndex(const String& outputName)
|
||||
{
|
||||
if (outputName.toLowerCase() == "h")
|
||||
return 0;
|
||||
|
||||
@@ -57,4 +57,22 @@ TEST(imagesFromBlob, Regression)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(readNet, Regression)
|
||||
{
|
||||
Net net = readNet(findDataFile("dnn/squeezenet_v1.1.prototxt", false),
|
||||
findDataFile("dnn/squeezenet_v1.1.caffemodel", false));
|
||||
EXPECT_FALSE(net.empty());
|
||||
net = readNet(findDataFile("dnn/opencv_face_detector.caffemodel", false),
|
||||
findDataFile("dnn/opencv_face_detector.prototxt", false));
|
||||
EXPECT_FALSE(net.empty());
|
||||
net = readNet(findDataFile("dnn/openface_nn4.small2.v1.t7", false));
|
||||
EXPECT_FALSE(net.empty());
|
||||
net = readNet(findDataFile("dnn/tiny-yolo-voc.cfg", false),
|
||||
findDataFile("dnn/tiny-yolo-voc.weights", false));
|
||||
EXPECT_FALSE(net.empty());
|
||||
net = readNet(findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false),
|
||||
findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false));
|
||||
EXPECT_FALSE(net.empty());
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
Reference in New Issue
Block a user