Commit bd8e6b7e authored by Dmitry Kurtaev's avatar Dmitry Kurtaev

Make external cv::dnn::Importer usage is deprecated

parent 37ba1d6f
...@@ -55,7 +55,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -55,7 +55,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
Classes listed here, in fact, provides C++ API for creating intances of bult-in layers. Classes listed here, in fact, provides C++ API for creating intances of bult-in layers.
In addition to this way of layers instantiation, there is a more common factory API (see @ref dnnLayerFactory), it allows to create layers dynamically (by name) and register new ones. In addition to this way of layers instantiation, there is a more common factory API (see @ref dnnLayerFactory), it allows to create layers dynamically (by name) and register new ones.
You can use both API, but factory API is less convinient for native C++ programming and basically designed for use inside importers (see @ref Importer, @ref createCaffeImporter(), @ref createTorchImporter()). You can use both API, but factory API is less convinient for native C++ programming and basically designed for use inside importers (see @ref readNetFromCaffe(), @ref readNetFromTorch(), @ref readNetFromTensorflow()).
Bult-in layers partially reproduce functionality of corresponding Caffe and Torch7 layers. Bult-in layers partially reproduce functionality of corresponding Caffe and Torch7 layers.
In partuclar, the following layers and Caffe @ref Importer were tested to reproduce <a href="http://caffe.berkeleyvision.org/tutorial/layers.html">Caffe</a> functionality: In partuclar, the following layers and Caffe @ref Importer were tested to reproduce <a href="http://caffe.berkeleyvision.org/tutorial/layers.html">Caffe</a> functionality:
......
...@@ -598,23 +598,27 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -598,23 +598,27 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
Ptr<Impl> impl; Ptr<Impl> impl;
}; };
/** @brief Small interface class for loading trained serialized models of different dnn-frameworks. */ /**
* @deprecated Deprecated as external interface. Will be for internal needs only.
* @brief Small interface class for loading trained serialized models of different dnn-frameworks. */
class CV_EXPORTS_W Importer : public Algorithm class CV_EXPORTS_W Importer : public Algorithm
{ {
public: public:
/** @brief Adds loaded layers into the @p net and sets connections between them. */ /** @brief Adds loaded layers into the @p net and sets connections between them. */
CV_WRAP virtual void populateNet(Net net) = 0; CV_DEPRECATED CV_WRAP virtual void populateNet(Net net) = 0;
virtual ~Importer(); virtual ~Importer();
}; };
/** @brief Creates the importer of <a href="http://caffe.berkeleyvision.org">Caffe</a> framework network. /**
* @deprecated Use @ref readNetFromCaffe instead.
* @brief Creates the importer of <a href="http://caffe.berkeleyvision.org">Caffe</a> framework network.
* @param prototxt path to the .prototxt file with text description of the network architecture. * @param prototxt path to the .prototxt file with text description of the network architecture.
* @param caffeModel path to the .caffemodel file with learned network. * @param caffeModel path to the .caffemodel file with learned network.
* @returns Pointer to the created importer, NULL in failure cases. * @returns Pointer to the created importer, NULL in failure cases.
*/ */
CV_EXPORTS_W Ptr<Importer> createCaffeImporter(const String &prototxt, const String &caffeModel = String()); CV_DEPRECATED CV_EXPORTS_W Ptr<Importer> createCaffeImporter(const String &prototxt, const String &caffeModel = String());
/** @brief Reads a network model stored in Caffe model files. /** @brief Reads a network model stored in Caffe model files.
* @details This is shortcut consisting from createCaffeImporter and Net::populateNet calls. * @details This is shortcut consisting from createCaffeImporter and Net::populateNet calls.
...@@ -631,13 +635,17 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -631,13 +635,17 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
*/ */
CV_EXPORTS_W Net readNetFromTorch(const String &model, bool isBinary = true); CV_EXPORTS_W Net readNetFromTorch(const String &model, bool isBinary = true);
/** @brief Creates the importer of <a href="http://www.tensorflow.org">TensorFlow</a> framework network. /**
* @deprecated Use @ref readNetFromTensorflow instead.
* @brief Creates the importer of <a href="http://www.tensorflow.org">TensorFlow</a> framework network.
* @param model path to the .pb file with binary protobuf description of the network architecture. * @param model path to the .pb file with binary protobuf description of the network architecture.
* @returns Pointer to the created importer, NULL in failure cases. * @returns Pointer to the created importer, NULL in failure cases.
*/ */
CV_EXPORTS_W Ptr<Importer> createTensorflowImporter(const String &model); CV_DEPRECATED CV_EXPORTS_W Ptr<Importer> createTensorflowImporter(const String &model);
/** @brief Creates the importer of <a href="http://torch.ch">Torch7</a> framework network. /**
* @deprecated Use @ref readNetFromTorch instead.
* @brief Creates the importer of <a href="http://torch.ch">Torch7</a> framework network.
* @param filename path to the file, dumped from Torch by using torch.save() function. * @param filename path to the file, dumped from Torch by using torch.save() function.
* @param isBinary specifies whether the network was serialized in ascii mode or binary. * @param isBinary specifies whether the network was serialized in ascii mode or binary.
* @returns Pointer to the created importer, NULL in failure cases. * @returns Pointer to the created importer, NULL in failure cases.
...@@ -663,7 +671,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN ...@@ -663,7 +671,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
* *
* Also some equivalents of these classes from cunn, cudnn, and fbcunn may be successfully imported. * Also some equivalents of these classes from cunn, cudnn, and fbcunn may be successfully imported.
*/ */
CV_EXPORTS_W Ptr<Importer> createTorchImporter(const String &filename, bool isBinary = true); CV_DEPRECATED CV_EXPORTS_W Ptr<Importer> createTorchImporter(const String &filename, bool isBinary = true);
/** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework. /** @brief Loads blob which was serialized as torch.Tensor object of Torch7 framework.
* @warning This function has the same limitations as createTorchImporter(). * @warning This function has the same limitations as createTorchImporter().
......
...@@ -51,12 +51,7 @@ public class DnnTensorFlowTest extends OpenCVTestCase { ...@@ -51,12 +51,7 @@ public class DnnTensorFlowTest extends OpenCVTestCase {
sourceImageFile = f.toString(); sourceImageFile = f.toString();
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile); if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
net = new Net(); net = Dnn.readNetFromTensorflow(modelFileName);
if(net.empty()) {
Importer importer = Dnn.createTensorflowImporter(modelFileName);
importer.populateNet(net);
}
} }
public void testGetLayerTypes() { public void testGetLayerTypes() {
......
...@@ -370,24 +370,15 @@ Ptr<Importer> createCaffeImporter(const String &prototxt, const String &caffeMod ...@@ -370,24 +370,15 @@ Ptr<Importer> createCaffeImporter(const String &prototxt, const String &caffeMod
return Ptr<Importer>(new CaffeImporter(prototxt.c_str(), caffeModel.c_str())); return Ptr<Importer>(new CaffeImporter(prototxt.c_str(), caffeModel.c_str()));
} }
#else //HAVE_PROTOBUF
Ptr<Importer> createCaffeImporter(const String&, const String&)
{
CV_Error(cv::Error::StsNotImplemented, "libprotobuf required to import data from Caffe models");
return Ptr<Importer>();
}
#endif //HAVE_PROTOBUF
Net readNetFromCaffe(const String &prototxt, const String &caffeModel /*= String()*/) Net readNetFromCaffe(const String &prototxt, const String &caffeModel /*= String()*/)
{ {
Ptr<Importer> caffeImporter = createCaffeImporter(prototxt, caffeModel); CaffeImporter caffeImporter(prototxt.c_str(), caffeModel.c_str());
Net net; Net net;
if (caffeImporter) caffeImporter.populateNet(net);
caffeImporter->populateNet(net);
return net; return net;
} }
#endif //HAVE_PROTOBUF
CV__DNN_EXPERIMENTAL_NS_END CV__DNN_EXPERIMENTAL_NS_END
}} // namespace }} // namespace
...@@ -1045,10 +1045,9 @@ Ptr<Importer> createTensorflowImporter(const String&) ...@@ -1045,10 +1045,9 @@ Ptr<Importer> createTensorflowImporter(const String&)
Net readNetFromTensorflow(const String &model) Net readNetFromTensorflow(const String &model)
{ {
Ptr<Importer> importer = createTensorflowImporter(model); TFImporter importer(model.c_str());
Net net; Net net;
if (importer) importer.populateNet(net);
importer->populateNet(net);
return net; return net;
} }
......
...@@ -1150,10 +1150,9 @@ Net readNetFromTorch(const String &model, bool isBinary) ...@@ -1150,10 +1150,9 @@ Net readNetFromTorch(const String &model, bool isBinary)
{ {
CV_TRACE_FUNCTION(); CV_TRACE_FUNCTION();
Ptr<Importer> importer = createTorchImporter(model, isBinary); TorchImporter importer(model, isBinary);
Net net; Net net;
if (importer) importer.populateNet(net);
importer->populateNet(net);
return net; return net;
} }
......
...@@ -57,22 +57,14 @@ static std::string _tf(TString filename) ...@@ -57,22 +57,14 @@ static std::string _tf(TString filename)
TEST(Test_Caffe, read_gtsrb) TEST(Test_Caffe, read_gtsrb)
{ {
Net net; Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));
{ ASSERT_FALSE(net.empty());
Ptr<Importer> importer = createCaffeImporter(_tf("gtsrb.prototxt"), "");
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
} }
TEST(Test_Caffe, read_googlenet) TEST(Test_Caffe, read_googlenet)
{ {
Net net; Net net = readNetFromCaffe(_tf("bvlc_googlenet.prototxt"));
{ ASSERT_FALSE(net.empty());
Ptr<Importer> importer = createCaffeImporter(_tf("bvlc_googlenet.prototxt"), "");
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
} }
TEST(Reproducibility_AlexNet, Accuracy) TEST(Reproducibility_AlexNet, Accuracy)
...@@ -81,9 +73,8 @@ TEST(Reproducibility_AlexNet, Accuracy) ...@@ -81,9 +73,8 @@ TEST(Reproducibility_AlexNet, Accuracy)
{ {
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false); const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false); const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
Ptr<Importer> importer = createCaffeImporter(proto, model); net = readNetFromCaffe(proto, model);
ASSERT_TRUE(importer != NULL); ASSERT_FALSE(net.empty());
importer->populateNet(net);
} }
Mat sample = imread(_tf("grace_hopper_227.png")); Mat sample = imread(_tf("grace_hopper_227.png"));
...@@ -107,9 +98,8 @@ TEST(Reproducibility_FCN, Accuracy) ...@@ -107,9 +98,8 @@ TEST(Reproducibility_FCN, Accuracy)
{ {
const string proto = findDataFile("dnn/fcn8s-heavy-pascal.prototxt", false); const string proto = findDataFile("dnn/fcn8s-heavy-pascal.prototxt", false);
const string model = findDataFile("dnn/fcn8s-heavy-pascal.caffemodel", false); const string model = findDataFile("dnn/fcn8s-heavy-pascal.caffemodel", false);
Ptr<Importer> importer = createCaffeImporter(proto, model); net = readNetFromCaffe(proto, model);
ASSERT_TRUE(importer != NULL); ASSERT_FALSE(net.empty());
importer->populateNet(net);
} }
Mat sample = imread(_tf("street.png")); Mat sample = imread(_tf("street.png"));
...@@ -136,9 +126,8 @@ TEST(Reproducibility_SSD, Accuracy) ...@@ -136,9 +126,8 @@ TEST(Reproducibility_SSD, Accuracy)
{ {
const string proto = findDataFile("dnn/ssd_vgg16.prototxt", false); const string proto = findDataFile("dnn/ssd_vgg16.prototxt", false);
const string model = findDataFile("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", false); const string model = findDataFile("dnn/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel", false);
Ptr<Importer> importer = createCaffeImporter(proto, model); net = readNetFromCaffe(proto, model);
ASSERT_TRUE(importer != NULL); ASSERT_FALSE(net.empty());
importer->populateNet(net);
} }
Mat sample = imread(_tf("street.png")); Mat sample = imread(_tf("street.png"));
......
...@@ -108,12 +108,8 @@ void testLayerUsingCaffeModels(String basename, bool useCaffeModel = false, bool ...@@ -108,12 +108,8 @@ void testLayerUsingCaffeModels(String basename, bool useCaffeModel = false, bool
cv::setNumThreads(cv::getNumberOfCPUs()); cv::setNumThreads(cv::getNumberOfCPUs());
Net net; Net net = readNetFromCaffe(prototxt, (useCaffeModel) ? caffemodel : String());
{ ASSERT_FALSE(net.empty());
Ptr<Importer> importer = createCaffeImporter(prototxt, (useCaffeModel) ? caffemodel : String());
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
Mat inp = blobFromNPY(inpfile); Mat inp = blobFromNPY(inpfile);
Mat ref = blobFromNPY(outfile); Mat ref = blobFromNPY(outfile);
...@@ -252,12 +248,8 @@ TEST(Layer_Test_Concat, Accuracy) ...@@ -252,12 +248,8 @@ TEST(Layer_Test_Concat, Accuracy)
static void test_Reshape_Split_Slice_layers() static void test_Reshape_Split_Slice_layers()
{ {
Net net; Net net = readNetFromCaffe(_tf("reshape_and_slice_routines.prototxt"));
{ ASSERT_FALSE(net.empty());
Ptr<Importer> importer = createCaffeImporter(_tf("reshape_and_slice_routines.prototxt"));
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
Mat input(6, 12, CV_32F); Mat input(6, 12, CV_32F);
RNG rng(0); RNG rng(0);
...@@ -276,12 +268,9 @@ TEST(Layer_Test_Reshape_Split_Slice, Accuracy) ...@@ -276,12 +268,9 @@ TEST(Layer_Test_Reshape_Split_Slice, Accuracy)
TEST(Layer_Conv_Elu, Accuracy) TEST(Layer_Conv_Elu, Accuracy)
{ {
Net net; Net net = readNetFromTensorflow(_tf("layer_elu_model.pb"));
{ ASSERT_FALSE(net.empty());
Ptr<Importer> importer = createTensorflowImporter(_tf("layer_elu_model.pb"));
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
}
Mat inp = blobFromNPY(_tf("layer_elu_in.npy")); Mat inp = blobFromNPY(_tf("layer_elu_in.npy"));
Mat ref = blobFromNPY(_tf("layer_elu_out.npy")); Mat ref = blobFromNPY(_tf("layer_elu_out.npy"));
......
...@@ -29,9 +29,8 @@ TEST(Test_TensorFlow, read_inception) ...@@ -29,9 +29,8 @@ TEST(Test_TensorFlow, read_inception)
Net net; Net net;
{ {
const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false); const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
Ptr<Importer> importer = createTensorflowImporter(model); net = readNetFromTensorflow(model);
ASSERT_TRUE(importer != NULL); ASSERT_FALSE(net.empty());
importer->populateNet(net);
} }
Mat sample = imread(_tf("grace_hopper_227.png")); Mat sample = imread(_tf("grace_hopper_227.png"));
...@@ -53,9 +52,8 @@ TEST(Test_TensorFlow, inception_accuracy) ...@@ -53,9 +52,8 @@ TEST(Test_TensorFlow, inception_accuracy)
Net net; Net net;
{ {
const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false); const string model = findDataFile("dnn/tensorflow_inception_graph.pb", false);
Ptr<Importer> importer = createTensorflowImporter(model); net = readNetFromTensorflow(model);
ASSERT_TRUE(importer != NULL); ASSERT_FALSE(net.empty());
importer->populateNet(net);
} }
Mat sample = imread(_tf("grace_hopper_227.png")); Mat sample = imread(_tf("grace_hopper_227.png"));
......
...@@ -66,11 +66,8 @@ static std::string _tf(TStr filename, bool inTorchDir = true) ...@@ -66,11 +66,8 @@ static std::string _tf(TStr filename, bool inTorchDir = true)
TEST(Torch_Importer, simple_read) TEST(Torch_Importer, simple_read)
{ {
Net net; Net net;
Ptr<Importer> importer; ASSERT_NO_THROW(net = readNetFromTorch(_tf("net_simple_net.txt"), false));
ASSERT_FALSE(net.empty());
ASSERT_NO_THROW( importer = createTorchImporter(_tf("net_simple_net.txt"), false) );
ASSERT_TRUE( importer != NULL );
importer->populateNet(net);
} }
static void runTorchNet(String prefix, String outLayerName = "", static void runTorchNet(String prefix, String outLayerName = "",
...@@ -78,10 +75,8 @@ static void runTorchNet(String prefix, String outLayerName = "", ...@@ -78,10 +75,8 @@ static void runTorchNet(String prefix, String outLayerName = "",
{ {
String suffix = (isBinary) ? ".dat" : ".txt"; String suffix = (isBinary) ? ".dat" : ".txt";
Net net; Net net = readNetFromTorch(_tf(prefix + "_net" + suffix), isBinary);
Ptr<Importer> importer = createTorchImporter(_tf(prefix + "_net" + suffix), isBinary); ASSERT_FALSE(net.empty());
ASSERT_TRUE(importer != NULL);
importer->populateNet(net);
Mat inp, outRef; Mat inp, outRef;
ASSERT_NO_THROW( inp = readTorchBlob(_tf(prefix + "_input" + suffix), isBinary) ); ASSERT_NO_THROW( inp = readTorchBlob(_tf(prefix + "_input" + suffix), isBinary) );
...@@ -200,9 +195,8 @@ TEST(Torch_Importer, ENet_accuracy) ...@@ -200,9 +195,8 @@ TEST(Torch_Importer, ENet_accuracy)
Net net; Net net;
{ {
const string model = findDataFile("dnn/Enet-model-best.net", false); const string model = findDataFile("dnn/Enet-model-best.net", false);
Ptr<Importer> importer = createTorchImporter(model, true); net = readNetFromTorch(model, true);
ASSERT_TRUE(importer != NULL); ASSERT_FALSE(net.empty());
importer->populateNet(net);
} }
Mat sample = imread(_tf("street.png", false)); Mat sample = imread(_tf("street.png", false));
......
...@@ -413,7 +413,12 @@ endif(ANDROID) ...@@ -413,7 +413,12 @@ endif(ANDROID)
# workarounding lack of `__attribute__ ((visibility("default")))` in jni_md.h/JNIEXPORT # workarounding lack of `__attribute__ ((visibility("default")))` in jni_md.h/JNIEXPORT
string(REPLACE "-fvisibility=hidden" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") string(REPLACE "-fvisibility=hidden" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wunused-const-variable -Wundef) if(MSVC)
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4996)
else()
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wunused-const-variable -Wundef -Wdeprecated-declarations)
endif()
ocv_add_library(${the_module} SHARED ${handwritten_h_sources} ${handwritten_cpp_sources} ${generated_cpp_sources} ocv_add_library(${the_module} SHARED ${handwritten_h_sources} ${handwritten_cpp_sources} ${generated_cpp_sources}
${copied_files} ${copied_files}
......
...@@ -13,3 +13,9 @@ include(../common.cmake) ...@@ -13,3 +13,9 @@ include(../common.cmake)
unset(MODULE_NAME) unset(MODULE_NAME)
unset(MODULE_INSTALL_SUBDIR) unset(MODULE_INSTALL_SUBDIR)
if(MSVC)
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4996)
else()
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wdeprecated-declarations)
endif()
...@@ -12,3 +12,9 @@ include(../common.cmake) ...@@ -12,3 +12,9 @@ include(../common.cmake)
unset(MODULE_NAME) unset(MODULE_NAME)
unset(MODULE_INSTALL_SUBDIR) unset(MODULE_INSTALL_SUBDIR)
if(MSVC)
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4996)
else()
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wdeprecated-declarations)
endif()
...@@ -410,7 +410,8 @@ class CppHeaderParser(object): ...@@ -410,7 +410,8 @@ class CppHeaderParser(object):
# note that we do not strip "static" prefix, which does matter; # note that we do not strip "static" prefix, which does matter;
# it means class methods, not instance methods # it means class methods, not instance methods
decl_str = self.batch_replace(decl_str, [("virtual", ""), ("static inline", ""), ("inline", ""),\ decl_str = self.batch_replace(decl_str, [("virtual", ""), ("static inline", ""), ("inline", ""),\
("CV_EXPORTS_W", ""), ("CV_EXPORTS", ""), ("CV_CDECL", ""), ("CV_WRAP ", " "), ("CV_INLINE", "")]).strip() ("CV_EXPORTS_W", ""), ("CV_EXPORTS", ""), ("CV_CDECL", ""), ("CV_WRAP ", " "), ("CV_INLINE", ""),
("CV_DEPRECATED", "")]).strip()
static_method = False static_method = False
context = top[0] context = top[0]
......
...@@ -91,19 +91,11 @@ int main(int argc, char **argv) ...@@ -91,19 +91,11 @@ int main(int argc, char **argv)
vector<cv::Vec3b> colors = readColors(); vector<cv::Vec3b> colors = readColors();
//! [Create the importer of Caffe model] //! [Initialize network]
Ptr<dnn::Importer> importer; dnn::Net net = readNetFromCaffe(modelTxt, modelBin);
try //Try to import Caffe GoogleNet model //! [Initialize network]
{
importer = dnn::createCaffeImporter(modelTxt, modelBin);
}
catch (const cv::Exception &err) //Importer can throw errors, we will catch them
{
cerr << err.msg << endl;
}
//! [Create the importer of Caffe model]
if (!importer) if (net.empty())
{ {
cerr << "Can't load network by using the following files: " << endl; cerr << "Can't load network by using the following files: " << endl;
cerr << "prototxt: " << modelTxt << endl; cerr << "prototxt: " << modelTxt << endl;
...@@ -113,12 +105,6 @@ int main(int argc, char **argv) ...@@ -113,12 +105,6 @@ int main(int argc, char **argv)
exit(-1); exit(-1);
} }
//! [Initialize network]
dnn::Net net;
importer->populateNet(net);
importer.release(); //We don't need importer anymore
//! [Initialize network]
//! [Prepare blob] //! [Prepare blob]
Mat img = imread(imageFile); Mat img = imread(imageFile);
if (img.empty()) if (img.empty())
......
...@@ -65,21 +65,11 @@ int main(int argc, char** argv) ...@@ -65,21 +65,11 @@ int main(int argc, char** argv)
String modelConfiguration = parser.get<string>("proto"); String modelConfiguration = parser.get<string>("proto");
String modelBinary = parser.get<string>("model"); String modelBinary = parser.get<string>("model");
//! [Create the importer of Caffe model] //! [Initialize network]
Ptr<dnn::Importer> importer; dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
//! [Initialize network]
// Import Caffe SSD model
try
{
importer = dnn::createCaffeImporter(modelConfiguration, modelBinary);
}
catch (const cv::Exception &err) //Importer can throw errors, we will catch them
{
cerr << err.msg << endl;
}
//! [Create the importer of Caffe model]
if (!importer) if (net.empty())
{ {
cerr << "Can't load network by using the following files: " << endl; cerr << "Can't load network by using the following files: " << endl;
cerr << "prototxt: " << modelConfiguration << endl; cerr << "prototxt: " << modelConfiguration << endl;
...@@ -89,12 +79,6 @@ int main(int argc, char** argv) ...@@ -89,12 +79,6 @@ int main(int argc, char** argv)
exit(-1); exit(-1);
} }
//! [Initialize network]
dnn::Net net;
importer->populateNet(net);
importer.release(); //We don't need importer anymore
//! [Initialize network]
cv::Mat frame = cv::imread(parser.get<string>("image"), -1); cv::Mat frame = cv::imread(parser.get<string>("image"), -1);
if (frame.channels() == 4) if (frame.channels() == 4)
......
...@@ -59,31 +59,17 @@ int main(int argc, char **argv) ...@@ -59,31 +59,17 @@ int main(int argc, char **argv)
String classNamesFile = parser.get<String>("c_names"); String classNamesFile = parser.get<String>("c_names");
String resultFile = parser.get<String>("result"); String resultFile = parser.get<String>("result");
//! [Create the importer of TensorFlow model] //! [Initialize network]
Ptr<dnn::Importer> importer; dnn::Net net = readNetFromTensorflow(modelFile);
try //Try to import TensorFlow AlexNet model //! [Initialize network]
{
importer = dnn::createTensorflowImporter(modelFile);
}
catch (const cv::Exception &err) //Importer can throw errors, we will catch them
{
std::cerr << err.msg << std::endl;
}
//! [Create the importer of Caffe model]
if (!importer) if (net.empty())
{ {
std::cerr << "Can't load network by using the mode file: " << std::endl; std::cerr << "Can't load network by using the mode file: " << std::endl;
std::cerr << modelFile << std::endl; std::cerr << modelFile << std::endl;
exit(-1); exit(-1);
} }
//! [Initialize network]
dnn::Net net;
importer->populateNet(net);
importer.release(); //We don't need importer anymore
//! [Initialize network]
//! [Prepare blob] //! [Prepare blob]
Mat img = imread(imageFile); Mat img = imread(imageFile);
if (img.empty()) if (img.empty())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment