mirror of
https://github.com/opencv/opencv.git
synced 2025-06-08 01:53:19 +08:00
Reading net from std::ifstream
Remove some assertions Replace std::ifstream to std::istream Add test for new importer Remove constructor to load file Rename cfgStream and darknetModelStream to ifile Add error notification to inform pathname to user Use FileStorage instead of std::istream Use FileNode instead of FileStorage Fix typo
This commit is contained in:
parent
0fd74fa177
commit
61d8719b8d
@ -644,6 +644,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
|||||||
*/
|
*/
|
||||||
CV_EXPORTS_W Net readNetFromDarknet(const String &cfgFile, const String &darknetModel = String());
|
CV_EXPORTS_W Net readNetFromDarknet(const String &cfgFile, const String &darknetModel = String());
|
||||||
|
|
||||||
|
/** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
|
||||||
|
* @param cfgFile file node to the .cfg file with text description of the network architecture.
|
||||||
|
* @param darknetModel file node to the .weights file with learned network.
|
||||||
|
* @returns Network object that ready to do forward, throw an exception in failure cases.
|
||||||
|
* @returns Net object.
|
||||||
|
*/
|
||||||
|
CV_EXPORTS_W Net readNetFromDarknet(const FileNode &cfgFile, const FileNode &darknetModel = FileNode());
|
||||||
|
|
||||||
/** @brief Reads a network model stored in <a href="http://caffe.berkeleyvision.org">Caffe</a> framework's format.
|
/** @brief Reads a network model stored in <a href="http://caffe.berkeleyvision.org">Caffe</a> framework's format.
|
||||||
* @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.
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
#include "../precomp.hpp"
|
#include "../precomp.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -66,14 +67,19 @@ public:
|
|||||||
|
|
||||||
DarknetImporter() {}
|
DarknetImporter() {}
|
||||||
|
|
||||||
DarknetImporter(const char *cfgFile, const char *darknetModel)
|
DarknetImporter(std::istream &cfgStream, std::istream &darknetModelStream)
|
||||||
{
|
{
|
||||||
CV_TRACE_FUNCTION();
|
CV_TRACE_FUNCTION();
|
||||||
|
|
||||||
ReadNetParamsFromCfgFileOrDie(cfgFile, &net);
|
ReadNetParamsFromCfgStreamOrDie(cfgStream, &net);
|
||||||
|
ReadNetParamsFromBinaryStreamOrDie(darknetModelStream, &net);
|
||||||
|
}
|
||||||
|
|
||||||
if (darknetModel && darknetModel[0])
|
DarknetImporter(std::istream &cfgStream)
|
||||||
ReadNetParamsFromBinaryFileOrDie(darknetModel, &net);
|
{
|
||||||
|
CV_TRACE_FUNCTION();
|
||||||
|
|
||||||
|
ReadNetParamsFromCfgStreamOrDie(cfgStream, &net);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BlobNote
|
struct BlobNote
|
||||||
@ -179,7 +185,38 @@ public:
|
|||||||
|
|
||||||
Net readNetFromDarknet(const String &cfgFile, const String &darknetModel /*= String()*/)
|
Net readNetFromDarknet(const String &cfgFile, const String &darknetModel /*= String()*/)
|
||||||
{
|
{
|
||||||
DarknetImporter darknetImporter(cfgFile.c_str(), darknetModel.c_str());
|
Net net;
|
||||||
|
std::ifstream cfgStream(cfgFile.c_str());
|
||||||
|
if(!cfgStream.is_open()) {
|
||||||
|
CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter file: " + std::string(cfgFile));
|
||||||
|
return net;
|
||||||
|
}
|
||||||
|
DarknetImporter darknetImporter;
|
||||||
|
if (darknetModel != String()) {
|
||||||
|
std::ifstream darknetModelStream(darknetModel.c_str());
|
||||||
|
if(!darknetModelStream.is_open()){
|
||||||
|
CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter file: " + std::string(darknetModel));
|
||||||
|
return net;
|
||||||
|
}
|
||||||
|
darknetImporter = DarknetImporter(cfgStream, darknetModelStream);
|
||||||
|
} else {
|
||||||
|
darknetImporter = DarknetImporter(cfgStream);
|
||||||
|
}
|
||||||
|
darknetImporter.populateNet(net);
|
||||||
|
return net;
|
||||||
|
}
|
||||||
|
|
||||||
|
Net readNetFromDarknet(const FileNode &cfgFile, const FileNode &darknetModel /*= FileNode()*/)
|
||||||
|
{
|
||||||
|
DarknetImporter darknetImporter;
|
||||||
|
if(darknetModel.empty()){
|
||||||
|
std::istringstream cfgStream((std::string)cfgFile);
|
||||||
|
darknetImporter = DarknetImporter(cfgStream);
|
||||||
|
}else{
|
||||||
|
std::istringstream cfgStream((std::string)cfgFile);
|
||||||
|
std::istringstream darknetModelStream((std::string)darknetModel);
|
||||||
|
darknetImporter = DarknetImporter(cfgStream, darknetModelStream);
|
||||||
|
}
|
||||||
Net net;
|
Net net;
|
||||||
darknetImporter.populateNet(net);
|
darknetImporter.populateNet(net);
|
||||||
return net;
|
return net;
|
||||||
|
@ -476,11 +476,7 @@ namespace cv {
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ReadDarknetFromCfgFile(const char *cfgFile, NetParameter *net)
|
bool ReadDarknetFromCfgStream(std::istream &ifile, NetParameter *net)
|
||||||
{
|
|
||||||
std::ifstream ifile;
|
|
||||||
ifile.open(cfgFile);
|
|
||||||
if (ifile.is_open())
|
|
||||||
{
|
{
|
||||||
bool read_net = false;
|
bool read_net = false;
|
||||||
int layers_counter = -1;
|
int layers_counter = -1;
|
||||||
@ -530,14 +526,11 @@ namespace cv {
|
|||||||
net->height = getParam(net_params, "height", 416);
|
net->height = getParam(net_params, "height", 416);
|
||||||
net->channels = getParam(net_params, "channels", 3);
|
net->channels = getParam(net_params, "channels", 3);
|
||||||
CV_Assert(net->width > 0 && net->height > 0 && net->channels > 0);
|
CV_Assert(net->width > 0 && net->height > 0 && net->channels > 0);
|
||||||
}
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int current_channels = net->channels;
|
int current_channels = net->channels;
|
||||||
net->out_channels_vec.resize(net->layers_cfg.size());
|
net->out_channels_vec.resize(net->layers_cfg.size());
|
||||||
|
|
||||||
int layers_counter = -1;
|
layers_counter = -1;
|
||||||
|
|
||||||
setLayersParams setParams(net);
|
setLayersParams setParams(net);
|
||||||
|
|
||||||
@ -676,13 +669,8 @@ namespace cv {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ReadDarknetFromWeightsStream(std::istream &ifile, NetParameter *net)
|
||||||
bool ReadDarknetFromWeightsFile(const char *darknetModel, NetParameter *net)
|
|
||||||
{
|
{
|
||||||
std::ifstream ifile;
|
|
||||||
ifile.open(darknetModel, std::ios::binary);
|
|
||||||
CV_Assert(ifile.is_open());
|
|
||||||
|
|
||||||
int32_t major_ver, minor_ver, revision;
|
int32_t major_ver, minor_ver, revision;
|
||||||
ifile.read(reinterpret_cast<char *>(&major_ver), sizeof(int32_t));
|
ifile.read(reinterpret_cast<char *>(&major_ver), sizeof(int32_t));
|
||||||
ifile.read(reinterpret_cast<char *>(&minor_ver), sizeof(int32_t));
|
ifile.read(reinterpret_cast<char *>(&minor_ver), sizeof(int32_t));
|
||||||
@ -778,19 +766,18 @@ namespace cv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ReadNetParamsFromCfgFileOrDie(const char *cfgFile, darknet::NetParameter *net)
|
void ReadNetParamsFromCfgStreamOrDie(std::istream &ifile, darknet::NetParameter *net)
|
||||||
{
|
{
|
||||||
if (!darknet::ReadDarknetFromCfgFile(cfgFile, net)) {
|
if (!darknet::ReadDarknetFromCfgStream(ifile, net)) {
|
||||||
CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter file: " + std::string(cfgFile));
|
CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter stream");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadNetParamsFromBinaryFileOrDie(const char *darknetModel, darknet::NetParameter *net)
|
void ReadNetParamsFromBinaryStreamOrDie(std::istream &ifile, darknet::NetParameter *net)
|
||||||
{
|
{
|
||||||
if (!darknet::ReadDarknetFromWeightsFile(darknetModel, net)) {
|
if (!darknet::ReadDarknetFromWeightsStream(ifile, net)) {
|
||||||
CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter file: " + std::string(darknetModel));
|
CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter stream");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,10 +109,9 @@ namespace cv {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read parameters from a file into a NetParameter message.
|
// Read parameters from a stream into a NetParameter message.
|
||||||
void ReadNetParamsFromCfgFileOrDie(const char *cfgFile, darknet::NetParameter *net);
|
void ReadNetParamsFromCfgStreamOrDie(std::istream &ifile, darknet::NetParameter *net);
|
||||||
void ReadNetParamsFromBinaryFileOrDie(const char *darknetModel, darknet::NetParameter *net);
|
void ReadNetParamsFromBinaryStreamOrDie(std::istream &ifile, darknet::NetParameter *net);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -65,6 +65,18 @@ TEST(Test_Darknet, read_yolo_voc)
|
|||||||
ASSERT_FALSE(net.empty());
|
ASSERT_FALSE(net.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Test_Darknet, read_filestorage_yolo_voc)
|
||||||
|
{
|
||||||
|
std::ifstream ifile(_tf("yolo-voc.cfg").c_str());
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << " " << ifile.rdbuf(); // FIXME: FileStorage drops first character.
|
||||||
|
FileStorage ofs(".xml", FileStorage::WRITE | FileStorage::MEMORY);
|
||||||
|
ofs.write("cfgFile", buffer.str());
|
||||||
|
FileStorage ifs(ofs.releaseAndGetString(), FileStorage::READ | FileStorage::MEMORY | FileStorage::FORMAT_XML);
|
||||||
|
Net net = readNetFromDarknet(ifs["cfgFile"]);
|
||||||
|
ASSERT_FALSE(net.empty());
|
||||||
|
}
|
||||||
|
|
||||||
class Test_Darknet_layers : public DNNTestLayer
|
class Test_Darknet_layers : public DNNTestLayer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Loading…
Reference in New Issue
Block a user